JavascriptOperators.cpp 523 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "RuntimeLanguagePch.h"
  6. #include "Types/PathTypeHandler.h"
  7. #include "Types/PropertyIndexRanges.h"
  8. #include "Types/UnscopablesWrapperObject.h"
  9. #include "Types/SpreadArgument.h"
  10. #include "Library/JavascriptPromise.h"
  11. #include "Library/JavascriptRegularExpression.h"
  12. #include "Library/ThrowErrorObject.h"
  13. #include "Library/JavascriptGeneratorFunction.h"
  14. #include "Library/ForInObjectEnumerator.h"
  15. #include "Library/ES5Array.h"
  16. #include "Types/SimpleDictionaryPropertyDescriptor.h"
  17. #include "Types/SimpleDictionaryTypeHandler.h"
  18. #include "Language/ModuleNamespace.h"
  19. #ifndef SCRIPT_DIRECT_TYPE
  20. typedef enum JsNativeValueType: int
  21. {
  22. JsInt8Type,
  23. JsUint8Type,
  24. JsInt16Type,
  25. JsUint16Type,
  26. JsInt32Type,
  27. JsUint32Type,
  28. JsInt64Type,
  29. JsUint64Type,
  30. JsFloatType,
  31. JsDoubleType,
  32. JsNativeStringType
  33. } JsNativeValueType;
  34. typedef struct JsNativeString
  35. {
  36. unsigned int length;
  37. LPCWSTR str;
  38. } JsNativeString;
  39. #endif
  40. using namespace Js;
  41. DEFINE_RECYCLER_TRACKER_ARRAY_PERF_COUNTER(Var);
  42. DEFINE_RECYCLER_TRACKER_PERF_COUNTER(FrameDisplay);
  43. enum IndexType
  44. {
  45. IndexType_Number,
  46. IndexType_PropertyId,
  47. IndexType_JavascriptString
  48. };
  49. IndexType GetIndexTypeFromString(char16 const * propertyName, charcount_t propertyLength, ScriptContext* scriptContext, uint32* index, PropertyRecord const** propertyRecord, bool createIfNotFound)
  50. {
  51. if (JavascriptOperators::TryConvertToUInt32(propertyName, propertyLength, index) &&
  52. (*index != JavascriptArray::InvalidIndex))
  53. {
  54. return IndexType_Number;
  55. }
  56. else
  57. {
  58. if (createIfNotFound)
  59. {
  60. scriptContext->GetOrAddPropertyRecord(propertyName, propertyLength, propertyRecord);
  61. }
  62. else
  63. {
  64. scriptContext->FindPropertyRecord(propertyName, propertyLength, propertyRecord);
  65. }
  66. return IndexType_PropertyId;
  67. }
  68. }
  69. IndexType GetIndexTypeFromPrimitive(Var indexVar, ScriptContext* scriptContext, uint32* index, PropertyRecord const ** propertyRecord, JavascriptString ** propertyNameString, bool createIfNotFound, bool preferJavascriptStringOverPropertyRecord)
  70. {
  71. // CONSIDER: Only OP_SetElementI and OP_GetElementI use and take advantage of the
  72. // IndexType_JavascriptString result. Consider modifying other callers of GetIndexType to take
  73. // advantage of non-interned property strings where appropriate.
  74. if (TaggedInt::Is(indexVar))
  75. {
  76. int indexInt = TaggedInt::ToInt32(indexVar);
  77. if (indexInt >= 0)
  78. {
  79. *index = (uint)indexInt;
  80. return IndexType_Number;
  81. }
  82. else
  83. {
  84. char16 stringBuffer[22];
  85. int pos = TaggedInt::ToBuffer(indexInt, stringBuffer, _countof(stringBuffer));
  86. charcount_t length = (_countof(stringBuffer) - 1) - pos;
  87. if (createIfNotFound || preferJavascriptStringOverPropertyRecord)
  88. {
  89. // When preferring JavascriptString objects, just return a PropertyRecord instead
  90. // of creating temporary JavascriptString objects for every negative integer that
  91. // comes through here.
  92. scriptContext->GetOrAddPropertyRecord(stringBuffer + pos, length, propertyRecord);
  93. }
  94. else
  95. {
  96. scriptContext->FindPropertyRecord(stringBuffer + pos, length, propertyRecord);
  97. }
  98. return IndexType_PropertyId;
  99. }
  100. }
  101. if (JavascriptNumber::Is_NoTaggedIntCheck(indexVar))
  102. {
  103. // If this double can be a positive integer index, convert it.
  104. int32 value = 0;
  105. bool isInt32 = false;
  106. if (JavascriptNumber::TryGetInt32OrUInt32Value(JavascriptNumber::GetValue(indexVar), &value, &isInt32)
  107. && !isInt32
  108. && static_cast<uint32>(value) < JavascriptArray::InvalidIndex)
  109. {
  110. *index = static_cast<uint32>(value);
  111. return IndexType_Number;
  112. }
  113. // Fall through to slow string conversion.
  114. }
  115. JavascriptSymbol * symbol = JavascriptOperators::TryFromVar<JavascriptSymbol>(indexVar);
  116. if (symbol)
  117. {
  118. // JavascriptSymbols cannot add a new PropertyRecord - they correspond to one and only one existing PropertyRecord.
  119. // We already know what the PropertyRecord is since it is stored in the JavascriptSymbol itself so just return it.
  120. *propertyRecord = symbol->GetValue();
  121. return IndexType_PropertyId;
  122. }
  123. else
  124. {
  125. JavascriptString* indexStr = JavascriptConversion::ToString(indexVar, scriptContext);
  126. char16 const * propertyName = indexStr->GetString();
  127. charcount_t const propertyLength = indexStr->GetLength();
  128. if (!createIfNotFound && preferJavascriptStringOverPropertyRecord)
  129. {
  130. if (JavascriptOperators::TryConvertToUInt32(propertyName, propertyLength, index) &&
  131. (*index != JavascriptArray::InvalidIndex))
  132. {
  133. return IndexType_Number;
  134. }
  135. *propertyNameString = indexStr;
  136. return IndexType_JavascriptString;
  137. }
  138. return GetIndexTypeFromString(propertyName, propertyLength, scriptContext, index, propertyRecord, createIfNotFound);
  139. }
  140. }
  141. IndexType GetIndexTypeFromPrimitive(Var indexVar, ScriptContext* scriptContext, uint32* index, PropertyRecord const ** propertyRecord, bool createIfNotFound)
  142. {
  143. return GetIndexTypeFromPrimitive(indexVar, scriptContext, index, propertyRecord, nullptr, createIfNotFound, false);
  144. }
  145. IndexType GetIndexType(Var& indexVar, ScriptContext* scriptContext, uint32* index, PropertyRecord const ** propertyRecord, JavascriptString ** propertyNameString, bool createIfNotFound, bool preferJavascriptStringOverPropertyRecord)
  146. {
  147. indexVar = JavascriptConversion::ToPrimitive<JavascriptHint::HintString>(indexVar, scriptContext);
  148. return GetIndexTypeFromPrimitive(indexVar, scriptContext, index, propertyRecord, propertyNameString, createIfNotFound, preferJavascriptStringOverPropertyRecord);
  149. }
  150. IndexType GetIndexType(Var& indexVar, ScriptContext* scriptContext, uint32* index, PropertyRecord const ** propertyRecord, bool createIfNotFound)
  151. {
  152. return GetIndexType(indexVar, scriptContext, index, propertyRecord, nullptr, createIfNotFound, false);
  153. }
  154. BOOL FEqualDbl(double dbl1, double dbl2)
  155. {
  156. // If the low ulongs don't match, they can't be equal.
  157. if (Js::NumberUtilities::LuLoDbl(dbl1) != Js::NumberUtilities::LuLoDbl(dbl2))
  158. return FALSE;
  159. // If the high ulongs don't match, they can be equal iff one is -0 and
  160. // the other is +0.
  161. if (Js::NumberUtilities::LuHiDbl(dbl1) != Js::NumberUtilities::LuHiDbl(dbl2))
  162. {
  163. return 0x80000000 == (Js::NumberUtilities::LuHiDbl(dbl1) | Js::NumberUtilities::LuHiDbl(dbl2)) &&
  164. 0 == Js::NumberUtilities::LuLoDbl(dbl1);
  165. }
  166. // The bit patterns match. They are equal iff they are not Nan.
  167. return !Js::NumberUtilities::IsNan(dbl1);
  168. }
  169. Var JavascriptOperators::OP_ApplyArgs(Var func, Var instance, __in_xcount(8) void** stackPtr, CallInfo callInfo, ScriptContext* scriptContext)
  170. {
  171. JIT_HELPER_REENTRANT_HEADER(Op_OP_ApplyArgs);
  172. int argCount = callInfo.Count;
  173. ///
  174. /// Check func has internal [[Call]] property
  175. /// If not, throw TypeError
  176. ///
  177. if (!JavascriptConversion::IsCallable(func)) {
  178. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedFunction);
  179. }
  180. // Fix callInfo: expect result/value, and none of other flags are currently applicable.
  181. // OP_ApplyArgs expects a result. Neither of {jit, interpreted} mode sends correct callFlags:
  182. // LdArgCnt -- jit sends whatever was passed to current function, interpreter always sends 0.
  183. // See Win8 bug 490489.
  184. callInfo.Flags = CallFlags_Value;
  185. RecyclableObject *funcPtr = RecyclableObject::UnsafeFromVar(func);
  186. PROBE_STACK(scriptContext, Js::Constants::MinStackDefault + argCount * 4);
  187. JavascriptMethod entryPoint = funcPtr->GetEntryPoint();
  188. Var ret;
  189. switch (argCount) {
  190. case 0:
  191. Assert(false);
  192. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo);
  193. break;
  194. case 1:
  195. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance);
  196. break;
  197. case 2:
  198. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0]);
  199. break;
  200. case 3:
  201. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0], stackPtr[1]);
  202. break;
  203. case 4:
  204. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0], stackPtr[1], stackPtr[2]);
  205. break;
  206. case 5:
  207. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0], stackPtr[1], stackPtr[2], stackPtr[3]);
  208. break;
  209. case 6:
  210. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0], stackPtr[1], stackPtr[2], stackPtr[3], stackPtr[4]);
  211. break;
  212. case 7:
  213. ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0], stackPtr[1], stackPtr[2], stackPtr[3], stackPtr[4], stackPtr[5]);
  214. break;
  215. default:
  216. {
  217. // Don't need stack probe here- we just did so above
  218. Arguments args(callInfo, stackPtr - 1);
  219. BEGIN_SAFE_REENTRANT_CALL(scriptContext->GetThreadContext())
  220. {
  221. ret = JavascriptFunction::CallFunction<false>(funcPtr, entryPoint, args);
  222. }
  223. END_SAFE_REENTRANT_CALL
  224. break;
  225. }
  226. }
  227. return ret;
  228. JIT_HELPER_END(Op_OP_ApplyArgs);
  229. }
  230. #ifdef _M_IX86
  231. // Alias for overloaded JavascriptNumber::ToVar so it can be called unambiguously from native code
  232. Var JavascriptOperators::Int32ToVar(int32 value, ScriptContext* scriptContext)
  233. {
  234. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_Int32ToAtom);
  235. return JavascriptNumber::ToVar(value, scriptContext);
  236. JIT_HELPER_END(Op_Int32ToAtom);
  237. }
  238. // Alias for overloaded JavascriptNumber::ToVar so it can be called unambiguously from native code
  239. Var JavascriptOperators::Int32ToVarInPlace(int32 value, ScriptContext* scriptContext, JavascriptNumber* result)
  240. {
  241. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_Int32ToAtomInPlace);
  242. return JavascriptNumber::ToVarInPlace(value, scriptContext, result);
  243. JIT_HELPER_END(Op_Int32ToAtomInPlace);
  244. }
  245. // Alias for overloaded JavascriptNumber::ToVar so it can be called unambiguously from native code
  246. Var JavascriptOperators::UInt32ToVar(uint32 value, ScriptContext* scriptContext)
  247. {
  248. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_UInt32ToAtom);
  249. return JavascriptNumber::ToVar(value, scriptContext);
  250. JIT_HELPER_END(Op_UInt32ToAtom);
  251. }
  252. // Alias for overloaded JavascriptNumber::ToVar so it can be called unambiguously from native code
  253. Var JavascriptOperators::UInt32ToVarInPlace(uint32 value, ScriptContext* scriptContext, JavascriptNumber* result)
  254. {
  255. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_UInt32ToAtomInPlace);
  256. return JavascriptNumber::ToVarInPlace(value, scriptContext, result);
  257. JIT_HELPER_END(Op_UInt32ToAtomInPlace);
  258. }
  259. #endif
  260. Var JavascriptOperators::OP_FinishOddDivBy2(uint32 value, ScriptContext *scriptContext)
  261. {
  262. return JavascriptNumber::New((double)(value + 0.5), scriptContext);
  263. }
  264. Var JavascriptOperators::ToNumberInPlace(Var aRight, ScriptContext* scriptContext, JavascriptNumber* result)
  265. {
  266. JIT_HELPER_REENTRANT_HEADER(Op_ConvNumberInPlace);
  267. if (TaggedInt::Is(aRight) || JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  268. {
  269. return aRight;
  270. }
  271. return JavascriptNumber::ToVarInPlace(JavascriptConversion::ToNumber(aRight, scriptContext), scriptContext, result);
  272. JIT_HELPER_END(Op_ConvNumberInPlace);
  273. }
  274. Var JavascriptOperators::Typeof(Var var, ScriptContext* scriptContext)
  275. {
  276. JIT_HELPER_REENTRANT_HEADER(Op_Typeof);
  277. switch (JavascriptOperators::GetTypeId(var))
  278. {
  279. case TypeIds_Undefined:
  280. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  281. case TypeIds_Null:
  282. //null
  283. return scriptContext->GetLibrary()->GetObjectTypeDisplayString();
  284. case TypeIds_Integer:
  285. case TypeIds_Number:
  286. case TypeIds_Int64Number:
  287. case TypeIds_UInt64Number:
  288. return scriptContext->GetLibrary()->GetNumberTypeDisplayString();
  289. default:
  290. // Falsy objects are typeof 'undefined'.
  291. if (RecyclableObject::FromVar(var)->GetType()->IsFalsy())
  292. {
  293. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  294. }
  295. else
  296. {
  297. return RecyclableObject::FromVar(var)->GetTypeOfString(scriptContext);
  298. }
  299. }
  300. JIT_HELPER_END(Op_Typeof);
  301. }
  302. Var JavascriptOperators::TypeofFld(Var instance, PropertyId propertyId, ScriptContext* scriptContext)
  303. {
  304. return TypeofFld_Internal(instance, false, propertyId, scriptContext);
  305. }
  306. Var JavascriptOperators::TypeofRootFld(Var instance, PropertyId propertyId, ScriptContext* scriptContext)
  307. {
  308. return TypeofFld_Internal(instance, true, propertyId, scriptContext);
  309. }
  310. Var JavascriptOperators::TypeofFld_Internal(Var instance, const bool isRoot, PropertyId propertyId, ScriptContext* scriptContext)
  311. {
  312. RecyclableObject* object = nullptr;
  313. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  314. {
  315. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined , scriptContext->GetPropertyName(propertyId)->GetBuffer());
  316. }
  317. Var value = nullptr;
  318. try
  319. {
  320. Js::JavascriptExceptionOperators::AutoCatchHandlerExists autoCatchHandlerExists(scriptContext);
  321. // In edge mode, spec compat is more important than backward compat. Use spec/web behavior here
  322. if (isRoot
  323. ? !JavascriptOperators::GetRootProperty(instance, propertyId, &value, scriptContext)
  324. : !JavascriptOperators::GetProperty(instance, object, propertyId, &value, scriptContext))
  325. {
  326. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  327. }
  328. if (!scriptContext->IsUndeclBlockVar(value))
  329. {
  330. return JavascriptOperators::Typeof(value, scriptContext);
  331. }
  332. }
  333. catch(const JavascriptException& err)
  334. {
  335. err.GetAndClear(); // discard exception object
  336. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  337. }
  338. Assert(scriptContext->IsUndeclBlockVar(value));
  339. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UseBeforeDeclaration);
  340. }
  341. Var JavascriptOperators::TypeofElem_UInt32(Var instance, uint32 index, ScriptContext* scriptContext)
  342. {
  343. JIT_HELPER_REENTRANT_HEADER(Op_TypeofElem_UInt32);
  344. if (JavascriptOperators::IsNumberFromNativeArray(instance, index, scriptContext))
  345. return scriptContext->GetLibrary()->GetNumberTypeDisplayString();
  346. #if FLOATVAR
  347. return TypeofElem(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext);
  348. #else
  349. char buffer[sizeof(Js::JavascriptNumber)];
  350. return TypeofElem(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  351. (Js::JavascriptNumber *)buffer), scriptContext);
  352. #endif
  353. JIT_HELPER_END(Op_TypeofElem_UInt32);
  354. }
  355. Var JavascriptOperators::TypeofElem_Int32(Var instance, int32 index, ScriptContext* scriptContext)
  356. {
  357. JIT_HELPER_REENTRANT_HEADER(Op_TypeofElem_Int32);
  358. if (JavascriptOperators::IsNumberFromNativeArray(instance, index, scriptContext))
  359. return scriptContext->GetLibrary()->GetNumberTypeDisplayString();
  360. #if FLOATVAR
  361. return TypeofElem(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext);
  362. #else
  363. char buffer[sizeof(Js::JavascriptNumber)];
  364. return TypeofElem(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  365. (Js::JavascriptNumber *)buffer), scriptContext);
  366. #endif
  367. JIT_HELPER_END(Op_TypeofElem_Int32);
  368. }
  369. Js::JavascriptString* GetPropertyDisplayNameForError(Var prop, ScriptContext* scriptContext)
  370. {
  371. JavascriptString* str;
  372. JavascriptSymbol *symbol = JavascriptOperators::TryFromVar<JavascriptSymbol>(prop);
  373. if (symbol)
  374. {
  375. str = JavascriptSymbol::ToString(symbol->GetValue(), scriptContext);
  376. }
  377. else
  378. {
  379. str = JavascriptConversion::ToString(prop, scriptContext);
  380. }
  381. return str;
  382. }
  383. Var JavascriptOperators::TypeofElem(Var instance, Var index, ScriptContext* scriptContext)
  384. {
  385. JIT_HELPER_REENTRANT_HEADER(Op_TypeofElem);
  386. RecyclableObject* object = nullptr;
  387. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  388. {
  389. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined, GetPropertyDisplayNameForError(index, scriptContext));
  390. }
  391. Var member = nullptr;
  392. uint32 indexVal;
  393. PropertyRecord const * propertyRecord = nullptr;
  394. ThreadContext* threadContext = scriptContext->GetThreadContext();
  395. ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  396. threadContext->ClearImplicitCallFlags();
  397. try
  398. {
  399. Js::JavascriptExceptionOperators::AutoCatchHandlerExists autoCatchHandlerExists(scriptContext);
  400. IndexType indexType = GetIndexType(index, scriptContext, &indexVal, &propertyRecord, false);
  401. // For JS Objects, don't create the propertyId if not already added
  402. if (indexType == IndexType_Number)
  403. {
  404. // In edge mode, we don't need to worry about the special "unknown" behavior. If the item is not available from Get,
  405. // just return undefined.
  406. if (!JavascriptOperators::GetItem(instance, object, indexVal, &member, scriptContext))
  407. {
  408. // If the instance doesn't have the item, typeof result is "undefined".
  409. threadContext->CheckAndResetImplicitCallAccessorFlag();
  410. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  411. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  412. }
  413. }
  414. else
  415. {
  416. Assert(indexType == IndexType_PropertyId);
  417. if (propertyRecord == nullptr && !JavascriptOperators::CanShortcutOnUnknownPropertyName(object))
  418. {
  419. indexType = GetIndexTypeFromPrimitive(index, scriptContext, &indexVal, &propertyRecord, true);
  420. Assert(indexType == IndexType_PropertyId);
  421. Assert(propertyRecord != nullptr);
  422. }
  423. if (propertyRecord != nullptr)
  424. {
  425. if (!JavascriptOperators::GetProperty(instance, object, propertyRecord->GetPropertyId(), &member, scriptContext))
  426. {
  427. // If the instance doesn't have the property, typeof result is "undefined".
  428. threadContext->CheckAndResetImplicitCallAccessorFlag();
  429. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  430. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  431. }
  432. }
  433. else
  434. {
  435. #if DBG
  436. JavascriptString* indexStr = JavascriptConversion::ToString(index, scriptContext);
  437. PropertyRecord const * debugPropertyRecord;
  438. scriptContext->GetOrAddPropertyRecord(indexStr, &debugPropertyRecord);
  439. AssertMsg(!JavascriptOperators::GetProperty(instance, object, debugPropertyRecord->GetPropertyId(), &member, scriptContext), "how did this property come? See OS Bug 2727708 if you see this come from the web");
  440. #endif
  441. // If the instance doesn't have the property, typeof result is "undefined".
  442. threadContext->CheckAndResetImplicitCallAccessorFlag();
  443. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  444. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  445. }
  446. }
  447. threadContext->CheckAndResetImplicitCallAccessorFlag();
  448. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  449. return JavascriptOperators::Typeof(member, scriptContext);
  450. }
  451. catch(const JavascriptException& err)
  452. {
  453. err.GetAndClear(); // discard exception object
  454. threadContext->CheckAndResetImplicitCallAccessorFlag();
  455. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  456. return scriptContext->GetLibrary()->GetUndefinedDisplayString();
  457. }
  458. JIT_HELPER_END(Op_TypeofElem);
  459. }
  460. //
  461. // Delete the given Var
  462. //
  463. Var JavascriptOperators::Delete(Var var, ScriptContext* scriptContext)
  464. {
  465. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_Delete);
  466. return scriptContext->GetLibrary()->GetTrue();
  467. JIT_HELPER_END(Op_Delete);
  468. }
  469. BOOL JavascriptOperators::Equal_Full(Var aLeft, Var aRight, ScriptContext* requestContext)
  470. {
  471. JIT_HELPER_REENTRANT_HEADER(Op_Equal_Full);
  472. //
  473. // Fast-path SmInts and paired Number combinations.
  474. //
  475. if (aLeft == aRight)
  476. {
  477. if (JavascriptNumber::Is(aLeft) && JavascriptNumber::IsNan(JavascriptNumber::GetValue(aLeft)))
  478. {
  479. return false;
  480. }
  481. else if (JavascriptVariantDate::Is(aLeft) == false) // only need to check on aLeft - since they are the same var, aRight would do the same
  482. {
  483. return true;
  484. }
  485. else
  486. {
  487. //In ES5 mode strict equals (===) on same instance of object type VariantDate succeeds.
  488. //Hence equals needs to succeed.
  489. return true;
  490. }
  491. }
  492. BOOL result = false;
  493. if (TaggedInt::Is(aLeft))
  494. {
  495. if (TaggedInt::Is(aRight))
  496. {
  497. // If aLeft == aRight, we would already have returned true above.
  498. return false;
  499. }
  500. else if (JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  501. {
  502. return TaggedInt::ToDouble(aLeft) == JavascriptNumber::GetValue(aRight);
  503. }
  504. else
  505. {
  506. BOOL res = RecyclableObject::UnsafeFromVar(aRight)->Equals(aLeft, &result, requestContext);
  507. AssertMsg(res, "Should have handled this");
  508. return result;
  509. }
  510. }
  511. else if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft))
  512. {
  513. if (TaggedInt::Is(aRight))
  514. {
  515. return TaggedInt::ToDouble(aRight) == JavascriptNumber::GetValue(aLeft);
  516. }
  517. else if(JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  518. {
  519. return JavascriptNumber::GetValue(aLeft) == JavascriptNumber::GetValue(aRight);
  520. }
  521. else
  522. {
  523. BOOL res = RecyclableObject::UnsafeFromVar(aRight)->Equals(aLeft, &result, requestContext);
  524. AssertMsg(res, "Should have handled this");
  525. return result;
  526. }
  527. }
  528. if (RecyclableObject::UnsafeFromVar(aLeft)->Equals(aRight, &result, requestContext))
  529. {
  530. return result;
  531. }
  532. else
  533. {
  534. return false;
  535. }
  536. JIT_HELPER_END(Op_Equal_Full);
  537. }
  538. BOOL JavascriptOperators::Greater_Full(Var aLeft,Var aRight,ScriptContext* scriptContext)
  539. {
  540. JIT_HELPER_REENTRANT_HEADER(Op_Greater_Full);
  541. return RelationalComparisonHelper(aRight, aLeft, scriptContext, false, false);
  542. JIT_HELPER_END(Op_Greater_Full);
  543. }
  544. BOOL JavascriptOperators::Less_Full(Var aLeft, Var aRight, ScriptContext* scriptContext)
  545. {
  546. return RelationalComparisonHelper(aLeft, aRight, scriptContext, true, false);
  547. }
  548. BOOL JavascriptOperators::RelationalComparisonHelper(Var aLeft, Var aRight, ScriptContext* scriptContext, bool leftFirst, bool undefinedAs)
  549. {
  550. TypeId typeId = JavascriptOperators::GetTypeId(aLeft);
  551. if (typeId == TypeIds_Null)
  552. {
  553. aLeft=TaggedInt::ToVarUnchecked(0);
  554. }
  555. else if (typeId == TypeIds_Undefined)
  556. {
  557. aLeft=scriptContext->GetLibrary()->GetNaN();
  558. }
  559. typeId = JavascriptOperators::GetTypeId(aRight);
  560. if (typeId == TypeIds_Null)
  561. {
  562. aRight=TaggedInt::ToVarUnchecked(0);
  563. }
  564. else if (typeId == TypeIds_Undefined)
  565. {
  566. aRight=scriptContext->GetLibrary()->GetNaN();
  567. }
  568. double dblLeft, dblRight;
  569. TypeId leftType = JavascriptOperators::GetTypeId(aLeft);
  570. TypeId rightType = JavascriptOperators::GetTypeId(aRight);
  571. switch (leftType)
  572. {
  573. case TypeIds_Integer:
  574. dblLeft = TaggedInt::ToDouble(aLeft);
  575. switch (rightType)
  576. {
  577. case TypeIds_Integer:
  578. dblRight = TaggedInt::ToDouble(aRight);
  579. break;
  580. case TypeIds_Number:
  581. dblRight = JavascriptNumber::GetValue(aRight);
  582. break;
  583. default:
  584. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  585. break;
  586. }
  587. break;
  588. case TypeIds_Number:
  589. dblLeft = JavascriptNumber::GetValue(aLeft);
  590. switch (rightType)
  591. {
  592. case TypeIds_Integer:
  593. dblRight = TaggedInt::ToDouble(aRight);
  594. break;
  595. case TypeIds_Number:
  596. dblRight = JavascriptNumber::GetValue(aRight);
  597. break;
  598. default:
  599. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  600. break;
  601. }
  602. break;
  603. case TypeIds_Int64Number:
  604. {
  605. switch (rightType)
  606. {
  607. case TypeIds_Int64Number:
  608. {
  609. __int64 leftValue = JavascriptInt64Number::UnsafeFromVar(aLeft)->GetValue();
  610. __int64 rightValue = JavascriptInt64Number::UnsafeFromVar(aRight)->GetValue();
  611. return leftValue < rightValue;
  612. }
  613. break;
  614. case TypeIds_UInt64Number:
  615. {
  616. __int64 leftValue = JavascriptInt64Number::UnsafeFromVar(aLeft)->GetValue();
  617. unsigned __int64 rightValue = JavascriptUInt64Number::UnsafeFromVar(aRight)->GetValue();
  618. if (rightValue <= INT_MAX && leftValue >= 0)
  619. {
  620. return leftValue < (__int64)rightValue;
  621. }
  622. }
  623. break;
  624. }
  625. dblLeft = (double)JavascriptInt64Number::UnsafeFromVar(aLeft)->GetValue();
  626. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  627. }
  628. break;
  629. // we cannot do double conversion between 2 int64 numbers as we can get wrong result after conversion
  630. // i.e., two different numbers become the same after losing precision. We'll continue dbl comparison
  631. // if either number is not an int64 number.
  632. case TypeIds_UInt64Number:
  633. {
  634. switch (rightType)
  635. {
  636. case TypeIds_Int64Number:
  637. {
  638. unsigned __int64 leftValue = JavascriptUInt64Number::UnsafeFromVar(aLeft)->GetValue();
  639. __int64 rightValue = JavascriptInt64Number::UnsafeFromVar(aRight)->GetValue();
  640. if (leftValue < INT_MAX && rightValue >= 0)
  641. {
  642. return (__int64)leftValue < rightValue;
  643. }
  644. }
  645. break;
  646. case TypeIds_UInt64Number:
  647. {
  648. unsigned __int64 leftValue = JavascriptUInt64Number::UnsafeFromVar(aLeft)->GetValue();
  649. unsigned __int64 rightValue = JavascriptUInt64Number::UnsafeFromVar(aRight)->GetValue();
  650. return leftValue < rightValue;
  651. }
  652. break;
  653. }
  654. dblLeft = (double)JavascriptUInt64Number::UnsafeFromVar(aLeft)->GetValue();
  655. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  656. }
  657. break;
  658. case TypeIds_String:
  659. switch (rightType)
  660. {
  661. case TypeIds_Integer:
  662. case TypeIds_Number:
  663. case TypeIds_Boolean:
  664. break;
  665. default:
  666. aRight = JavascriptConversion::ToPrimitive<JavascriptHint::HintNumber>(aRight, scriptContext);
  667. rightType = JavascriptOperators::GetTypeId(aRight);
  668. if (rightType != TypeIds_String)
  669. {
  670. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  671. break;
  672. }
  673. case TypeIds_String:
  674. return JavascriptString::LessThan(aLeft, aRight);
  675. }
  676. dblLeft = JavascriptConversion::ToNumber(aLeft, scriptContext);
  677. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  678. break;
  679. case TypeIds_Boolean:
  680. case TypeIds_Null:
  681. case TypeIds_Undefined:
  682. case TypeIds_Symbol:
  683. dblLeft = JavascriptConversion::ToNumber(aLeft, scriptContext);
  684. dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
  685. break;
  686. default:
  687. if (leftFirst)
  688. {
  689. aLeft = JavascriptConversion::ToPrimitive<JavascriptHint::HintNumber>(aLeft, scriptContext);
  690. aRight = JavascriptConversion::ToPrimitive<JavascriptHint::HintNumber>(aRight, scriptContext);
  691. }
  692. else
  693. {
  694. aRight = JavascriptConversion::ToPrimitive<JavascriptHint::HintNumber>(aRight, scriptContext);
  695. aLeft = JavascriptConversion::ToPrimitive<JavascriptHint::HintNumber>(aLeft, scriptContext);
  696. }
  697. //BugFix: When @@ToPrimitive of an object is overridden with a function that returns null/undefined
  698. //this helper will fall into a inescapable goto loop as the checks for null/undefined were outside of the path
  699. return RelationalComparisonHelper(aLeft, aRight, scriptContext, leftFirst, undefinedAs);
  700. }
  701. //
  702. // And +0,-0 that is not implemented fully
  703. //
  704. if (JavascriptNumber::IsNan(dblLeft) || JavascriptNumber::IsNan(dblRight))
  705. {
  706. return undefinedAs;
  707. }
  708. // this will succeed for -0.0 == 0.0 case as well
  709. if (dblLeft == dblRight)
  710. {
  711. return false;
  712. }
  713. return dblLeft < dblRight;
  714. }
  715. BOOL JavascriptOperators::StrictEqualString(Var aLeft, JavascriptString* aRight)
  716. {
  717. JIT_HELPER_REENTRANT_HEADER(Op_StrictEqualString);
  718. JIT_HELPER_SAME_ATTRIBUTES(Op_StrictEqualString, Op_StrictEqual);
  719. JavascriptString* leftStr = TryFromVar<JavascriptString>(aLeft);
  720. if (!leftStr)
  721. {
  722. return false;
  723. }
  724. JIT_HELPER_REENTRANT_HEADER(Op_StrictEqualString);
  725. JIT_HELPER_SAME_ATTRIBUTES(Op_StrictEqualString, Op_StrictEqual);
  726. return JavascriptString::Equals(leftStr, aRight);
  727. JIT_HELPER_END(Op_StrictEqualString);
  728. }
  729. BOOL JavascriptOperators::StrictEqualEmptyString(Var aLeft)
  730. {
  731. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_StrictEqualEmptyString);
  732. JavascriptString * string = JavascriptOperators::TryFromVar<JavascriptString>(aLeft);
  733. if (!string)
  734. {
  735. return false;
  736. }
  737. Assert(string);
  738. return string->GetLength() == 0;
  739. JIT_HELPER_END(Op_StrictEqualEmptyString);
  740. }
  741. BOOL JavascriptOperators::StrictEqual(Var aLeft, Var aRight, ScriptContext* requestContext)
  742. {
  743. JIT_HELPER_REENTRANT_HEADER(Op_StrictEqual);
  744. double dblLeft, dblRight;
  745. TypeId rightType, leftType;
  746. leftType = JavascriptOperators::GetTypeId(aLeft);
  747. // Because NaN !== NaN, we may not return TRUE when typeId is Number
  748. if (aLeft == aRight && leftType != TypeIds_Number) return TRUE;
  749. rightType = JavascriptOperators::GetTypeId(aRight);
  750. switch (leftType)
  751. {
  752. case TypeIds_String:
  753. switch (rightType)
  754. {
  755. case TypeIds_String:
  756. return JavascriptString::Equals(JavascriptString::UnsafeFromVar(aLeft), JavascriptString::UnsafeFromVar(aRight));
  757. }
  758. return FALSE;
  759. case TypeIds_Integer:
  760. switch (rightType)
  761. {
  762. case TypeIds_Integer:
  763. return aLeft == aRight;
  764. // we don't need to worry about int64: it cannot equal as we create
  765. // JavascriptInt64Number only in overflow scenarios.
  766. case TypeIds_Number:
  767. dblLeft = TaggedInt::ToDouble(aLeft);
  768. dblRight = JavascriptNumber::GetValue(aRight);
  769. goto CommonNumber;
  770. }
  771. return FALSE;
  772. case TypeIds_Int64Number:
  773. switch (rightType)
  774. {
  775. case TypeIds_Int64Number:
  776. {
  777. __int64 leftValue = JavascriptInt64Number::UnsafeFromVar(aLeft)->GetValue();
  778. __int64 rightValue = JavascriptInt64Number::UnsafeFromVar(aRight)->GetValue();
  779. return leftValue == rightValue;
  780. }
  781. case TypeIds_UInt64Number:
  782. {
  783. __int64 leftValue = JavascriptInt64Number::UnsafeFromVar(aLeft)->GetValue();
  784. unsigned __int64 rightValue = JavascriptInt64Number::FromVar(aRight)->GetValue();
  785. return ((unsigned __int64)leftValue == rightValue);
  786. }
  787. case TypeIds_Number:
  788. dblLeft = (double)JavascriptInt64Number::UnsafeFromVar(aLeft)->GetValue();
  789. dblRight = JavascriptNumber::GetValue(aRight);
  790. goto CommonNumber;
  791. }
  792. return FALSE;
  793. case TypeIds_UInt64Number:
  794. switch (rightType)
  795. {
  796. case TypeIds_Int64Number:
  797. {
  798. unsigned __int64 leftValue = JavascriptUInt64Number::UnsafeFromVar(aLeft)->GetValue();
  799. __int64 rightValue = JavascriptInt64Number::UnsafeFromVar(aRight)->GetValue();
  800. return (leftValue == (unsigned __int64)rightValue);
  801. }
  802. case TypeIds_UInt64Number:
  803. {
  804. unsigned __int64 leftValue = JavascriptUInt64Number::UnsafeFromVar(aLeft)->GetValue();
  805. unsigned __int64 rightValue = JavascriptInt64Number::FromVar(aRight)->GetValue();
  806. return leftValue == rightValue;
  807. }
  808. case TypeIds_Number:
  809. dblLeft = (double)JavascriptUInt64Number::UnsafeFromVar(aLeft)->GetValue();
  810. dblRight = JavascriptNumber::GetValue(aRight);
  811. goto CommonNumber;
  812. }
  813. return FALSE;
  814. case TypeIds_Number:
  815. switch (rightType)
  816. {
  817. case TypeIds_Integer:
  818. dblLeft = JavascriptNumber::GetValue(aLeft);
  819. dblRight = TaggedInt::ToDouble(aRight);
  820. goto CommonNumber;
  821. case TypeIds_Int64Number:
  822. dblLeft = JavascriptNumber::GetValue(aLeft);
  823. dblRight = (double)JavascriptInt64Number::FromVar(aRight)->GetValue();
  824. goto CommonNumber;
  825. case TypeIds_UInt64Number:
  826. dblLeft = JavascriptNumber::GetValue(aLeft);
  827. dblRight = (double)JavascriptUInt64Number::UnsafeFromVar(aRight)->GetValue();
  828. goto CommonNumber;
  829. case TypeIds_Number:
  830. dblLeft = JavascriptNumber::GetValue(aLeft);
  831. dblRight = JavascriptNumber::GetValue(aRight);
  832. CommonNumber:
  833. return FEqualDbl(dblLeft, dblRight);
  834. }
  835. return FALSE;
  836. case TypeIds_Boolean:
  837. switch (rightType)
  838. {
  839. case TypeIds_Boolean:
  840. return aLeft == aRight;
  841. }
  842. return FALSE;
  843. case TypeIds_Undefined:
  844. return rightType == TypeIds_Undefined;
  845. case TypeIds_Null:
  846. return rightType == TypeIds_Null;
  847. case TypeIds_Array:
  848. return (rightType == TypeIds_Array && aLeft == aRight);
  849. #if DBG
  850. case TypeIds_Symbol:
  851. if (rightType == TypeIds_Symbol)
  852. {
  853. const PropertyRecord* leftValue = JavascriptSymbol::UnsafeFromVar(aLeft)->GetValue();
  854. const PropertyRecord* rightValue = JavascriptSymbol::UnsafeFromVar(aRight)->GetValue();
  855. Assert(leftValue != rightValue);
  856. }
  857. break;
  858. #endif
  859. case TypeIds_GlobalObject:
  860. case TypeIds_HostDispatch:
  861. switch (rightType)
  862. {
  863. case TypeIds_HostDispatch:
  864. case TypeIds_GlobalObject:
  865. {
  866. BOOL result;
  867. if(RecyclableObject::UnsafeFromVar(aLeft)->StrictEquals(aRight, &result, requestContext))
  868. {
  869. return result;
  870. }
  871. return false;
  872. }
  873. }
  874. break;
  875. }
  876. if (RecyclableObject::FromVar(aLeft)->IsExternal())
  877. {
  878. BOOL result;
  879. if (RecyclableObject::FromVar(aLeft)->StrictEquals(aRight, &result, requestContext))
  880. {
  881. if (result)
  882. {
  883. return TRUE;
  884. }
  885. }
  886. }
  887. if (!TaggedNumber::Is(aRight) && RecyclableObject::FromVar(aRight)->IsExternal())
  888. {
  889. BOOL result;
  890. if (RecyclableObject::FromVar(aRight)->StrictEquals(aLeft, &result, requestContext))
  891. {
  892. if (result)
  893. {
  894. return TRUE;
  895. }
  896. }
  897. }
  898. return aLeft == aRight;
  899. JIT_HELPER_END(Op_StrictEqual);
  900. }
  901. BOOL JavascriptOperators::HasOwnProperty(
  902. Var instance,
  903. PropertyId propertyId,
  904. _In_ ScriptContext* requestContext,
  905. _In_opt_ PropertyString* propString)
  906. {
  907. if (TaggedNumber::Is(instance))
  908. {
  909. return FALSE;
  910. }
  911. RecyclableObject* object = RecyclableObject::UnsafeFromVar(instance);
  912. if (JavascriptProxy::Is(instance))
  913. {
  914. PropertyDescriptor desc;
  915. return GetOwnPropertyDescriptor(object, propertyId, requestContext, &desc);
  916. }
  917. // If we have a PropertyString, attempt to shortcut the lookup by using its caches
  918. if (propString != nullptr)
  919. {
  920. PropertyCacheOperationInfo info;
  921. if (propString->GetLdElemInlineCache()->PretendTryGetProperty(object->GetType(), &info))
  922. {
  923. switch (info.cacheType)
  924. {
  925. case CacheType_Local:
  926. Assert(object->HasOwnProperty(propertyId));
  927. return TRUE;
  928. case CacheType_Proto:
  929. Assert(!object->HasOwnProperty(propertyId));
  930. return FALSE;
  931. default:
  932. // We had a cache hit, but cache doesn't tell us if we have an own property
  933. break;
  934. }
  935. }
  936. if (propString->GetStElemInlineCache()->PretendTrySetProperty(object->GetType(), object->GetType(), &info))
  937. {
  938. switch (info.cacheType)
  939. {
  940. case CacheType_Local:
  941. Assert(object->HasOwnProperty(propertyId));
  942. return TRUE;
  943. case CacheType_LocalWithoutProperty:
  944. Assert(!object->HasOwnProperty(propertyId));
  945. return FALSE;
  946. default:
  947. // We had a cache hit, but cache doesn't tell us if we have an own property
  948. break;
  949. }
  950. }
  951. }
  952. return object && object->HasOwnProperty(propertyId);
  953. }
  954. BOOL JavascriptOperators::GetOwnAccessors(Var instance, PropertyId propertyId, Var* getter, Var* setter, ScriptContext * requestContext)
  955. {
  956. BOOL result;
  957. if (TaggedNumber::Is(instance))
  958. {
  959. result = false;
  960. }
  961. else
  962. {
  963. RecyclableObject* object = RecyclableObject::UnsafeFromVar(instance);
  964. result = object && object->GetAccessors(propertyId, getter, setter, requestContext);
  965. }
  966. return result;
  967. }
  968. JavascriptArray* JavascriptOperators::GetOwnPropertyNames(Var instance, ScriptContext *scriptContext)
  969. {
  970. RecyclableObject *object = RecyclableObject::FromVar(ToObject(instance, scriptContext));
  971. JavascriptProxy * proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(instance);
  972. if (proxy)
  973. {
  974. return proxy->PropertyKeysTrap(JavascriptProxy::KeysTrapKind::GetOwnPropertyNamesKind, scriptContext);
  975. }
  976. return JavascriptObject::CreateOwnStringPropertiesHelper(object, scriptContext);
  977. }
  978. JavascriptArray* JavascriptOperators::GetOwnPropertySymbols(Var instance, ScriptContext *scriptContext)
  979. {
  980. RecyclableObject *object = RecyclableObject::FromVar(ToObject(instance, scriptContext));
  981. CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_getOwnPropertySymbols);
  982. JavascriptProxy* proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(instance);
  983. if (proxy)
  984. {
  985. return proxy->PropertyKeysTrap(JavascriptProxy::KeysTrapKind::GetOwnPropertySymbolKind, scriptContext);
  986. }
  987. return JavascriptObject::CreateOwnSymbolPropertiesHelper(object, scriptContext);
  988. }
  989. JavascriptArray* JavascriptOperators::GetOwnPropertyKeys(Var instance, ScriptContext* scriptContext)
  990. {
  991. RecyclableObject *object = RecyclableObject::FromVar(ToObject(instance, scriptContext));
  992. JavascriptProxy* proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(instance);
  993. if (proxy)
  994. {
  995. return proxy->PropertyKeysTrap(JavascriptProxy::KeysTrapKind::KeysKind, scriptContext);
  996. }
  997. return JavascriptObject::CreateOwnStringSymbolPropertiesHelper(object, scriptContext);
  998. }
  999. JavascriptArray* JavascriptOperators::GetOwnEnumerablePropertyNames(RecyclableObject* object, ScriptContext* scriptContext)
  1000. {
  1001. JavascriptProxy* proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(object);
  1002. if (proxy)
  1003. {
  1004. JavascriptArray* proxyResult = proxy->PropertyKeysTrap(JavascriptProxy::KeysTrapKind::GetOwnPropertyNamesKind, scriptContext);
  1005. JavascriptArray* proxyResultToReturn = scriptContext->GetLibrary()->CreateArray(0);
  1006. // filter enumerable keys
  1007. uint32 resultLength = proxyResult->GetLength();
  1008. Var element;
  1009. const Js::PropertyRecord *propertyRecord = nullptr;
  1010. uint32 index = 0;
  1011. for (uint32 i = 0; i < resultLength; i++)
  1012. {
  1013. element = proxyResult->DirectGetItem(i);
  1014. Assert(!JavascriptSymbol::Is(element));
  1015. PropertyDescriptor propertyDescriptor;
  1016. JavascriptConversion::ToPropertyKey(element, scriptContext, &propertyRecord, nullptr);
  1017. if (JavascriptOperators::GetOwnPropertyDescriptor(object, propertyRecord->GetPropertyId(), scriptContext, &propertyDescriptor))
  1018. {
  1019. if (propertyDescriptor.IsEnumerable())
  1020. {
  1021. proxyResultToReturn->DirectSetItemAt(index++, CrossSite::MarshalVar(scriptContext, element));
  1022. }
  1023. }
  1024. }
  1025. return proxyResultToReturn;
  1026. }
  1027. return JavascriptObject::CreateOwnEnumerableStringPropertiesHelper(object, scriptContext);
  1028. }
  1029. JavascriptArray* JavascriptOperators::GetOwnEnumerablePropertyNamesSymbols(RecyclableObject* object, ScriptContext* scriptContext)
  1030. {
  1031. JavascriptProxy* proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(object);
  1032. if (proxy)
  1033. {
  1034. return proxy->PropertyKeysTrap(JavascriptProxy::KeysTrapKind::KeysKind, scriptContext);
  1035. }
  1036. return JavascriptObject::CreateOwnEnumerableStringSymbolPropertiesHelper(object, scriptContext);
  1037. }
  1038. BOOL JavascriptOperators::GetOwnProperty(Var instance, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo * propertyValueInfo)
  1039. {
  1040. BOOL result;
  1041. if (TaggedNumber::Is(instance))
  1042. {
  1043. result = false;
  1044. }
  1045. else
  1046. {
  1047. RecyclableObject* object = RecyclableObject::FromVar(instance);
  1048. result = object && object->GetProperty(object, propertyId, value, propertyValueInfo, requestContext);
  1049. if (propertyValueInfo && result)
  1050. {
  1051. // We can only update the cache in case a property was found, because if it wasn't found, we don't know if it is missing or on a prototype
  1052. CacheOperators::CachePropertyRead(instance, object, false /* isRoot */, propertyId, false /* isMissing */, propertyValueInfo, requestContext);
  1053. }
  1054. }
  1055. return result;
  1056. }
  1057. BOOL JavascriptOperators::GetOwnPropertyDescriptor(RecyclableObject* obj, JavascriptString* propertyKey, ScriptContext* scriptContext, PropertyDescriptor* propertyDescriptor)
  1058. {
  1059. return JavascriptOperators::GetOwnPropertyDescriptor(obj, JavascriptOperators::GetPropertyId(propertyKey, scriptContext), scriptContext, propertyDescriptor);
  1060. }
  1061. // ES5's [[GetOwnProperty]].
  1062. // Return value:
  1063. // FALSE means "undefined" PD.
  1064. // TRUE means success. The propertyDescriptor parameter gets the descriptor.
  1065. //
  1066. BOOL JavascriptOperators::GetOwnPropertyDescriptor(RecyclableObject* obj, PropertyId propertyId, ScriptContext* scriptContext, PropertyDescriptor* propertyDescriptor)
  1067. {
  1068. Assert(obj);
  1069. Assert(scriptContext);
  1070. Assert(propertyDescriptor);
  1071. if (JavascriptProxy::Is(obj))
  1072. {
  1073. return JavascriptProxy::GetOwnPropertyDescriptor(obj, propertyId, scriptContext, propertyDescriptor);
  1074. }
  1075. Var getter, setter;
  1076. if (false == JavascriptOperators::GetOwnAccessors(obj, propertyId, &getter, &setter, scriptContext))
  1077. {
  1078. Var value = nullptr;
  1079. if (false == JavascriptOperators::GetOwnProperty(obj, propertyId, &value, scriptContext, nullptr))
  1080. {
  1081. return FALSE;
  1082. }
  1083. if (nullptr != value)
  1084. {
  1085. propertyDescriptor->SetValue(value);
  1086. }
  1087. //CONSIDER : Its expensive to query for each flag from type system. Combine this with the GetOwnProperty to get all the flags
  1088. //at once. This will require a new API from type system and override in all the types which overrides IsEnumerable etc.
  1089. //Currently there is no performance tuning for ES5. This should be ok.
  1090. propertyDescriptor->SetWritable(FALSE != obj->IsWritable(propertyId));
  1091. }
  1092. else
  1093. {
  1094. if (nullptr == getter)
  1095. {
  1096. getter = scriptContext->GetLibrary()->GetUndefined();
  1097. }
  1098. propertyDescriptor->SetGetter(getter);
  1099. if (nullptr == setter)
  1100. {
  1101. setter = scriptContext->GetLibrary()->GetUndefined();
  1102. }
  1103. propertyDescriptor->SetSetter(setter);
  1104. }
  1105. propertyDescriptor->SetConfigurable(FALSE != obj->IsConfigurable(propertyId));
  1106. propertyDescriptor->SetEnumerable(FALSE != obj->IsEnumerable(propertyId));
  1107. return TRUE;
  1108. }
  1109. inline RecyclableObject* JavascriptOperators::GetPrototypeNoTrap(RecyclableObject* instance)
  1110. {
  1111. Type* type = instance->GetType();
  1112. if (type->HasSpecialPrototype())
  1113. {
  1114. if (type->GetTypeId() == TypeIds_Proxy)
  1115. {
  1116. // get back null
  1117. Assert(type->GetPrototype() == instance->GetScriptContext()->GetLibrary()->GetNull());
  1118. return type->GetPrototype();
  1119. }
  1120. else
  1121. {
  1122. return instance->GetPrototypeSpecial();
  1123. }
  1124. }
  1125. return type->GetPrototype();
  1126. }
  1127. BOOL JavascriptOperators::IsRemoteArray(RecyclableObject* instance)
  1128. {
  1129. TypeId remoteTypeId = TypeIds_Limit;
  1130. return (JavascriptOperators::GetRemoteTypeId(instance, &remoteTypeId) &&
  1131. DynamicObject::IsAnyArrayTypeId(remoteTypeId));
  1132. }
  1133. bool JavascriptOperators::IsArray(_In_ JavascriptProxy * instance)
  1134. {
  1135. // If it is a proxy, follow to the end of the proxy chain before checking if it is an array again.
  1136. JavascriptProxy * proxy = instance;
  1137. while (true)
  1138. {
  1139. RecyclableObject * targetInstance = proxy->GetTarget();
  1140. proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(targetInstance);
  1141. if (proxy == nullptr)
  1142. {
  1143. return DynamicObject::IsAnyArray(targetInstance) || IsRemoteArray(targetInstance);
  1144. }
  1145. }
  1146. }
  1147. bool JavascriptOperators::IsArray(_In_ RecyclableObject* instance)
  1148. {
  1149. if (DynamicObject::IsAnyArray(instance))
  1150. {
  1151. return TRUE;
  1152. }
  1153. JavascriptProxy* proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(instance);
  1154. if (proxy)
  1155. {
  1156. return IsArray(proxy);
  1157. }
  1158. return IsRemoteArray(instance);
  1159. }
  1160. bool JavascriptOperators::IsArray(_In_ Var instanceVar)
  1161. {
  1162. RecyclableObject* instanceObj = TryFromVar<RecyclableObject>(instanceVar);
  1163. return instanceObj && IsArray(instanceObj);
  1164. }
  1165. bool JavascriptOperators::IsConstructor(_In_ JavascriptProxy * instance)
  1166. {
  1167. // If it is a proxy, follow to the end of the proxy chain before checking if it is a constructor again.
  1168. JavascriptProxy * proxy = instance;
  1169. while (true)
  1170. {
  1171. RecyclableObject* targetInstance = proxy->GetTarget();
  1172. proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(targetInstance);
  1173. if (proxy == nullptr)
  1174. {
  1175. JavascriptFunction* function = JavascriptOperators::TryFromVar<JavascriptFunction>(targetInstance);
  1176. return function && function->IsConstructor();
  1177. }
  1178. }
  1179. }
  1180. bool JavascriptOperators::IsConstructor(_In_ RecyclableObject* instanceObj)
  1181. {
  1182. JavascriptProxy* proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(instanceObj);
  1183. if (proxy)
  1184. {
  1185. return IsConstructor(proxy);
  1186. }
  1187. JavascriptFunction* function = JavascriptOperators::TryFromVar<JavascriptFunction>(instanceObj);
  1188. return function && function->IsConstructor();
  1189. }
  1190. bool JavascriptOperators::IsConstructor(_In_ Var instanceVar)
  1191. {
  1192. RecyclableObject* instanceObj = TryFromVar<RecyclableObject>(instanceVar);
  1193. return instanceObj && IsConstructor(instanceObj);
  1194. }
  1195. BOOL JavascriptOperators::IsConcatSpreadable(Var instanceVar)
  1196. {
  1197. // an object is spreadable under two condition, either it is a JsArray
  1198. // or you define an isconcatSpreadable flag on it.
  1199. if (!JavascriptOperators::IsObject(instanceVar))
  1200. {
  1201. return false;
  1202. }
  1203. RecyclableObject* instance = RecyclableObject::UnsafeFromVar(instanceVar);
  1204. ScriptContext* scriptContext = instance->GetScriptContext();
  1205. if (!PHASE_OFF1(IsConcatSpreadableCachePhase))
  1206. {
  1207. BOOL retVal = FALSE;
  1208. Type *instanceType = instance->GetType();
  1209. IsConcatSpreadableCache *isConcatSpreadableCache = scriptContext->GetThreadContext()->GetIsConcatSpreadableCache();
  1210. if (isConcatSpreadableCache->TryGetIsConcatSpreadable(instanceType, &retVal))
  1211. {
  1212. OUTPUT_TRACE(Phase::IsConcatSpreadableCachePhase, _u("IsConcatSpreadableCache hit: %p\n"), instanceType);
  1213. return retVal;
  1214. }
  1215. Var spreadable = nullptr;
  1216. BOOL hasUserDefinedSpreadable = JavascriptOperators::GetProperty(instance, instance, PropertyIds::_symbolIsConcatSpreadable, &spreadable, scriptContext);
  1217. if (hasUserDefinedSpreadable && spreadable != scriptContext->GetLibrary()->GetUndefined())
  1218. {
  1219. return JavascriptConversion::ToBoolean(spreadable, scriptContext);
  1220. }
  1221. retVal = JavascriptOperators::IsArray(instance);
  1222. if (!hasUserDefinedSpreadable)
  1223. {
  1224. OUTPUT_TRACE(Phase::IsConcatSpreadableCachePhase, _u("IsConcatSpreadableCache saved: %p\n"), instanceType);
  1225. isConcatSpreadableCache->CacheIsConcatSpreadable(instanceType, retVal);
  1226. }
  1227. return retVal;
  1228. }
  1229. Var spreadable = JavascriptOperators::GetProperty(instance, PropertyIds::_symbolIsConcatSpreadable, scriptContext);
  1230. if (spreadable != scriptContext->GetLibrary()->GetUndefined())
  1231. {
  1232. return JavascriptConversion::ToBoolean(spreadable, scriptContext);
  1233. }
  1234. return JavascriptOperators::IsArray(instance);
  1235. }
  1236. bool JavascriptOperators::IsConstructorSuperCall(Arguments args)
  1237. {
  1238. Var newTarget = args.GetNewTarget();
  1239. return args.IsNewCall() && newTarget != nullptr
  1240. && !JavascriptOperators::IsUndefined(newTarget);
  1241. }
  1242. bool JavascriptOperators::GetAndAssertIsConstructorSuperCall(Arguments args)
  1243. {
  1244. bool isCtorSuperCall = JavascriptOperators::IsConstructorSuperCall(args);
  1245. Assert(isCtorSuperCall || !args.IsNewCall()
  1246. || args[0] == nullptr || JavascriptOperators::GetTypeId(args[0]) == TypeIds_HostDispatch);
  1247. return isCtorSuperCall;
  1248. }
  1249. Var JavascriptOperators::OP_LdCustomSpreadIteratorList(Var aRight, ScriptContext* scriptContext)
  1250. {
  1251. JIT_HELPER_REENTRANT_HEADER(Op_ToSpreadedFunctionArgument);
  1252. #if ENABLE_COPYONACCESS_ARRAY
  1253. // We know we're going to read from this array. Do the conversion before we try to perform checks on the head segment.
  1254. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray(aRight);
  1255. #endif
  1256. #ifdef ENABLE_JS_BUILTINS
  1257. scriptContext->GetLibrary()->EnsureBuiltInEngineIsReady();
  1258. #endif
  1259. RecyclableObject* function = GetIteratorFunction(aRight, scriptContext);
  1260. JavascriptMethod method = function->GetEntryPoint();
  1261. if (((JavascriptArray::Is(aRight) &&
  1262. (
  1263. JavascriptLibrary::IsDefaultArrayValuesFunction(function, scriptContext)
  1264. // Verify that the head segment of the array covers all elements with no gaps.
  1265. // Accessing an element on the prototype could have side-effects that would invalidate the optimization.
  1266. && JavascriptArray::UnsafeFromVar(aRight)->GetHead()->next == nullptr
  1267. && JavascriptArray::UnsafeFromVar(aRight)->GetHead()->left == 0
  1268. && JavascriptArray::UnsafeFromVar(aRight)->GetHead()->length == JavascriptArray::FromVar(aRight)->GetLength()
  1269. && JavascriptArray::UnsafeFromVar(aRight)->HasNoMissingValues()
  1270. && !JavascriptArray::UnsafeFromVar(aRight)->IsCrossSiteObject()
  1271. )) ||
  1272. (TypedArrayBase::Is(aRight) && method == TypedArrayBase::EntryInfo::Values.GetOriginalEntryPoint()))
  1273. // We can't optimize away the iterator if the array iterator prototype is user defined.
  1274. && !JavascriptLibrary::ArrayIteratorPrototypeHasUserDefinedNext(scriptContext))
  1275. {
  1276. return RecyclerNew(scriptContext->GetRecycler(), SpreadArgument, aRight, true /*useDirectCall*/, scriptContext->GetLibrary()->GetSpreadArgumentType());
  1277. }
  1278. ThreadContext *threadContext = scriptContext->GetThreadContext();
  1279. Var iteratorVar =
  1280. threadContext->ExecuteImplicitCall(function, ImplicitCall_Accessor, [=]() -> Var
  1281. {
  1282. return CALL_FUNCTION(threadContext, function, CallInfo(Js::CallFlags_Value, 1), aRight);
  1283. });
  1284. if (!JavascriptOperators::IsObject(iteratorVar))
  1285. {
  1286. if (!threadContext->RecordImplicitException())
  1287. {
  1288. return scriptContext->GetLibrary()->GetUndefined();
  1289. }
  1290. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject);
  1291. }
  1292. return RecyclerNew(scriptContext->GetRecycler(), SpreadArgument, iteratorVar, false /*useDirectCall*/, scriptContext->GetLibrary()->GetSpreadArgumentType());
  1293. JIT_HELPER_END(Op_ToSpreadedFunctionArgument);
  1294. }
  1295. BOOL JavascriptOperators::IsPropertyUnscopable(Var instanceVar, JavascriptString *propertyString)
  1296. {
  1297. // This never gets called.
  1298. Throw::InternalError();
  1299. }
  1300. BOOL JavascriptOperators::IsPropertyUnscopable(Var instanceVar, PropertyId propertyId)
  1301. {
  1302. RecyclableObject* instance = RecyclableObject::FromVar(instanceVar);
  1303. ScriptContext * scriptContext = instance->GetScriptContext();
  1304. Var unscopables = JavascriptOperators::GetProperty(instance, PropertyIds::_symbolUnscopables, scriptContext);
  1305. if (JavascriptOperators::IsObject(unscopables))
  1306. {
  1307. DynamicObject *unscopablesList = DynamicObject::FromVar(unscopables);
  1308. Var value = nullptr;
  1309. //8.1.1.2.1.9.c If blocked is not undefined
  1310. if (JavascriptOperators::GetProperty(unscopablesList, propertyId, &value, scriptContext))
  1311. {
  1312. return JavascriptConversion::ToBoolean(value, scriptContext);
  1313. }
  1314. }
  1315. return false;
  1316. }
  1317. BOOL JavascriptOperators::HasProperty(RecyclableObject* instance, PropertyId propertyId)
  1318. {
  1319. while (!JavascriptOperators::IsNull(instance))
  1320. {
  1321. PropertyQueryFlags result = instance->HasPropertyQuery(propertyId, nullptr /*info*/);
  1322. if (result != PropertyQueryFlags::Property_NotFound)
  1323. {
  1324. return JavascriptConversion::PropertyQueryFlagsToBoolean(result); // return false if instance is typed array and HasPropertyQuery() returns PropertyQueryFlags::Property_Found_Undefined
  1325. }
  1326. instance = JavascriptOperators::GetPrototypeNoTrap(instance);
  1327. }
  1328. return false;
  1329. }
  1330. BOOL JavascriptOperators::HasPropertyUnscopables(RecyclableObject* instance, PropertyId propertyId)
  1331. {
  1332. return JavascriptOperators::HasProperty(instance, propertyId)
  1333. && !IsPropertyUnscopable(instance, propertyId);
  1334. }
  1335. BOOL JavascriptOperators::HasRootProperty(RecyclableObject* instance, PropertyId propertyId)
  1336. {
  1337. Assert(RootObjectBase::Is(instance));
  1338. RootObjectBase* rootObject = static_cast<RootObjectBase*>(instance);
  1339. if (rootObject->HasRootProperty(propertyId))
  1340. {
  1341. return true;
  1342. }
  1343. instance = instance->GetPrototype();
  1344. return HasProperty(instance, propertyId);
  1345. }
  1346. BOOL JavascriptOperators::HasProxyOrPrototypeInlineCacheProperty(RecyclableObject* instance, PropertyId propertyId)
  1347. {
  1348. TypeId typeId;
  1349. typeId = JavascriptOperators::GetTypeId(instance);
  1350. if (typeId == Js::TypeIds_Proxy)
  1351. {
  1352. // let's be more aggressive to disable inline prototype cache when proxy is presented in the prototypechain
  1353. return true;
  1354. }
  1355. do
  1356. {
  1357. instance = instance->GetPrototype();
  1358. typeId = JavascriptOperators::GetTypeId(instance);
  1359. if (typeId == Js::TypeIds_Proxy)
  1360. {
  1361. // let's be more aggressive to disable inline prototype cache when proxy is presented in the prototypechain
  1362. return true;
  1363. }
  1364. if (typeId == TypeIds_Null)
  1365. {
  1366. break;
  1367. }
  1368. /* We can rule out object with deferred type handler, because they would have expanded if they are in the cache */
  1369. if (!instance->HasDeferredTypeHandler() && instance->HasProperty(propertyId)) { return true; }
  1370. } while (typeId != TypeIds_Null);
  1371. return false;
  1372. }
  1373. BOOL JavascriptOperators::OP_HasProperty(Var instance, PropertyId propertyId, ScriptContext* scriptContext)
  1374. {
  1375. JIT_HELPER_REENTRANT_HEADER(Op_HasProperty);
  1376. RecyclableObject* object = TaggedNumber::Is(instance) ?
  1377. scriptContext->GetLibrary()->GetNumberPrototype() :
  1378. RecyclableObject::FromVar(instance);
  1379. BOOL result = HasProperty(object, propertyId);
  1380. return result;
  1381. JIT_HELPER_END(Op_HasProperty);
  1382. }
  1383. BOOL JavascriptOperators::OP_HasOwnProperty(Var instance, PropertyId propertyId, ScriptContext* scriptContext)
  1384. {
  1385. RecyclableObject* object = TaggedNumber::Is(instance) ?
  1386. scriptContext->GetLibrary()->GetNumberPrototype() :
  1387. RecyclableObject::FromVar(instance);
  1388. BOOL result = HasOwnProperty(object, propertyId, scriptContext, nullptr);
  1389. return result;
  1390. }
  1391. // CONSIDER: Have logic similar to HasOwnPropertyNoHostObjectForHeapEnum
  1392. BOOL JavascriptOperators::HasOwnPropertyNoHostObject(Var instance, PropertyId propertyId)
  1393. {
  1394. AssertMsg(!TaggedNumber::Is(instance), "HasOwnPropertyNoHostObject int passed");
  1395. RecyclableObject* object = RecyclableObject::FromVar(instance);
  1396. return object && object->HasOwnPropertyNoHostObject(propertyId);
  1397. }
  1398. // CONSIDER: Remove HasOwnPropertyNoHostObjectForHeapEnum and use GetOwnPropertyNoHostObjectForHeapEnum in its place by changing it
  1399. // to return BOOL, true or false with whether the property exists or not, and return the value if not getter/setter as an out param.
  1400. BOOL JavascriptOperators::HasOwnPropertyNoHostObjectForHeapEnum(Var instance, PropertyId propertyId, ScriptContext* requestContext, Var& getter, Var& setter)
  1401. {
  1402. AssertMsg(!TaggedNumber::Is(instance), "HasOwnPropertyNoHostObjectForHeapEnum int passed");
  1403. RecyclableObject * object = RecyclableObject::FromVar(instance);
  1404. if (StaticType::Is(object->GetTypeId()))
  1405. {
  1406. return FALSE;
  1407. }
  1408. getter = setter = NULL;
  1409. DynamicObject* dynamicObject = DynamicObject::FromVar(instance);
  1410. Assert(dynamicObject->GetScriptContext()->IsHeapEnumInProgress());
  1411. if (dynamicObject->UseDynamicObjectForNoHostObjectAccess())
  1412. {
  1413. if (!dynamicObject->DynamicObject::GetAccessors(propertyId, &getter, &setter, requestContext))
  1414. {
  1415. Var value = nullptr;
  1416. if (!JavascriptConversion::PropertyQueryFlagsToBoolean(dynamicObject->DynamicObject::GetPropertyQuery(instance, propertyId, &value, NULL, requestContext)) ||
  1417. (requestContext->IsUndeclBlockVar(value) && (ActivationObject::Is(instance) || RootObjectBase::Is(instance))))
  1418. {
  1419. return FALSE;
  1420. }
  1421. }
  1422. }
  1423. else
  1424. {
  1425. if (!object->GetAccessors(propertyId, &getter, &setter, requestContext))
  1426. {
  1427. Var value = nullptr;
  1428. if (!object->GetProperty(instance, propertyId, &value, NULL, requestContext) ||
  1429. (requestContext->IsUndeclBlockVar(value) && (ActivationObject::Is(instance) || RootObjectBase::Is(instance))))
  1430. {
  1431. return FALSE;
  1432. }
  1433. }
  1434. }
  1435. return TRUE;
  1436. }
  1437. Var JavascriptOperators::GetOwnPropertyNoHostObjectForHeapEnum(Var instance, PropertyId propertyId, ScriptContext* requestContext, Var& getter, Var& setter)
  1438. {
  1439. AssertMsg(!TaggedNumber::Is(instance), "GetDataPropertyNoHostObject int passed");
  1440. Assert(HasOwnPropertyNoHostObjectForHeapEnum(instance, propertyId, requestContext, getter, setter) || getter || setter);
  1441. DynamicObject* dynamicObject = DynamicObject::FromVar(instance);
  1442. getter = setter = NULL;
  1443. if (NULL == dynamicObject)
  1444. {
  1445. return requestContext->GetLibrary()->GetUndefined();
  1446. }
  1447. Var returnVar = requestContext->GetLibrary()->GetUndefined();
  1448. BOOL result = FALSE;
  1449. if (dynamicObject->UseDynamicObjectForNoHostObjectAccess())
  1450. {
  1451. if (! dynamicObject->DynamicObject::GetAccessors(propertyId, &getter, &setter, requestContext))
  1452. {
  1453. result = JavascriptConversion::PropertyQueryFlagsToBoolean((dynamicObject->DynamicObject::GetPropertyQuery(instance, propertyId, &returnVar, NULL, requestContext)));
  1454. }
  1455. }
  1456. else
  1457. {
  1458. if (! dynamicObject->GetAccessors(propertyId, &getter, &setter, requestContext))
  1459. {
  1460. result = dynamicObject->GetProperty(instance, propertyId, &returnVar, NULL, requestContext);
  1461. }
  1462. }
  1463. if (result)
  1464. {
  1465. return returnVar;
  1466. }
  1467. return requestContext->GetLibrary()->GetUndefined();
  1468. }
  1469. BOOL JavascriptOperators::OP_HasOwnPropScoped(Var scope, PropertyId propertyId, Var defaultInstance, ScriptContext* scriptContext)
  1470. {
  1471. AssertMsg(scope == scriptContext->GetLibrary()->GetNull() || JavascriptArray::Is(scope),
  1472. "Invalid scope chain pointer passed - should be null or an array");
  1473. JavascriptArray* arrScope = JavascriptOperators::TryFromVar<JavascriptArray>(scope);
  1474. if (arrScope)
  1475. {
  1476. Var instance = arrScope->DirectGetItem(0);
  1477. return JavascriptOperators::OP_HasOwnProperty(instance, propertyId, scriptContext);
  1478. }
  1479. return JavascriptOperators::OP_HasOwnProperty(defaultInstance, propertyId, scriptContext);
  1480. }
  1481. BOOL JavascriptOperators::GetPropertyUnscopable(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  1482. {
  1483. return GetProperty_Internal<true>(instance, propertyObject, false, propertyId, value, requestContext, info);
  1484. }
  1485. BOOL JavascriptOperators::GetProperty(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  1486. {
  1487. return GetProperty_Internal<false>(instance, propertyObject, false, propertyId, value, requestContext, info);
  1488. }
  1489. BOOL JavascriptOperators::GetRootProperty(Var instance, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  1490. {
  1491. return GetProperty_Internal<false>(instance, RecyclableObject::FromVar(instance), true, propertyId, value, requestContext, info);
  1492. }
  1493. BOOL JavascriptOperators::GetProperty_InternalSimple(Var instance, RecyclableObject* object, PropertyId propertyId, _Outptr_result_maybenull_ Var* value, ScriptContext* requestContext)
  1494. {
  1495. BOOL foundProperty = FALSE;
  1496. Assert(value != nullptr);
  1497. while (!JavascriptOperators::IsNull(object))
  1498. {
  1499. PropertyQueryFlags result = object->GetPropertyQuery(instance, propertyId, value, nullptr, requestContext);
  1500. if (result != PropertyQueryFlags::Property_NotFound)
  1501. {
  1502. foundProperty = JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  1503. break;
  1504. }
  1505. if (object->SkipsPrototype())
  1506. {
  1507. break;
  1508. }
  1509. object = JavascriptOperators::GetPrototypeNoTrap(object);
  1510. }
  1511. if (!foundProperty)
  1512. {
  1513. *value = requestContext->GetMissingPropertyResult();
  1514. }
  1515. return foundProperty;
  1516. }
  1517. template <bool unscopables>
  1518. BOOL JavascriptOperators::GetProperty_Internal(Var instance, RecyclableObject* propertyObject, const bool isRoot, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  1519. {
  1520. if (TaggedNumber::Is(instance))
  1521. {
  1522. PropertyValueInfo::ClearCacheInfo(info);
  1523. }
  1524. RecyclableObject* object = propertyObject;
  1525. BOOL foundProperty = FALSE;
  1526. if (isRoot)
  1527. {
  1528. Assert(RootObjectBase::Is(object));
  1529. RootObjectBase* rootObject = static_cast<RootObjectBase*>(object);
  1530. foundProperty = rootObject->GetRootProperty(instance, propertyId, value, info, requestContext);
  1531. }
  1532. while (!foundProperty && !JavascriptOperators::IsNull(object))
  1533. {
  1534. if (unscopables && IsPropertyUnscopable(object, propertyId))
  1535. {
  1536. break;
  1537. }
  1538. else
  1539. {
  1540. PropertyQueryFlags result = object->GetPropertyQuery(instance, propertyId, value, info, requestContext);
  1541. if (result != PropertyQueryFlags::Property_NotFound)
  1542. {
  1543. foundProperty = JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  1544. break;
  1545. }
  1546. }
  1547. if (object->SkipsPrototype())
  1548. {
  1549. break;
  1550. }
  1551. object = JavascriptOperators::GetPrototypeNoTrap(object);
  1552. }
  1553. if (foundProperty)
  1554. {
  1555. #if ENABLE_FIXED_FIELDS && DBG
  1556. if (DynamicObject::Is(object))
  1557. {
  1558. DynamicObject* dynamicObject = (DynamicObject*)object;
  1559. DynamicTypeHandler* dynamicTypeHandler = dynamicObject->GetDynamicType()->GetTypeHandler();
  1560. Var property;
  1561. if (dynamicTypeHandler->CheckFixedProperty(requestContext->GetPropertyName(propertyId), &property, requestContext))
  1562. {
  1563. bool skipAssert = false;
  1564. if (value != nullptr && Js::RecyclableObject::Is(property))
  1565. {
  1566. Js::RecyclableObject* pObject = Js::RecyclableObject::FromVar(property);
  1567. Js::RecyclableObject* pValue = Js::RecyclableObject::FromVar(*value);
  1568. if (pValue->GetScriptContext() != pObject->GetScriptContext())
  1569. {
  1570. // value was marshaled. skip check
  1571. skipAssert = true;
  1572. }
  1573. }
  1574. Assert(skipAssert || value == nullptr || *value == property);
  1575. }
  1576. }
  1577. #endif
  1578. // Don't cache the information if the value is undecl block var
  1579. // REVIEW: We might want to only check this if we need to (For LdRootFld or ScopedLdFld)
  1580. // Also we might want to throw here instead of checking it again in the caller
  1581. if (value && !requestContext->IsUndeclBlockVar(*value) && !UnscopablesWrapperObject::Is(object))
  1582. {
  1583. CacheOperators::CachePropertyRead(propertyObject, object, isRoot, propertyId, false, info, requestContext);
  1584. }
  1585. #ifdef TELEMETRY_JSO
  1586. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  1587. {
  1588. requestContext->GetTelemetry().GetOpcodeTelemetry().GetProperty(instance, propertyId, value, /*successful: */true);
  1589. }
  1590. #endif
  1591. return TRUE;
  1592. }
  1593. else
  1594. {
  1595. #ifdef MISSING_PROPERTY_STATS
  1596. if (PHASE_STATS1(MissingPropertyCachePhase))
  1597. {
  1598. requestContext->RecordMissingPropertyMiss();
  1599. }
  1600. #endif
  1601. if (PHASE_TRACE1(MissingPropertyCachePhase))
  1602. {
  1603. Output::Print(_u("MissingPropertyCaching: Missing property %d on slow path.\n"), propertyId);
  1604. }
  1605. TryCacheMissingProperty(instance, propertyObject, isRoot, propertyId, requestContext, info);
  1606. #if defined(TELEMETRY_JSO) || defined(TELEMETRY_AddToCache) // enabled for `TELEMETRY_AddToCache`, because this is the property-not-found codepath where the normal TELEMETRY_AddToCache code wouldn't be executed.
  1607. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  1608. {
  1609. if (info && info->AllowResizingPolymorphicInlineCache()) // If in interpreted mode, not JIT.
  1610. {
  1611. requestContext->GetTelemetry().GetOpcodeTelemetry().GetProperty(instance, propertyId, nullptr);
  1612. }
  1613. }
  1614. #endif
  1615. *value = requestContext->GetMissingPropertyResult();
  1616. return FALSE;
  1617. }
  1618. }
  1619. // If the given instance is a type where we can cache missing properties, then cache that the given property ID is missing.
  1620. // cacheInstance is used as startingObject in CachePropertyRead, and might be instance's proto if we are fetching a super property (see #3064).
  1621. void JavascriptOperators::TryCacheMissingProperty(Var instance, Var cacheInstance, bool isRoot, PropertyId propertyId, ScriptContext* requestContext, _Inout_ PropertyValueInfo * info)
  1622. {
  1623. // Here, any well-behaved subclasses of DynamicObject can opt in to getting included in the missing property cache.
  1624. // For now, we only include basic objects and arrays. CustomExternalObject in particular is problematic because in
  1625. // some cases it can add new properties without transitioning its type handler.
  1626. if (PHASE_OFF1(MissingPropertyCachePhase) || isRoot || !(DynamicObject::Is(instance) || DynamicObject::IsAnyArray(instance)))
  1627. {
  1628. return;
  1629. }
  1630. DynamicTypeHandler* handler = DynamicObject::UnsafeFromVar(instance)->GetDynamicType()->GetTypeHandler();
  1631. // Only cache missing property lookups for non-root field loads on objects that have PathTypeHandlers, because only these types have the right behavior
  1632. // when the missing property is later added. DictionaryTypeHandler's introduce the possibility that a stale TypePropertyCache entry with isMissing==true can
  1633. // be left in the cache after the property has been installed in the object's prototype chain. Other changes to optimize accesses to objects that don't
  1634. // override special symbols make it unnecessary to introduce an invalidation scheme to deal with DictionaryTypeHandler's.
  1635. if (!handler->IsPathTypeHandler())
  1636. {
  1637. return;
  1638. }
  1639. #ifdef MISSING_PROPERTY_STATS
  1640. if (PHASE_STATS1(MissingPropertyCachePhase))
  1641. {
  1642. requestContext->RecordMissingPropertyCacheAttempt();
  1643. }
  1644. #endif
  1645. if (PHASE_TRACE1(MissingPropertyCachePhase))
  1646. {
  1647. Output::Print(_u("MissingPropertyCache: Caching missing property for property %d.\n"), propertyId);
  1648. }
  1649. PropertyValueInfo::Set(info, requestContext->GetLibrary()->GetMissingPropertyHolder(), 0);
  1650. CacheOperators::CachePropertyRead(cacheInstance, requestContext->GetLibrary()->GetMissingPropertyHolder(), isRoot, propertyId, true /*isMissing*/, info, requestContext);
  1651. }
  1652. template<bool OutputExistence, typename PropertyKeyType> PropertyQueryFlags QueryGetOrHasProperty(
  1653. Var originalInstance, RecyclableObject* object, PropertyKeyType propertyKey, Var* value, PropertyValueInfo* info, ScriptContext* requestContext);
  1654. template<> PropertyQueryFlags QueryGetOrHasProperty<false /*OutputExistence*/, PropertyId /*PropertyKeyType*/>(
  1655. Var originalInstance, RecyclableObject* object, PropertyId propertyKey, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  1656. {
  1657. return object->GetPropertyQuery(originalInstance, propertyKey, value, info, requestContext);
  1658. }
  1659. template<> PropertyQueryFlags QueryGetOrHasProperty<false /*OutputExistence*/, JavascriptString* /*PropertyKeyType*/>(
  1660. Var originalInstance, RecyclableObject* object, JavascriptString* propertyKey, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  1661. {
  1662. return object->GetPropertyQuery(originalInstance, propertyKey, value, info, requestContext);
  1663. }
  1664. template<> PropertyQueryFlags QueryGetOrHasProperty<true /*OutputExistence*/, PropertyId /*PropertyKeyType*/>(
  1665. Var originalInstance, RecyclableObject* object, PropertyId propertyKey, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  1666. {
  1667. PropertyQueryFlags result = object->HasPropertyQuery(propertyKey, info);
  1668. *value = JavascriptBoolean::ToVar(JavascriptConversion::PropertyQueryFlagsToBoolean(result), requestContext);
  1669. return result;
  1670. }
  1671. template<bool OutputExistence, typename PropertyKeyType>
  1672. BOOL JavascriptOperators::GetPropertyWPCache(Var instance, RecyclableObject* propertyObject, PropertyKeyType propertyKey, Var* value, ScriptContext* requestContext, _Inout_ PropertyValueInfo * info)
  1673. {
  1674. Assert(value);
  1675. RecyclableObject* object = propertyObject;
  1676. while (!JavascriptOperators::IsNull(object))
  1677. {
  1678. PropertyQueryFlags result = QueryGetOrHasProperty<OutputExistence>(instance, object, propertyKey, value, info, requestContext);
  1679. if (result != PropertyQueryFlags::Property_NotFound)
  1680. {
  1681. if (!UnscopablesWrapperObject::Is(object) && info->GetPropertyRecordUsageCache())
  1682. {
  1683. PropertyId propertyId = info->GetPropertyRecordUsageCache()->GetPropertyRecord()->GetPropertyId();
  1684. CacheOperators::CachePropertyRead(instance, object, false, propertyId, false, info, requestContext);
  1685. }
  1686. return JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  1687. }
  1688. // SkipsPrototype refers only to the Get operation, not Has. Some objects like CustomExternalObject respond
  1689. // to HasPropertyQuery with info only about the object itself and GetPropertyQuery with info about its prototype chain.
  1690. // For consistency with the behavior of JavascriptOperators::HasProperty, don't skip prototypes when outputting existence.
  1691. if (!OutputExistence && object->SkipsPrototype())
  1692. {
  1693. break;
  1694. }
  1695. object = JavascriptOperators::GetPrototypeNoTrap(object);
  1696. }
  1697. if (info->GetPropertyRecordUsageCache())
  1698. {
  1699. TryCacheMissingProperty(instance, instance, false /*isRoot*/, info->GetPropertyRecordUsageCache()->GetPropertyRecord()->GetPropertyId(), requestContext, info);
  1700. }
  1701. *value = OutputExistence
  1702. ? requestContext->GetLibrary()->GetFalse()
  1703. : requestContext->GetMissingPropertyResult();
  1704. return FALSE;
  1705. }
  1706. bool JavascriptOperators::GetPropertyObjectForElementAccess(
  1707. _In_ Var instance,
  1708. _In_ Var index,
  1709. _In_ ScriptContext* scriptContext,
  1710. _Out_ RecyclableObject** propertyObject,
  1711. _In_ rtErrors error)
  1712. {
  1713. BOOL isNullOrUndefined = !GetPropertyObject(instance, scriptContext, propertyObject);
  1714. Assert(*propertyObject == instance || TaggedNumber::Is(instance));
  1715. if (isNullOrUndefined)
  1716. {
  1717. if (!scriptContext->GetThreadContext()->RecordImplicitException())
  1718. {
  1719. return false;
  1720. }
  1721. JavascriptError::ThrowTypeError(scriptContext, error, GetPropertyDisplayNameForError(index, scriptContext));
  1722. }
  1723. return true;
  1724. }
  1725. bool JavascriptOperators::GetPropertyObjectForSetElementI(
  1726. _In_ Var instance,
  1727. _In_ Var index,
  1728. _In_ ScriptContext* scriptContext,
  1729. _Out_ RecyclableObject** propertyObject)
  1730. {
  1731. return GetPropertyObjectForElementAccess(instance, index, scriptContext, propertyObject, JSERR_Property_CannotSet_NullOrUndefined);
  1732. }
  1733. bool JavascriptOperators::GetPropertyObjectForGetElementI(
  1734. _In_ Var instance,
  1735. _In_ Var index,
  1736. _In_ ScriptContext* scriptContext,
  1737. _Out_ RecyclableObject** propertyObject)
  1738. {
  1739. return GetPropertyObjectForElementAccess(instance, index, scriptContext, propertyObject, JSERR_Property_CannotGet_NullOrUndefined);
  1740. }
  1741. BOOL JavascriptOperators::GetPropertyObject(Var instance, ScriptContext * scriptContext, RecyclableObject** propertyObject)
  1742. {
  1743. Assert(propertyObject);
  1744. if (TaggedNumber::Is(instance))
  1745. {
  1746. *propertyObject = scriptContext->GetLibrary()->GetNumberPrototype();
  1747. return TRUE;
  1748. }
  1749. RecyclableObject* object = RecyclableObject::UnsafeFromVar(instance);
  1750. *propertyObject = object;
  1751. if (JavascriptOperators::IsUndefinedOrNull(object))
  1752. {
  1753. return FALSE;
  1754. }
  1755. return TRUE;
  1756. }
  1757. #if DBG
  1758. BOOL JavascriptOperators::IsPropertyObject(RecyclableObject * instance)
  1759. {
  1760. TypeId typeId = JavascriptOperators::GetTypeId(instance);
  1761. return (typeId != TypeIds_Integer && typeId != TypeIds_Null && typeId != TypeIds_Undefined);
  1762. }
  1763. #endif
  1764. Var JavascriptOperators::OP_GetProperty(Var instance, PropertyId propertyId, ScriptContext* scriptContext)
  1765. {
  1766. JIT_HELPER_REENTRANT_HEADER(Op_GetProperty);
  1767. RecyclableObject* object = nullptr;
  1768. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  1769. {
  1770. if (scriptContext->GetThreadContext()->RecordImplicitException())
  1771. {
  1772. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined, scriptContext->GetPropertyName(propertyId)->GetBuffer());
  1773. }
  1774. else
  1775. {
  1776. return scriptContext->GetLibrary()->GetUndefined();
  1777. }
  1778. }
  1779. Var result = JavascriptOperators::GetPropertyNoCache(instance, object, propertyId, scriptContext);
  1780. AssertMsg(result != nullptr, "result null in OP_GetProperty");
  1781. return result;
  1782. JIT_HELPER_END(Op_GetProperty);
  1783. }
  1784. Var JavascriptOperators::OP_GetRootProperty(Var instance, PropertyId propertyId, PropertyValueInfo * info, ScriptContext* scriptContext)
  1785. {
  1786. AssertMsg(RootObjectBase::Is(instance), "Root must be an object!");
  1787. Var value = nullptr;
  1788. if (JavascriptOperators::GetRootProperty(RecyclableObject::FromVar(instance), propertyId, &value, scriptContext, info))
  1789. {
  1790. if (scriptContext->IsUndeclBlockVar(value) && scriptContext->GetThreadContext()->RecordImplicitException())
  1791. {
  1792. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UseBeforeDeclaration);
  1793. }
  1794. return value;
  1795. }
  1796. const char16* propertyName = scriptContext->GetPropertyName(propertyId)->GetBuffer();
  1797. JavascriptFunction * caller = nullptr;
  1798. if (JavascriptStackWalker::GetCaller(&caller, scriptContext))
  1799. {
  1800. FunctionBody * callerBody = caller->GetFunctionBody();
  1801. if (callerBody && callerBody->GetUtf8SourceInfo()->GetIsXDomain())
  1802. {
  1803. propertyName = nullptr;
  1804. }
  1805. }
  1806. // Don't error if we disabled implicit calls
  1807. if (scriptContext->GetThreadContext()->RecordImplicitException())
  1808. {
  1809. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UndefVariable, propertyName);
  1810. }
  1811. return scriptContext->GetMissingPropertyResult();
  1812. }
  1813. Var JavascriptOperators::OP_GetThisScoped(FrameDisplay *pScope, Var defaultInstance, ScriptContext* scriptContext)
  1814. {
  1815. // NOTE: If changes are made to this logic be sure to update the debuggers as well
  1816. int length = pScope->GetLength();
  1817. for (int i = 0; i < length; i += 1)
  1818. {
  1819. Var value = nullptr;
  1820. RecyclableObject *obj = RecyclableObject::FromVar(pScope->GetItem(i));
  1821. if (JavascriptOperators::GetProperty(obj, Js::PropertyIds::_this, &value, scriptContext))
  1822. {
  1823. return value;
  1824. }
  1825. }
  1826. return defaultInstance;
  1827. }
  1828. Var JavascriptOperators::OP_UnwrapWithObj(Var aValue)
  1829. {
  1830. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_UnwrapWithObj);
  1831. return RecyclableObject::FromVar(aValue)->GetThisObjectOrUnWrap();
  1832. JIT_HELPER_END(Op_UnwrapWithObj);
  1833. }
  1834. Var JavascriptOperators::OP_GetInstanceScoped(FrameDisplay *pScope, PropertyId propertyId, Var rootObject, Var* thisVar, ScriptContext* scriptContext)
  1835. {
  1836. JIT_HELPER_REENTRANT_HEADER(Op_GetInstanceScoped);
  1837. // Similar to GetPropertyScoped, but instead of returning the property value, we return the instance that
  1838. // owns it, or the global object if no instance is found.
  1839. int i;
  1840. int length = pScope->GetLength();
  1841. for (i = 0; i < length; i++)
  1842. {
  1843. RecyclableObject *obj = (RecyclableObject*)pScope->GetItem(i);
  1844. if (JavascriptOperators::HasProperty(obj, propertyId))
  1845. {
  1846. // HasProperty will call UnscopablesWrapperObject's HasProperty which will do the filtering
  1847. // All we have to do here is unwrap the object hence the api call
  1848. return obj->GetThisAndUnwrappedInstance(thisVar);
  1849. }
  1850. }
  1851. *thisVar = scriptContext->GetLibrary()->GetUndefined();
  1852. if (rootObject != scriptContext->GetGlobalObject())
  1853. {
  1854. if (JavascriptOperators::OP_HasProperty(rootObject, propertyId, scriptContext))
  1855. {
  1856. return rootObject;
  1857. }
  1858. }
  1859. return scriptContext->GetGlobalObject();
  1860. JIT_HELPER_END(Op_GetInstanceScoped);
  1861. }
  1862. Var JavascriptOperators::GetPropertyReference(RecyclableObject *instance, PropertyId propertyId, ScriptContext* requestContext)
  1863. {
  1864. Var value = nullptr;
  1865. PropertyValueInfo info;
  1866. if (JavascriptOperators::GetPropertyReference(instance, propertyId, &value, requestContext, &info))
  1867. {
  1868. Assert(value != nullptr);
  1869. return value;
  1870. }
  1871. return requestContext->GetMissingPropertyResult();
  1872. }
  1873. BOOL JavascriptOperators::GetPropertyReference(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  1874. {
  1875. return GetPropertyReference_Internal(instance, propertyObject, false, propertyId, value, requestContext, info);
  1876. }
  1877. BOOL JavascriptOperators::GetRootPropertyReference(RecyclableObject* instance, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  1878. {
  1879. return GetPropertyReference_Internal(instance, instance, true, propertyId, value, requestContext, info);
  1880. }
  1881. BOOL JavascriptOperators::PropertyReferenceWalkUnscopable(Var instance, RecyclableObject** propertyObject, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  1882. {
  1883. return PropertyReferenceWalk_Impl<true>(instance, propertyObject, propertyId, value, info, requestContext);
  1884. }
  1885. BOOL JavascriptOperators::PropertyReferenceWalk(Var instance, RecyclableObject** propertyObject, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  1886. {
  1887. return PropertyReferenceWalk_Impl<false>(instance, propertyObject, propertyId, value, info, requestContext);
  1888. }
  1889. template <bool unscopables>
  1890. BOOL JavascriptOperators::PropertyReferenceWalk_Impl(Var instance, RecyclableObject** propertyObject, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  1891. {
  1892. BOOL foundProperty = false;
  1893. RecyclableObject* object = *propertyObject;
  1894. while (!foundProperty && !JavascriptOperators::IsNull(object))
  1895. {
  1896. if (unscopables && JavascriptOperators::IsPropertyUnscopable(object, propertyId))
  1897. {
  1898. break;
  1899. }
  1900. else
  1901. {
  1902. PropertyQueryFlags result = object->GetPropertyReferenceQuery(instance, propertyId, value, info, requestContext);
  1903. if (result != PropertyQueryFlags::Property_NotFound)
  1904. {
  1905. foundProperty = JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  1906. break;
  1907. }
  1908. }
  1909. if (object->SkipsPrototype())
  1910. {
  1911. break; // will return false
  1912. }
  1913. object = JavascriptOperators::GetPrototypeNoTrap(object);
  1914. }
  1915. *propertyObject = object;
  1916. return foundProperty;
  1917. }
  1918. BOOL JavascriptOperators::GetPropertyReference_Internal(Var instance, RecyclableObject* propertyObject, const bool isRoot, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  1919. {
  1920. if (TaggedNumber::Is(instance))
  1921. {
  1922. PropertyValueInfo::ClearCacheInfo(info);
  1923. }
  1924. BOOL foundProperty = FALSE;
  1925. RecyclableObject* object = propertyObject;
  1926. if (isRoot)
  1927. {
  1928. foundProperty = RootObjectBase::FromVar(object)->GetRootPropertyReference(instance, propertyId, value, info, requestContext);
  1929. }
  1930. if (!foundProperty)
  1931. {
  1932. foundProperty = PropertyReferenceWalk(instance, &object, propertyId, value, info, requestContext);
  1933. }
  1934. if (!foundProperty)
  1935. {
  1936. #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.
  1937. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  1938. {
  1939. if (info && info->AllowResizingPolymorphicInlineCache()) // If in interpreted mode, not JIT.
  1940. {
  1941. requestContext->GetTelemetry().GetOpcodeTelemetry().GetProperty(instance, propertyId, nullptr);
  1942. }
  1943. }
  1944. #endif
  1945. *value = requestContext->GetMissingPropertyResult();
  1946. return foundProperty;
  1947. }
  1948. if (requestContext->IsUndeclBlockVar(*value))
  1949. {
  1950. JavascriptError::ThrowReferenceError(requestContext, JSERR_UseBeforeDeclaration);
  1951. }
  1952. #if ENABLE_FIXED_FIELDS && DBG
  1953. if (DynamicObject::Is(object))
  1954. {
  1955. DynamicObject* dynamicObject = (DynamicObject*)object;
  1956. DynamicTypeHandler* dynamicTypeHandler = dynamicObject->GetDynamicType()->GetTypeHandler();
  1957. Var property = nullptr;
  1958. if (dynamicTypeHandler->CheckFixedProperty(requestContext->GetPropertyName(propertyId), &property, requestContext))
  1959. {
  1960. Assert(value == nullptr || *value == property);
  1961. }
  1962. }
  1963. #endif
  1964. CacheOperators::CachePropertyRead(instance, object, isRoot, propertyId, false, info, requestContext);
  1965. return TRUE;
  1966. }
  1967. template <typename PropertyKeyType, bool unscopable>
  1968. DescriptorFlags JavascriptOperators::GetterSetter_Impl(RecyclableObject* instance, PropertyKeyType propertyKey, Var* setterValue, PropertyValueInfo* info, ScriptContext* scriptContext)
  1969. {
  1970. DescriptorFlags flags = None;
  1971. RecyclableObject* object = instance;
  1972. while (flags == None && !JavascriptOperators::IsNull(object))
  1973. {
  1974. if (unscopable && IsPropertyUnscopable(object, propertyKey))
  1975. {
  1976. break;
  1977. }
  1978. else
  1979. {
  1980. flags = object->GetSetter(propertyKey, setterValue, info, scriptContext);
  1981. if (flags != None)
  1982. {
  1983. break;
  1984. }
  1985. }
  1986. // CONSIDER: we should add SkipsPrototype support. DOM has no ES 5 concepts built in that aren't
  1987. // already part of our prototype objects which are chakra objects.
  1988. object = object->GetPrototype();
  1989. }
  1990. return flags;
  1991. }
  1992. DescriptorFlags JavascriptOperators::GetterSetterUnscopable(RecyclableObject* instance, PropertyId propertyId, Var* setterValue, PropertyValueInfo* info, ScriptContext* scriptContext)
  1993. {
  1994. return GetterSetter_Impl<PropertyId, true>(instance, propertyId, setterValue, info, scriptContext);
  1995. }
  1996. DescriptorFlags JavascriptOperators::GetterSetter(RecyclableObject* instance, PropertyId propertyId, Var* setterValue, PropertyValueInfo* info, ScriptContext* scriptContext)
  1997. {
  1998. return GetterSetter_Impl<PropertyId, false>(instance, propertyId, setterValue, info, scriptContext);
  1999. }
  2000. DescriptorFlags JavascriptOperators::GetterSetter(RecyclableObject* instance, JavascriptString * propertyName, Var* setterValue, PropertyValueInfo* info, ScriptContext* scriptContext)
  2001. {
  2002. return GetterSetter_Impl<JavascriptString*, false>(instance, propertyName, setterValue, info, scriptContext);
  2003. }
  2004. void JavascriptOperators::OP_InvalidateProtoCaches(PropertyId propertyId, ScriptContext *scriptContext)
  2005. {
  2006. JIT_HELPER_NOT_REENTRANT_HEADER(InvalidateProtoCaches, reentrancylock, scriptContext->GetThreadContext());
  2007. scriptContext->InvalidateProtoCaches(propertyId);
  2008. JIT_HELPER_END(InvalidateProtoCaches);
  2009. }
  2010. // Checks to see if any object in the prototype chain has a property descriptor for the given index
  2011. // that specifies either an accessor or a non-writable attribute.
  2012. // If TRUE, check flags for details.
  2013. BOOL JavascriptOperators::CheckPrototypesForAccessorOrNonWritableItem(RecyclableObject* instance, uint32 index,
  2014. Var* setterValue, DescriptorFlags *flags, ScriptContext* scriptContext, BOOL skipPrototypeCheck /* = FALSE */)
  2015. {
  2016. Assert(setterValue);
  2017. Assert(flags);
  2018. // Do a quick walk up the prototype chain to see if any of the prototypes has ever had ANY setter or non-writable property.
  2019. if (CheckIfObjectAndPrototypeChainHasOnlyWritableDataProperties(instance))
  2020. {
  2021. return FALSE;
  2022. }
  2023. RecyclableObject* object = instance;
  2024. while (!JavascriptOperators::IsNull(object))
  2025. {
  2026. *flags = object->GetItemSetter(index, setterValue, scriptContext);
  2027. if (*flags != None || skipPrototypeCheck)
  2028. {
  2029. break;
  2030. }
  2031. object = object->GetPrototype();
  2032. }
  2033. return ((*flags & Accessor) == Accessor) || ((*flags & Proxy) == Proxy) || ((*flags & Data) == Data && (*flags & Writable) == None);
  2034. }
  2035. BOOL JavascriptOperators::SetGlobalPropertyNoHost(char16 const * propertyName, charcount_t propertyLength, Var value, ScriptContext * scriptContext)
  2036. {
  2037. GlobalObject * globalObject = scriptContext->GetGlobalObject();
  2038. uint32 index;
  2039. PropertyRecord const * propertyRecord = nullptr;
  2040. IndexType indexType = GetIndexTypeFromString(propertyName, propertyLength, scriptContext, &index, &propertyRecord, true);
  2041. if (indexType == IndexType_Number)
  2042. {
  2043. return globalObject->DynamicObject::SetItem(index, value, PropertyOperation_None);
  2044. }
  2045. return globalObject->DynamicObject::SetProperty(propertyRecord->GetPropertyId(), value, PropertyOperation_None, NULL);
  2046. }
  2047. template<typename PropertyKeyType>
  2048. BOOL JavascriptOperators::SetPropertyWPCache(Var receiver, RecyclableObject* object, PropertyKeyType propertyKey, Var newValue, ScriptContext* requestContext, PropertyOperationFlags propertyOperationFlags, _Inout_ PropertyValueInfo * info)
  2049. {
  2050. if (receiver)
  2051. {
  2052. AnalysisAssert(object);
  2053. Assert(!TaggedNumber::Is(receiver));
  2054. Var setterValueOrProxy = nullptr;
  2055. DescriptorFlags flags = None;
  2056. if (JavascriptOperators::CheckPrototypesForAccessorOrNonWritableProperty(object, propertyKey, &setterValueOrProxy, &flags, info, requestContext))
  2057. {
  2058. if ((flags & Accessor) == Accessor)
  2059. {
  2060. if (JavascriptError::ThrowIfStrictModeUndefinedSetter(propertyOperationFlags, setterValueOrProxy, requestContext))
  2061. {
  2062. return TRUE;
  2063. }
  2064. if (setterValueOrProxy)
  2065. {
  2066. if (!UnscopablesWrapperObject::Is(receiver) && info->GetPropertyRecordUsageCache() && !JavascriptOperators::IsUndefinedAccessor(setterValueOrProxy, requestContext))
  2067. {
  2068. CacheOperators::CachePropertyWrite(RecyclableObject::FromVar(receiver), false, object->GetType(), info->GetPropertyRecordUsageCache()->GetPropertyRecord()->GetPropertyId(), info, requestContext);
  2069. }
  2070. receiver = (RecyclableObject::FromVar(receiver))->GetThisObjectOrUnWrap();
  2071. RecyclableObject* func = RecyclableObject::FromVar(setterValueOrProxy);
  2072. JavascriptOperators::CallSetter(func, receiver, newValue, requestContext);
  2073. }
  2074. return TRUE;
  2075. }
  2076. else if ((flags & Proxy) == Proxy)
  2077. {
  2078. Assert(JavascriptProxy::Is(setterValueOrProxy));
  2079. JavascriptProxy* proxy = JavascriptProxy::FromVar(setterValueOrProxy);
  2080. auto fn = [&](RecyclableObject* target) -> BOOL {
  2081. return JavascriptOperators::SetPropertyWPCache(receiver, target, propertyKey, newValue, requestContext, propertyOperationFlags, info);
  2082. };
  2083. if (info->GetPropertyRecordUsageCache())
  2084. {
  2085. PropertyValueInfo::SetNoCache(info, proxy);
  2086. PropertyValueInfo::DisablePrototypeCache(info, proxy);
  2087. }
  2088. return proxy->SetPropertyTrap(receiver, JavascriptProxy::SetPropertyTrapKind::SetPropertyWPCacheKind, propertyKey, newValue, requestContext, propertyOperationFlags);
  2089. }
  2090. else
  2091. {
  2092. Assert((flags & Data) == Data && (flags & Writable) == None);
  2093. requestContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  2094. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2095. return FALSE;
  2096. }
  2097. }
  2098. else if (!JavascriptOperators::IsObject(receiver))
  2099. {
  2100. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2101. return FALSE;
  2102. }
  2103. RecyclableObject* receiverObject = RecyclableObject::FromVar(receiver);
  2104. if (receiver != object)
  2105. {
  2106. // If the receiver object has the property and it is an accessor then return false
  2107. PropertyDescriptor existingDesc;
  2108. if (JavascriptOperators::GetOwnPropertyDescriptor(receiverObject, propertyKey, requestContext, &existingDesc)
  2109. && existingDesc.IsAccessorDescriptor())
  2110. {
  2111. return FALSE;
  2112. }
  2113. }
  2114. Type *typeWithoutProperty = object->GetType();
  2115. // in 9.1.9, step 5, we should return false if receiver is not object, and that will happen in default RecyclableObject operation anyhow.
  2116. if (receiverObject->SetProperty(propertyKey, newValue, propertyOperationFlags, info))
  2117. {
  2118. if (!JavascriptProxy::Is(receiver) && info->GetPropertyRecordUsageCache() && info->GetFlags() != InlineCacheSetterFlag && !object->IsExternal())
  2119. {
  2120. CacheOperators::CachePropertyWrite(RecyclableObject::FromVar(receiver), false, typeWithoutProperty, info->GetPropertyRecordUsageCache()->GetPropertyRecord()->GetPropertyId(), info, requestContext);
  2121. if (info->GetInstance() == receiverObject)
  2122. {
  2123. PropertyValueInfo::SetCacheInfo(info, info->GetPropertyRecordUsageCache()->GetLdElemInlineCache(), info->AllowResizingPolymorphicInlineCache());
  2124. CacheOperators::CachePropertyRead(object, receiverObject, false, info->GetPropertyRecordUsageCache()->GetPropertyRecord()->GetPropertyId(), false, info, requestContext);
  2125. }
  2126. }
  2127. return TRUE;
  2128. }
  2129. }
  2130. return FALSE;
  2131. }
  2132. BOOL JavascriptOperators::SetItemOnTaggedNumber(Var receiver, RecyclableObject* object, uint32 index, Var newValue, ScriptContext* requestContext,
  2133. PropertyOperationFlags propertyOperationFlags)
  2134. {
  2135. Assert(TaggedNumber::Is(receiver));
  2136. if (requestContext->optimizationOverrides.GetSideEffects() & SideEffects_Accessor)
  2137. {
  2138. Var setterValueOrProxy = nullptr;
  2139. DescriptorFlags flags = None;
  2140. if (object == nullptr)
  2141. {
  2142. GetPropertyObject(receiver, requestContext, &object);
  2143. }
  2144. if (JavascriptOperators::CheckPrototypesForAccessorOrNonWritableItem(object, index, &setterValueOrProxy, &flags, requestContext))
  2145. {
  2146. if ((flags & Accessor) == Accessor)
  2147. {
  2148. if (JavascriptError::ThrowIfStrictModeUndefinedSetter(propertyOperationFlags, setterValueOrProxy, requestContext))
  2149. {
  2150. return TRUE;
  2151. }
  2152. if (setterValueOrProxy)
  2153. {
  2154. RecyclableObject* func = RecyclableObject::FromVar(setterValueOrProxy);
  2155. JavascriptOperators::CallSetter(func, receiver, newValue, requestContext);
  2156. return TRUE;
  2157. }
  2158. }
  2159. else if ((flags & Proxy) == Proxy)
  2160. {
  2161. Assert(JavascriptProxy::Is(setterValueOrProxy));
  2162. JavascriptProxy* proxy = JavascriptProxy::FromVar(setterValueOrProxy);
  2163. const PropertyRecord* propertyRecord = nullptr;
  2164. proxy->PropertyIdFromInt(index, &propertyRecord);
  2165. return proxy->SetPropertyTrap(receiver, JavascriptProxy::SetPropertyTrapKind::SetItemOnTaggedNumberKind, propertyRecord->GetPropertyId(), newValue, requestContext, propertyOperationFlags);
  2166. }
  2167. else
  2168. {
  2169. Assert((flags & Data) == Data && (flags & Writable) == None);
  2170. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2171. }
  2172. }
  2173. }
  2174. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2175. return FALSE;
  2176. }
  2177. BOOL JavascriptOperators::SetPropertyOnTaggedNumber(Var receiver, RecyclableObject* object, PropertyId propertyId, Var newValue, ScriptContext* requestContext,
  2178. PropertyOperationFlags propertyOperationFlags)
  2179. {
  2180. Assert (TaggedNumber::Is(receiver));
  2181. if (requestContext->optimizationOverrides.GetSideEffects() & SideEffects_Accessor)
  2182. {
  2183. Var setterValueOrProxy = nullptr;
  2184. PropertyValueInfo info;
  2185. DescriptorFlags flags = None;
  2186. if (object == nullptr)
  2187. {
  2188. GetPropertyObject(receiver, requestContext, &object);
  2189. }
  2190. if (JavascriptOperators::CheckPrototypesForAccessorOrNonWritableProperty(object, propertyId, &setterValueOrProxy, &flags, &info, requestContext))
  2191. {
  2192. if ((flags & Accessor) == Accessor)
  2193. {
  2194. if (JavascriptError::ThrowIfStrictModeUndefinedSetter(propertyOperationFlags, setterValueOrProxy, requestContext))
  2195. {
  2196. return TRUE;
  2197. }
  2198. if (setterValueOrProxy)
  2199. {
  2200. RecyclableObject* func = RecyclableObject::FromVar(setterValueOrProxy);
  2201. Assert(info.GetFlags() == InlineCacheSetterFlag || info.GetPropertyIndex() == Constants::NoSlot);
  2202. JavascriptOperators::CallSetter(func, receiver, newValue, requestContext);
  2203. return TRUE;
  2204. }
  2205. }
  2206. else if ((flags & Proxy) == Proxy)
  2207. {
  2208. Assert(JavascriptProxy::Is(setterValueOrProxy));
  2209. JavascriptProxy* proxy = JavascriptProxy::FromVar(setterValueOrProxy);
  2210. return proxy->SetPropertyTrap(receiver, JavascriptProxy::SetPropertyTrapKind::SetPropertyOnTaggedNumberKind, propertyId, newValue, requestContext, propertyOperationFlags);
  2211. }
  2212. else
  2213. {
  2214. Assert((flags & Data) == Data && (flags & Writable) == None);
  2215. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2216. }
  2217. }
  2218. }
  2219. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  2220. requestContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  2221. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2222. return FALSE;
  2223. }
  2224. BOOL JavascriptOperators::SetPropertyUnscopable(Var instance, RecyclableObject* receiver, PropertyId propertyId, Var newValue, PropertyValueInfo * info, ScriptContext* requestContext, PropertyOperationFlags propertyOperationFlags)
  2225. {
  2226. return SetProperty_Internal<true>(instance, receiver, false, propertyId, newValue, info, requestContext, propertyOperationFlags);
  2227. }
  2228. BOOL JavascriptOperators::SetProperty(Var receiver, RecyclableObject* object, PropertyId propertyId, Var newValue, PropertyValueInfo * info, ScriptContext* requestContext, PropertyOperationFlags propertyOperationFlags)
  2229. {
  2230. return SetProperty_Internal<false>(receiver, object, false, propertyId, newValue, info, requestContext, propertyOperationFlags);
  2231. }
  2232. BOOL JavascriptOperators::SetRootProperty(RecyclableObject* instance, PropertyId propertyId, Var newValue, PropertyValueInfo * info, ScriptContext* requestContext, PropertyOperationFlags propertyOperationFlags)
  2233. {
  2234. return SetProperty_Internal<false>(instance, instance, true, propertyId, newValue, info, requestContext, propertyOperationFlags);
  2235. }
  2236. // Returns true if a result was written.
  2237. bool JavascriptOperators::SetAccessorOrNonWritableProperty(
  2238. Var receiver,
  2239. RecyclableObject* object,
  2240. PropertyId propertyId,
  2241. Var newValue,
  2242. PropertyValueInfo * info,
  2243. ScriptContext* requestContext,
  2244. PropertyOperationFlags propertyOperationFlags,
  2245. bool isRoot,
  2246. bool allowUndecInConsoleScope,
  2247. BOOL *result)
  2248. {
  2249. *result = FALSE;
  2250. Var setterValueOrProxy = nullptr;
  2251. DescriptorFlags flags = None;
  2252. if ((isRoot && JavascriptOperators::CheckPrototypesForAccessorOrNonWritableRootProperty(object, propertyId, &setterValueOrProxy, &flags, info, requestContext)) ||
  2253. (!isRoot && JavascriptOperators::CheckPrototypesForAccessorOrNonWritableProperty(object, propertyId, &setterValueOrProxy, &flags, info, requestContext)))
  2254. {
  2255. if ((flags & Accessor) == Accessor)
  2256. {
  2257. if (JavascriptError::ThrowIfStrictModeUndefinedSetter(propertyOperationFlags, setterValueOrProxy, requestContext) ||
  2258. JavascriptError::ThrowIfNotExtensibleUndefinedSetter(propertyOperationFlags, setterValueOrProxy, requestContext))
  2259. {
  2260. *result = TRUE;
  2261. return true;
  2262. }
  2263. if (setterValueOrProxy)
  2264. {
  2265. RecyclableObject* func = RecyclableObject::FromVar(setterValueOrProxy);
  2266. Assert(!info || info->GetFlags() == InlineCacheSetterFlag || info->GetPropertyIndex() == Constants::NoSlot);
  2267. if (UnscopablesWrapperObject::Is(receiver))
  2268. {
  2269. receiver = (RecyclableObject::FromVar(receiver))->GetThisObjectOrUnWrap();
  2270. }
  2271. else if (!JavascriptOperators::IsUndefinedAccessor(setterValueOrProxy, requestContext))
  2272. {
  2273. CacheOperators::CachePropertyWrite(RecyclableObject::FromVar(receiver), isRoot, object->GetType(), propertyId, info, requestContext);
  2274. }
  2275. #ifdef ENABLE_MUTATION_BREAKPOINT
  2276. if (MutationBreakpoint::IsFeatureEnabled(requestContext))
  2277. {
  2278. MutationBreakpoint::HandleSetProperty(requestContext, object, propertyId, newValue);
  2279. }
  2280. #endif
  2281. JavascriptOperators::CallSetter(func, receiver, newValue, requestContext);
  2282. }
  2283. *result = TRUE;
  2284. return true;
  2285. }
  2286. else if ((flags & Proxy) == Proxy)
  2287. {
  2288. Assert(JavascriptProxy::Is(setterValueOrProxy));
  2289. JavascriptProxy* proxy = JavascriptProxy::FromVar(setterValueOrProxy);
  2290. // 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
  2291. // invalidate when target, handler, or handler prototype has changed. We don't have a way to achieve this yet.
  2292. PropertyValueInfo::SetNoCache(info, proxy);
  2293. PropertyValueInfo::DisablePrototypeCache(info, proxy); // We can't cache prototype property either
  2294. *result = proxy->SetPropertyTrap(receiver, JavascriptProxy::SetPropertyTrapKind::SetPropertyKind, propertyId, newValue, requestContext, propertyOperationFlags);
  2295. return true;
  2296. }
  2297. else
  2298. {
  2299. Assert((flags & Data) == Data && (flags & Writable) == None);
  2300. if (!allowUndecInConsoleScope)
  2301. {
  2302. if (flags & Const)
  2303. {
  2304. JavascriptError::ThrowTypeError(requestContext, ERRAssignmentToConst);
  2305. }
  2306. JavascriptError::ThrowCantAssign(propertyOperationFlags, requestContext, propertyId);
  2307. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2308. *result = FALSE;
  2309. return true;
  2310. }
  2311. }
  2312. }
  2313. return false;
  2314. }
  2315. template <bool unscopables>
  2316. BOOL JavascriptOperators::SetProperty_Internal(Var receiver, RecyclableObject* object, const bool isRoot, PropertyId propertyId, Var newValue, PropertyValueInfo * info, ScriptContext* requestContext, PropertyOperationFlags propertyOperationFlags)
  2317. {
  2318. if (receiver == nullptr)
  2319. {
  2320. return FALSE;
  2321. }
  2322. Assert(!TaggedNumber::Is(receiver));
  2323. BOOL setAccessorResult = FALSE;
  2324. if (SetAccessorOrNonWritableProperty(receiver, object, propertyId, newValue, info, requestContext, propertyOperationFlags, isRoot, false, &setAccessorResult))
  2325. {
  2326. return setAccessorResult;
  2327. }
  2328. else if (!JavascriptOperators::IsObject(receiver))
  2329. {
  2330. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, requestContext);
  2331. return FALSE;
  2332. }
  2333. #ifdef ENABLE_MUTATION_BREAKPOINT
  2334. // Break on mutation if needed
  2335. bool doNotUpdateCacheForMbp = MutationBreakpoint::IsFeatureEnabled(requestContext) ?
  2336. MutationBreakpoint::HandleSetProperty(requestContext, object, propertyId, newValue) : false;
  2337. #endif
  2338. // Get the original type before setting the property
  2339. Type *typeWithoutProperty = object->GetType();
  2340. BOOL didSetProperty = false;
  2341. if (isRoot)
  2342. {
  2343. AssertMsg(JavascriptOperators::GetTypeId(receiver) == TypeIds_GlobalObject
  2344. || JavascriptOperators::GetTypeId(receiver) == TypeIds_ModuleRoot,
  2345. "Root must be a global object!");
  2346. RootObjectBase* rootObject = static_cast<RootObjectBase*>(receiver);
  2347. didSetProperty = rootObject->SetRootProperty(propertyId, newValue, propertyOperationFlags, info);
  2348. }
  2349. else
  2350. {
  2351. RecyclableObject* instanceObject = RecyclableObject::FromVar(receiver);
  2352. while (!JavascriptOperators::IsNull(instanceObject))
  2353. {
  2354. if (unscopables && JavascriptOperators::IsPropertyUnscopable(instanceObject, propertyId))
  2355. {
  2356. break;
  2357. }
  2358. else
  2359. {
  2360. didSetProperty = instanceObject->SetProperty(propertyId, newValue, propertyOperationFlags, info);
  2361. if (didSetProperty || !unscopables)
  2362. {
  2363. break;
  2364. }
  2365. }
  2366. instanceObject = JavascriptOperators::GetPrototypeNoTrap(instanceObject);
  2367. }
  2368. }
  2369. if (didSetProperty)
  2370. {
  2371. bool updateCache = true;
  2372. #ifdef ENABLE_MUTATION_BREAKPOINT
  2373. updateCache = updateCache && !doNotUpdateCacheForMbp;
  2374. #endif
  2375. if (updateCache)
  2376. {
  2377. if (!JavascriptProxy::Is(receiver))
  2378. {
  2379. CacheOperators::CachePropertyWrite(RecyclableObject::FromVar(receiver), isRoot, typeWithoutProperty, propertyId, info, requestContext);
  2380. }
  2381. }
  2382. return TRUE;
  2383. }
  2384. return FALSE;
  2385. }
  2386. BOOL JavascriptOperators::IsNumberFromNativeArray(Var instance, uint32 index, ScriptContext* scriptContext)
  2387. {
  2388. #if ENABLE_COPYONACCESS_ARRAY
  2389. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  2390. #endif
  2391. Js::TypeId instanceType = JavascriptOperators::GetTypeId(instance);
  2392. // Fast path for native and typed arrays.
  2393. bool isNativeArray = instanceType == TypeIds_NativeIntArray || instanceType == TypeIds_NativeFloatArray;
  2394. bool isTypedArray = instanceType >= TypeIds_Int8Array && instanceType <= TypeIds_Uint64Array;
  2395. if (isNativeArray || isTypedArray)
  2396. {
  2397. // Check if the typed array is detached to prevent an exception in GetOwnItem
  2398. if (isTypedArray && TypedArrayBase::IsDetachedTypedArray(instance))
  2399. {
  2400. return FALSE;
  2401. }
  2402. RecyclableObject* object = RecyclableObject::FromVar(instance);
  2403. Var member = nullptr;
  2404. // If the item is found in the array own body, then it is a number
  2405. if (JavascriptOperators::GetOwnItem(object, index, &member, scriptContext)
  2406. && !JavascriptOperators::IsUndefined(member))
  2407. {
  2408. return TRUE;
  2409. }
  2410. }
  2411. return FALSE;
  2412. }
  2413. BOOL _Check_return_ _Success_(return) JavascriptOperators::GetAccessors(RecyclableObject* instance, PropertyId propertyId, ScriptContext* requestContext, _Out_ Var* getter, _Out_ Var* setter)
  2414. {
  2415. RecyclableObject* object = instance;
  2416. while (!JavascriptOperators::IsNull(object))
  2417. {
  2418. if (object->GetAccessors(propertyId, getter, setter, requestContext))
  2419. {
  2420. *getter = JavascriptOperators::CanonicalizeAccessor(*getter, requestContext);
  2421. *setter = JavascriptOperators::CanonicalizeAccessor(*setter, requestContext);
  2422. return TRUE;
  2423. }
  2424. if (object->SkipsPrototype())
  2425. {
  2426. break;
  2427. }
  2428. object = JavascriptOperators::GetPrototype(object);
  2429. }
  2430. return FALSE;
  2431. }
  2432. BOOL JavascriptOperators::SetAccessors(RecyclableObject* instance, PropertyId propertyId, Var getter, Var setter, PropertyOperationFlags flags)
  2433. {
  2434. BOOL result = instance && instance->SetAccessors(propertyId, getter, setter, flags);
  2435. return result;
  2436. }
  2437. BOOL JavascriptOperators::OP_SetProperty(Var instance, PropertyId propertyId, Var newValue, ScriptContext* scriptContext, PropertyValueInfo * info, PropertyOperationFlags flags, Var thisInstance)
  2438. {
  2439. // The call into ToObject(dynamicObject) is avoided here by checking for null and undefined and doing nothing when dynamicObject is a primitive value.
  2440. if (thisInstance == nullptr)
  2441. {
  2442. thisInstance = instance;
  2443. }
  2444. TypeId typeId = JavascriptOperators::GetTypeId(instance);
  2445. if (JavascriptOperators::IsUndefinedOrNullType(typeId))
  2446. {
  2447. if (scriptContext->GetThreadContext()->RecordImplicitException())
  2448. {
  2449. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotSet_NullOrUndefined, scriptContext->GetPropertyName(propertyId)->GetBuffer());
  2450. }
  2451. return TRUE;
  2452. }
  2453. else if (typeId == TypeIds_VariantDate)
  2454. {
  2455. if (scriptContext->GetThreadContext()->RecordImplicitException())
  2456. {
  2457. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_VarDate, scriptContext->GetPropertyName(propertyId)->GetBuffer());
  2458. }
  2459. return TRUE;
  2460. }
  2461. if (!TaggedNumber::Is(instance) && !TaggedNumber::Is(thisInstance))
  2462. {
  2463. return JavascriptOperators::SetProperty(RecyclableObject::UnsafeFromVar(thisInstance), RecyclableObject::UnsafeFromVar(instance), propertyId, newValue, info, scriptContext, flags);
  2464. }
  2465. JavascriptError::ThrowCantAssignIfStrictMode(flags, scriptContext);
  2466. return false;
  2467. }
  2468. BOOL JavascriptOperators::OP_StFunctionExpression(Var obj, PropertyId propertyId, Var newValue)
  2469. {
  2470. RecyclableObject* instance = RecyclableObject::FromVar(obj);
  2471. JIT_HELPER_NOT_REENTRANT_HEADER(Op_StFunctionExpression, reentrancylock, instance->GetScriptContext()->GetThreadContext());
  2472. instance->SetProperty(propertyId, newValue, PropertyOperation_None, NULL);
  2473. instance->SetWritable(propertyId, FALSE);
  2474. instance->SetConfigurable(propertyId, FALSE);
  2475. return TRUE;
  2476. JIT_HELPER_END(Op_StFunctionExpression);
  2477. }
  2478. BOOL JavascriptOperators::OP_InitClassMember(Var obj, PropertyId propertyId, Var newValue)
  2479. {
  2480. JIT_HELPER_REENTRANT_HEADER(Op_InitClassMember);
  2481. RecyclableObject* instance = RecyclableObject::FromVar(obj);
  2482. PropertyOperationFlags flags = PropertyOperation_None;
  2483. PropertyAttributes attributes = PropertyClassMemberDefaults;
  2484. instance->SetPropertyWithAttributes(propertyId, newValue, attributes, NULL, flags);
  2485. return TRUE;
  2486. JIT_HELPER_END(Op_InitClassMember);
  2487. }
  2488. BOOL JavascriptOperators::OP_InitLetProperty(Var obj, PropertyId propertyId, Var newValue)
  2489. {
  2490. JIT_HELPER_REENTRANT_HEADER(Op_InitLetFld);
  2491. RecyclableObject* instance = RecyclableObject::FromVar(obj);
  2492. PropertyOperationFlags flags = instance->GetScriptContext()->IsUndeclBlockVar(newValue) ? PropertyOperation_SpecialValue : PropertyOperation_None;
  2493. PropertyAttributes attributes = PropertyLetDefaults;
  2494. if (RootObjectBase::Is(instance))
  2495. {
  2496. attributes |= PropertyLetConstGlobal;
  2497. }
  2498. instance->SetPropertyWithAttributes(propertyId, newValue, attributes, NULL, (PropertyOperationFlags)(flags | PropertyOperation_AllowUndecl));
  2499. return TRUE;
  2500. JIT_HELPER_END(Op_InitLetFld);
  2501. }
  2502. BOOL JavascriptOperators::OP_InitConstProperty(Var obj, PropertyId propertyId, Var newValue)
  2503. {
  2504. RecyclableObject* instance = RecyclableObject::FromVar(obj);
  2505. JIT_HELPER_REENTRANT_HEADER(Op_InitConstFld);
  2506. PropertyOperationFlags flags = instance->GetScriptContext()->IsUndeclBlockVar(newValue) ? PropertyOperation_SpecialValue : PropertyOperation_None;
  2507. PropertyAttributes attributes = PropertyConstDefaults;
  2508. if (RootObjectBase::Is(instance))
  2509. {
  2510. attributes |= PropertyLetConstGlobal;
  2511. }
  2512. instance->SetPropertyWithAttributes(propertyId, newValue, attributes, NULL, (PropertyOperationFlags)(flags | PropertyOperation_AllowUndecl));
  2513. return TRUE;
  2514. JIT_HELPER_END(Op_InitConstFld);
  2515. }
  2516. BOOL JavascriptOperators::OP_InitUndeclRootLetProperty(Var obj, PropertyId propertyId)
  2517. {
  2518. RecyclableObject* instance = RecyclableObject::FromVar(obj);
  2519. JIT_HELPER_NOT_REENTRANT_HEADER(Op_InitUndeclRootLetFld, reentrancylock, instance->GetScriptContext()->GetThreadContext());
  2520. PropertyOperationFlags flags = static_cast<PropertyOperationFlags>(PropertyOperation_SpecialValue | PropertyOperation_AllowUndecl);
  2521. PropertyAttributes attributes = PropertyLetDefaults | PropertyLetConstGlobal;
  2522. instance->SetPropertyWithAttributes(propertyId, instance->GetLibrary()->GetUndeclBlockVar(), attributes, NULL, flags);
  2523. return TRUE;
  2524. JIT_HELPER_END(Op_InitUndeclRootLetFld);
  2525. }
  2526. BOOL JavascriptOperators::OP_InitUndeclRootConstProperty(Var obj, PropertyId propertyId)
  2527. {
  2528. RecyclableObject* instance = RecyclableObject::FromVar(obj);
  2529. JIT_HELPER_NOT_REENTRANT_HEADER(Op_InitUndeclRootConstFld, reentrancylock, instance->GetScriptContext()->GetThreadContext());
  2530. PropertyOperationFlags flags = static_cast<PropertyOperationFlags>(PropertyOperation_SpecialValue | PropertyOperation_AllowUndecl);
  2531. PropertyAttributes attributes = PropertyConstDefaults | PropertyLetConstGlobal;
  2532. instance->SetPropertyWithAttributes(propertyId, instance->GetLibrary()->GetUndeclBlockVar(), attributes, NULL, flags);
  2533. return TRUE;
  2534. JIT_HELPER_END(Op_InitUndeclRootConstFld);
  2535. }
  2536. BOOL JavascriptOperators::OP_InitUndeclConsoleLetProperty(Var obj, PropertyId propertyId)
  2537. {
  2538. FrameDisplay *pScope = (FrameDisplay*)obj;
  2539. AssertMsg(ConsoleScopeActivationObject::Is((DynamicObject*)pScope->GetItem(pScope->GetLength() - 1)), "How come we got this opcode without ConsoleScopeActivationObject?");
  2540. RecyclableObject* instance = RecyclableObject::FromVar(pScope->GetItem(0));
  2541. JIT_HELPER_NOT_REENTRANT_HEADER(Op_InitUndeclConsoleLetFld, reentrancylock, instance->GetScriptContext()->GetThreadContext());
  2542. PropertyOperationFlags flags = static_cast<PropertyOperationFlags>(PropertyOperation_SpecialValue | PropertyOperation_AllowUndecl);
  2543. PropertyAttributes attributes = PropertyLetDefaults;
  2544. instance->SetPropertyWithAttributes(propertyId, instance->GetLibrary()->GetUndeclBlockVar(), attributes, NULL, flags);
  2545. return TRUE;
  2546. JIT_HELPER_END(Op_InitUndeclConsoleLetFld);
  2547. }
  2548. BOOL JavascriptOperators::OP_InitUndeclConsoleConstProperty(Var obj, PropertyId propertyId)
  2549. {
  2550. FrameDisplay *pScope = (FrameDisplay*)obj;
  2551. AssertMsg(ConsoleScopeActivationObject::Is((DynamicObject*)pScope->GetItem(pScope->GetLength() - 1)), "How come we got this opcode without ConsoleScopeActivationObject?");
  2552. RecyclableObject* instance = RecyclableObject::FromVar(pScope->GetItem(0));
  2553. JIT_HELPER_NOT_REENTRANT_HEADER(Op_InitUndeclConsoleConstFld, reentrancylock, instance->GetScriptContext()->GetThreadContext());
  2554. PropertyOperationFlags flags = static_cast<PropertyOperationFlags>(PropertyOperation_SpecialValue | PropertyOperation_AllowUndecl);
  2555. PropertyAttributes attributes = PropertyConstDefaults;
  2556. instance->SetPropertyWithAttributes(propertyId, instance->GetLibrary()->GetUndeclBlockVar(), attributes, NULL, flags);
  2557. return TRUE;
  2558. JIT_HELPER_END(Op_InitUndeclConsoleConstFld);
  2559. }
  2560. BOOL JavascriptOperators::InitProperty(RecyclableObject* instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  2561. {
  2562. return instance && instance->InitProperty(propertyId, newValue, flags);
  2563. }
  2564. BOOL JavascriptOperators::OP_InitProperty(Var instance, PropertyId propertyId, Var newValue)
  2565. {
  2566. if(TaggedNumber::Is(instance)) { return false; }
  2567. return JavascriptOperators::InitProperty(RecyclableObject::FromVar(instance), propertyId, newValue);
  2568. }
  2569. BOOL JavascriptOperators::DeleteProperty(RecyclableObject* instance, PropertyId propertyId, PropertyOperationFlags propertyOperationFlags)
  2570. {
  2571. return DeleteProperty_Impl<false>(instance, propertyId, propertyOperationFlags);
  2572. }
  2573. bool JavascriptOperators::ShouldTryDeleteProperty(RecyclableObject* instance, JavascriptString *propertyNameString, PropertyRecord const **pPropertyRecord)
  2574. {
  2575. PropertyRecord const *propertyRecord = nullptr;
  2576. if (!JavascriptOperators::CanShortcutOnUnknownPropertyName(instance))
  2577. {
  2578. instance->GetScriptContext()->GetOrAddPropertyRecord(propertyNameString, &propertyRecord);
  2579. }
  2580. else
  2581. {
  2582. instance->GetScriptContext()->FindPropertyRecord(propertyNameString, &propertyRecord);
  2583. }
  2584. if (propertyRecord == nullptr)
  2585. {
  2586. return false;
  2587. }
  2588. *pPropertyRecord = propertyRecord;
  2589. return true;
  2590. }
  2591. BOOL JavascriptOperators::DeleteProperty(RecyclableObject* instance, JavascriptString *propertyNameString, PropertyOperationFlags propertyOperationFlags)
  2592. {
  2593. #ifdef ENABLE_MUTATION_BREAKPOINT
  2594. ScriptContext *scriptContext = instance->GetScriptContext();
  2595. if (MutationBreakpoint::IsFeatureEnabled(scriptContext)
  2596. && scriptContext->HasMutationBreakpoints())
  2597. {
  2598. MutationBreakpoint::HandleDeleteProperty(scriptContext, instance, propertyNameString);
  2599. }
  2600. #endif
  2601. return instance->DeleteProperty(propertyNameString, propertyOperationFlags);
  2602. }
  2603. BOOL JavascriptOperators::DeletePropertyUnscopables(RecyclableObject* instance, PropertyId propertyId, PropertyOperationFlags propertyOperationFlags)
  2604. {
  2605. return DeleteProperty_Impl<true>(instance, propertyId, propertyOperationFlags);
  2606. }
  2607. template<bool unscopables>
  2608. BOOL JavascriptOperators::DeleteProperty_Impl(RecyclableObject* instance, PropertyId propertyId, PropertyOperationFlags propertyOperationFlags)
  2609. {
  2610. if (unscopables && JavascriptOperators::IsPropertyUnscopable(instance, propertyId))
  2611. {
  2612. return false;
  2613. }
  2614. #ifdef ENABLE_MUTATION_BREAKPOINT
  2615. ScriptContext *scriptContext = instance->GetScriptContext();
  2616. if (MutationBreakpoint::IsFeatureEnabled(scriptContext)
  2617. && scriptContext->HasMutationBreakpoints())
  2618. {
  2619. MutationBreakpoint::HandleDeleteProperty(scriptContext, instance, propertyId);
  2620. }
  2621. #endif
  2622. // !unscopables will hit the return statement on the first iteration
  2623. return instance->DeleteProperty(propertyId, propertyOperationFlags);
  2624. }
  2625. Var JavascriptOperators::OP_DeleteProperty(Var instance, PropertyId propertyId, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags)
  2626. {
  2627. JIT_HELPER_REENTRANT_HEADER(Op_DeleteProperty);
  2628. if(TaggedNumber::Is(instance))
  2629. {
  2630. return scriptContext->GetLibrary()->GetTrue();
  2631. }
  2632. RecyclableObject* recyclableObject = RecyclableObject::FromVar(instance);
  2633. if (JavascriptOperators::IsUndefinedOrNull(recyclableObject))
  2634. {
  2635. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotDelete_NullOrUndefined,
  2636. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  2637. }
  2638. return scriptContext->GetLibrary()->CreateBoolean(
  2639. JavascriptOperators::DeleteProperty(recyclableObject, propertyId, propertyOperationFlags));
  2640. JIT_HELPER_END(Op_DeleteProperty);
  2641. }
  2642. Var JavascriptOperators::OP_DeleteRootProperty(Var instance, PropertyId propertyId, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags)
  2643. {
  2644. // In Edge the root is an External Object which can call Dispose and thus, can have reentrancy.
  2645. JIT_HELPER_REENTRANT_HEADER(Op_DeleteRootProperty);
  2646. AssertMsg(RootObjectBase::Is(instance), "Root must be a global object!");
  2647. RootObjectBase* rootObject = static_cast<RootObjectBase*>(instance);
  2648. return scriptContext->GetLibrary()->CreateBoolean(
  2649. rootObject->DeleteRootProperty(propertyId, propertyOperationFlags));
  2650. JIT_HELPER_END(Op_DeleteRootProperty);
  2651. }
  2652. template <bool IsFromFullJit, class TInlineCache>
  2653. inline void JavascriptOperators::PatchSetPropertyScoped(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var newValue, Var defaultInstance, PropertyOperationFlags propertyOperationFlags)
  2654. {
  2655. JIT_HELPER_REENTRANT_HEADER(Op_PatchSetPropertyScoped);
  2656. // Set the property using a scope stack rather than an individual instance.
  2657. // Walk the stack until we find an instance that has the property and store
  2658. // the new value there.
  2659. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  2660. uint16 length = pDisplay->GetLength();
  2661. RecyclableObject *object;
  2662. PropertyValueInfo info;
  2663. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  2664. bool allowUndecInConsoleScope = (propertyOperationFlags & PropertyOperation_AllowUndeclInConsoleScope) == PropertyOperation_AllowUndeclInConsoleScope;
  2665. bool isLexicalThisSlotSymbol = (propertyId == PropertyIds::_this);
  2666. for (uint16 i = 0; i < length; i++)
  2667. {
  2668. object = RecyclableObject::UnsafeFromVar(pDisplay->GetItem(i));
  2669. AssertMsg(!ConsoleScopeActivationObject::Is(object) || (i == length - 1), "Invalid location for ConsoleScopeActivationObject");
  2670. Type* type = object->GetType();
  2671. if (CacheOperators::TrySetProperty<true, true, true, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false>(
  2672. object, false, propertyId, newValue, scriptContext, propertyOperationFlags, nullptr, &info))
  2673. {
  2674. return;
  2675. }
  2676. // In scoped set property, we need to set the property when it is available; it could be a setter
  2677. // or normal property. we need to check setter first, and if no setter is available, but HasProperty
  2678. // is true, this must be a normal property.
  2679. // TODO: merge OP_HasProperty and GetSetter in one pass if there is perf problem. In fastDOM we have quite
  2680. // a lot of setters so separating the two might be actually faster.
  2681. BOOL setAccessorResult = FALSE;
  2682. if (SetAccessorOrNonWritableProperty(object, object, propertyId, newValue, &info, scriptContext, propertyOperationFlags, false, allowUndecInConsoleScope, &setAccessorResult))
  2683. {
  2684. return;
  2685. }
  2686. else if (!JavascriptOperators::IsObject(object))
  2687. {
  2688. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, scriptContext);
  2689. }
  2690. // Need to do a "get" of the current value (if any) to make sure that we're not writing to
  2691. // let/const before declaration, but we need to disable implicit calls around the "get",
  2692. // so we need to do a "has" first to make sure the "get" is valid (e.g., "get" on a HostDispatch
  2693. // with implicit calls disabled will always "succeed").
  2694. if (JavascriptOperators::HasProperty(object, propertyId))
  2695. {
  2696. DisableImplicitFlags disableImplicitFlags = scriptContext->GetThreadContext()->GetDisableImplicitFlags();
  2697. scriptContext->GetThreadContext()->SetDisableImplicitFlags(DisableImplicitCallAndExceptionFlag);
  2698. Var value;
  2699. BOOL result = JavascriptOperators::GetProperty(object, propertyId, &value, scriptContext, nullptr);
  2700. scriptContext->GetThreadContext()->SetDisableImplicitFlags(disableImplicitFlags);
  2701. if (result && scriptContext->IsUndeclBlockVar(value) && !allowUndecInConsoleScope && !isLexicalThisSlotSymbol)
  2702. {
  2703. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UseBeforeDeclaration);
  2704. }
  2705. PropertyValueInfo info2;
  2706. PropertyValueInfo::SetCacheInfo(&info2, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  2707. PropertyOperationFlags setPropertyOpFlags = allowUndecInConsoleScope ? PropertyOperation_AllowUndeclInConsoleScope : PropertyOperation_None;
  2708. object->SetProperty(propertyId, newValue, setPropertyOpFlags, &info2);
  2709. #if DBG_DUMP
  2710. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  2711. {
  2712. CacheOperators::TraceCache(inlineCache, _u("PatchSetPropertyScoped"), propertyId, scriptContext, object);
  2713. }
  2714. #endif
  2715. if (!JavascriptProxy::Is(object) && !allowUndecInConsoleScope)
  2716. {
  2717. CacheOperators::CachePropertyWrite(object, false, type, propertyId, &info2, scriptContext);
  2718. }
  2719. return;
  2720. }
  2721. }
  2722. Assert(!isLexicalThisSlotSymbol);
  2723. // If we have console scope and no one in the scope had the property add it to console scope
  2724. if ((length > 0) && ConsoleScopeActivationObject::Is(pDisplay->GetItem(length - 1)))
  2725. {
  2726. // CheckPrototypesForAccessorOrNonWritableProperty does not check for const in global object. We should check it here.
  2727. if (length > 1)
  2728. {
  2729. Js::GlobalObject * globalObject = JavascriptOperators::TryFromVar<Js::GlobalObject>(pDisplay->GetItem(length - 2));
  2730. if (globalObject)
  2731. {
  2732. Var setterValue = nullptr;
  2733. DescriptorFlags flags = JavascriptOperators::GetRootSetter(globalObject, propertyId, &setterValue, &info, scriptContext);
  2734. Assert((flags & Accessor) != Accessor);
  2735. Assert((flags & Proxy) != Proxy);
  2736. if ((flags & Data) == Data && (flags & Writable) == None)
  2737. {
  2738. if (!allowUndecInConsoleScope)
  2739. {
  2740. if (flags & Const)
  2741. {
  2742. JavascriptError::ThrowTypeError(scriptContext, ERRAssignmentToConst);
  2743. }
  2744. Assert(!isLexicalThisSlotSymbol);
  2745. return;
  2746. }
  2747. }
  2748. }
  2749. }
  2750. RecyclableObject* obj = RecyclableObject::FromVar((DynamicObject*)pDisplay->GetItem(length - 1));
  2751. OUTPUT_TRACE(Js::ConsoleScopePhase, _u("Adding property '%s' to console scope object\n"), scriptContext->GetPropertyName(propertyId)->GetBuffer());
  2752. JavascriptOperators::SetProperty(obj, obj, propertyId, newValue, scriptContext, propertyOperationFlags);
  2753. return;
  2754. }
  2755. // No one in the scope stack has the property, so add it to the default instance provided by the caller.
  2756. AssertMsg(!TaggedNumber::Is(defaultInstance), "Root object is an int or tagged float?");
  2757. Assert(defaultInstance != nullptr);
  2758. RecyclableObject* obj = RecyclableObject::FromVar(defaultInstance);
  2759. {
  2760. //SetPropertyScoped does not use inline cache for default instance
  2761. PropertyValueInfo info2;
  2762. JavascriptOperators::SetRootProperty(obj, propertyId, newValue, &info2, scriptContext, (PropertyOperationFlags)(propertyOperationFlags | PropertyOperation_Root));
  2763. }
  2764. JIT_HELPER_END(Op_PatchSetPropertyScoped);
  2765. }
  2766. JIT_HELPER_TEMPLATE(Op_PatchSetPropertyScoped, Op_ConsolePatchSetPropertyScoped)
  2767. 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);
  2768. 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);
  2769. 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);
  2770. 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);
  2771. BOOL JavascriptOperators::OP_InitFuncScoped(FrameDisplay *pScope, PropertyId propertyId, Var newValue, Var defaultInstance, ScriptContext* scriptContext)
  2772. {
  2773. JIT_HELPER_NOT_REENTRANT_HEADER(Op_InitFuncScoped, reentrancylock, scriptContext->GetThreadContext());
  2774. int i;
  2775. int length = pScope->GetLength();
  2776. DynamicObject *obj;
  2777. for (i = 0; i < length; i++)
  2778. {
  2779. obj = (DynamicObject*)pScope->GetItem(i);
  2780. if (obj->InitFuncScoped(propertyId, newValue))
  2781. {
  2782. return TRUE;
  2783. }
  2784. }
  2785. AssertMsg(!TaggedNumber::Is(defaultInstance), "Root object is an int or tagged float?");
  2786. return RecyclableObject::FromVar(defaultInstance)->InitFuncScoped(propertyId, newValue);
  2787. JIT_HELPER_END(Op_InitFuncScoped);
  2788. }
  2789. BOOL JavascriptOperators::OP_InitPropertyScoped(FrameDisplay *pScope, PropertyId propertyId, Var newValue, Var defaultInstance, ScriptContext* scriptContext)
  2790. {
  2791. int i;
  2792. int length = pScope->GetLength();
  2793. DynamicObject *obj;
  2794. for (i = 0; i < length; i++)
  2795. {
  2796. obj = (DynamicObject*)pScope->GetItem(i);
  2797. if (obj->InitPropertyScoped(propertyId, newValue))
  2798. {
  2799. return TRUE;
  2800. }
  2801. }
  2802. AssertMsg(!TaggedNumber::Is(defaultInstance), "Root object is an int or tagged float?");
  2803. return RecyclableObject::FromVar(defaultInstance)->InitPropertyScoped(propertyId, newValue);
  2804. }
  2805. Var JavascriptOperators::OP_DeletePropertyScoped(
  2806. FrameDisplay *pScope,
  2807. PropertyId propertyId,
  2808. Var defaultInstance,
  2809. ScriptContext* scriptContext,
  2810. PropertyOperationFlags propertyOperationFlags)
  2811. {
  2812. JIT_HELPER_REENTRANT_HEADER(Op_DeletePropertyScoped);
  2813. JIT_HELPER_SAME_ATTRIBUTES(Op_DeleteRootProperty, Op_DeletePropertyScoped);
  2814. int i;
  2815. int length = pScope->GetLength();
  2816. for (i = 0; i < length; i++)
  2817. {
  2818. DynamicObject *obj = (DynamicObject*)pScope->GetItem(i);
  2819. if (JavascriptOperators::HasProperty(obj, propertyId))
  2820. {
  2821. return scriptContext->GetLibrary()->CreateBoolean(JavascriptOperators::DeleteProperty(obj, propertyId, propertyOperationFlags));
  2822. }
  2823. }
  2824. return JavascriptOperators::OP_DeleteRootProperty(RecyclableObject::FromVar(defaultInstance), propertyId, scriptContext, propertyOperationFlags);
  2825. JIT_HELPER_END(Op_DeletePropertyScoped);
  2826. }
  2827. Var JavascriptOperators::OP_TypeofPropertyScoped(FrameDisplay *pScope, PropertyId propertyId, Var defaultInstance, ScriptContext* scriptContext)
  2828. {
  2829. JIT_HELPER_REENTRANT_HEADER(Op_TypeofPropertyScoped);
  2830. int i;
  2831. int length = pScope->GetLength();
  2832. for (i = 0; i < length; i++)
  2833. {
  2834. DynamicObject *obj = (DynamicObject*)pScope->GetItem(i);
  2835. if (JavascriptOperators::HasProperty(obj, propertyId))
  2836. {
  2837. return JavascriptOperators::TypeofFld(obj, propertyId, scriptContext);
  2838. }
  2839. }
  2840. return JavascriptOperators::TypeofRootFld(RecyclableObject::FromVar(defaultInstance), propertyId, scriptContext);
  2841. JIT_HELPER_END(Op_TypeofPropertyScoped);
  2842. }
  2843. BOOL JavascriptOperators::HasOwnItem(RecyclableObject* object, uint32 index)
  2844. {
  2845. return object->HasOwnItem(index);
  2846. }
  2847. BOOL JavascriptOperators::HasItem(RecyclableObject* object, uint64 index)
  2848. {
  2849. PropertyRecord const * propertyRecord = nullptr;
  2850. ScriptContext* scriptContext = object->GetScriptContext();
  2851. JavascriptOperators::GetPropertyIdForInt(index, scriptContext, &propertyRecord);
  2852. return JavascriptOperators::HasProperty(object, propertyRecord->GetPropertyId());
  2853. }
  2854. BOOL JavascriptOperators::HasItem(RecyclableObject* object, uint32 index)
  2855. {
  2856. #if ENABLE_COPYONACCESS_ARRAY
  2857. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(object);
  2858. #endif
  2859. while (!JavascriptOperators::IsNull(object))
  2860. {
  2861. PropertyQueryFlags result;
  2862. if ((result = object->HasItemQuery(index)) != PropertyQueryFlags::Property_NotFound)
  2863. {
  2864. return JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  2865. }
  2866. // CONSIDER: Numeric property values shouldn't be on the prototype for now but if this changes
  2867. // we should add SkipsPrototype support here as well
  2868. object = JavascriptOperators::GetPrototypeNoTrap(object);
  2869. }
  2870. return false;
  2871. }
  2872. BOOL JavascriptOperators::GetOwnItem(RecyclableObject* object, uint32 index, Var* value, ScriptContext* requestContext)
  2873. {
  2874. return object->GetItem(object, index, value, requestContext);
  2875. }
  2876. BOOL JavascriptOperators::GetItem(Var instance, RecyclableObject* propertyObject, uint32 index, Var* value, ScriptContext* requestContext)
  2877. {
  2878. RecyclableObject* object = propertyObject;
  2879. while (!JavascriptOperators::IsNull(object))
  2880. {
  2881. PropertyQueryFlags result;
  2882. if ((result = object->GetItemQuery(instance, index, value, requestContext)) != PropertyQueryFlags::Property_NotFound)
  2883. {
  2884. return JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  2885. }
  2886. if (object->SkipsPrototype())
  2887. {
  2888. break;
  2889. }
  2890. object = JavascriptOperators::GetPrototypeNoTrap(object);
  2891. }
  2892. *value = requestContext->GetMissingItemResult();
  2893. return false;
  2894. }
  2895. BOOL JavascriptOperators::GetItemReference(Var instance, RecyclableObject* propertyObject, uint32 index, Var* value, ScriptContext* requestContext)
  2896. {
  2897. RecyclableObject* object = propertyObject;
  2898. while (!JavascriptOperators::IsNull(object))
  2899. {
  2900. PropertyQueryFlags result;
  2901. if ((result = object->GetItemReferenceQuery(instance, index, value, requestContext)) != PropertyQueryFlags::Property_NotFound)
  2902. {
  2903. return JavascriptConversion::PropertyQueryFlagsToBoolean(result);
  2904. }
  2905. if (object->SkipsPrototype())
  2906. {
  2907. break;
  2908. }
  2909. object = JavascriptOperators::GetPrototypeNoTrap(object);
  2910. }
  2911. *value = requestContext->GetMissingItemResult();
  2912. return false;
  2913. }
  2914. BOOL JavascriptOperators::SetItem(Var receiver, RecyclableObject* object, uint64 index, Var value, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags)
  2915. {
  2916. PropertyRecord const * propertyRecord = nullptr;
  2917. JavascriptOperators::GetPropertyIdForInt(index, scriptContext, &propertyRecord);
  2918. return JavascriptOperators::SetProperty(receiver, object, propertyRecord->GetPropertyId(), value, scriptContext, propertyOperationFlags);
  2919. }
  2920. BOOL JavascriptOperators::SetItem(Var receiver, RecyclableObject* object, uint32 index, Var value, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags, BOOL skipPrototypeCheck /* = FALSE */)
  2921. {
  2922. Var setterValueOrProxy = nullptr;
  2923. DescriptorFlags flags = None;
  2924. Assert(!TaggedNumber::Is(receiver));
  2925. if (JavascriptOperators::CheckPrototypesForAccessorOrNonWritableItem(object, index, &setterValueOrProxy, &flags, scriptContext, skipPrototypeCheck))
  2926. {
  2927. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  2928. if ((flags & Accessor) == Accessor)
  2929. {
  2930. if (JavascriptError::ThrowIfStrictModeUndefinedSetter(propertyOperationFlags, setterValueOrProxy, scriptContext) ||
  2931. JavascriptError::ThrowIfNotExtensibleUndefinedSetter(propertyOperationFlags, setterValueOrProxy, scriptContext))
  2932. {
  2933. return TRUE;
  2934. }
  2935. if (setterValueOrProxy)
  2936. {
  2937. RecyclableObject* func = RecyclableObject::FromVar(setterValueOrProxy);
  2938. JavascriptOperators::CallSetter(func, receiver, value, scriptContext);
  2939. }
  2940. return TRUE;
  2941. }
  2942. else if ((flags & Proxy) == Proxy)
  2943. {
  2944. Assert(JavascriptProxy::Is(setterValueOrProxy));
  2945. JavascriptProxy* proxy = JavascriptProxy::FromVar(setterValueOrProxy);
  2946. const PropertyRecord* propertyRecord = nullptr;
  2947. proxy->PropertyIdFromInt(index, &propertyRecord);
  2948. return proxy->SetPropertyTrap(receiver, JavascriptProxy::SetPropertyTrapKind::SetItemKind, propertyRecord->GetPropertyId(), value, scriptContext, propertyOperationFlags, skipPrototypeCheck);
  2949. }
  2950. else
  2951. {
  2952. Assert((flags & Data) == Data && (flags & Writable) == None);
  2953. if ((propertyOperationFlags & PropertyOperationFlags::PropertyOperation_ThrowIfNotExtensible) == PropertyOperationFlags::PropertyOperation_ThrowIfNotExtensible)
  2954. {
  2955. JavascriptError::ThrowTypeError(scriptContext, JSERR_NonExtensibleObject);
  2956. }
  2957. JavascriptError::ThrowCantAssign(propertyOperationFlags, scriptContext, index);
  2958. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, scriptContext);
  2959. return FALSE;
  2960. }
  2961. }
  2962. else if (!JavascriptOperators::IsObject(receiver))
  2963. {
  2964. JavascriptError::ThrowCantAssignIfStrictMode(propertyOperationFlags, scriptContext);
  2965. return FALSE;
  2966. }
  2967. return (RecyclableObject::FromVar(receiver))->SetItem(index, value, propertyOperationFlags);
  2968. }
  2969. BOOL JavascriptOperators::DeleteItem(RecyclableObject* object, uint32 index, PropertyOperationFlags propertyOperationFlags)
  2970. {
  2971. return object->DeleteItem(index, propertyOperationFlags);
  2972. }
  2973. BOOL JavascriptOperators::DeleteItem(RecyclableObject* object, uint64 index, PropertyOperationFlags propertyOperationFlags)
  2974. {
  2975. PropertyRecord const * propertyRecord = nullptr;
  2976. JavascriptOperators::GetPropertyIdForInt(index, object->GetScriptContext(), &propertyRecord);
  2977. return JavascriptOperators::DeleteProperty(object, propertyRecord->GetPropertyId(), propertyOperationFlags);
  2978. }
  2979. BOOL JavascriptOperators::OP_HasItem(Var instance, Var index, ScriptContext* scriptContext)
  2980. {
  2981. RecyclableObject* object = TaggedNumber::Is(instance) ?
  2982. scriptContext->GetLibrary()->GetNumberPrototype() :
  2983. RecyclableObject::FromVar(instance);
  2984. uint32 indexVal;
  2985. PropertyRecord const * propertyRecord = nullptr;
  2986. IndexType indexType = GetIndexType(index, scriptContext, &indexVal, &propertyRecord, false);
  2987. if (indexType == IndexType_Number)
  2988. {
  2989. return HasItem(object, indexVal);
  2990. }
  2991. else
  2992. {
  2993. Assert(indexType == IndexType_PropertyId);
  2994. if (propertyRecord == nullptr && !JavascriptOperators::CanShortcutOnUnknownPropertyName(object))
  2995. {
  2996. indexType = GetIndexTypeFromPrimitive(index, scriptContext, &indexVal, &propertyRecord, true);
  2997. Assert(indexType == IndexType_PropertyId);
  2998. Assert(propertyRecord != nullptr);
  2999. }
  3000. if (propertyRecord != nullptr)
  3001. {
  3002. return HasProperty(object, propertyRecord->GetPropertyId());
  3003. }
  3004. else
  3005. {
  3006. #if DBG
  3007. JavascriptString* indexStr = JavascriptConversion::ToString(index, scriptContext);
  3008. PropertyRecord const * debugPropertyRecord;
  3009. scriptContext->GetOrAddPropertyRecord(indexStr, &debugPropertyRecord);
  3010. AssertMsg(!JavascriptOperators::HasProperty(object, debugPropertyRecord->GetPropertyId()), "how did this property come? See OS Bug 2727708 if you see this come from the web");
  3011. #endif
  3012. return FALSE;
  3013. }
  3014. }
  3015. }
  3016. #if ENABLE_PROFILE_INFO
  3017. void JavascriptOperators::UpdateNativeArrayProfileInfoToCreateVarArray(Var instance, const bool expectingNativeFloatArray, const bool expectingVarArray)
  3018. {
  3019. Assert(instance);
  3020. Assert(expectingNativeFloatArray ^ expectingVarArray);
  3021. JavascriptNativeArray * nativeArr = JavascriptOperators::TryFromVar<JavascriptNativeArray>(instance);
  3022. if (!nativeArr)
  3023. {
  3024. return;
  3025. }
  3026. ArrayCallSiteInfo *const arrayCallSiteInfo = nativeArr->GetArrayCallSiteInfo();
  3027. if (!arrayCallSiteInfo)
  3028. {
  3029. return;
  3030. }
  3031. if (expectingNativeFloatArray)
  3032. {
  3033. // Profile data is expecting a native float array. Ensure that at the array's creation site, that a native int array
  3034. // is not created, such that the profiled array type would be correct.
  3035. arrayCallSiteInfo->SetIsNotNativeIntArray();
  3036. }
  3037. else
  3038. {
  3039. // Profile data is expecting a var array. Ensure that at the array's creation site, that a native array is not
  3040. // created, such that the profiled array type would be correct.
  3041. Assert(expectingVarArray);
  3042. arrayCallSiteInfo->SetIsNotNativeArray();
  3043. }
  3044. }
  3045. bool JavascriptOperators::SetElementMayHaveImplicitCalls(ScriptContext *const scriptContext)
  3046. {
  3047. return
  3048. scriptContext->optimizationOverrides.GetArraySetElementFastPathVtable() ==
  3049. ScriptContextOptimizationOverrideInfo::InvalidVtable;
  3050. }
  3051. #endif
  3052. RecyclableObject *JavascriptOperators::GetCallableObjectOrThrow(const Var callee, ScriptContext *const scriptContext)
  3053. {
  3054. Assert(callee);
  3055. Assert(scriptContext);
  3056. if (TaggedNumber::Is(callee))
  3057. {
  3058. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedFunction /* TODO-ERROR: get arg name - aFunc */);
  3059. }
  3060. return RecyclableObject::UnsafeFromVar(callee);
  3061. }
  3062. Var JavascriptOperators::OP_GetElementI_JIT(Var instance, Var index, ScriptContext *scriptContext)
  3063. {
  3064. JIT_HELPER_REENTRANT_HEADER(Op_GetElementI);
  3065. #if ENABLE_NATIVE_CODEGEN
  3066. Assert(Js::JavascriptStackWalker::ValidateTopJitFrame(scriptContext));
  3067. #endif
  3068. return OP_GetElementI(instance, index, scriptContext);
  3069. JIT_HELPER_END(Op_GetElementI);
  3070. }
  3071. Var JavascriptOperators::OP_GetElementI_UInt32(Var instance, uint32 index, ScriptContext* scriptContext)
  3072. {
  3073. JIT_HELPER_REENTRANT_HEADER(Op_GetElementI_UInt32);
  3074. #if FLOATVAR
  3075. return OP_GetElementI_JIT(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext);
  3076. #else
  3077. char buffer[sizeof(Js::JavascriptNumber)];
  3078. return OP_GetElementI_JIT(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3079. (Js::JavascriptNumber *)buffer), scriptContext);
  3080. #endif
  3081. JIT_HELPER_END(Op_GetElementI_UInt32);
  3082. }
  3083. Var JavascriptOperators::OP_GetElementI_Int32(Var instance, int32 index, ScriptContext* scriptContext)
  3084. {
  3085. JIT_HELPER_REENTRANT_HEADER(Op_GetElementI_Int32);
  3086. #if FLOATVAR
  3087. return OP_GetElementI_JIT(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext);
  3088. #else
  3089. char buffer[sizeof(Js::JavascriptNumber)];
  3090. return OP_GetElementI_JIT(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3091. (Js::JavascriptNumber *)buffer), scriptContext);
  3092. #endif
  3093. JIT_HELPER_END(Op_GetElementI_Int32);
  3094. }
  3095. BOOL JavascriptOperators::GetItemFromArrayPrototype(JavascriptArray * arr, int32 indexInt, Var * result, ScriptContext * scriptContext)
  3096. {
  3097. // try get from Array prototype
  3098. RecyclableObject* prototype = arr->GetPrototype();
  3099. if (JavascriptOperators::GetTypeId(prototype) != TypeIds_Array) //This can be TypeIds_ES5Array (or any other object changed through __proto__).
  3100. {
  3101. return false;
  3102. }
  3103. JavascriptArray* arrayPrototype = JavascriptArray::UnsafeFromVar(prototype); //Prototype must be Array.prototype (unless changed through __proto__)
  3104. if (arrayPrototype->GetLength() && arrayPrototype->GetItem(arrayPrototype, (uint32)indexInt, result, scriptContext))
  3105. {
  3106. return true;
  3107. }
  3108. prototype = arrayPrototype->GetPrototype(); //Its prototype must be Object.prototype (unless changed through __proto__)
  3109. if (prototype->GetScriptContext()->GetLibrary()->GetObjectPrototype() != prototype)
  3110. {
  3111. return false;
  3112. }
  3113. if (DynamicObject::FromVar(prototype)->HasNonEmptyObjectArray())
  3114. {
  3115. if (prototype->GetItem(arr, (uint32)indexInt, result, scriptContext))
  3116. {
  3117. return true;
  3118. }
  3119. }
  3120. *result = scriptContext->GetMissingItemResult();
  3121. return true;
  3122. }
  3123. Var JavascriptOperators::GetElementIIntIndex(_In_ Var instance, _In_ Var index, _In_ ScriptContext* scriptContext)
  3124. {
  3125. Assert(TaggedInt::Is(index));
  3126. switch (JavascriptOperators::GetTypeId(instance))
  3127. {
  3128. case TypeIds_Array: //fast path for array
  3129. {
  3130. Var result;
  3131. if (OP_GetElementI_ArrayFastPath(JavascriptArray::UnsafeFromVar(instance), TaggedInt::ToInt32(index), &result, scriptContext))
  3132. {
  3133. return result;
  3134. }
  3135. break;
  3136. }
  3137. case TypeIds_NativeIntArray:
  3138. {
  3139. #if ENABLE_COPYONACCESS_ARRAY
  3140. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  3141. #endif
  3142. Var result;
  3143. if (OP_GetElementI_ArrayFastPath(JavascriptNativeIntArray::UnsafeFromVar(instance), TaggedInt::ToInt32(index), &result, scriptContext))
  3144. {
  3145. return result;
  3146. }
  3147. break;
  3148. }
  3149. case TypeIds_NativeFloatArray:
  3150. {
  3151. Var result;
  3152. if (OP_GetElementI_ArrayFastPath(JavascriptNativeFloatArray::UnsafeFromVar(instance), TaggedInt::ToInt32(index), &result, scriptContext))
  3153. {
  3154. return result;
  3155. }
  3156. break;
  3157. }
  3158. case TypeIds_String: // fast path for string
  3159. {
  3160. charcount_t indexInt = TaggedInt::ToUInt32(index);
  3161. JavascriptString* string = JavascriptString::UnsafeFromVar(instance);
  3162. Var result;
  3163. if (JavascriptConversion::PropertyQueryFlagsToBoolean(string->JavascriptString::GetItemQuery(instance, indexInt, &result, scriptContext)))
  3164. {
  3165. return result;
  3166. }
  3167. break;
  3168. }
  3169. case TypeIds_Int8Array:
  3170. {
  3171. // The typed array will deal with all possible values for the index
  3172. int32 indexInt = TaggedInt::ToInt32(index);
  3173. if (VirtualTableInfo<Int8VirtualArray>::HasVirtualTable(instance))
  3174. {
  3175. Int8VirtualArray* int8Array = Int8VirtualArray::UnsafeFromVar(instance);
  3176. if (indexInt >= 0)
  3177. {
  3178. return int8Array->DirectGetItem(indexInt);
  3179. }
  3180. }
  3181. else if (VirtualTableInfo<Int8Array>::HasVirtualTable(instance))
  3182. {
  3183. Int8Array* int8Array = Int8Array::UnsafeFromVar(instance);
  3184. if (indexInt >= 0)
  3185. {
  3186. return int8Array->DirectGetItem(indexInt);
  3187. }
  3188. }
  3189. break;
  3190. }
  3191. case TypeIds_Uint8Array:
  3192. {
  3193. // The typed array will deal with all possible values for the index
  3194. int32 indexInt = TaggedInt::ToInt32(index);
  3195. if (VirtualTableInfo<Uint8VirtualArray>::HasVirtualTable(instance))
  3196. {
  3197. Uint8VirtualArray* uint8Array = Uint8VirtualArray::UnsafeFromVar(instance);
  3198. if (indexInt >= 0)
  3199. {
  3200. return uint8Array->DirectGetItem(indexInt);
  3201. }
  3202. }
  3203. else if (VirtualTableInfo<Uint8Array>::HasVirtualTable(instance))
  3204. {
  3205. Uint8Array* uint8Array = Uint8Array::UnsafeFromVar(instance);
  3206. if (indexInt >= 0)
  3207. {
  3208. return uint8Array->DirectGetItem(indexInt);
  3209. }
  3210. }
  3211. break;
  3212. }
  3213. case TypeIds_Uint8ClampedArray:
  3214. {
  3215. // The typed array will deal with all possible values for the index
  3216. int32 indexInt = TaggedInt::ToInt32(index);
  3217. if (VirtualTableInfo<Uint8ClampedVirtualArray>::HasVirtualTable(instance))
  3218. {
  3219. Uint8ClampedVirtualArray* uint8ClampedArray = Uint8ClampedVirtualArray::UnsafeFromVar(instance);
  3220. if (indexInt >= 0)
  3221. {
  3222. return uint8ClampedArray->DirectGetItem(indexInt);
  3223. }
  3224. }
  3225. else if (VirtualTableInfo<Uint8ClampedArray>::HasVirtualTable(instance))
  3226. {
  3227. Uint8ClampedArray* uint8ClampedArray = Uint8ClampedArray::UnsafeFromVar(instance);
  3228. if (indexInt >= 0)
  3229. {
  3230. return uint8ClampedArray->DirectGetItem(indexInt);
  3231. }
  3232. }
  3233. break;
  3234. }
  3235. case TypeIds_Int16Array:
  3236. {
  3237. // The type array will deal with all possible values for the index
  3238. int32 indexInt = TaggedInt::ToInt32(index);
  3239. if (VirtualTableInfo<Int16VirtualArray>::HasVirtualTable(instance))
  3240. {
  3241. Int16VirtualArray* int16Array = Int16VirtualArray::UnsafeFromVar(instance);
  3242. if (indexInt >= 0)
  3243. {
  3244. return int16Array->DirectGetItem(indexInt);
  3245. }
  3246. }
  3247. else if (VirtualTableInfo<Int16Array>::HasVirtualTable(instance))
  3248. {
  3249. Int16Array* int16Array = Int16Array::UnsafeFromVar(instance);
  3250. if (indexInt >= 0)
  3251. {
  3252. return int16Array->DirectGetItem(indexInt);
  3253. }
  3254. }
  3255. break;
  3256. }
  3257. case TypeIds_Uint16Array:
  3258. {
  3259. // The type array will deal with all possible values for the index
  3260. int32 indexInt = TaggedInt::ToInt32(index);
  3261. if (VirtualTableInfo<Uint16VirtualArray>::HasVirtualTable(instance))
  3262. {
  3263. Uint16VirtualArray* uint16Array = Uint16VirtualArray::UnsafeFromVar(instance);
  3264. if (indexInt >= 0)
  3265. {
  3266. return uint16Array->DirectGetItem(indexInt);
  3267. }
  3268. }
  3269. else if (VirtualTableInfo<Uint16Array>::HasVirtualTable(instance))
  3270. {
  3271. Uint16Array* uint16Array = Uint16Array::UnsafeFromVar(instance);
  3272. if (indexInt >= 0)
  3273. {
  3274. return uint16Array->DirectGetItem(indexInt);
  3275. }
  3276. }
  3277. break;
  3278. }
  3279. case TypeIds_Int32Array:
  3280. {
  3281. // The type array will deal with all possible values for the index
  3282. int32 indexInt = TaggedInt::ToInt32(index);
  3283. if (VirtualTableInfo<Int32VirtualArray>::HasVirtualTable(instance))
  3284. {
  3285. Int32VirtualArray* int32Array = Int32VirtualArray::UnsafeFromVar(instance);
  3286. if (indexInt >= 0)
  3287. {
  3288. return int32Array->DirectGetItem(indexInt);
  3289. }
  3290. }
  3291. else if (VirtualTableInfo<Int32Array>::HasVirtualTable(instance))
  3292. {
  3293. Int32Array* int32Array = Int32Array::UnsafeFromVar(instance);
  3294. if (indexInt >= 0)
  3295. {
  3296. return int32Array->DirectGetItem(indexInt);
  3297. }
  3298. }
  3299. break;
  3300. }
  3301. case TypeIds_Uint32Array:
  3302. {
  3303. // The type array will deal with all possible values for the index
  3304. int32 indexInt = TaggedInt::ToInt32(index);
  3305. if (VirtualTableInfo<Uint32VirtualArray>::HasVirtualTable(instance))
  3306. {
  3307. Uint32VirtualArray* uint32Array = Uint32VirtualArray::UnsafeFromVar(instance);
  3308. if (indexInt >= 0)
  3309. {
  3310. return uint32Array->DirectGetItem(indexInt);
  3311. }
  3312. }
  3313. else if (VirtualTableInfo<Uint32Array>::HasVirtualTable(instance))
  3314. {
  3315. Uint32Array* uint32Array = Uint32Array::UnsafeFromVar(instance);
  3316. if (indexInt >= 0)
  3317. {
  3318. return uint32Array->DirectGetItem(indexInt);
  3319. }
  3320. }
  3321. break;
  3322. }
  3323. case TypeIds_Float32Array:
  3324. {
  3325. // The type array will deal with all possible values for the index
  3326. int32 indexInt = TaggedInt::ToInt32(index);
  3327. if (VirtualTableInfo<Float32VirtualArray>::HasVirtualTable(instance))
  3328. {
  3329. Float32VirtualArray* float32Array = Float32VirtualArray::UnsafeFromVar(instance);
  3330. if (indexInt >= 0)
  3331. {
  3332. return float32Array->DirectGetItem(indexInt);
  3333. }
  3334. }
  3335. else if (VirtualTableInfo<Float32Array>::HasVirtualTable(instance))
  3336. {
  3337. Float32Array* float32Array = Float32Array::UnsafeFromVar(instance);
  3338. if (indexInt >= 0)
  3339. {
  3340. return float32Array->DirectGetItem(indexInt);
  3341. }
  3342. }
  3343. break;
  3344. }
  3345. case TypeIds_Float64Array:
  3346. {
  3347. // The type array will deal with all possible values for the index
  3348. int32 indexInt = TaggedInt::ToInt32(index);
  3349. if (VirtualTableInfo<Float64VirtualArray>::HasVirtualTable(instance))
  3350. {
  3351. Float64VirtualArray* float64Array = Float64VirtualArray::UnsafeFromVar(instance);
  3352. if (indexInt >= 0)
  3353. {
  3354. return float64Array->DirectGetItem(indexInt);
  3355. }
  3356. }
  3357. else if (VirtualTableInfo<Float64Array>::HasVirtualTable(instance))
  3358. {
  3359. Float64Array* float64Array = Float64Array::UnsafeFromVar(instance);
  3360. if (indexInt >= 0)
  3361. {
  3362. return float64Array->DirectGetItem(indexInt);
  3363. }
  3364. }
  3365. break;
  3366. }
  3367. default:
  3368. break;
  3369. }
  3370. return JavascriptOperators::GetElementIHelper(instance, index, instance, scriptContext);
  3371. }
  3372. template <typename T>
  3373. BOOL JavascriptOperators::OP_GetElementI_ArrayFastPath(T * arr, int indexInt, Var * result, ScriptContext * scriptContext)
  3374. {
  3375. #if ENABLE_COPYONACCESS_ARRAY
  3376. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(arr);
  3377. #endif
  3378. if (indexInt >= 0)
  3379. {
  3380. if (!CrossSite::IsCrossSiteObjectTyped(arr))
  3381. {
  3382. if (arr->T::DirectGetVarItemAt((uint32)indexInt, result, scriptContext))
  3383. {
  3384. return true;
  3385. }
  3386. }
  3387. else
  3388. {
  3389. if (arr->GetItem(arr, (uint32)indexInt, result, scriptContext))
  3390. {
  3391. return true;
  3392. }
  3393. }
  3394. return GetItemFromArrayPrototype(arr, indexInt, result, scriptContext);
  3395. }
  3396. return false;
  3397. }
  3398. Var JavascriptOperators::OP_GetElementI(Var instance, Var index, ScriptContext* scriptContext)
  3399. {
  3400. instance = BreakSpeculation(instance);
  3401. if (TaggedInt::Is(index))
  3402. {
  3403. return GetElementIIntIndex(instance, index, scriptContext);
  3404. }
  3405. if (JavascriptNumber::Is_NoTaggedIntCheck(index))
  3406. {
  3407. uint32 uint32Index = JavascriptConversion::ToUInt32(index, scriptContext);
  3408. if ((double)uint32Index == JavascriptNumber::GetValue(index) && !TaggedInt::IsOverflow(uint32Index))
  3409. {
  3410. index = TaggedInt::ToVarUnchecked(uint32Index);
  3411. return GetElementIIntIndex(instance, index, scriptContext);
  3412. }
  3413. }
  3414. else if (RecyclableObject::Is(instance))
  3415. {
  3416. RecyclableObject* cacheOwner;
  3417. PropertyRecordUsageCache* propertyRecordUsageCache;
  3418. if (GetPropertyRecordUsageCache(index, scriptContext, &propertyRecordUsageCache, &cacheOwner))
  3419. {
  3420. return GetElementIWithCache<false /* ReturnOperationInfo */>(instance, cacheOwner, propertyRecordUsageCache, scriptContext, nullptr);
  3421. }
  3422. }
  3423. return JavascriptOperators::GetElementIHelper(instance, index, instance, scriptContext);
  3424. }
  3425. _Success_(return) bool JavascriptOperators::GetPropertyRecordUsageCache(Var index, ScriptContext* scriptContext, _Outptr_ PropertyRecordUsageCache** propertyRecordUsageCache, _Outptr_ RecyclableObject** cacheOwner)
  3426. {
  3427. JavascriptString* string = JavascriptOperators::TryFromVar<JavascriptString>(index);
  3428. if (string)
  3429. {
  3430. PropertyString * propertyString = nullptr;
  3431. if (VirtualTableInfo<Js::PropertyString>::HasVirtualTable(string))
  3432. {
  3433. propertyString = (PropertyString*)string;
  3434. }
  3435. else if (VirtualTableInfo<Js::LiteralStringWithPropertyStringPtr>::HasVirtualTable(string))
  3436. {
  3437. LiteralStringWithPropertyStringPtr * strWithPtr = (LiteralStringWithPropertyStringPtr *)string;
  3438. if (!strWithPtr->HasPropertyRecord())
  3439. {
  3440. PropertyRecord const * propertyRecord;
  3441. strWithPtr->GetPropertyRecord(&propertyRecord); // lookup-cache propertyRecord
  3442. }
  3443. else
  3444. {
  3445. propertyString = strWithPtr->GetOrAddPropertyString();
  3446. // this is the second time this property string is used
  3447. // we already had created the propertyRecord..
  3448. // now create the propertyString!
  3449. }
  3450. }
  3451. if (propertyString != nullptr)
  3452. {
  3453. *propertyRecordUsageCache = propertyString->GetPropertyRecordUsageCache();
  3454. *cacheOwner = propertyString;
  3455. return true;
  3456. }
  3457. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  3458. if (PHASE_TRACE1(PropertyCachePhase))
  3459. {
  3460. Output::Print(_u("PropertyCache: GetElem No property string for '%s'\n"), string->GetString());
  3461. }
  3462. #endif
  3463. #if DBG_DUMP
  3464. scriptContext->forinNoCache++;
  3465. #endif
  3466. }
  3467. JavascriptSymbol* symbol = JavascriptOperators::TryFromVar<JavascriptSymbol>(index);
  3468. if (symbol)
  3469. {
  3470. *propertyRecordUsageCache = symbol->GetPropertyRecordUsageCache();
  3471. *cacheOwner = symbol;
  3472. return true;
  3473. }
  3474. return false;
  3475. }
  3476. bool JavascriptOperators::SetElementIOnTaggedNumber(
  3477. _In_ Var receiver,
  3478. _In_ RecyclableObject* object,
  3479. _In_ Var index,
  3480. _In_ Var value,
  3481. _In_ ScriptContext* requestContext,
  3482. _In_ PropertyOperationFlags propertyOperationFlags)
  3483. {
  3484. Assert(TaggedNumber::Is(receiver));
  3485. uint32 indexVal = 0;
  3486. PropertyRecord const * propertyRecord = nullptr;
  3487. IndexType indexType = GetIndexType(index, requestContext, &indexVal, &propertyRecord, true);
  3488. if (indexType == IndexType_Number)
  3489. {
  3490. return JavascriptOperators::SetItemOnTaggedNumber(receiver, object, indexVal, value, requestContext, propertyOperationFlags);
  3491. }
  3492. else
  3493. {
  3494. return JavascriptOperators::SetPropertyOnTaggedNumber(receiver, object, propertyRecord->GetPropertyId(), value, requestContext, propertyOperationFlags);
  3495. }
  3496. }
  3497. template <bool ReturnOperationInfo>
  3498. bool JavascriptOperators::SetElementIWithCache(
  3499. _In_ Var receiver,
  3500. _In_ RecyclableObject* object,
  3501. _In_ RecyclableObject* index,
  3502. _In_ Var value,
  3503. _In_ PropertyRecordUsageCache* propertyRecordUsageCache,
  3504. _In_ ScriptContext* scriptContext,
  3505. _In_ PropertyOperationFlags flags,
  3506. _Inout_opt_ PropertyCacheOperationInfo* operationInfo)
  3507. {
  3508. if (TaggedNumber::Is(receiver))
  3509. {
  3510. return JavascriptOperators::SetElementIOnTaggedNumber(receiver, object, index, value, scriptContext, flags);
  3511. }
  3512. PropertyRecord const * propertyRecord = propertyRecordUsageCache->GetPropertyRecord();
  3513. if (propertyRecord->IsNumeric())
  3514. {
  3515. return JavascriptOperators::SetItem(receiver, object, propertyRecord->GetNumericValue(), value, scriptContext, flags);
  3516. }
  3517. PropertyValueInfo info;
  3518. if (receiver == object)
  3519. {
  3520. if (propertyRecordUsageCache->TrySetPropertyFromCache<ReturnOperationInfo>(object, value, scriptContext, flags, &info, index, operationInfo))
  3521. {
  3522. return true;
  3523. }
  3524. }
  3525. PropertyId propId = propertyRecord->GetPropertyId();
  3526. if (propId == PropertyIds::NaN || propId == PropertyIds::Infinity)
  3527. {
  3528. // As we no longer convert o[x] into o.x for NaN and Infinity, we need to follow SetProperty convention for these,
  3529. // which would check for read-only properties, strict mode, etc.
  3530. // Note that "-Infinity" does not qualify as property name, so we don't have to take care of it.
  3531. return JavascriptOperators::SetProperty(receiver, object, propId, value, scriptContext, flags);
  3532. }
  3533. return JavascriptOperators::SetPropertyWPCache(receiver, object, propId, value, scriptContext, flags, &info);
  3534. }
  3535. template bool JavascriptOperators::SetElementIWithCache<false>(Var receiver, RecyclableObject* object, RecyclableObject* index, Var value, PropertyRecordUsageCache* propertyRecordUsageCache, ScriptContext* scriptContext, PropertyOperationFlags flags, PropertyCacheOperationInfo* operationInfo);
  3536. template bool JavascriptOperators::SetElementIWithCache<true>(Var receiver, RecyclableObject* object, RecyclableObject* index, Var value, PropertyRecordUsageCache* propertyRecordUsageCache, ScriptContext* scriptContext, PropertyOperationFlags flags, PropertyCacheOperationInfo* operationInfo);
  3537. template <bool ReturnOperationInfo>
  3538. Var JavascriptOperators::GetElementIWithCache(
  3539. _In_ Var instance,
  3540. _In_ RecyclableObject* index,
  3541. _In_ PropertyRecordUsageCache* propertyRecordUsageCache,
  3542. _In_ ScriptContext* scriptContext,
  3543. _Inout_opt_ PropertyCacheOperationInfo* operationInfo)
  3544. {
  3545. RecyclableObject* object = nullptr;
  3546. if (!JavascriptOperators::GetPropertyObjectForGetElementI(instance, index, scriptContext, &object))
  3547. {
  3548. return scriptContext->GetLibrary()->GetUndefined();
  3549. }
  3550. PropertyRecord const * propertyRecord = propertyRecordUsageCache->GetPropertyRecord();
  3551. Var value;
  3552. if (propertyRecord->IsNumeric())
  3553. {
  3554. if (JavascriptOperators::GetItem(instance, object, propertyRecord->GetNumericValue(), &value, scriptContext))
  3555. {
  3556. return value;
  3557. }
  3558. }
  3559. else
  3560. {
  3561. PropertyValueInfo info;
  3562. if (propertyRecordUsageCache->TryGetPropertyFromCache<false /* OwnPropertyOnly */, false /* OutputExistence */, ReturnOperationInfo>(instance, object, &value, scriptContext, &info, index, operationInfo))
  3563. {
  3564. return value;
  3565. }
  3566. if (JavascriptOperators::GetPropertyWPCache<false /* OutputExistence */>(instance, object, propertyRecord->GetPropertyId(), &value, scriptContext, &info))
  3567. {
  3568. return value;
  3569. }
  3570. }
  3571. return scriptContext->GetLibrary()->GetUndefined();
  3572. }
  3573. template Var JavascriptOperators::GetElementIWithCache<false>(Var instance, RecyclableObject* index, PropertyRecordUsageCache* propertyRecordUsageCache, ScriptContext* scriptContext, PropertyCacheOperationInfo* operationInfo);
  3574. template Var JavascriptOperators::GetElementIWithCache<true>(Var instance, RecyclableObject* index, PropertyRecordUsageCache* propertyRecordUsageCache, ScriptContext* scriptContext, PropertyCacheOperationInfo* operationInfo);
  3575. Var JavascriptOperators::GetElementIHelper(Var instance, Var index, Var receiver, ScriptContext* scriptContext)
  3576. {
  3577. RecyclableObject* object = nullptr;
  3578. if (!JavascriptOperators::GetPropertyObjectForGetElementI(instance, index, scriptContext, &object))
  3579. {
  3580. return scriptContext->GetLibrary()->GetUndefined();
  3581. }
  3582. uint32 indexVal;
  3583. PropertyRecord const * propertyRecord = nullptr;
  3584. JavascriptString * propertyNameString = nullptr;
  3585. Var value = nullptr;
  3586. IndexType indexType = GetIndexType(index, scriptContext, &indexVal, &propertyRecord, &propertyNameString, false, true);
  3587. if (indexType == IndexType_Number)
  3588. {
  3589. if (JavascriptOperators::GetItem(receiver, object, indexVal, &value, scriptContext))
  3590. {
  3591. return value;
  3592. }
  3593. }
  3594. else if (indexType == IndexType_JavascriptString)
  3595. {
  3596. PropertyValueInfo info;
  3597. if (JavascriptOperators::GetPropertyWPCache<false /* OutputExistence */>(receiver, object, propertyNameString, &value, scriptContext, &info))
  3598. {
  3599. return value;
  3600. }
  3601. }
  3602. else
  3603. {
  3604. Assert(indexType == IndexType_PropertyId);
  3605. if (propertyRecord == nullptr && !JavascriptOperators::CanShortcutOnUnknownPropertyName(object))
  3606. {
  3607. indexType = GetIndexTypeFromPrimitive(index, scriptContext, &indexVal, &propertyRecord, &propertyNameString, true, true);
  3608. Assert(indexType == IndexType_PropertyId);
  3609. Assert(propertyRecord != nullptr);
  3610. }
  3611. if (propertyRecord != nullptr)
  3612. {
  3613. PropertyValueInfo info;
  3614. if (JavascriptOperators::GetPropertyWPCache<false /* OutputExistence */>(receiver, object, propertyRecord->GetPropertyId(), &value, scriptContext, &info))
  3615. {
  3616. return value;
  3617. }
  3618. }
  3619. #if DBG
  3620. else
  3621. {
  3622. JavascriptString* indexStr = JavascriptConversion::ToString(index, scriptContext);
  3623. PropertyRecord const * debugPropertyRecord;
  3624. scriptContext->GetOrAddPropertyRecord(indexStr, &debugPropertyRecord);
  3625. 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");
  3626. }
  3627. #endif
  3628. }
  3629. return scriptContext->GetMissingItemResult();
  3630. }
  3631. int32 JavascriptOperators::OP_GetNativeIntElementI(Var instance, Var index)
  3632. {
  3633. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_GetNativeIntElementI);
  3634. #if ENABLE_COPYONACCESS_ARRAY
  3635. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  3636. #endif
  3637. if (TaggedInt::Is(index))
  3638. {
  3639. int32 indexInt = TaggedInt::ToInt32(index);
  3640. if (indexInt < 0)
  3641. {
  3642. return JavascriptNativeIntArray::MissingItem;
  3643. }
  3644. JavascriptArray * arr = JavascriptArray::FromVar(instance);
  3645. int32 result;
  3646. if (arr->DirectGetItemAt((uint32)indexInt, &result))
  3647. {
  3648. return result;
  3649. }
  3650. }
  3651. else if (JavascriptNumber::Is_NoTaggedIntCheck(index))
  3652. {
  3653. int32 indexInt;
  3654. bool isInt32;
  3655. double dIndex = JavascriptNumber::GetValue(index);
  3656. if (JavascriptNumber::TryGetInt32OrUInt32Value(dIndex, &indexInt, &isInt32))
  3657. {
  3658. if (isInt32 && indexInt < 0)
  3659. {
  3660. return JavascriptNativeIntArray::MissingItem;
  3661. }
  3662. JavascriptArray * arr = JavascriptArray::FromVar(instance);
  3663. int32 result;
  3664. if (arr->DirectGetItemAt((uint32)indexInt, &result))
  3665. {
  3666. return result;
  3667. }
  3668. }
  3669. }
  3670. else
  3671. {
  3672. AssertMsg(false, "Non-numerical index in this helper?");
  3673. }
  3674. return JavascriptNativeIntArray::MissingItem;
  3675. JIT_HELPER_END(Op_GetNativeIntElementI);
  3676. }
  3677. int32 JavascriptOperators::OP_GetNativeIntElementI_UInt32(Var instance, uint32 index, ScriptContext* scriptContext)
  3678. {
  3679. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_GetNativeIntElementI_UInt32);
  3680. JIT_HELPER_SAME_ATTRIBUTES(Op_GetNativeIntElementI_UInt32, Op_GetNativeIntElementI);
  3681. #if FLOATVAR
  3682. return OP_GetNativeIntElementI(instance, Js::JavascriptNumber::ToVar(index, scriptContext));
  3683. #else
  3684. char buffer[sizeof(Js::JavascriptNumber)];
  3685. return OP_GetNativeIntElementI(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3686. (Js::JavascriptNumber *)buffer));
  3687. #endif
  3688. JIT_HELPER_END(Op_GetNativeIntElementI_UInt32);
  3689. }
  3690. int32 JavascriptOperators::OP_GetNativeIntElementI_Int32(Var instance, int32 index, ScriptContext* scriptContext)
  3691. {
  3692. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_GetNativeIntElementI_Int32);
  3693. JIT_HELPER_SAME_ATTRIBUTES(Op_GetNativeIntElementI_Int32, Op_GetNativeIntElementI);
  3694. #if FLOATVAR
  3695. return OP_GetNativeIntElementI(instance, Js::JavascriptNumber::ToVar(index, scriptContext));
  3696. #else
  3697. char buffer[sizeof(Js::JavascriptNumber)];
  3698. return OP_GetNativeIntElementI(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3699. (Js::JavascriptNumber *)buffer));
  3700. #endif
  3701. JIT_HELPER_END(Op_GetNativeIntElementI_Int32);
  3702. }
  3703. double JavascriptOperators::OP_GetNativeFloatElementI(Var instance, Var index)
  3704. {
  3705. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_GetNativeFloatElementI);
  3706. double result = 0;
  3707. if (TaggedInt::Is(index))
  3708. {
  3709. int32 indexInt = TaggedInt::ToInt32(index);
  3710. if (indexInt < 0)
  3711. {
  3712. result = JavascriptNativeFloatArray::MissingItem;
  3713. }
  3714. else
  3715. {
  3716. JavascriptArray * arr = JavascriptArray::FromVar(instance);
  3717. if (!arr->DirectGetItemAt((uint32)indexInt, &result))
  3718. {
  3719. result = JavascriptNativeFloatArray::MissingItem;
  3720. }
  3721. }
  3722. }
  3723. else if (JavascriptNumber::Is_NoTaggedIntCheck(index))
  3724. {
  3725. int32 indexInt;
  3726. bool isInt32;
  3727. double dIndex = JavascriptNumber::GetValue(index);
  3728. if (JavascriptNumber::TryGetInt32OrUInt32Value(dIndex, &indexInt, &isInt32))
  3729. {
  3730. if (isInt32 && indexInt < 0)
  3731. {
  3732. result = JavascriptNativeFloatArray::MissingItem;
  3733. }
  3734. else
  3735. {
  3736. JavascriptArray * arr = JavascriptArray::FromVar(instance);
  3737. if (!arr->DirectGetItemAt((uint32)indexInt, &result))
  3738. {
  3739. result = JavascriptNativeFloatArray::MissingItem;
  3740. }
  3741. }
  3742. }
  3743. }
  3744. else
  3745. {
  3746. AssertMsg(false, "Non-numerical index in this helper?");
  3747. }
  3748. return result;
  3749. JIT_HELPER_END(Op_GetNativeFloatElementI);
  3750. }
  3751. double JavascriptOperators::OP_GetNativeFloatElementI_UInt32(Var instance, uint32 index, ScriptContext* scriptContext)
  3752. {
  3753. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_GetNativeFloatElementI_UInt32);
  3754. JIT_HELPER_SAME_ATTRIBUTES(Op_GetNativeFloatElementI_UInt32, Op_GetNativeFloatElementI);
  3755. #if FLOATVAR
  3756. return OP_GetNativeFloatElementI(instance, Js::JavascriptNumber::ToVar(index, scriptContext));
  3757. #else
  3758. char buffer[sizeof(Js::JavascriptNumber)];
  3759. return OP_GetNativeFloatElementI(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3760. (Js::JavascriptNumber *)buffer));
  3761. #endif
  3762. JIT_HELPER_END(Op_GetNativeFloatElementI_UInt32);
  3763. }
  3764. double JavascriptOperators::OP_GetNativeFloatElementI_Int32(Var instance, int32 index, ScriptContext* scriptContext)
  3765. {
  3766. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_GetNativeFloatElementI_Int32);
  3767. JIT_HELPER_SAME_ATTRIBUTES(Op_GetNativeFloatElementI_Int32, Op_GetNativeFloatElementI);
  3768. #if FLOATVAR
  3769. return OP_GetNativeFloatElementI(instance, Js::JavascriptNumber::ToVar(index, scriptContext));
  3770. #else
  3771. char buffer[sizeof(Js::JavascriptNumber)];
  3772. return OP_GetNativeFloatElementI(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3773. (Js::JavascriptNumber *)buffer));
  3774. #endif
  3775. JIT_HELPER_END(Op_GetNativeFloatElementI_Int32);
  3776. }
  3777. Var JavascriptOperators::OP_GetMethodElement_UInt32(Var instance, uint32 index, ScriptContext* scriptContext)
  3778. {
  3779. JIT_HELPER_REENTRANT_HEADER(Op_GetMethodElement_UInt32);
  3780. JIT_HELPER_SAME_ATTRIBUTES(Op_GetMethodElement_UInt32, Op_GetMethodElement);
  3781. #if FLOATVAR
  3782. return OP_GetMethodElement(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext);
  3783. #else
  3784. char buffer[sizeof(Js::JavascriptNumber)];
  3785. return OP_GetMethodElement(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3786. (Js::JavascriptNumber *)buffer), scriptContext);
  3787. #endif
  3788. JIT_HELPER_END(Op_GetMethodElement_UInt32);
  3789. }
  3790. Var JavascriptOperators::OP_GetMethodElement_Int32(Var instance, int32 index, ScriptContext* scriptContext)
  3791. {
  3792. JIT_HELPER_REENTRANT_HEADER(Op_GetMethodElement_Int32);
  3793. JIT_HELPER_SAME_ATTRIBUTES(Op_GetMethodElement_Int32, Op_GetMethodElement);
  3794. #if FLOATVAR
  3795. return OP_GetElementI(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext);
  3796. #else
  3797. char buffer[sizeof(Js::JavascriptNumber)];
  3798. return OP_GetMethodElement(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3799. (Js::JavascriptNumber *)buffer), scriptContext);
  3800. #endif
  3801. JIT_HELPER_END(Op_GetMethodElement_Int32);
  3802. }
  3803. Var JavascriptOperators::OP_GetMethodElement(Var instance, Var index, ScriptContext* scriptContext)
  3804. {
  3805. JIT_HELPER_REENTRANT_HEADER(Op_GetMethodElement);
  3806. RecyclableObject* object = nullptr;
  3807. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  3808. {
  3809. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined, GetPropertyDisplayNameForError(index, scriptContext));
  3810. }
  3811. ThreadContext* threadContext = scriptContext->GetThreadContext();
  3812. ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  3813. threadContext->ClearImplicitCallFlags();
  3814. uint32 indexVal;
  3815. PropertyRecord const * propertyRecord = nullptr;
  3816. Var value = NULL;
  3817. BOOL hasProperty = FALSE;
  3818. IndexType indexType = GetIndexType(index, scriptContext, &indexVal, &propertyRecord, false);
  3819. if (indexType == IndexType_Number)
  3820. {
  3821. hasProperty = JavascriptOperators::GetItemReference(instance, object, indexVal, &value, scriptContext);
  3822. }
  3823. else
  3824. {
  3825. Assert(indexType == IndexType_PropertyId);
  3826. if (propertyRecord == nullptr && !JavascriptOperators::CanShortcutOnUnknownPropertyName(object))
  3827. {
  3828. indexType = GetIndexTypeFromPrimitive(index, scriptContext, &indexVal, &propertyRecord, true);
  3829. Assert(indexType == IndexType_PropertyId);
  3830. Assert(propertyRecord != nullptr);
  3831. }
  3832. if (propertyRecord != nullptr)
  3833. {
  3834. hasProperty = JavascriptOperators::GetPropertyReference(instance, object, propertyRecord->GetPropertyId(), &value, scriptContext, NULL);
  3835. }
  3836. #if DBG
  3837. else
  3838. {
  3839. JavascriptString* indexStr = JavascriptConversion::ToString(index, scriptContext);
  3840. PropertyRecord const * debugPropertyRecord;
  3841. scriptContext->GetOrAddPropertyRecord(indexStr, &debugPropertyRecord);
  3842. AssertMsg(!JavascriptOperators::GetPropertyReference(instance, object, debugPropertyRecord->GetPropertyId(), &value, scriptContext, NULL),
  3843. "how did this property come? See OS Bug 2727708 if you see this come from the web");
  3844. }
  3845. #endif
  3846. }
  3847. if (!hasProperty)
  3848. {
  3849. JavascriptString* varName = nullptr;
  3850. if (indexType == IndexType_PropertyId && propertyRecord != nullptr && propertyRecord->IsSymbol())
  3851. {
  3852. varName = JavascriptSymbol::ToString(propertyRecord, scriptContext);
  3853. }
  3854. else
  3855. {
  3856. varName = JavascriptConversion::ToString(index, scriptContext);
  3857. }
  3858. // 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
  3859. // first (#3). Postpone throwing error to invoke time.
  3860. value = ThrowErrorObject::CreateThrowTypeErrorObject(scriptContext, VBSERR_OLENoPropOrMethod, varName);
  3861. }
  3862. else if(!JavascriptConversion::IsCallable(value))
  3863. {
  3864. // 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
  3865. // first (#3). Postpone throwing error to invoke time.
  3866. JavascriptString* varName = JavascriptConversion::ToString(index, scriptContext);
  3867. value = ThrowErrorObject::CreateThrowTypeErrorObject(scriptContext, JSERR_Property_NeedFunction, varName);
  3868. }
  3869. threadContext->CheckAndResetImplicitCallAccessorFlag();
  3870. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  3871. return value;
  3872. JIT_HELPER_END(Op_GetMethodElement);
  3873. }
  3874. BOOL JavascriptOperators::OP_SetElementI_UInt32(Var instance, uint32 index, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  3875. {
  3876. JIT_HELPER_REENTRANT_HEADER(Op_SetElementI_UInt32);
  3877. JIT_HELPER_SAME_ATTRIBUTES(Op_SetElementI_UInt32, Op_SetElementI);
  3878. #if FLOATVAR
  3879. return OP_SetElementI_JIT(instance, Js::JavascriptNumber::ToVar(index, scriptContext), value, scriptContext, flags);
  3880. #else
  3881. char buffer[sizeof(Js::JavascriptNumber)];
  3882. return OP_SetElementI_JIT(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3883. (Js::JavascriptNumber *)buffer), value, scriptContext, flags);
  3884. #endif
  3885. JIT_HELPER_END(Op_SetElementI_UInt32);
  3886. }
  3887. BOOL JavascriptOperators::OP_SetElementI_Int32(Var instance, int32 index, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  3888. {
  3889. JIT_HELPER_REENTRANT_HEADER(Op_SetElementI_Int32);
  3890. JIT_HELPER_SAME_ATTRIBUTES(Op_SetElementI_Int32, Op_SetElementI);
  3891. #if FLOATVAR
  3892. return OP_SetElementI_JIT(instance, Js::JavascriptNumber::ToVar(index, scriptContext), value, scriptContext, flags);
  3893. #else
  3894. char buffer[sizeof(Js::JavascriptNumber)];
  3895. return OP_SetElementI_JIT(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  3896. (Js::JavascriptNumber *)buffer), value, scriptContext, flags);
  3897. #endif
  3898. JIT_HELPER_END(Op_SetElementI_Int32);
  3899. }
  3900. BOOL JavascriptOperators::OP_SetElementI_JIT(Var instance, Var index, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  3901. {
  3902. JIT_HELPER_REENTRANT_HEADER(Op_SetElementI);
  3903. if (TaggedNumber::Is(instance))
  3904. {
  3905. return OP_SetElementI(instance, index, value, scriptContext, flags);
  3906. }
  3907. INT_PTR vt = VirtualTableInfoBase::GetVirtualTable(instance);
  3908. OP_SetElementI(instance, index, value, scriptContext, flags);
  3909. return vt != VirtualTableInfoBase::GetVirtualTable(instance);
  3910. JIT_HELPER_END(Op_SetElementI);
  3911. }
  3912. BOOL JavascriptOperators::OP_SetElementI(Var instance, Var index, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  3913. {
  3914. #if ENABLE_COPYONACCESS_ARRAY
  3915. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  3916. #endif
  3917. TypeId instanceType = JavascriptOperators::GetTypeId(instance);
  3918. bool isTypedArray = (instanceType >= TypeIds_Int8Array && instanceType <= TypeIds_Float64Array);
  3919. if (isTypedArray)
  3920. {
  3921. if (TaggedInt::Is(index) || JavascriptNumber::Is_NoTaggedIntCheck(index) || JavascriptString::Is(index))
  3922. {
  3923. BOOL returnValue = FALSE;
  3924. bool isNumericIndex = false;
  3925. // CrossSite types will go down the slow path.
  3926. switch (instanceType)
  3927. {
  3928. case TypeIds_Int8Array:
  3929. {
  3930. // The typed array will deal with all possible values for the index
  3931. if (VirtualTableInfo<Int8VirtualArray>::HasVirtualTable(instance))
  3932. {
  3933. Int8VirtualArray* int8Array = Int8VirtualArray::UnsafeFromVar(instance);
  3934. returnValue = int8Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  3935. }
  3936. else if( VirtualTableInfo<Int8Array>::HasVirtualTable(instance))
  3937. {
  3938. Int8Array* int8Array = Int8Array::UnsafeFromVar(instance);
  3939. returnValue = int8Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  3940. }
  3941. break;
  3942. }
  3943. case TypeIds_Uint8Array:
  3944. {
  3945. // The typed array will deal with all possible values for the index
  3946. if (VirtualTableInfo<Uint8VirtualArray>::HasVirtualTable(instance))
  3947. {
  3948. Uint8VirtualArray* uint8Array = Uint8VirtualArray::UnsafeFromVar(instance);
  3949. returnValue = uint8Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  3950. }
  3951. else if (VirtualTableInfo<Uint8Array>::HasVirtualTable(instance))
  3952. {
  3953. Uint8Array* uint8Array = Uint8Array::UnsafeFromVar(instance);
  3954. returnValue = uint8Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  3955. }
  3956. break;
  3957. }
  3958. case TypeIds_Uint8ClampedArray:
  3959. {
  3960. // The typed array will deal with all possible values for the index
  3961. if (VirtualTableInfo<Uint8ClampedVirtualArray>::HasVirtualTable(instance))
  3962. {
  3963. Uint8ClampedVirtualArray* uint8ClampedArray = Uint8ClampedVirtualArray::UnsafeFromVar(instance);
  3964. returnValue = uint8ClampedArray->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  3965. }
  3966. else if(VirtualTableInfo<Uint8ClampedArray>::HasVirtualTable(instance))
  3967. {
  3968. Uint8ClampedArray* uint8ClampedArray = Uint8ClampedArray::UnsafeFromVar(instance);
  3969. returnValue = uint8ClampedArray->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  3970. }
  3971. break;
  3972. }
  3973. case TypeIds_Int16Array:
  3974. {
  3975. // The type array will deal with all possible values for the index
  3976. if (VirtualTableInfo<Int16VirtualArray>::HasVirtualTable(instance))
  3977. {
  3978. Int16VirtualArray* int16Array = Int16VirtualArray::UnsafeFromVar(instance);
  3979. returnValue = int16Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  3980. }
  3981. else if (VirtualTableInfo<Int16Array>::HasVirtualTable(instance))
  3982. {
  3983. Int16Array* int16Array = Int16Array::UnsafeFromVar(instance);
  3984. returnValue = int16Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  3985. }
  3986. break;
  3987. }
  3988. case TypeIds_Uint16Array:
  3989. {
  3990. // The type array will deal with all possible values for the index
  3991. if (VirtualTableInfo<Uint16VirtualArray>::HasVirtualTable(instance))
  3992. {
  3993. Uint16VirtualArray* uint16Array = Uint16VirtualArray::UnsafeFromVar(instance);
  3994. returnValue = uint16Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  3995. }
  3996. else if (VirtualTableInfo<Uint16Array>::HasVirtualTable(instance))
  3997. {
  3998. Uint16Array* uint16Array = Uint16Array::UnsafeFromVar(instance);
  3999. returnValue = uint16Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4000. }
  4001. break;
  4002. }
  4003. case TypeIds_Int32Array:
  4004. {
  4005. // The type array will deal with all possible values for the index
  4006. if (VirtualTableInfo<Int32VirtualArray>::HasVirtualTable(instance))
  4007. {
  4008. Int32VirtualArray* int32Array = Int32VirtualArray::UnsafeFromVar(instance);
  4009. returnValue = int32Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4010. }
  4011. else if(VirtualTableInfo<Int32Array>::HasVirtualTable(instance))
  4012. {
  4013. Int32Array* int32Array = Int32Array::UnsafeFromVar(instance);
  4014. returnValue = int32Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4015. }
  4016. break;
  4017. }
  4018. case TypeIds_Uint32Array:
  4019. {
  4020. // The type array will deal with all possible values for the index
  4021. if (VirtualTableInfo<Uint32VirtualArray>::HasVirtualTable(instance))
  4022. {
  4023. Uint32VirtualArray* uint32Array = Uint32VirtualArray::UnsafeFromVar(instance);
  4024. returnValue = uint32Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4025. }
  4026. else if (VirtualTableInfo<Uint32Array>::HasVirtualTable(instance))
  4027. {
  4028. Uint32Array* uint32Array = Uint32Array::UnsafeFromVar(instance);
  4029. returnValue = uint32Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4030. }
  4031. break;
  4032. }
  4033. case TypeIds_Float32Array:
  4034. {
  4035. // The type array will deal with all possible values for the index
  4036. if (VirtualTableInfo<Float32VirtualArray>::HasVirtualTable(instance))
  4037. {
  4038. Float32VirtualArray* float32Array = Float32VirtualArray::UnsafeFromVar(instance);
  4039. returnValue = float32Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4040. }
  4041. else if (VirtualTableInfo<Float32Array>::HasVirtualTable(instance))
  4042. {
  4043. Float32Array* float32Array = Float32Array::UnsafeFromVar(instance);
  4044. returnValue = float32Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4045. }
  4046. break;
  4047. }
  4048. case TypeIds_Float64Array:
  4049. {
  4050. // The type array will deal with all possible values for the index
  4051. if (VirtualTableInfo<Float64VirtualArray>::HasVirtualTable(instance))
  4052. {
  4053. Float64VirtualArray* float64Array = Float64VirtualArray::UnsafeFromVar(instance);
  4054. returnValue = float64Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4055. }
  4056. else if (VirtualTableInfo<Float64Array>::HasVirtualTable(instance))
  4057. {
  4058. Float64Array* float64Array = Float64Array::UnsafeFromVar(instance);
  4059. returnValue = float64Array->ValidateIndexAndDirectSetItem(index, value, &isNumericIndex);
  4060. }
  4061. break;
  4062. }
  4063. }
  4064. // if this was numeric index, return operation status else
  4065. // Return the result of calling the default ordinary object [[Set]] internal method (9.1.8) on O passing P, V, and Receiver as arguments.
  4066. if (isNumericIndex)
  4067. return returnValue;
  4068. }
  4069. }
  4070. else
  4071. {
  4072. if (TaggedInt::Is(index))
  4073. {
  4074. TaggedIntIndex:
  4075. switch (instanceType)
  4076. {
  4077. case TypeIds_NativeIntArray:
  4078. case TypeIds_NativeFloatArray:
  4079. case TypeIds_Array: // fast path for array
  4080. {
  4081. int indexInt = TaggedInt::ToInt32(index);
  4082. if (indexInt >= 0 && scriptContext->optimizationOverrides.IsEnabledArraySetElementFastPath())
  4083. {
  4084. JavascriptArray::UnsafeFromVar(instance)->SetItem((uint32)indexInt, value, flags);
  4085. return TRUE;
  4086. }
  4087. break;
  4088. }
  4089. }
  4090. }
  4091. else if (JavascriptNumber::Is_NoTaggedIntCheck(index))
  4092. {
  4093. double dIndexValue = JavascriptNumber::GetValue(index);
  4094. uint32 uint32Index = JavascriptConversion::ToUInt32(index, scriptContext);
  4095. if ((double)uint32Index == dIndexValue && !TaggedInt::IsOverflow(uint32Index))
  4096. {
  4097. index = TaggedInt::ToVarUnchecked(uint32Index);
  4098. goto TaggedIntIndex;
  4099. }
  4100. }
  4101. }
  4102. RecyclableObject* object = nullptr;
  4103. if (!GetPropertyObjectForSetElementI(instance, index, scriptContext, &object))
  4104. {
  4105. return FALSE;
  4106. }
  4107. return JavascriptOperators::SetElementIHelper(instance, object, index, value, scriptContext, flags);
  4108. }
  4109. BOOL JavascriptOperators::SetElementIHelper(Var receiver, RecyclableObject* object, Var index, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  4110. {
  4111. IndexType indexType;
  4112. uint32 indexVal = 0;
  4113. PropertyRecord const * propertyRecord = nullptr;
  4114. JavascriptString * propertyNameString = nullptr;
  4115. PropertyValueInfo propertyValueInfo;
  4116. RecyclableObject* cacheOwner;
  4117. PropertyRecordUsageCache* propertyRecordUsageCache;
  4118. if (JavascriptOperators::GetPropertyRecordUsageCache(index, scriptContext, &propertyRecordUsageCache, &cacheOwner))
  4119. {
  4120. return JavascriptOperators::SetElementIWithCache<false>(receiver, object, cacheOwner, value, propertyRecordUsageCache, scriptContext, flags, nullptr);
  4121. }
  4122. if (TaggedNumber::Is(receiver))
  4123. {
  4124. return JavascriptOperators::SetElementIOnTaggedNumber(receiver, object, index, value, scriptContext, flags);
  4125. }
  4126. #if DBG_DUMP
  4127. scriptContext->forinNoCache += (!TaggedInt::Is(index) && JavascriptString::Is(index));
  4128. #endif
  4129. indexType = GetIndexType(index, scriptContext, &indexVal, &propertyRecord, &propertyNameString, false, true);
  4130. if (scriptContext->GetThreadContext()->IsDisableImplicitCall() &&
  4131. scriptContext->GetThreadContext()->GetImplicitCallFlags() != ImplicitCall_None)
  4132. {
  4133. // We hit an implicit call trying to convert the index, and implicit calls are disabled, so
  4134. // quit before we try to store the element.
  4135. return FALSE;
  4136. }
  4137. if (indexType == IndexType_Number)
  4138. {
  4139. SetElementIHelper_INDEX_TYPE_IS_NUMBER:
  4140. return JavascriptOperators::SetItem(receiver, object, indexVal, value, scriptContext, flags);
  4141. }
  4142. else if (indexType == IndexType_JavascriptString)
  4143. {
  4144. Assert(propertyNameString);
  4145. // At this point, we know that the propertyNameString is neither PropertyString
  4146. // or LiteralStringWithPropertyStringPtr.. Get PropertyRecord!
  4147. // we will get it anyways otherwise. (Also, 1:1 string comparison for Builtin types will be expensive.)
  4148. if (propertyRecord == nullptr)
  4149. {
  4150. scriptContext->GetOrAddPropertyRecord(propertyNameString, &propertyRecord);
  4151. if (propertyRecord->IsNumeric())
  4152. {
  4153. indexVal = propertyRecord->GetNumericValue();
  4154. goto SetElementIHelper_INDEX_TYPE_IS_NUMBER;
  4155. }
  4156. }
  4157. }
  4158. Assert(indexType == IndexType_PropertyId || indexType == IndexType_JavascriptString);
  4159. Assert(propertyRecord);
  4160. return JavascriptOperators::SetProperty(receiver, object, propertyRecord->GetPropertyId(), value, scriptContext, flags);
  4161. }
  4162. BOOL JavascriptOperators::OP_SetNativeIntElementI_NoConvert(
  4163. Var instance,
  4164. Var aElementIndex,
  4165. int32 iValue,
  4166. ScriptContext* scriptContext,
  4167. PropertyOperationFlags flags)
  4168. {
  4169. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeIntElementI_NoConvert);
  4170. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeIntElementI_NoConvert, Op_SetNativeIntElementI);
  4171. BOOL converted = OP_SetNativeIntElementI(instance, aElementIndex, iValue, scriptContext, flags);
  4172. if (converted)
  4173. {
  4174. AssertMsg(false, "Unexpected native array conversion");
  4175. Js::Throw::FatalInternalError();
  4176. }
  4177. return FALSE;
  4178. JIT_HELPER_END(Op_SetNativeIntElementI_NoConvert);
  4179. }
  4180. BOOL JavascriptOperators::OP_SetNativeIntElementI_UInt32_NoConvert(
  4181. Var instance,
  4182. uint32 aElementIndex,
  4183. int32 iValue,
  4184. ScriptContext* scriptContext,
  4185. PropertyOperationFlags flags)
  4186. {
  4187. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeIntElementI_UInt32_NoConvert);
  4188. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeIntElementI_UInt32_NoConvert, Op_SetNativeIntElementI_UInt32);
  4189. BOOL converted = OP_SetNativeIntElementI_UInt32(instance, aElementIndex, iValue, scriptContext, flags);
  4190. if (converted)
  4191. {
  4192. AssertMsg(false, "Unexpected native array conversion");
  4193. Js::Throw::FatalInternalError();
  4194. }
  4195. return FALSE;
  4196. JIT_HELPER_END(Op_SetNativeIntElementI_UInt32_NoConvert);
  4197. }
  4198. BOOL JavascriptOperators::OP_SetNativeIntElementI_Int32_NoConvert(
  4199. Var instance,
  4200. int32 aElementIndex,
  4201. int32 iValue,
  4202. ScriptContext* scriptContext,
  4203. PropertyOperationFlags flags)
  4204. {
  4205. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeIntElementI_Int32_NoConvert);
  4206. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeIntElementI_Int32_NoConvert, Op_SetNativeIntElementI_Int32);
  4207. BOOL converted = OP_SetNativeIntElementI_Int32(instance, aElementIndex, iValue, scriptContext, flags);
  4208. if (converted)
  4209. {
  4210. AssertMsg(false, "Unexpected native array conversion");
  4211. Js::Throw::FatalInternalError();
  4212. }
  4213. return FALSE;
  4214. JIT_HELPER_END(Op_SetNativeIntElementI_Int32_NoConvert);
  4215. }
  4216. BOOL JavascriptOperators::OP_SetNativeFloatElementI_NoConvert(
  4217. Var instance,
  4218. Var aElementIndex,
  4219. ScriptContext* scriptContext,
  4220. PropertyOperationFlags flags,
  4221. double dValue)
  4222. {
  4223. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeFloatElementI_NoConvert);
  4224. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeFloatElementI_NoConvert, Op_SetNativeFloatElementI);
  4225. BOOL converted = OP_SetNativeFloatElementI(instance, aElementIndex, scriptContext, flags, dValue);
  4226. if (converted)
  4227. {
  4228. AssertMsg(false, "Unexpected native array conversion");
  4229. Js::Throw::FatalInternalError();
  4230. }
  4231. return FALSE;
  4232. JIT_HELPER_END(Op_SetNativeFloatElementI_NoConvert);
  4233. }
  4234. BOOL JavascriptOperators::OP_SetNativeFloatElementI_UInt32_NoConvert(
  4235. Var instance,
  4236. uint32 aElementIndex,
  4237. ScriptContext* scriptContext,
  4238. PropertyOperationFlags flags,
  4239. double dValue)
  4240. {
  4241. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeFloatElementI_UInt32_NoConvert);
  4242. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeFloatElementI_NoConvert, Op_SetNativeFloatElementI_UInt32);
  4243. BOOL converted = OP_SetNativeFloatElementI_UInt32(instance, aElementIndex, scriptContext, flags, dValue);
  4244. if (converted)
  4245. {
  4246. AssertMsg(false, "Unexpected native array conversion");
  4247. Js::Throw::FatalInternalError();
  4248. }
  4249. return FALSE;
  4250. JIT_HELPER_END(Op_SetNativeFloatElementI_UInt32_NoConvert);
  4251. }
  4252. BOOL JavascriptOperators::OP_SetNativeFloatElementI_Int32_NoConvert(
  4253. Var instance,
  4254. int32 aElementIndex,
  4255. ScriptContext* scriptContext,
  4256. PropertyOperationFlags flags,
  4257. double dValue)
  4258. {
  4259. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeFloatElementI_Int32_NoConvert);
  4260. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeFloatElementI_NoConvert, Op_SetNativeFloatElementI_Int32);
  4261. BOOL converted = OP_SetNativeFloatElementI_Int32(instance, aElementIndex, scriptContext, flags, dValue);
  4262. if (converted)
  4263. {
  4264. AssertMsg(false, "Unexpected native array conversion");
  4265. Js::Throw::FatalInternalError();
  4266. }
  4267. return FALSE;
  4268. JIT_HELPER_END(Op_SetNativeFloatElementI_Int32_NoConvert);
  4269. }
  4270. BOOL JavascriptOperators::OP_SetNativeIntElementI(
  4271. Var instance,
  4272. Var aElementIndex,
  4273. int32 iValue,
  4274. ScriptContext* scriptContext,
  4275. PropertyOperationFlags flags)
  4276. {
  4277. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeIntElementI);
  4278. INT_PTR vt = (INT_PTR)nullptr;
  4279. vt = VirtualTableInfoBase::GetVirtualTable(instance);
  4280. if (TaggedInt::Is(aElementIndex))
  4281. {
  4282. int32 indexInt = TaggedInt::ToInt32(aElementIndex);
  4283. if (indexInt >= 0 && scriptContext->optimizationOverrides.IsEnabledArraySetElementFastPath())
  4284. {
  4285. JavascriptNativeIntArray *arr = JavascriptNativeIntArray::FromVar(instance);
  4286. if (!(arr->TryGrowHeadSegmentAndSetItem<int32, JavascriptNativeIntArray>((uint32)indexInt, iValue)))
  4287. {
  4288. arr->SetItem(indexInt, iValue);
  4289. }
  4290. return vt != VirtualTableInfoBase::GetVirtualTable(instance);
  4291. }
  4292. }
  4293. JavascriptOperators::OP_SetElementI(instance, aElementIndex, JavascriptNumber::ToVar(iValue, scriptContext), scriptContext, flags);
  4294. return vt != VirtualTableInfoBase::GetVirtualTable(instance);
  4295. JIT_HELPER_END(Op_SetNativeIntElementI);
  4296. }
  4297. BOOL JavascriptOperators::OP_SetNativeIntElementI_UInt32(
  4298. Var instance,
  4299. uint32 aElementIndex,
  4300. int32 iValue,
  4301. ScriptContext* scriptContext,
  4302. PropertyOperationFlags flags)
  4303. {
  4304. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeIntElementI_UInt32);
  4305. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeIntElementI_UInt32, Op_SetNativeIntElementI);
  4306. #if FLOATVAR
  4307. return OP_SetNativeIntElementI(instance, Js::JavascriptNumber::ToVar(aElementIndex, scriptContext), iValue, scriptContext, flags);
  4308. #else
  4309. char buffer[sizeof(Js::JavascriptNumber)];
  4310. return OP_SetNativeIntElementI(instance, Js::JavascriptNumber::ToVarInPlace(aElementIndex, scriptContext,
  4311. (Js::JavascriptNumber *)buffer), iValue, scriptContext, flags);
  4312. #endif
  4313. JIT_HELPER_END(Op_SetNativeIntElementI_UInt32);
  4314. }
  4315. BOOL JavascriptOperators::OP_SetNativeIntElementI_Int32(
  4316. Var instance,
  4317. int aElementIndex,
  4318. int32 iValue,
  4319. ScriptContext* scriptContext,
  4320. PropertyOperationFlags flags)
  4321. {
  4322. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeIntElementI_Int32);
  4323. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeIntElementI_Int32, Op_SetNativeIntElementI);
  4324. #if FLOATVAR
  4325. return OP_SetNativeIntElementI(instance, Js::JavascriptNumber::ToVar(aElementIndex, scriptContext), iValue, scriptContext, flags);
  4326. #else
  4327. char buffer[sizeof(Js::JavascriptNumber)];
  4328. return OP_SetNativeIntElementI(instance, Js::JavascriptNumber::ToVarInPlace(aElementIndex, scriptContext,
  4329. (Js::JavascriptNumber *)buffer), iValue, scriptContext, flags);
  4330. #endif
  4331. JIT_HELPER_END(Op_SetNativeIntElementI_Int32);
  4332. }
  4333. BOOL JavascriptOperators::OP_SetNativeFloatElementI(
  4334. Var instance,
  4335. Var aElementIndex,
  4336. ScriptContext* scriptContext,
  4337. PropertyOperationFlags flags,
  4338. double dValue)
  4339. {
  4340. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeFloatElementI);
  4341. INT_PTR vt = (INT_PTR)nullptr;
  4342. vt = VirtualTableInfoBase::GetVirtualTable(instance);
  4343. if (TaggedInt::Is(aElementIndex))
  4344. {
  4345. int32 indexInt = TaggedInt::ToInt32(aElementIndex);
  4346. if (indexInt >= 0 && scriptContext->optimizationOverrides.IsEnabledArraySetElementFastPath())
  4347. {
  4348. JavascriptNativeFloatArray *arr = JavascriptNativeFloatArray::FromVar(instance);
  4349. if (!(arr->TryGrowHeadSegmentAndSetItem<double, JavascriptNativeFloatArray>((uint32)indexInt, dValue)))
  4350. {
  4351. arr->SetItem(indexInt, dValue);
  4352. }
  4353. return vt != VirtualTableInfoBase::GetVirtualTable(instance);
  4354. }
  4355. }
  4356. JavascriptOperators::OP_SetElementI(instance, aElementIndex, JavascriptNumber::ToVarWithCheck(dValue, scriptContext), scriptContext, flags);
  4357. return vt != VirtualTableInfoBase::GetVirtualTable(instance);
  4358. JIT_HELPER_END(Op_SetNativeFloatElementI);
  4359. }
  4360. BOOL JavascriptOperators::OP_SetNativeFloatElementI_UInt32(
  4361. Var instance,
  4362. uint32 aElementIndex,
  4363. ScriptContext* scriptContext,
  4364. PropertyOperationFlags flags,
  4365. double dValue)
  4366. {
  4367. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeFloatElementI_UInt32);
  4368. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeFloatElementI_UInt32, Op_SetNativeFloatElementI);
  4369. #if FLOATVAR
  4370. return OP_SetNativeFloatElementI(instance, JavascriptNumber::ToVar(aElementIndex, scriptContext), scriptContext, flags, dValue);
  4371. #else
  4372. char buffer[sizeof(Js::JavascriptNumber)];
  4373. return OP_SetNativeFloatElementI(instance, JavascriptNumber::ToVarInPlace(aElementIndex, scriptContext,
  4374. (Js::JavascriptNumber *)buffer), scriptContext, flags, dValue);
  4375. #endif
  4376. JIT_HELPER_END(Op_SetNativeFloatElementI_UInt32);
  4377. }
  4378. BOOL JavascriptOperators::OP_SetNativeFloatElementI_Int32(
  4379. Var instance,
  4380. int aElementIndex,
  4381. ScriptContext* scriptContext,
  4382. PropertyOperationFlags flags,
  4383. double dValue)
  4384. {
  4385. JIT_HELPER_REENTRANT_HEADER(Op_SetNativeFloatElementI_Int32);
  4386. JIT_HELPER_SAME_ATTRIBUTES(Op_SetNativeFloatElementI_Int32, Op_SetNativeFloatElementI);
  4387. #if FLOATVAR
  4388. return OP_SetNativeFloatElementI(instance, JavascriptNumber::ToVar(aElementIndex, scriptContext), scriptContext, flags, dValue);
  4389. #else
  4390. char buffer[sizeof(Js::JavascriptNumber)];
  4391. return OP_SetNativeFloatElementI(instance, JavascriptNumber::ToVarInPlace(aElementIndex, scriptContext,
  4392. (Js::JavascriptNumber *)buffer), scriptContext, flags, dValue);
  4393. #endif
  4394. JIT_HELPER_END(Op_SetNativeFloatElementI_Int32);
  4395. }
  4396. BOOL JavascriptOperators::OP_Memcopy(Var dstInstance, int32 dstStart, Var srcInstance, int32 srcStart, int32 length, ScriptContext* scriptContext)
  4397. {
  4398. JIT_HELPER_NOT_REENTRANT_HEADER(Op_Memcopy, reentrancylock, scriptContext->GetThreadContext());
  4399. if (length <= 0)
  4400. {
  4401. return false;
  4402. }
  4403. TypeId instanceType = JavascriptOperators::GetTypeId(srcInstance);
  4404. if (instanceType != JavascriptOperators::GetTypeId(dstInstance))
  4405. {
  4406. return false;
  4407. }
  4408. if (srcStart != dstStart)
  4409. {
  4410. return false;
  4411. }
  4412. BOOL returnValue = false;
  4413. #define MEMCOPY_TYPED_ARRAY(type, conversion) type ## ::FromVar(dstInstance)->DirectSetItemAtRange( type ## ::FromVar(srcInstance), srcStart, dstStart, length, JavascriptConversion:: ## conversion)
  4414. switch (instanceType)
  4415. {
  4416. case TypeIds_Int8Array:
  4417. {
  4418. returnValue = MEMCOPY_TYPED_ARRAY(Int8Array, ToInt8);
  4419. break;
  4420. }
  4421. case TypeIds_Uint8Array:
  4422. {
  4423. returnValue = MEMCOPY_TYPED_ARRAY(Uint8Array, ToUInt8);
  4424. break;
  4425. }
  4426. case TypeIds_Uint8ClampedArray:
  4427. {
  4428. returnValue = MEMCOPY_TYPED_ARRAY(Uint8ClampedArray, ToUInt8Clamped);
  4429. break;
  4430. }
  4431. case TypeIds_Int16Array:
  4432. {
  4433. returnValue = MEMCOPY_TYPED_ARRAY(Int16Array, ToInt16);
  4434. break;
  4435. }
  4436. case TypeIds_Uint16Array:
  4437. {
  4438. returnValue = MEMCOPY_TYPED_ARRAY(Uint16Array, ToUInt16);
  4439. break;
  4440. }
  4441. case TypeIds_Int32Array:
  4442. {
  4443. returnValue = MEMCOPY_TYPED_ARRAY(Int32Array, ToInt32);
  4444. break;
  4445. }
  4446. case TypeIds_Uint32Array:
  4447. {
  4448. returnValue = MEMCOPY_TYPED_ARRAY(Uint32Array, ToUInt32);
  4449. break;
  4450. }
  4451. case TypeIds_Float32Array:
  4452. {
  4453. returnValue = MEMCOPY_TYPED_ARRAY(Float32Array, ToFloat);
  4454. break;
  4455. }
  4456. case TypeIds_Float64Array:
  4457. {
  4458. returnValue = MEMCOPY_TYPED_ARRAY(Float64Array, ToNumber);
  4459. break;
  4460. }
  4461. case TypeIds_Array:
  4462. case TypeIds_NativeFloatArray:
  4463. case TypeIds_NativeIntArray:
  4464. {
  4465. if (dstStart < 0 || srcStart < 0)
  4466. {
  4467. // This is not supported, Bailout
  4468. break;
  4469. }
  4470. // Upper bounds check for source array
  4471. JavascriptArray* srcArray = JavascriptArray::UnsafeFromVar(srcInstance);
  4472. JavascriptArray* dstArray = JavascriptArray::FromVar(dstInstance);
  4473. if (scriptContext->optimizationOverrides.IsEnabledArraySetElementFastPath())
  4474. {
  4475. INT_PTR vt = VirtualTableInfoBase::GetVirtualTable(dstInstance);
  4476. if (instanceType == TypeIds_Array)
  4477. {
  4478. returnValue = dstArray->DirectSetItemAtRangeFromArray<Var>(dstStart, length, srcArray, srcStart);
  4479. }
  4480. else if (instanceType == TypeIds_NativeIntArray)
  4481. {
  4482. returnValue = dstArray->DirectSetItemAtRangeFromArray<int32>(dstStart, length, srcArray, srcStart);
  4483. }
  4484. else
  4485. {
  4486. returnValue = dstArray->DirectSetItemAtRangeFromArray<double>(dstStart, length, srcArray, srcStart);
  4487. }
  4488. returnValue &= vt == VirtualTableInfoBase::GetVirtualTable(dstInstance);
  4489. }
  4490. break;
  4491. }
  4492. default:
  4493. AssertMsg(false, "We don't support this type for memcopy yet.");
  4494. break;
  4495. }
  4496. #undef MEMCOPY_TYPED_ARRAY
  4497. return returnValue;
  4498. JIT_HELPER_END(Op_Memcopy);
  4499. }
  4500. template<typename T, T(*func)(Var, ScriptContext*)> bool MemsetConversion(Var value, ScriptContext* scriptContext, T* result)
  4501. {
  4502. ImplicitCallFlags flags = scriptContext->GetThreadContext()->TryWithDisabledImplicitCall([&]
  4503. {
  4504. *result = func(value, scriptContext);
  4505. });
  4506. return (flags & (~ImplicitCall_None)) == 0;
  4507. }
  4508. BOOL JavascriptOperators::OP_Memset(Var instance, int32 start, Var value, int32 length, ScriptContext* scriptContext)
  4509. {
  4510. JIT_HELPER_NOT_REENTRANT_HEADER(Op_Memset, reentrancylock, scriptContext->GetThreadContext());
  4511. if (length <= 0)
  4512. {
  4513. return false;
  4514. }
  4515. TypeId instanceType = JavascriptOperators::GetTypeId(instance);
  4516. BOOL returnValue = false;
  4517. // The typed array will deal with all possible values for the index
  4518. #define MEMSET_TYPED_ARRAY_CASE(type, conversion) \
  4519. case TypeIds_##type: \
  4520. { \
  4521. type## ::TypedArrayType typedValue = 0; \
  4522. if (!MemsetConversion<type## ::TypedArrayType, JavascriptConversion:: ##conversion>(value, scriptContext, &typedValue)) return false; \
  4523. returnValue = type## ::FromVar(instance)->DirectSetItemAtRange(start, length, typedValue); \
  4524. break; \
  4525. }
  4526. switch (instanceType)
  4527. {
  4528. MEMSET_TYPED_ARRAY_CASE(Int8Array, ToInt8)
  4529. MEMSET_TYPED_ARRAY_CASE(Uint8Array, ToUInt8)
  4530. MEMSET_TYPED_ARRAY_CASE(Uint8ClampedArray, ToUInt8Clamped)
  4531. MEMSET_TYPED_ARRAY_CASE(Int16Array, ToInt16)
  4532. MEMSET_TYPED_ARRAY_CASE(Uint16Array, ToUInt16)
  4533. MEMSET_TYPED_ARRAY_CASE(Int32Array, ToInt32)
  4534. MEMSET_TYPED_ARRAY_CASE(Uint32Array, ToUInt32)
  4535. MEMSET_TYPED_ARRAY_CASE(Float32Array, ToFloat)
  4536. MEMSET_TYPED_ARRAY_CASE(Float64Array, ToNumber)
  4537. case TypeIds_NativeFloatArray:
  4538. case TypeIds_NativeIntArray:
  4539. case TypeIds_Array:
  4540. {
  4541. if (start < 0)
  4542. {
  4543. for (start; start < 0 && length > 0; ++start, --length)
  4544. {
  4545. if (!OP_SetElementI(instance, JavascriptNumber::ToVar(start, scriptContext), value, scriptContext))
  4546. {
  4547. return false;
  4548. }
  4549. }
  4550. }
  4551. if (scriptContext->optimizationOverrides.IsEnabledArraySetElementFastPath())
  4552. {
  4553. INT_PTR vt = VirtualTableInfoBase::GetVirtualTable(instance);
  4554. if (instanceType == TypeIds_Array)
  4555. {
  4556. returnValue = JavascriptArray::UnsafeFromVar(instance)->DirectSetItemAtRange<Var>(start, length, value);
  4557. }
  4558. else if (instanceType == TypeIds_NativeIntArray)
  4559. {
  4560. // Only accept tagged int.
  4561. if (!TaggedInt::Is(value))
  4562. {
  4563. return false;
  4564. }
  4565. int32 intValue = 0;
  4566. if (!MemsetConversion<int32, JavascriptConversion::ToInt32>(value, scriptContext, &intValue))
  4567. {
  4568. return false;
  4569. }
  4570. // Special case for missing item
  4571. if (SparseArraySegment<int32>::IsMissingItem(&intValue))
  4572. {
  4573. return false;
  4574. }
  4575. returnValue = JavascriptArray::UnsafeFromVar(instance)->DirectSetItemAtRange<int32>(start, length, intValue);
  4576. }
  4577. else
  4578. {
  4579. // For native float arrays, the jit doesn't check the type of the source so we have to do it here
  4580. if (!JavascriptNumber::Is(value) && !TaggedNumber::Is(value))
  4581. {
  4582. return false;
  4583. }
  4584. double doubleValue = 0;
  4585. if (!MemsetConversion<double, JavascriptConversion::ToNumber>(value, scriptContext, &doubleValue))
  4586. {
  4587. return false;
  4588. }
  4589. // Special case for missing item
  4590. if (SparseArraySegment<double>::IsMissingItem(&doubleValue))
  4591. {
  4592. return false;
  4593. }
  4594. returnValue = JavascriptArray::UnsafeFromVar(instance)->DirectSetItemAtRange<double>(start, length, doubleValue);
  4595. }
  4596. returnValue &= vt == VirtualTableInfoBase::GetVirtualTable(instance);
  4597. }
  4598. break;
  4599. }
  4600. default:
  4601. AssertMsg(false, "We don't support this type for memset yet.");
  4602. break;
  4603. }
  4604. #undef MEMSET_TYPED_ARRAY
  4605. return returnValue;
  4606. JIT_HELPER_END(Op_Memset);
  4607. }
  4608. Var JavascriptOperators::OP_DeleteElementI_UInt32(Var instance, uint32 index, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags)
  4609. {
  4610. JIT_HELPER_REENTRANT_HEADER(Op_DeleteElementI_UInt32);
  4611. JIT_HELPER_SAME_ATTRIBUTES(Op_DeleteElementI_UInt32, Op_DeleteElementI);
  4612. #if FLOATVAR
  4613. return OP_DeleteElementI(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext, propertyOperationFlags);
  4614. #else
  4615. char buffer[sizeof(Js::JavascriptNumber)];
  4616. return OP_DeleteElementI(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  4617. (Js::JavascriptNumber *)buffer), scriptContext, propertyOperationFlags);
  4618. #endif
  4619. JIT_HELPER_END(Op_DeleteElementI_UInt32);
  4620. }
  4621. Var JavascriptOperators::OP_DeleteElementI_Int32(Var instance, int32 index, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags)
  4622. {
  4623. JIT_HELPER_REENTRANT_HEADER(Op_DeleteElementI_Int32);
  4624. JIT_HELPER_SAME_ATTRIBUTES(Op_DeleteElementI_Int32, Op_DeleteElementI);
  4625. #if FLOATVAR
  4626. return OP_DeleteElementI(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext, propertyOperationFlags);
  4627. #else
  4628. char buffer[sizeof(Js::JavascriptNumber)];
  4629. return OP_DeleteElementI(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
  4630. (Js::JavascriptNumber *)buffer), scriptContext, propertyOperationFlags);
  4631. #endif
  4632. JIT_HELPER_END(Op_DeleteElementI_Int32);
  4633. }
  4634. Var JavascriptOperators::OP_DeleteElementI(Var instance, Var index, ScriptContext* scriptContext, PropertyOperationFlags propertyOperationFlags)
  4635. {
  4636. JIT_HELPER_REENTRANT_HEADER(Op_DeleteElementI);
  4637. if(TaggedNumber::Is(instance))
  4638. {
  4639. return scriptContext->GetLibrary()->GetTrue();
  4640. }
  4641. #if ENABLE_COPYONACCESS_ARRAY
  4642. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  4643. #endif
  4644. RecyclableObject* object = RecyclableObject::FromVar(instance);
  4645. if (JavascriptOperators::IsUndefinedOrNull(object))
  4646. {
  4647. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotDelete_NullOrUndefined, GetPropertyDisplayNameForError(index, scriptContext));
  4648. }
  4649. uint32 indexVal;
  4650. PropertyRecord const * propertyRecord = nullptr;
  4651. JavascriptString * propertyNameString = nullptr;
  4652. BOOL result = TRUE;
  4653. IndexType indexType = GetIndexType(index, scriptContext, &indexVal, &propertyRecord, &propertyNameString, false, true);
  4654. if (indexType == IndexType_Number)
  4655. {
  4656. result = JavascriptOperators::DeleteItem(object, indexVal, propertyOperationFlags);
  4657. }
  4658. else if (indexType == IndexType_JavascriptString)
  4659. {
  4660. result = JavascriptOperators::DeleteProperty(object, propertyNameString, propertyOperationFlags);
  4661. }
  4662. else
  4663. {
  4664. Assert(indexType == IndexType_PropertyId);
  4665. if (propertyRecord == nullptr && !JavascriptOperators::CanShortcutOnUnknownPropertyName(object))
  4666. {
  4667. indexType = GetIndexTypeFromPrimitive(index, scriptContext, &indexVal, &propertyRecord, true);
  4668. Assert(indexType == IndexType_PropertyId);
  4669. Assert(propertyRecord != nullptr);
  4670. }
  4671. if (propertyRecord != nullptr)
  4672. {
  4673. result = JavascriptOperators::DeleteProperty(object, propertyRecord->GetPropertyId(), propertyOperationFlags);
  4674. }
  4675. #if DBG
  4676. else
  4677. {
  4678. JavascriptString* indexStr = JavascriptConversion::ToString(index, scriptContext);
  4679. PropertyRecord const * debugPropertyRecord;
  4680. scriptContext->GetOrAddPropertyRecord(indexStr, &debugPropertyRecord);
  4681. AssertMsg(JavascriptOperators::DeleteProperty(object, debugPropertyRecord->GetPropertyId(), propertyOperationFlags), "delete should have been true. See OS Bug 2727708 if you see this come from the web");
  4682. }
  4683. #endif
  4684. }
  4685. Assert(result || !(propertyOperationFlags & (PropertyOperation_StrictMode | PropertyOperation_ThrowOnDeleteIfNotConfig)));
  4686. return scriptContext->GetLibrary()->CreateBoolean(result);
  4687. JIT_HELPER_END(Op_DeleteElementI);
  4688. }
  4689. Var JavascriptOperators::OP_ToPropertyKey(Js::Var argument, ScriptContext* scriptContext)
  4690. {
  4691. JIT_HELPER_REENTRANT_HEADER(Op_ConvPropertyKey);
  4692. PropertyRecord const* unused = nullptr;
  4693. return JavascriptConversion::ToPropertyKey(argument, scriptContext, &unused, nullptr);
  4694. JIT_HELPER_END(Op_ConvPropertyKey);
  4695. }
  4696. Var JavascriptOperators::OP_GetLength(Var instance, ScriptContext* scriptContext)
  4697. {
  4698. return JavascriptOperators::OP_GetProperty(instance, PropertyIds::length, scriptContext);
  4699. }
  4700. Var JavascriptOperators::GetThisFromModuleRoot(Var thisVar)
  4701. {
  4702. RootObjectBase * rootObject = static_cast<RootObjectBase*>(thisVar);
  4703. RecyclableObject* hostObject = rootObject->GetHostObject();
  4704. //
  4705. // if the module root has the host object, use that as "this"
  4706. //
  4707. if (hostObject)
  4708. {
  4709. thisVar = hostObject->GetHostDispatchVar();
  4710. }
  4711. return thisVar;
  4712. }
  4713. inline void JavascriptOperators::TryLoadRoot(Var& thisVar, TypeId typeId, int moduleID, ScriptContextInfo* scriptContext)
  4714. {
  4715. bool loadRoot = false;
  4716. if (JavascriptOperators::IsUndefinedOrNullType(typeId) || typeId == TypeIds_ActivationObject)
  4717. {
  4718. loadRoot = true;
  4719. }
  4720. else if (typeId == TypeIds_HostDispatch)
  4721. {
  4722. TypeId remoteTypeId = TypeIds_Limit;
  4723. if (RecyclableObject::FromVar(thisVar)->GetRemoteTypeId(&remoteTypeId))
  4724. {
  4725. if (remoteTypeId == TypeIds_Null || remoteTypeId == TypeIds_Undefined || remoteTypeId == TypeIds_ActivationObject)
  4726. {
  4727. loadRoot = true;
  4728. }
  4729. }
  4730. }
  4731. if (loadRoot)
  4732. {
  4733. if (moduleID == 0)
  4734. {
  4735. thisVar = (Js::Var)scriptContext->GetGlobalObjectThisAddr();
  4736. }
  4737. else
  4738. {
  4739. // TODO: OOP JIT, create a copy of module roots in server side
  4740. Js::ModuleRoot * moduleRoot = JavascriptOperators::GetModuleRoot(moduleID, (ScriptContext*)scriptContext);
  4741. if (moduleRoot == nullptr)
  4742. {
  4743. Assert(false);
  4744. thisVar = (Js::Var)scriptContext->GetUndefinedAddr();
  4745. }
  4746. else
  4747. {
  4748. thisVar = GetThisFromModuleRoot(moduleRoot);
  4749. }
  4750. }
  4751. }
  4752. }
  4753. Var JavascriptOperators::OP_GetThis(Var thisVar, int moduleID, ScriptContextInfo* scriptContext)
  4754. {
  4755. JIT_HELPER_REENTRANT_HEADER(LdThis);
  4756. //
  4757. // if "this" is null or undefined
  4758. // Pass the global object
  4759. // Else
  4760. // Pass ToObject(this)
  4761. //
  4762. TypeId typeId = JavascriptOperators::GetTypeId(thisVar);
  4763. Assert(!JavascriptOperators::IsThisSelf(typeId));
  4764. return JavascriptOperators::GetThisHelper(thisVar, typeId, moduleID, scriptContext);
  4765. JIT_HELPER_END(LdThis);
  4766. }
  4767. Var JavascriptOperators::OP_GetThisNoFastPath(Var thisVar, int moduleID, ScriptContext* scriptContext)
  4768. {
  4769. JIT_HELPER_REENTRANT_HEADER(LdThisNoFastPath);
  4770. TypeId typeId = JavascriptOperators::GetTypeId(thisVar);
  4771. if (JavascriptOperators::IsThisSelf(typeId))
  4772. {
  4773. Assert(typeId != TypeIds_GlobalObject || ((Js::GlobalObject*)thisVar)->ToThis() == thisVar);
  4774. Assert(typeId != TypeIds_ModuleRoot || JavascriptOperators::GetThisFromModuleRoot(thisVar) == thisVar);
  4775. return thisVar;
  4776. }
  4777. return JavascriptOperators::GetThisHelper(thisVar, typeId, moduleID, scriptContext);
  4778. JIT_HELPER_END(LdThisNoFastPath);
  4779. }
  4780. bool JavascriptOperators::IsThisSelf(TypeId typeId)
  4781. {
  4782. return (JavascriptOperators::IsObjectType(typeId) && ! JavascriptOperators::IsSpecialObjectType(typeId));
  4783. }
  4784. Var JavascriptOperators::GetThisHelper(Var thisVar, TypeId typeId, int moduleID, ScriptContextInfo *scriptContext)
  4785. {
  4786. if (! JavascriptOperators::IsObjectType(typeId) && ! JavascriptOperators::IsUndefinedOrNullType(typeId))
  4787. {
  4788. #if ENABLE_NATIVE_CODEGEN
  4789. Assert(!JITManager::GetJITManager()->IsJITServer());
  4790. #endif
  4791. #if !FLOATVAR
  4792. // We allowed stack number to be used as the "this" for getter and setter activation of
  4793. // n.x and n[prop], where n is the Javascript Number
  4794. return JavascriptOperators::ToObject(
  4795. JavascriptNumber::BoxStackNumber(thisVar, (ScriptContext*)scriptContext), (ScriptContext*)scriptContext);
  4796. #else
  4797. return JavascriptOperators::ToObject(thisVar, (ScriptContext*)scriptContext);
  4798. #endif
  4799. }
  4800. else
  4801. {
  4802. TryLoadRoot(thisVar, typeId, moduleID, scriptContext);
  4803. return thisVar;
  4804. }
  4805. }
  4806. Var JavascriptOperators::OP_StrictGetThis(Var thisVar, ScriptContext* scriptContext)
  4807. {
  4808. JIT_HELPER_NOT_REENTRANT_HEADER(StrictLdThis, reentrancylock, scriptContext->GetThreadContext());
  4809. TypeId typeId = JavascriptOperators::GetTypeId(thisVar);
  4810. if (typeId == TypeIds_ActivationObject)
  4811. {
  4812. return scriptContext->GetLibrary()->GetUndefined();
  4813. }
  4814. return thisVar;
  4815. JIT_HELPER_END(StrictLdThis);
  4816. }
  4817. BOOL JavascriptOperators::GetRemoteTypeId(Var aValue, __out TypeId* typeId)
  4818. {
  4819. *typeId = TypeIds_Limit;
  4820. if (GetTypeId(aValue) != TypeIds_HostDispatch)
  4821. {
  4822. return FALSE;
  4823. }
  4824. return RecyclableObject::FromVar(aValue)->GetRemoteTypeId(typeId);
  4825. }
  4826. BOOL JavascriptOperators::IsJsNativeType(TypeId type)
  4827. {
  4828. switch(type)
  4829. {
  4830. case TypeIds_Object:
  4831. case TypeIds_Function:
  4832. case TypeIds_Array:
  4833. case TypeIds_NativeIntArray:
  4834. #if ENABLE_COPYONACCESS_ARRAY
  4835. case TypeIds_CopyOnAccessNativeIntArray:
  4836. #endif
  4837. case TypeIds_NativeFloatArray:
  4838. case TypeIds_ES5Array:
  4839. case TypeIds_Date:
  4840. case TypeIds_WinRTDate:
  4841. case TypeIds_RegEx:
  4842. case TypeIds_Error:
  4843. case TypeIds_BooleanObject:
  4844. case TypeIds_NumberObject:
  4845. case TypeIds_StringObject:
  4846. case TypeIds_Symbol:
  4847. case TypeIds_SymbolObject:
  4848. //case TypeIds_GlobalObject:
  4849. //case TypeIds_ModuleRoot:
  4850. //case TypeIds_HostObject:
  4851. case TypeIds_Arguments:
  4852. case TypeIds_ActivationObject:
  4853. case TypeIds_Map:
  4854. case TypeIds_Set:
  4855. case TypeIds_WeakMap:
  4856. case TypeIds_WeakSet:
  4857. case TypeIds_ArrayIterator:
  4858. case TypeIds_MapIterator:
  4859. case TypeIds_SetIterator:
  4860. case TypeIds_StringIterator:
  4861. case TypeIds_Generator:
  4862. case TypeIds_Promise:
  4863. case TypeIds_Proxy:
  4864. return true;
  4865. default:
  4866. return false;
  4867. }
  4868. }
  4869. BOOL JavascriptOperators::IsJsNativeObject(Var instance)
  4870. {
  4871. return IsJsNativeType(GetTypeId(instance));
  4872. }
  4873. BOOL JavascriptOperators::IsJsNativeObject(_In_ RecyclableObject* instance)
  4874. {
  4875. return IsJsNativeType(GetTypeId(instance));
  4876. }
  4877. bool JavascriptOperators::CanShortcutOnUnknownPropertyName(RecyclableObject *instance)
  4878. {
  4879. if (!CanShortcutInstanceOnUnknownPropertyName(instance))
  4880. {
  4881. return false;
  4882. }
  4883. return CanShortcutPrototypeChainOnUnknownPropertyName(instance->GetPrototype());
  4884. }
  4885. bool JavascriptOperators::CanShortcutInstanceOnUnknownPropertyName(RecyclableObject *instance)
  4886. {
  4887. if (PHASE_OFF1(Js::OptUnknownElementNamePhase))
  4888. {
  4889. return false;
  4890. }
  4891. TypeId typeId = instance->GetTypeId();
  4892. if (typeId == TypeIds_Proxy || typeId == TypeIds_HostDispatch)
  4893. {
  4894. return false;
  4895. }
  4896. if (DynamicType::Is(typeId) &&
  4897. static_cast<DynamicObject*>(instance)->GetTypeHandler()->IsStringTypeHandler())
  4898. {
  4899. return false;
  4900. }
  4901. if (instance->IsExternal())
  4902. {
  4903. return false;
  4904. }
  4905. if (!(instance->HasDeferredTypeHandler()))
  4906. {
  4907. JavascriptFunction * function = JavascriptOperators::TryFromVar<JavascriptFunction>(instance);
  4908. return function && function->IsExternalFunction();
  4909. }
  4910. return false;
  4911. }
  4912. bool JavascriptOperators::CanShortcutPrototypeChainOnUnknownPropertyName(RecyclableObject *prototype)
  4913. {
  4914. Assert(prototype);
  4915. for (; !JavascriptOperators::IsNull(prototype); prototype = prototype->GetPrototype())
  4916. {
  4917. if (!CanShortcutInstanceOnUnknownPropertyName(prototype))
  4918. {
  4919. return false;
  4920. }
  4921. }
  4922. return true;
  4923. }
  4924. RecyclableObject* JavascriptOperators::GetPrototype(RecyclableObject* instance)
  4925. {
  4926. if (JavascriptOperators::GetTypeId(instance) == TypeIds_Null)
  4927. {
  4928. return instance;
  4929. }
  4930. return instance->GetPrototype();
  4931. }
  4932. RecyclableObject* JavascriptOperators::OP_GetPrototype(Var instance, ScriptContext* scriptContext)
  4933. {
  4934. if (TaggedNumber::Is(instance))
  4935. {
  4936. return scriptContext->GetLibrary()->GetNumberPrototype();
  4937. }
  4938. else
  4939. {
  4940. RecyclableObject* object = RecyclableObject::FromVar(instance);
  4941. if (JavascriptOperators::IsNull(object))
  4942. {
  4943. return object;
  4944. }
  4945. return JavascriptOperators::GetPrototype(object);
  4946. }
  4947. }
  4948. BOOL JavascriptOperators::OP_BrFncEqApply(Var instance, ScriptContext *scriptContext)
  4949. {
  4950. JIT_HELPER_NOT_REENTRANT_HEADER(Op_OP_BrFncEqApply, reentrancylock, scriptContext->GetThreadContext());
  4951. // JavascriptFunction && !HostDispatch
  4952. if (JavascriptOperators::GetTypeId(instance) == TypeIds_Function)
  4953. {
  4954. FunctionProxy *bod= ((JavascriptFunction*)instance)->GetFunctionProxy();
  4955. if (bod != nullptr)
  4956. {
  4957. return bod->GetDirectEntryPoint(bod->GetDefaultEntryPointInfo()) == &Js::JavascriptFunction::EntryApply;
  4958. }
  4959. else
  4960. {
  4961. FunctionInfo* info = ((JavascriptFunction *)instance)->GetFunctionInfo();
  4962. if (info != nullptr)
  4963. {
  4964. return &Js::JavascriptFunction::EntryApply == info->GetOriginalEntryPoint();
  4965. }
  4966. else
  4967. {
  4968. return false;
  4969. }
  4970. }
  4971. }
  4972. return false;
  4973. JIT_HELPER_END(Op_OP_BrFncEqApply);
  4974. }
  4975. BOOL JavascriptOperators::OP_BrFncNeqApply(Var instance, ScriptContext *scriptContext)
  4976. {
  4977. JIT_HELPER_NOT_REENTRANT_HEADER(Op_OP_BrFncNeqApply, reentrancylock, scriptContext->GetThreadContext());
  4978. // JavascriptFunction and !HostDispatch
  4979. if (JavascriptOperators::GetTypeId(instance) == TypeIds_Function)
  4980. {
  4981. FunctionProxy *bod = ((JavascriptFunction *)instance)->GetFunctionProxy();
  4982. if (bod != nullptr)
  4983. {
  4984. return bod->GetDirectEntryPoint(bod->GetDefaultEntryPointInfo()) != &Js::JavascriptFunction::EntryApply;
  4985. }
  4986. else
  4987. {
  4988. FunctionInfo* info = ((JavascriptFunction *)instance)->GetFunctionInfo();
  4989. if (info != nullptr)
  4990. {
  4991. return &Js::JavascriptFunction::EntryApply != info->GetOriginalEntryPoint();
  4992. }
  4993. else
  4994. {
  4995. return true;
  4996. }
  4997. }
  4998. }
  4999. return true;
  5000. JIT_HELPER_END(Op_OP_BrFncNeqApply);
  5001. }
  5002. BOOL JavascriptOperators::OP_BrHasSideEffects(int se, ScriptContext* scriptContext)
  5003. {
  5004. return (scriptContext->optimizationOverrides.GetSideEffects() & se) != SideEffects_None;
  5005. }
  5006. BOOL JavascriptOperators::OP_BrNotHasSideEffects(int se, ScriptContext* scriptContext)
  5007. {
  5008. return (scriptContext->optimizationOverrides.GetSideEffects() & se) == SideEffects_None;
  5009. }
  5010. // returns NULL if there is no more elements to enumerate.
  5011. Var JavascriptOperators::OP_BrOnEmpty(ForInObjectEnumerator * aEnumerator)
  5012. {
  5013. JIT_HELPER_REENTRANT_HEADER(Op_OP_BrOnEmpty);
  5014. PropertyId id;
  5015. return aEnumerator->MoveAndGetNext(id);
  5016. JIT_HELPER_END(Op_OP_BrOnEmpty);
  5017. }
  5018. void JavascriptOperators::OP_InitForInEnumerator(Var enumerable, ForInObjectEnumerator * enumerator, ScriptContext* scriptContext, EnumeratorCache * forInCache)
  5019. {
  5020. JIT_HELPER_REENTRANT_HEADER(Op_OP_InitForInEnumerator);
  5021. RecyclableObject* enumerableObject;
  5022. #if ENABLE_COPYONACCESS_ARRAY
  5023. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(enumerable);
  5024. #endif
  5025. if (!GetPropertyObject(enumerable, scriptContext, &enumerableObject))
  5026. {
  5027. enumerableObject = nullptr;
  5028. }
  5029. enumerator->Initialize(enumerableObject, scriptContext, false, forInCache);
  5030. JIT_HELPER_END(Op_OP_InitForInEnumerator);
  5031. }
  5032. Js::Var JavascriptOperators::OP_CmEq_A(Var a, Var b, ScriptContext* scriptContext)
  5033. {
  5034. JIT_HELPER_REENTRANT_HEADER(OP_CmEq_A);
  5035. return JavascriptBoolean::ToVar(JavascriptOperators::Equal(a, b, scriptContext), scriptContext);
  5036. JIT_HELPER_END(OP_CmEq_A);
  5037. }
  5038. Var JavascriptOperators::OP_CmNeq_A(Var a, Var b, ScriptContext* scriptContext)
  5039. {
  5040. JIT_HELPER_REENTRANT_HEADER(OP_CmNeq_A);
  5041. return JavascriptBoolean::ToVar(JavascriptOperators::NotEqual(a,b,scriptContext), scriptContext);
  5042. JIT_HELPER_END(OP_CmNeq_A);
  5043. }
  5044. Var JavascriptOperators::OP_CmSrEq_A(Var a, Var b, ScriptContext* scriptContext)
  5045. {
  5046. JIT_HELPER_REENTRANT_HEADER(OP_CmSrEq_A);
  5047. return JavascriptBoolean::ToVar(JavascriptOperators::StrictEqual(a, b, scriptContext), scriptContext);
  5048. JIT_HELPER_END(OP_CmSrEq_A);
  5049. }
  5050. Var JavascriptOperators::OP_CmSrEq_String(Var a, JavascriptString* b, ScriptContext *scriptContext)
  5051. {
  5052. JIT_HELPER_REENTRANT_HEADER(OP_CmSrEq_String);
  5053. return JavascriptBoolean::ToVar(JavascriptOperators::StrictEqualString(a, b), scriptContext);
  5054. JIT_HELPER_END(OP_CmSrEq_String);
  5055. }
  5056. Var JavascriptOperators::OP_CmSrEq_EmptyString(Var a, ScriptContext *scriptContext)
  5057. {
  5058. JIT_HELPER_REENTRANT_HEADER(OP_CmSrEq_EmptyString);
  5059. return JavascriptBoolean::ToVar(JavascriptOperators::StrictEqualEmptyString(a), scriptContext);
  5060. JIT_HELPER_END(OP_CmSrEq_EmptyString);
  5061. }
  5062. Var JavascriptOperators::OP_CmSrNeq_A(Var a, Var b, ScriptContext* scriptContext)
  5063. {
  5064. JIT_HELPER_REENTRANT_HEADER(OP_CmSrNeq_A);
  5065. return JavascriptBoolean::ToVar(JavascriptOperators::NotStrictEqual(a, b, scriptContext), scriptContext);
  5066. JIT_HELPER_END(OP_CmSrNeq_A);
  5067. }
  5068. Var JavascriptOperators::OP_CmLt_A(Var a, Var b, ScriptContext* scriptContext)
  5069. {
  5070. JIT_HELPER_REENTRANT_HEADER(OP_CmLt_A);
  5071. return JavascriptBoolean::ToVar(JavascriptOperators::Less(a, b, scriptContext), scriptContext);
  5072. JIT_HELPER_END(OP_CmLt_A);
  5073. }
  5074. Var JavascriptOperators::OP_CmLe_A(Var a, Var b, ScriptContext* scriptContext)
  5075. {
  5076. JIT_HELPER_REENTRANT_HEADER(OP_CmLe_A);
  5077. return JavascriptBoolean::ToVar(JavascriptOperators::LessEqual(a, b, scriptContext), scriptContext);
  5078. JIT_HELPER_END(OP_CmLe_A);
  5079. }
  5080. Var JavascriptOperators::OP_CmGt_A(Var a, Var b, ScriptContext* scriptContext)
  5081. {
  5082. JIT_HELPER_REENTRANT_HEADER(OP_CmGt_A);
  5083. return JavascriptBoolean::ToVar(JavascriptOperators::Greater(a, b, scriptContext), scriptContext);
  5084. JIT_HELPER_END(OP_CmGt_A);
  5085. }
  5086. Var JavascriptOperators::OP_CmGe_A(Var a, Var b, ScriptContext* scriptContext)
  5087. {
  5088. JIT_HELPER_REENTRANT_HEADER(OP_CmGe_A);
  5089. return JavascriptBoolean::ToVar(JavascriptOperators::GreaterEqual(a, b, scriptContext), scriptContext);
  5090. JIT_HELPER_END(OP_CmGe_A);
  5091. }
  5092. DetachedStateBase* JavascriptOperators::DetachVarAndGetState(Var var, bool queueForDelayFree/* = true*/)
  5093. {
  5094. switch (GetTypeId(var))
  5095. {
  5096. case TypeIds_ArrayBuffer:
  5097. return Js::ArrayBuffer::FromVar(var)->DetachAndGetState(queueForDelayFree);
  5098. default:
  5099. if (!Js::RecyclableObject::FromVar(var)->IsExternal())
  5100. {
  5101. AssertMsg(false, "We should explicitly have a case statement for each non-external object that can be detached.");
  5102. }
  5103. return nullptr;
  5104. }
  5105. }
  5106. bool JavascriptOperators::IsObjectDetached(Var var)
  5107. {
  5108. switch (GetTypeId(var))
  5109. {
  5110. case TypeIds_ArrayBuffer:
  5111. return Js::ArrayBuffer::FromVar(var)->IsDetached();
  5112. default:
  5113. return false;
  5114. }
  5115. }
  5116. Var JavascriptOperators::NewVarFromDetachedState(DetachedStateBase* state, JavascriptLibrary *library)
  5117. {
  5118. AssertOrFailFastMsg(state->GetTypeId() == TypeIds_ArrayBuffer, "We should only re-activate detached ArrayBuffer");
  5119. return Js::ArrayBuffer::NewFromDetachedState(state, library);
  5120. }
  5121. DynamicType *
  5122. JavascriptOperators::EnsureObjectLiteralType(ScriptContext* scriptContext, const Js::PropertyIdArray *propIds, Field(DynamicType*)* literalType)
  5123. {
  5124. JIT_HELPER_NOT_REENTRANT_HEADER(EnsureObjectLiteralType, reentrancylock, scriptContext->GetThreadContext());
  5125. DynamicType * newType = *literalType;
  5126. if (newType != nullptr)
  5127. {
  5128. if (!newType->GetIsShared())
  5129. {
  5130. newType->ShareType();
  5131. }
  5132. }
  5133. else
  5134. {
  5135. DynamicType* objectType =
  5136. FunctionBody::DoObjectHeaderInliningForObjectLiteral(propIds)
  5137. ? scriptContext->GetLibrary()->GetObjectHeaderInlinedLiteralType((uint16)propIds->count)
  5138. : scriptContext->GetLibrary()->GetObjectLiteralType(
  5139. static_cast<PropertyIndex>(
  5140. min(propIds->count, static_cast<uint32>(MaxPreInitializedObjectTypeInlineSlotCount))));
  5141. newType = PathTypeHandlerBase::CreateTypeForNewScObject(scriptContext, objectType, propIds, false);
  5142. *literalType = newType;
  5143. }
  5144. Assert(scriptContext);
  5145. Assert(GetLiteralInlineSlotCapacity(propIds) == newType->GetTypeHandler()->GetInlineSlotCapacity());
  5146. Assert(newType->GetTypeHandler()->GetSlotCapacity() >= 0);
  5147. Assert(GetLiteralSlotCapacity(propIds) == (uint)newType->GetTypeHandler()->GetSlotCapacity());
  5148. return newType;
  5149. JIT_HELPER_END(EnsureObjectLiteralType);
  5150. }
  5151. Var JavascriptOperators::NewScObjectLiteral(ScriptContext* scriptContext, const Js::PropertyIdArray *propIds, Field(DynamicType*)* literalType)
  5152. {
  5153. Assert(propIds->count != 0);
  5154. Assert(!propIds->hadDuplicates); // duplicates are removed by parser
  5155. #ifdef PROFILE_OBJECT_LITERALS
  5156. // Empty objects not counted in the object literal counts
  5157. scriptContext->objectLiteralInstanceCount++;
  5158. if (propIds->count > scriptContext->objectLiteralMaxLength)
  5159. {
  5160. scriptContext->objectLiteralMaxLength = propIds->count;
  5161. }
  5162. #endif
  5163. DynamicType* newType = EnsureObjectLiteralType(scriptContext, propIds, literalType);
  5164. DynamicObject* instance = DynamicObject::New(scriptContext->GetRecycler(), newType);
  5165. if (!newType->GetIsShared())
  5166. {
  5167. #if ENABLE_FIXED_FIELDS
  5168. newType->GetTypeHandler()->SetSingletonInstanceIfNeeded(instance);
  5169. #endif
  5170. }
  5171. #ifdef PROFILE_OBJECT_LITERALS
  5172. else
  5173. {
  5174. scriptContext->objectLiteralCacheCount++;
  5175. }
  5176. #endif
  5177. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(instance));
  5178. // can't auto-proxy here as object literal is not exactly "new" object and cannot be intercepted as proxy.
  5179. return instance;
  5180. }
  5181. uint JavascriptOperators::GetLiteralSlotCapacity(Js::PropertyIdArray const * propIds)
  5182. {
  5183. const uint inlineSlotCapacity = GetLiteralInlineSlotCapacity(propIds);
  5184. return DynamicTypeHandler::RoundUpSlotCapacity(propIds->count, static_cast<PropertyIndex>(inlineSlotCapacity));
  5185. }
  5186. uint JavascriptOperators::GetLiteralInlineSlotCapacity(
  5187. Js::PropertyIdArray const * propIds)
  5188. {
  5189. if (propIds->hadDuplicates)
  5190. {
  5191. return 0;
  5192. }
  5193. return
  5194. FunctionBody::DoObjectHeaderInliningForObjectLiteral(propIds)
  5195. ? DynamicTypeHandler::RoundUpObjectHeaderInlinedInlineSlotCapacity(static_cast<PropertyIndex>(propIds->count))
  5196. : DynamicTypeHandler::RoundUpInlineSlotCapacity(
  5197. static_cast<PropertyIndex>(
  5198. min(propIds->count, static_cast<uint32>(MaxPreInitializedObjectTypeInlineSlotCount))));
  5199. }
  5200. Var JavascriptOperators::OP_InitCachedScope(Var varFunc, const Js::PropertyIdArray *propIds, Field(DynamicType*)* literalType, bool formalsAreLetDecls, ScriptContext *scriptContext)
  5201. {
  5202. JIT_HELPER_NOT_REENTRANT_HEADER(OP_InitCachedScope, reentrancylock, scriptContext->GetThreadContext());
  5203. bool isGAFunction = JavascriptFunction::Is(varFunc);
  5204. Assert(isGAFunction);
  5205. if (isGAFunction)
  5206. {
  5207. JavascriptFunction *function = JavascriptFunction::FromVar(varFunc);
  5208. isGAFunction = JavascriptGeneratorFunction::Test(function) || JavascriptAsyncFunction::Test(function);
  5209. }
  5210. ScriptFunction *func = isGAFunction ?
  5211. JavascriptGeneratorFunction::FromVar(varFunc)->GetGeneratorVirtualScriptFunction() :
  5212. ScriptFunction::FromVar(varFunc);
  5213. #ifdef PROFILE_OBJECT_LITERALS
  5214. // Empty objects not counted in the object literal counts
  5215. scriptContext->objectLiteralInstanceCount++;
  5216. if (propIds->count > scriptContext->objectLiteralMaxLength)
  5217. {
  5218. scriptContext->objectLiteralMaxLength = propIds->count;
  5219. }
  5220. #endif
  5221. PropertyId cachedFuncCount = ActivationObjectEx::GetCachedFuncCount(propIds);
  5222. PropertyId firstFuncSlot = ActivationObjectEx::GetFirstFuncSlot(propIds);
  5223. PropertyId firstVarSlot = ActivationObjectEx::GetFirstVarSlot(propIds);
  5224. PropertyId lastFuncSlot = Constants::NoProperty;
  5225. if (firstFuncSlot != Constants::NoProperty)
  5226. {
  5227. if (firstVarSlot == Constants::NoProperty || firstVarSlot < firstFuncSlot)
  5228. {
  5229. lastFuncSlot = propIds->count - 1;
  5230. }
  5231. else
  5232. {
  5233. lastFuncSlot = firstVarSlot - 1;
  5234. }
  5235. }
  5236. DynamicType *type = *literalType;
  5237. if (type != nullptr)
  5238. {
  5239. #ifdef PROFILE_OBJECT_LITERALS
  5240. scriptContext->objectLiteralCacheCount++;
  5241. #endif
  5242. }
  5243. else
  5244. {
  5245. type = scriptContext->GetLibrary()->GetActivationObjectType();
  5246. if (formalsAreLetDecls)
  5247. {
  5248. uint formalsSlotLimit = (firstFuncSlot != Constants::NoProperty) ? (uint)firstFuncSlot :
  5249. (firstVarSlot != Constants::NoProperty) ? (uint)firstVarSlot :
  5250. propIds->count;
  5251. if (func->GetFunctionBody()->HasReferenceableBuiltInArguments())
  5252. {
  5253. type = PathTypeHandlerBase::CreateNewScopeObject<true>(scriptContext, type, propIds, PropertyLet, formalsSlotLimit);
  5254. }
  5255. else
  5256. {
  5257. type = PathTypeHandlerBase::CreateNewScopeObject<false>(scriptContext, type, propIds, PropertyLet, formalsSlotLimit);
  5258. }
  5259. }
  5260. else
  5261. {
  5262. type = PathTypeHandlerBase::CreateNewScopeObject<false>(scriptContext, type, propIds);
  5263. }
  5264. *literalType = type;
  5265. }
  5266. Var undef = scriptContext->GetLibrary()->GetUndefined();
  5267. ActivationObjectEx *scopeObjEx = func->GetCachedScope();
  5268. if (scopeObjEx && scopeObjEx->IsCommitted())
  5269. {
  5270. scopeObjEx->ReplaceType(type);
  5271. scopeObjEx->SetCommit(false);
  5272. #if DBG
  5273. for (uint i = firstVarSlot; i < propIds->count; i++)
  5274. {
  5275. AssertMsg(scopeObjEx->GetSlot(i) == undef, "Var attached to cached scope");
  5276. }
  5277. #endif
  5278. }
  5279. else
  5280. {
  5281. ActivationObjectEx *tmp = RecyclerNewPlus(scriptContext->GetRecycler(), (cachedFuncCount == 0 ? 0 : cachedFuncCount - 1) * sizeof(FuncCacheEntry), ActivationObjectEx, type, func, cachedFuncCount, firstFuncSlot, lastFuncSlot);
  5282. if (!scopeObjEx)
  5283. {
  5284. func->SetCachedScope(tmp);
  5285. }
  5286. scopeObjEx = tmp;
  5287. for (uint i = firstVarSlot; i < propIds->count; i++)
  5288. {
  5289. scopeObjEx->SetSlot(SetSlotArguments(propIds->elements[i], i, undef));
  5290. }
  5291. }
  5292. return scopeObjEx;
  5293. JIT_HELPER_END(OP_InitCachedScope);
  5294. }
  5295. void JavascriptOperators::OP_InvalidateCachedScope(void* varEnv, int32 envIndex)
  5296. {
  5297. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(OP_InvalidateCachedScope);
  5298. FrameDisplay *disp = (FrameDisplay*)varEnv;
  5299. Var item = disp->GetItem(envIndex);
  5300. if (item != nullptr)
  5301. {
  5302. Assert(ActivationObjectEx::Is(item));
  5303. RecyclableObject *objScope = RecyclableObject::FromVar(item);
  5304. objScope->InvalidateCachedScope();
  5305. }
  5306. JIT_HELPER_END(OP_InvalidateCachedScope);
  5307. }
  5308. void JavascriptOperators::OP_InitCachedFuncs(Var varScope, FrameDisplay *pDisplay, const FuncInfoArray *info, ScriptContext *scriptContext)
  5309. {
  5310. JIT_HELPER_NOT_REENTRANT_HEADER(OP_InitCachedFuncs, reentrancylock, scriptContext->GetThreadContext());
  5311. ActivationObjectEx *scopeObj = ActivationObjectEx::FromVar(varScope);
  5312. Assert(scopeObj->GetTypeHandler()->GetInlineSlotCapacity() == 0);
  5313. uint funcCount = info->count;
  5314. if (funcCount == 0)
  5315. {
  5316. // Degenerate case: no nested funcs at all
  5317. return;
  5318. }
  5319. if (scopeObj->HasCachedFuncs())
  5320. {
  5321. for (uint i = 0; i < funcCount; i++)
  5322. {
  5323. const FuncCacheEntry *entry = scopeObj->GetFuncCacheEntry(i);
  5324. ScriptFunction *func = entry->func;
  5325. FunctionProxy * proxy = func->GetFunctionProxy();
  5326. // Reset the function's type to the default type with no properties
  5327. // Use the cached type on the function proxy rather than the type in the func cache entry
  5328. // CONSIDER: Stop caching the function types in the scope object
  5329. func->ReplaceType(proxy->EnsureDeferredPrototypeType());
  5330. func->ResetConstructorCacheToDefault();
  5331. uint scopeSlot = info->elements[i].scopeSlot;
  5332. if (scopeSlot != Constants::NoProperty)
  5333. {
  5334. // CONSIDER: Store property IDs in FuncInfoArray in debug builds so we can properly assert in SetAuxSlot
  5335. scopeObj->SetAuxSlot(SetSlotArguments(Constants::NoProperty, scopeSlot, entry->func));
  5336. }
  5337. }
  5338. return;
  5339. }
  5340. // No cached functions, so create them and cache them.
  5341. JavascriptFunction *funcParent = scopeObj->GetParentFunc();
  5342. for (uint i = 0; i < funcCount; i++)
  5343. {
  5344. const FuncInfoEntry *entry = &info->elements[i];
  5345. uint nestedIndex = entry->nestedIndex;
  5346. uint scopeSlot = entry->scopeSlot;
  5347. FunctionProxy * proxy = funcParent->GetFunctionBody()->GetNestedFunctionProxy(nestedIndex);
  5348. ScriptFunction *func = scriptContext->GetLibrary()->CreateScriptFunction(proxy);
  5349. func->SetEnvironment(pDisplay);
  5350. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_FUNCTION(func, EtwTrace::GetFunctionId(proxy)));
  5351. scopeObj->SetCachedFunc(i, func);
  5352. if (scopeSlot != Constants::NoProperty)
  5353. {
  5354. // CONSIDER: Store property IDs in FuncInfoArray in debug builds so we can properly assert in SetAuxSlot
  5355. scopeObj->SetAuxSlot(SetSlotArguments(Constants::NoProperty, scopeSlot, func));
  5356. }
  5357. }
  5358. JIT_HELPER_END(OP_InitCachedFuncs);
  5359. }
  5360. Var JavascriptOperators::AddVarsToArraySegment(SparseArraySegment<Var> * segment, const Js::VarArray *vars)
  5361. {
  5362. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ArraySegmentVars);
  5363. uint32 count = vars->count;
  5364. Assert(segment->left == 0);
  5365. Assert(count <= segment->size);
  5366. if(count > segment->length)
  5367. {
  5368. segment->length = count;
  5369. segment->CheckLengthvsSize();
  5370. }
  5371. CopyArray(segment->elements, segment->length, vars->elements, count);
  5372. return segment;
  5373. JIT_HELPER_END(ArraySegmentVars);
  5374. }
  5375. void JavascriptOperators::AddIntsToArraySegment(SparseArraySegment<int32> * segment, const Js::AuxArray<int32> *ints)
  5376. {
  5377. uint32 count = ints->count;
  5378. Assert(segment->left == 0);
  5379. Assert(count <= segment->size);
  5380. if(count > segment->length)
  5381. {
  5382. segment->length = count;
  5383. segment->CheckLengthvsSize();
  5384. }
  5385. js_memcpy_s(segment->elements, sizeof(int32) * segment->length, ints->elements, sizeof(int32) * count);
  5386. }
  5387. void JavascriptOperators::AddFloatsToArraySegment(SparseArraySegment<double> * segment, const Js::AuxArray<double> *doubles)
  5388. {
  5389. uint32 count = doubles->count;
  5390. Assert(segment->left == 0);
  5391. Assert(count <= segment->size);
  5392. if(count > segment->length)
  5393. {
  5394. segment->length = count;
  5395. segment->CheckLengthvsSize();
  5396. }
  5397. js_memcpy_s(segment->elements, sizeof(double) * segment->length, doubles->elements, sizeof(double) * count);
  5398. }
  5399. RecyclableObject * JavascriptOperators::GetPrototypeObject(RecyclableObject * constructorFunction, ScriptContext * scriptContext)
  5400. {
  5401. Var prototypeProperty = JavascriptOperators::GetProperty(constructorFunction, PropertyIds::prototype, scriptContext);
  5402. RecyclableObject* prototypeObject;
  5403. PrototypeObject(prototypeProperty, constructorFunction, scriptContext, &prototypeObject);
  5404. return prototypeObject;
  5405. }
  5406. RecyclableObject * JavascriptOperators::GetPrototypeObjectForConstructorCache(RecyclableObject * constructor, ScriptContext* requestContext, bool& canBeCached)
  5407. {
  5408. PropertyValueInfo info;
  5409. Var prototypeValue;
  5410. RecyclableObject* prototypeObject;
  5411. canBeCached = false;
  5412. // Do a local property lookup. Since a function's prototype property is a non-configurable data property, we don't need to worry
  5413. // about the prototype being an accessor property, whose getter returns different values every time it's called.
  5414. if (constructor->GetProperty(constructor, PropertyIds::prototype, &prototypeValue, &info, requestContext))
  5415. {
  5416. if (!JavascriptOperators::PrototypeObject(prototypeValue, constructor, requestContext, &prototypeObject))
  5417. {
  5418. // The value returned by the property lookup is not a valid prototype object, default to object prototype.
  5419. Assert(prototypeObject == constructor->GetLibrary()->GetObjectPrototype());
  5420. }
  5421. // For these scenarios, we do not want to populate the cache.
  5422. if (constructor->GetScriptContext() != requestContext || info.GetInstance() != constructor)
  5423. {
  5424. return prototypeObject;
  5425. }
  5426. }
  5427. else
  5428. {
  5429. // It's ok to cache Object.prototype, because Object.prototype cannot be overwritten.
  5430. prototypeObject = constructor->GetLibrary()->GetObjectPrototype();
  5431. }
  5432. canBeCached = true;
  5433. return prototypeObject;
  5434. }
  5435. bool JavascriptOperators::PrototypeObject(Var prototypeProperty, RecyclableObject * constructorFunction, ScriptContext * scriptContext, RecyclableObject** prototypeObject)
  5436. {
  5437. TypeId prototypeType = JavascriptOperators::GetTypeId(prototypeProperty);
  5438. if (JavascriptOperators::IsObjectType(prototypeType))
  5439. {
  5440. *prototypeObject = RecyclableObject::FromVar(prototypeProperty);
  5441. return true;
  5442. }
  5443. *prototypeObject = constructorFunction->GetLibrary()->GetObjectPrototype();
  5444. return false;
  5445. }
  5446. FunctionInfo* JavascriptOperators::GetConstructorFunctionInfo(Var instance, ScriptContext * scriptContext)
  5447. {
  5448. TypeId typeId = JavascriptOperators::GetTypeId(instance);
  5449. if (typeId == TypeIds_Function)
  5450. {
  5451. JavascriptFunction * function = JavascriptFunction::UnsafeFromVar(instance);
  5452. return function->GetFunctionInfo();
  5453. }
  5454. if (typeId != TypeIds_HostDispatch && typeId != TypeIds_Proxy)
  5455. {
  5456. if (typeId == TypeIds_Null)
  5457. {
  5458. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject);
  5459. }
  5460. JavascriptError::ThrowTypeError(scriptContext, VBSERR_ActionNotSupported);
  5461. }
  5462. return nullptr;
  5463. }
  5464. Var JavascriptOperators::NewJavascriptObjectNoArg(ScriptContext* requestContext)
  5465. {
  5466. JIT_HELPER_NOT_REENTRANT_HEADER(NewJavascriptObjectNoArg, reentrancylock, requestContext->GetThreadContext());
  5467. DynamicObject * newObject = requestContext->GetLibrary()->CreateObject(true);
  5468. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(newObject));
  5469. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5470. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5471. {
  5472. newObject = DynamicObject::FromVar(JavascriptProxy::AutoProxyWrapper(newObject));
  5473. }
  5474. #endif
  5475. return newObject;
  5476. JIT_HELPER_END(NewJavascriptObjectNoArg);
  5477. }
  5478. Var JavascriptOperators::NewJavascriptArrayNoArg(ScriptContext* requestContext)
  5479. {
  5480. JIT_HELPER_NOT_REENTRANT_HEADER(NewJavascriptArrayNoArg, reentrancylock, requestContext->GetThreadContext());
  5481. JavascriptArray * newArray = requestContext->GetLibrary()->CreateArray();
  5482. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(newArray));
  5483. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5484. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5485. {
  5486. newArray = static_cast<JavascriptArray*>(JavascriptProxy::AutoProxyWrapper(newArray));
  5487. }
  5488. #endif
  5489. return newArray;
  5490. JIT_HELPER_END(NewJavascriptArrayNoArg);
  5491. }
  5492. Var JavascriptOperators::NewScObjectNoArgNoCtorFull(Var instance, ScriptContext* requestContext)
  5493. {
  5494. // This helper can be reentrant because although we don't call the Constructor, we might have to parse it if bytecode is missing
  5495. // In which case, we would leave script. When we leave script we DisposeObjects which can dispose of Edge objects that could
  5496. // have a javascript onDispose handler and call that handler.
  5497. JIT_HELPER_REENTRANT_HEADER(NewScObjectNoArgNoCtorFull);
  5498. return NewScObjectNoArgNoCtorCommon(instance, requestContext, true);
  5499. JIT_HELPER_END(NewScObjectNoArgNoCtorFull);
  5500. }
  5501. Var JavascriptOperators::NewScObjectNoArgNoCtor(Var instance, ScriptContext* requestContext)
  5502. {
  5503. // This helper can be reentrant because although we don't call the Constructor, we might have to parse it if bytecode is missing
  5504. // In which case, we would leave script. When we leave script we DisposeObjects which can dispose of Edge objects that could
  5505. // have a javascript onDispose handler and call that handler.
  5506. JIT_HELPER_REENTRANT_HEADER(NewScObjectNoArgNoCtor);
  5507. return NewScObjectNoArgNoCtorCommon(instance, requestContext, false);
  5508. JIT_HELPER_END(NewScObjectNoArgNoCtor);
  5509. }
  5510. Var JavascriptOperators::NewScObjectNoArgNoCtorCommon(Var instance, ScriptContext* requestContext, bool isBaseClassConstructorNewScObject)
  5511. {
  5512. RecyclableObject * object = RecyclableObject::FromVar(instance);
  5513. FunctionInfo* functionInfo = JavascriptOperators::GetConstructorFunctionInfo(instance, requestContext);
  5514. Assert(functionInfo != &JavascriptObject::EntryInfo::NewInstance); // built-ins are not inlined
  5515. Assert(functionInfo != &JavascriptArray::EntryInfo::NewInstance); // built-ins are not inlined
  5516. return functionInfo != nullptr ?
  5517. JavascriptOperators::NewScObjectCommon(object, functionInfo, requestContext, isBaseClassConstructorNewScObject) :
  5518. JavascriptOperators::NewScObjectHostDispatchOrProxy(object, requestContext);
  5519. }
  5520. Var JavascriptOperators::NewScObjectNoArg(Var instance, ScriptContext * requestContext)
  5521. {
  5522. JIT_HELPER_REENTRANT_HEADER(NewScObjectNoArg);
  5523. JavascriptProxy * proxy = JavascriptOperators::TryFromVar<JavascriptProxy>(instance);
  5524. if (proxy)
  5525. {
  5526. Arguments args(CallInfo(CallFlags_New, 1), &instance);
  5527. return requestContext->GetThreadContext()->ExecuteImplicitCall(proxy, Js::ImplicitCall_Accessor, [=]()->Js::Var
  5528. {
  5529. return proxy->ConstructorTrap(args, requestContext, 0);
  5530. });
  5531. }
  5532. FunctionInfo* functionInfo = JavascriptOperators::GetConstructorFunctionInfo(instance, requestContext);
  5533. RecyclableObject * object = RecyclableObject::FromVar(instance);
  5534. if (functionInfo == &JavascriptObject::EntryInfo::NewInstance)
  5535. {
  5536. // Fast path for new Object()
  5537. Assert((functionInfo->GetAttributes() & FunctionInfo::ErrorOnNew) == 0);
  5538. JavascriptLibrary* library = object->GetLibrary();
  5539. DynamicObject * newObject = library->CreateObject(true);
  5540. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(newObject));
  5541. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5542. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5543. {
  5544. newObject = DynamicObject::FromVar(JavascriptProxy::AutoProxyWrapper(newObject));
  5545. }
  5546. #endif
  5547. #if DBG
  5548. DynamicType* newObjectType = newObject->GetDynamicType();
  5549. Assert(newObjectType->GetIsShared());
  5550. JavascriptFunction* constructor = JavascriptFunction::FromVar(instance);
  5551. Assert(!constructor->GetConstructorCache()->NeedsUpdateAfterCtor());
  5552. #endif
  5553. ScriptContext * scriptContext = library->GetScriptContext();
  5554. if (scriptContext != requestContext)
  5555. {
  5556. CrossSite::MarshalDynamicObjectAndPrototype(requestContext, newObject);
  5557. }
  5558. return newObject;
  5559. }
  5560. else if (functionInfo == &JavascriptArray::EntryInfo::NewInstance)
  5561. {
  5562. Assert((functionInfo->GetAttributes() & FunctionInfo::ErrorOnNew) == 0);
  5563. JavascriptLibrary* library = object->GetLibrary();
  5564. JavascriptArray * newArray = library->CreateArray();
  5565. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(newArray));
  5566. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5567. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5568. {
  5569. newArray = static_cast<JavascriptArray*>(JavascriptProxy::AutoProxyWrapper(newArray));
  5570. }
  5571. #endif
  5572. #if DBG
  5573. DynamicType* newArrayType = newArray->GetDynamicType();
  5574. Assert(newArrayType->GetIsShared());
  5575. JavascriptFunction* constructor = JavascriptFunction::FromVar(instance);
  5576. Assert(!constructor->GetConstructorCache()->NeedsUpdateAfterCtor());
  5577. #endif
  5578. ScriptContext * scriptContext = library->GetScriptContext();
  5579. if (scriptContext != requestContext)
  5580. {
  5581. CrossSite::MarshalDynamicObjectAndPrototype(requestContext, newArray);
  5582. }
  5583. return newArray;
  5584. }
  5585. Var newObject = functionInfo != nullptr ?
  5586. JavascriptOperators::NewScObjectCommon(object, functionInfo, requestContext) :
  5587. JavascriptOperators::NewScObjectHostDispatchOrProxy(object, requestContext);
  5588. ThreadContext * threadContext = object->GetScriptContext()->GetThreadContext();
  5589. Var returnVar = threadContext->ExecuteImplicitCall(object, Js::ImplicitCall_Accessor, [=]()->Js::Var
  5590. {
  5591. return CALL_FUNCTION(threadContext, object, CallInfo(CallFlags_New, 1), newObject);
  5592. });
  5593. if (JavascriptOperators::IsObject(returnVar))
  5594. {
  5595. newObject = returnVar;
  5596. }
  5597. ConstructorCache * constructorCache = nullptr;
  5598. JavascriptFunction *function = JavascriptOperators::TryFromVar<JavascriptFunction>(instance);
  5599. if (function)
  5600. {
  5601. constructorCache = function->GetConstructorCache();
  5602. }
  5603. if (constructorCache != nullptr && constructorCache->NeedsUpdateAfterCtor())
  5604. {
  5605. JavascriptOperators::UpdateNewScObjectCache(object, newObject, requestContext);
  5606. }
  5607. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5608. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5609. {
  5610. DynamicObject* newDynamicObject = DynamicObject::FromVar(JavascriptProxy::AutoProxyWrapper(newObject));
  5611. // this might come from a different scriptcontext.
  5612. newObject = CrossSite::MarshalVar(requestContext, newDynamicObject, newDynamicObject->GetScriptContext());
  5613. }
  5614. #endif
  5615. return newObject;
  5616. JIT_HELPER_END(NewScObjectNoArg);
  5617. }
  5618. Var JavascriptOperators::NewScObjectNoCtorFull(Var instance, ScriptContext* requestContext)
  5619. {
  5620. // This helper can be reentrant because although we don't call the Constructor, we might have to parse it if bytecode is missing
  5621. // In which case, we would leave script. When we leave script we DisposeObjects which can dispose of Edge objects that could
  5622. // have a javascript onDispose handler and call that handler.
  5623. JIT_HELPER_REENTRANT_HEADER(NewScObjectNoCtorFull);
  5624. return NewScObjectNoCtorCommon(instance, requestContext, true);
  5625. JIT_HELPER_END(NewScObjectNoCtorFull);
  5626. }
  5627. Var JavascriptOperators::NewScObjectNoCtor(Var instance, ScriptContext * requestContext)
  5628. {
  5629. // This helper can be reentrant because although we don't call the Constructor, we might have to parse it if bytecode is missing
  5630. // In which case, we would leave script. When we leave script we DisposeObjects which can dispose of Edge objects that could
  5631. // have a javascript onDispose handler and call that handler.
  5632. JIT_HELPER_REENTRANT_HEADER(NewScObjectNoCtor);
  5633. // We can still call into NewScObjectNoCtor variations in JIT code for performance; however for proxy we don't
  5634. // really need the new object as the trap will handle the "this" pointer separately. pass back nullptr to ensure
  5635. // failure in invalid case.
  5636. return (JavascriptProxy::Is(instance)) ? nullptr : NewScObjectNoCtorCommon(instance, requestContext, false);
  5637. JIT_HELPER_END(NewScObjectNoCtor);
  5638. }
  5639. Var JavascriptOperators::NewScObjectNoCtorCommon(Var instance, ScriptContext* requestContext, bool isBaseClassConstructorNewScObject)
  5640. {
  5641. FunctionInfo* functionInfo = JavascriptOperators::GetConstructorFunctionInfo(instance, requestContext);
  5642. if (functionInfo)
  5643. {
  5644. return JavascriptOperators::NewScObjectCommon(RecyclableObject::UnsafeFromVar(instance), functionInfo, requestContext, isBaseClassConstructorNewScObject);
  5645. }
  5646. else
  5647. {
  5648. return JavascriptOperators::NewScObjectHostDispatchOrProxy(RecyclableObject::FromVar(instance), requestContext);
  5649. }
  5650. }
  5651. Var JavascriptOperators::NewScObjectHostDispatchOrProxy(RecyclableObject * function, ScriptContext * requestContext)
  5652. {
  5653. ScriptContext* functionScriptContext = function->GetScriptContext();
  5654. RecyclableObject * prototype = JavascriptOperators::GetPrototypeObject(function, functionScriptContext);
  5655. prototype = RecyclableObject::FromVar(CrossSite::MarshalVar(requestContext, prototype, functionScriptContext));
  5656. Var object = requestContext->GetLibrary()->CreateObject(prototype);
  5657. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(object));
  5658. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5659. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5660. {
  5661. object = DynamicObject::FromVar(JavascriptProxy::AutoProxyWrapper(object));
  5662. }
  5663. #endif
  5664. return object;
  5665. }
  5666. Var JavascriptOperators::NewScObjectCommon(RecyclableObject * function, FunctionInfo* functionInfo, ScriptContext * requestContext, bool isBaseClassConstructorNewScObject)
  5667. {
  5668. // CONSIDER: Allow for the cache to be repopulated if the type got collected, and a new one got populated with
  5669. // the same number of inlined slots. This requires that the JIT-ed code actually load the type from the cache
  5670. // (instead of hard-coding it), but it can (and must) keep the hard-coded number of inline slots.
  5671. // CONSIDER: Consider also not pinning the type in the cache. This can be done by using a registration based
  5672. // weak reference (we need to control the memory address), which we don't yet have, or by allocating the cache from
  5673. // the inline cache arena to allow it to be zeroed, but retain a recycler-allocated portion to hold on to the size of
  5674. // inlined slots.
  5675. JavascriptFunction* constructor = JavascriptFunction::UnsafeFromVar(function);
  5676. if (functionInfo->IsClassConstructor() && !isBaseClassConstructorNewScObject)
  5677. {
  5678. // If we are calling new on a class constructor, the contract is that we pass new.target as the 'this' argument.
  5679. // function is the constructor on which we called new - which is new.target.
  5680. // If we are trying to construct the object for a base class constructor as part of a super call, we should not
  5681. // store new.target in the 'this' argument.
  5682. return function;
  5683. }
  5684. ConstructorCache* constructorCache = constructor->GetConstructorCache();
  5685. AssertMsg(constructorCache->GetScriptContext() == nullptr || constructorCache->GetScriptContext() == constructor->GetScriptContext(),
  5686. "Why did we populate a constructor cache with a mismatched script context?");
  5687. Assert(constructorCache != nullptr);
  5688. DynamicType* type = constructorCache->GetGuardValueAsType();
  5689. if (type != nullptr && constructorCache->GetScriptContext() == requestContext)
  5690. {
  5691. #if DBG
  5692. bool cachedProtoCanBeCached;
  5693. Assert(type->GetPrototype() == JavascriptOperators::GetPrototypeObjectForConstructorCache(constructor, requestContext, cachedProtoCanBeCached));
  5694. Assert(cachedProtoCanBeCached);
  5695. Assert(type->GetIsShared());
  5696. #endif
  5697. #if DBG_DUMP
  5698. TraceUseConstructorCache(constructorCache, constructor, true);
  5699. #endif
  5700. Var object = DynamicObject::New(requestContext->GetRecycler(), type);
  5701. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(object));
  5702. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5703. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5704. {
  5705. object = DynamicObject::FromVar(JavascriptProxy::AutoProxyWrapper(object));
  5706. }
  5707. #endif
  5708. return object;
  5709. }
  5710. if (constructorCache->SkipDefaultNewObject())
  5711. {
  5712. Assert(!constructorCache->NeedsUpdateAfterCtor());
  5713. #if DBG_DUMP
  5714. TraceUseConstructorCache(constructorCache, constructor, true);
  5715. #endif
  5716. if (isBaseClassConstructorNewScObject)
  5717. {
  5718. return JavascriptOperators::CreateFromConstructor(function, requestContext);
  5719. }
  5720. return nullptr;
  5721. }
  5722. #if DBG_DUMP
  5723. TraceUseConstructorCache(constructorCache, constructor, false);
  5724. #endif
  5725. ScriptContext* constructorScriptContext = function->GetScriptContext();
  5726. Assert(!constructorScriptContext->GetThreadContext()->IsDisableImplicitException());
  5727. // we shouldn't try to call the constructor if it's closed already.
  5728. constructorScriptContext->VerifyAlive(TRUE, requestContext);
  5729. FunctionInfo::Attributes attributes = functionInfo->GetAttributes();
  5730. if (attributes & FunctionInfo::ErrorOnNew)
  5731. {
  5732. JavascriptError::ThrowTypeError(requestContext, JSERR_ErrorOnNew);
  5733. }
  5734. // Slow path
  5735. FunctionProxy * ctorProxy = constructor->GetFunctionProxy();
  5736. FunctionBody * functionBody = ctorProxy != nullptr ? ctorProxy->EnsureDeserialized()->Parse() : nullptr;
  5737. if (attributes & FunctionInfo::SkipDefaultNewObject)
  5738. {
  5739. // The constructor doesn't use the default new object.
  5740. #pragma prefast(suppress:6236, "DevDiv bug 830883. False positive when PHASE_OFF is #defined as '(false)'.")
  5741. if (!PHASE_OFF1(ConstructorCachePhase) && (functionBody == nullptr || !PHASE_OFF(ConstructorCachePhase, functionBody)))
  5742. {
  5743. constructorCache = constructor->EnsureValidConstructorCache();
  5744. constructorCache->PopulateForSkipDefaultNewObject(constructorScriptContext);
  5745. #if DBG_DUMP
  5746. if ((functionBody != nullptr && PHASE_TRACE(Js::ConstructorCachePhase, functionBody)) || (functionBody == nullptr && PHASE_TRACE1(Js::ConstructorCachePhase)))
  5747. {
  5748. const char16* ctorName = functionBody != nullptr ? functionBody->GetDisplayName() : _u("<unknown>");
  5749. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  5750. Output::Print(_u("CtorCache: populated cache (0x%p) for ctor %s (%s): "), constructorCache, ctorName,
  5751. functionBody ? functionBody->GetDebugNumberSet(debugStringBuffer) : _u("(null)"));
  5752. constructorCache->Dump();
  5753. Output::Print(_u("\n"));
  5754. Output::Flush();
  5755. }
  5756. #endif
  5757. }
  5758. Assert(!constructorCache->NeedsUpdateAfterCtor());
  5759. return nullptr;
  5760. }
  5761. // CONSIDER: Create some form of PatchGetProtoObjForCtorCache, which actually caches the prototype object in the constructor cache.
  5762. // 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
  5763. // after we fail the guard check. When invalidating the cache for proto change, make sure we zap the prototype field of the cache in
  5764. // addition to the guard value.
  5765. bool prototypeCanBeCached;
  5766. RecyclableObject* prototype = JavascriptOperators::GetPrototypeObjectForConstructorCache(
  5767. function, constructorScriptContext, prototypeCanBeCached);
  5768. prototype = RecyclableObject::FromVar(CrossSite::MarshalVar(requestContext,
  5769. prototype, constructorScriptContext));
  5770. DynamicObject* newObject = requestContext->GetLibrary()->CreateObject(prototype, 8);
  5771. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(newObject));
  5772. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5773. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5774. {
  5775. newObject = DynamicObject::FromVar(JavascriptProxy::AutoProxyWrapper(newObject));
  5776. }
  5777. #endif
  5778. Assert(newObject->GetTypeHandler()->GetPropertyCount() == 0);
  5779. if (prototypeCanBeCached && functionBody != nullptr && requestContext == constructorScriptContext &&
  5780. !Js::JavascriptProxy::Is(newObject) &&
  5781. !PHASE_OFF1(ConstructorCachePhase) && !PHASE_OFF(ConstructorCachePhase, functionBody))
  5782. {
  5783. DynamicType* newObjectType = newObject->GetDynamicType();
  5784. // Initial type (without any properties) should always be shared up-front. This allows us to populate the cache right away.
  5785. Assert(newObjectType->GetIsShared());
  5786. // Populate the cache here and set the updateAfterCtor flag. This way, if the ctor is called recursively the
  5787. // recursive calls will hit the cache and use the initial type. On the unwind path, we will update the cache
  5788. // after the innermost ctor and clear the flag. After subsequent ctors we won't attempt an update anymore.
  5789. // As long as the updateAfterCtor flag is set it is safe to update the cache, because it would not have been
  5790. // hard-coded in the JIT-ed code.
  5791. constructorCache = constructor->EnsureValidConstructorCache();
  5792. constructorCache->Populate(newObjectType, constructorScriptContext, functionBody->GetHasNoExplicitReturnValue(), true);
  5793. Assert(constructorCache->IsConsistent());
  5794. #if DBG_DUMP
  5795. if ((functionBody != nullptr && PHASE_TRACE(Js::ConstructorCachePhase, functionBody)) || (functionBody == nullptr && PHASE_TRACE1(Js::ConstructorCachePhase)))
  5796. {
  5797. const char16* ctorName = functionBody != nullptr ? functionBody->GetDisplayName() : _u("<unknown>");
  5798. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  5799. Output::Print(_u("CtorCache: populated cache (0x%p) for ctor %s (%s): "), constructorCache, ctorName,
  5800. functionBody ? functionBody->GetDebugNumberSet(debugStringBuffer) : _u("(null)"));
  5801. constructorCache->Dump();
  5802. Output::Print(_u("\n"));
  5803. Output::Flush();
  5804. }
  5805. #endif
  5806. }
  5807. else
  5808. {
  5809. #if DBG_DUMP
  5810. if ((functionBody != nullptr && PHASE_TRACE(Js::ConstructorCachePhase, functionBody)) || (functionBody == nullptr && PHASE_TRACE1(Js::ConstructorCachePhase)))
  5811. {
  5812. const char16* ctorName = functionBody != nullptr ? functionBody->GetDisplayName() : _u("<unknown>");
  5813. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  5814. 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"),
  5815. constructorCache, ctorName, functionBody ? functionBody->GetDebugNumberSet(debugStringBuffer) : _u("(null)"),
  5816. !prototypeCanBeCached ? _u("prototype cannot be cached") :
  5817. functionBody == nullptr ? _u("function has no body") :
  5818. requestContext != constructorScriptContext ? _u("of cross-context call") : _u("constructor cache phase is off"),
  5819. prototype, functionBody, constructorScriptContext, requestContext);
  5820. Output::Print(_u("\n"));
  5821. Output::Flush();
  5822. }
  5823. #endif
  5824. }
  5825. return newObject;
  5826. }
  5827. void JavascriptOperators::UpdateNewScObjectCache(Var function, Var instance, ScriptContext* requestContext)
  5828. {
  5829. JIT_HELPER_NOT_REENTRANT_HEADER(UpdateNewScObjectCache, reentrancylock, requestContext->GetThreadContext());
  5830. JavascriptFunction* constructor = JavascriptFunction::FromVar(function);
  5831. if(constructor->GetScriptContext() != requestContext)
  5832. {
  5833. // The cache is populated only when the constructor function's context is the same as the calling context. However,
  5834. // the cached type is not finalized yet and may not be until multiple calls to the constructor have been made (see
  5835. // flag ConstructorCallsRequiredToFinalizeCachedType). A subsequent call to the constructor may be made from a
  5836. // different context, so ignore those cross-context calls and wait for the constructor to be called from its own
  5837. // context again to finalize the cached type.
  5838. return;
  5839. }
  5840. // Review : What happens if the cache got invalidated between NewScObject and here?
  5841. // Should we allocate new? Should we mark it as polymorphic?
  5842. ConstructorCache* constructorCache = constructor->GetConstructorCache();
  5843. Assert(constructorCache->IsConsistent());
  5844. Assert(!ConstructorCache::IsDefault(constructorCache));
  5845. AssertMsg(constructorCache->GetScriptContext() == constructor->GetScriptContext(), "Why did we populate a constructor cache with a mismatched script context?");
  5846. AssertMsg(constructorCache->IsPopulated(), "Why are we updating a constructor cache that hasn't been populated?");
  5847. // The presence of the updateAfterCtor flag guarantees that this cache hasn't been used in JIT-ed fast path. Even, if the
  5848. // cache is invalidated, this flag is not changed.
  5849. AssertMsg(constructorCache->NeedsUpdateAfterCtor(), "Why are we updating a constructor cache that doesn't need to be updated?");
  5850. const bool finalizeCachedType =
  5851. constructorCache->CallCount() >= CONFIG_FLAG(ConstructorCallsRequiredToFinalizeCachedType);
  5852. if(!finalizeCachedType)
  5853. {
  5854. constructorCache->IncCallCount();
  5855. }
  5856. else
  5857. {
  5858. constructorCache->ClearUpdateAfterCtor();
  5859. }
  5860. FunctionBody* constructorBody = constructor->GetFunctionBody();
  5861. AssertMsg(constructorBody != nullptr, "Constructor function doesn't have a function body.");
  5862. Assert(RecyclableObject::Is(instance));
  5863. // The cache might have been invalidated between NewScObjectCommon and UpdateNewScObjectCache. This could occur, for example, if
  5864. // the constructor updates its own prototype property. If that happens we don't want to re-populate it here. A new cache will
  5865. // be created when the constructor is called again.
  5866. if (constructorCache->IsInvalidated())
  5867. {
  5868. #if DBG_DUMP
  5869. TraceUpdateConstructorCache(constructorCache, constructorBody, false, _u("because cache is invalidated"));
  5870. #endif
  5871. return;
  5872. }
  5873. Assert(constructorCache->GetGuardValueAsType() != nullptr);
  5874. if (DynamicType::Is(RecyclableObject::FromVar(instance)->GetTypeId()))
  5875. {
  5876. DynamicObject *object = DynamicObject::UnsafeFromVar(instance);
  5877. DynamicType* type = object->GetDynamicType();
  5878. DynamicTypeHandler* typeHandler = type->GetTypeHandler();
  5879. if (constructorBody->GetHasOnlyThisStmts())
  5880. {
  5881. if (typeHandler->IsSharable())
  5882. {
  5883. #if DBG
  5884. bool cachedProtoCanBeCached = false;
  5885. Assert(type->GetPrototype() == JavascriptOperators::GetPrototypeObjectForConstructorCache(constructor, requestContext, cachedProtoCanBeCached));
  5886. Assert(cachedProtoCanBeCached);
  5887. Assert(type->GetScriptContext() == constructorCache->GetScriptContext());
  5888. Assert(type->GetPrototype() == constructorCache->GetType()->GetPrototype());
  5889. #endif
  5890. typeHandler->SetMayBecomeShared();
  5891. // CONSIDER: Remove only this for delayed type sharing.
  5892. type->ShareType();
  5893. #if ENABLE_PROFILE_INFO
  5894. DynamicProfileInfo* profileInfo = constructorBody->HasDynamicProfileInfo() ? constructorBody->GetAnyDynamicProfileInfo() : nullptr;
  5895. if ((profileInfo != nullptr && profileInfo->GetImplicitCallFlags() <= ImplicitCall_None) ||
  5896. CheckIfPrototypeChainHasOnlyWritableDataProperties(type->GetPrototype()))
  5897. {
  5898. Assert(typeHandler->GetPropertyCount() < Js::PropertyIndexRanges<PropertyIndex>::MaxValue);
  5899. for (PropertyIndex pi = 0; pi < typeHandler->GetPropertyCount(); pi++)
  5900. {
  5901. requestContext->RegisterConstructorCache(typeHandler->GetPropertyId(requestContext, pi), constructorCache);
  5902. }
  5903. Assert(constructorBody->GetUtf8SourceInfo()->GetIsLibraryCode() || !constructor->GetScriptContext()->IsScriptContextInDebugMode());
  5904. if (constructorCache->TryUpdateAfterConstructor(type, constructor->GetScriptContext()))
  5905. {
  5906. #if DBG_DUMP
  5907. TraceUpdateConstructorCache(constructorCache, constructorBody, true, _u(""));
  5908. #endif
  5909. }
  5910. else
  5911. {
  5912. #if DBG_DUMP
  5913. TraceUpdateConstructorCache(constructorCache, constructorBody, false, _u("because number of slots > MaxCachedSlotCount"));
  5914. #endif
  5915. }
  5916. }
  5917. #if DBG_DUMP
  5918. else
  5919. {
  5920. if (profileInfo &&
  5921. ((profileInfo->GetImplicitCallFlags() & ~(Js::ImplicitCall_External | Js::ImplicitCall_Accessor)) == 0) &&
  5922. profileInfo != nullptr && CheckIfPrototypeChainHasOnlyWritableDataProperties(type->GetPrototype()) &&
  5923. Js::Configuration::Global.flags.Trace.IsEnabled(Js::HostOptPhase))
  5924. {
  5925. const char16* ctorName = constructorBody->GetDisplayName();
  5926. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  5927. Output::Print(_u("CtorCache: %s cache (0x%p) for ctor %s (#%u) did not update because external call"),
  5928. constructorCache, constructorBody, ctorName, constructorBody ? constructorBody->GetDebugNumberSet(debugStringBuffer) : _u("(null)"));
  5929. Output::Print(_u("\n"));
  5930. Output::Flush();
  5931. }
  5932. }
  5933. #endif
  5934. #endif
  5935. }
  5936. else
  5937. {
  5938. // Dynamic type created is not sharable.
  5939. // So in future don't try to check for "this assignment optimization".
  5940. constructorBody->SetHasOnlyThisStmts(false);
  5941. #if DBG_DUMP
  5942. TraceUpdateConstructorCache(constructorCache, constructorBody, false, _u("because final type is not shareable"));
  5943. #endif
  5944. }
  5945. }
  5946. else
  5947. {
  5948. #if DBG_DUMP
  5949. TraceUpdateConstructorCache(constructorCache, constructorBody, false, _u("because ctor has not only this statements"));
  5950. #endif
  5951. }
  5952. }
  5953. else
  5954. {
  5955. // Even though this constructor apparently returned something other than the default object we created,
  5956. // it still makes sense to cache the parameters of the default object, since we must create it every time, anyway.
  5957. #if DBG_DUMP
  5958. TraceUpdateConstructorCache(constructorCache, constructorBody, false, _u("because ctor return a non-object value"));
  5959. #endif
  5960. return;
  5961. }
  5962. // Whatever the constructor returned, if we're caching a type we want to be sure we shrink its inline slot capacity.
  5963. if (finalizeCachedType && constructorCache->IsEnabled())
  5964. {
  5965. DynamicType* cachedType = constructorCache->NeedsTypeUpdate() ? constructorCache->GetPendingType() : constructorCache->GetType();
  5966. DynamicTypeHandler* cachedTypeHandler = cachedType->GetTypeHandler();
  5967. // Consider: We could delay inline slot capacity shrinking until the second time this constructor is invoked. In some cases
  5968. // this might permit more properties to remain inlined if the objects grow after constructor. This would require flagging
  5969. // the cache as special (already possible) and forcing the shrinking during work item creation if we happen to JIT this
  5970. // constructor while the cache is in this special state.
  5971. if (cachedTypeHandler->GetInlineSlotCapacity())
  5972. {
  5973. #if DBG_DUMP
  5974. int inlineSlotCapacityBeforeShrink = cachedTypeHandler->GetInlineSlotCapacity();
  5975. #endif
  5976. // Note that after the cache has been updated and might have been used in the JIT-ed code, it is no longer legal to
  5977. // shrink the inline slot capacity of the type. That's because we allocate memory for a fixed number of inlined properties
  5978. // and if that number changed on the type, this update wouldn't get reflected in JIT-ed code and we would allocate objects
  5979. // of a wrong size. This could conceivably happen if the original object got collected, and with it some of the successor
  5980. // types also. If then another constructor has the same prototype and needs to populate its own cache, it would attempt to
  5981. // shrink inlined slots again. If all surviving type handlers have smaller inline slot capacity, we would shrink it further.
  5982. // To address this problem the type handler has a bit indicating its inline slots have been shrunk already. If that bit is
  5983. // set ShrinkSlotAndInlineSlotCapacity does nothing.
  5984. cachedTypeHandler->ShrinkSlotAndInlineSlotCapacity();
  5985. constructorCache->UpdateInlineSlotCount();
  5986. #if DBG_DUMP
  5987. Assert(inlineSlotCapacityBeforeShrink >= cachedTypeHandler->GetInlineSlotCapacity());
  5988. if (Js::Configuration::Global.flags.Trace.IsEnabled(Js::InlineSlotsPhase))
  5989. {
  5990. if (inlineSlotCapacityBeforeShrink != cachedTypeHandler->GetInlineSlotCapacity())
  5991. {
  5992. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  5993. Output::Print(_u("Inline slot capacity shrunk: Function:%04s Before:%d After:%d\n"),
  5994. constructorBody->GetDebugNumberSet(debugStringBuffer), inlineSlotCapacityBeforeShrink, cachedTypeHandler->GetInlineSlotCapacity());
  5995. }
  5996. }
  5997. #endif
  5998. }
  5999. }
  6000. JIT_HELPER_END(UpdateNewScObjectCache);
  6001. }
  6002. void JavascriptOperators::TraceUseConstructorCache(const ConstructorCache* ctorCache, const JavascriptFunction* ctor, bool isHit)
  6003. {
  6004. #if DBG_DUMP
  6005. // We are under debug, so we can incur the extra check here.
  6006. FunctionProxy* ctorBody = ctor->GetFunctionProxy();
  6007. if (ctorBody != nullptr && !ctorBody->GetScriptContext()->IsClosed())
  6008. {
  6009. ctorBody = ctorBody->EnsureDeserialized();
  6010. }
  6011. if ((ctorBody != nullptr && PHASE_TRACE(Js::ConstructorCachePhase, ctorBody)) || (ctorBody == nullptr && PHASE_TRACE1(Js::ConstructorCachePhase)))
  6012. {
  6013. const char16* ctorName = ctorBody != nullptr ? ctorBody->GetDisplayName() : _u("<unknown>");
  6014. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  6015. Output::Print(_u("CtorCache: %s cache (0x%p) for ctor %s (%s): "), isHit ? _u("hit") : _u("missed"), ctorCache, ctorName,
  6016. ctorBody ? ctorBody->GetDebugNumberSet(debugStringBuffer) : _u("(null)"));
  6017. ctorCache->Dump();
  6018. Output::Print(_u("\n"));
  6019. Output::Flush();
  6020. }
  6021. #endif
  6022. }
  6023. void JavascriptOperators::TraceUpdateConstructorCache(const ConstructorCache* ctorCache, const FunctionBody* ctorBody, bool updated, const char16* reason)
  6024. {
  6025. #if DBG_DUMP
  6026. if (PHASE_TRACE(Js::ConstructorCachePhase, ctorBody))
  6027. {
  6028. const char16* ctorName = ctorBody->GetDisplayName();
  6029. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  6030. Output::Print(_u("CtorCache: %s cache (0x%p) for ctor %s (%s)%s %s: "),
  6031. updated ? _u("updated") : _u("did not update"), ctorBody, ctorName,
  6032. ctorBody ? const_cast<Js::FunctionBody *>(ctorBody)->GetDebugNumberSet(debugStringBuffer) : _u("(null)"),
  6033. updated ? _u("") : _u(", because") , reason);
  6034. ctorCache->Dump();
  6035. Output::Print(_u("\n"));
  6036. Output::Flush();
  6037. }
  6038. #endif
  6039. }
  6040. Var JavascriptOperators::NewScObject(const Var callee, const Arguments args, ScriptContext *const scriptContext, const Js::AuxArray<uint32> *spreadIndices)
  6041. {
  6042. Assert(callee);
  6043. Assert(args.Info.Count != 0);
  6044. Assert(scriptContext);
  6045. // Always save and restore implicit call flags when calling out
  6046. // REVIEW: Can we avoid it if we don't collect dynamic profile info?
  6047. ThreadContext *const threadContext = scriptContext->GetThreadContext();
  6048. const ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  6049. const Var newVarInstance = JavascriptFunction::CallAsConstructor(callee, /* overridingNewTarget = */nullptr, args, scriptContext, spreadIndices);
  6050. threadContext->SetImplicitCallFlags(savedImplicitCallFlags);
  6051. return newVarInstance;
  6052. }
  6053. Js::GlobalObject * JavascriptOperators::OP_LdRoot(ScriptContext* scriptContext)
  6054. {
  6055. return scriptContext->GetGlobalObject();
  6056. }
  6057. Js::ModuleRoot * JavascriptOperators::GetModuleRoot(int moduleID, ScriptContext* scriptContext)
  6058. {
  6059. Assert(moduleID != kmodGlobal);
  6060. JavascriptLibrary* library = scriptContext->GetLibrary();
  6061. HostObjectBase *hostObject = library->GetGlobalObject()->GetHostObject();
  6062. if (hostObject)
  6063. {
  6064. Js::ModuleRoot * moduleRoot = hostObject->GetModuleRoot(moduleID);
  6065. Assert(!CrossSite::NeedMarshalVar(moduleRoot, scriptContext));
  6066. return moduleRoot;
  6067. }
  6068. HostScriptContext *hostScriptContext = scriptContext->GetHostScriptContext();
  6069. if (hostScriptContext)
  6070. {
  6071. Js::ModuleRoot * moduleRoot = hostScriptContext->GetModuleRoot(moduleID);
  6072. Assert(!CrossSite::NeedMarshalVar(moduleRoot, scriptContext));
  6073. return moduleRoot;
  6074. }
  6075. Assert(FALSE);
  6076. return nullptr;
  6077. }
  6078. Var JavascriptOperators::OP_LoadModuleRoot(int moduleID, ScriptContext* scriptContext)
  6079. {
  6080. Js::ModuleRoot * moduleRoot = GetModuleRoot(moduleID, scriptContext);
  6081. if (moduleRoot)
  6082. {
  6083. return moduleRoot;
  6084. }
  6085. Assert(false);
  6086. return scriptContext->GetLibrary()->GetUndefined();
  6087. }
  6088. Var JavascriptOperators::OP_LdNull(ScriptContext* scriptContext)
  6089. {
  6090. return scriptContext->GetLibrary()->GetNull();
  6091. }
  6092. Var JavascriptOperators::OP_LdUndef(ScriptContext* scriptContext)
  6093. {
  6094. return scriptContext->GetLibrary()->GetUndefined();
  6095. }
  6096. Var JavascriptOperators::OP_LdNaN(ScriptContext* scriptContext)
  6097. {
  6098. return scriptContext->GetLibrary()->GetNaN();
  6099. }
  6100. Var JavascriptOperators::OP_LdChakraLib(ScriptContext* scriptContext)
  6101. {
  6102. return scriptContext->GetLibrary()->GetChakraLib();
  6103. }
  6104. Var JavascriptOperators::OP_LdInfinity(ScriptContext* scriptContext)
  6105. {
  6106. return scriptContext->GetLibrary()->GetPositiveInfinite();
  6107. }
  6108. void JavascriptOperators::BuildHandlerScope(Var argThis, RecyclableObject * hostObject, FrameDisplay * pDisplay, ScriptContext * scriptContext)
  6109. {
  6110. // Event handlers need implicit lookups of @@unscopables on parent scopes.
  6111. // We can intercept the property accesses by wrapping the object with the unscopables handler.
  6112. // WebIDL: https://heycam.github.io/webidl/#ref-for-Unscopable
  6113. Assert(argThis != nullptr);
  6114. pDisplay->SetItem(0, TaggedNumber::Is(argThis) ? scriptContext->GetLibrary()->CreateNumberObject(argThis) : ToUnscopablesWrapperObject(argThis, scriptContext));
  6115. uint16 i = 1;
  6116. Var aChild = argThis;
  6117. uint16 length = pDisplay->GetLength();
  6118. // Now add any parent scopes
  6119. // We need to support the namespace parent lookup in both fastDOM on and off scenario.
  6120. while (aChild != NULL)
  6121. {
  6122. Var aParent = hostObject->GetNamespaceParent(aChild);
  6123. if (aParent == nullptr)
  6124. {
  6125. break;
  6126. }
  6127. aParent = CrossSite::MarshalVar(scriptContext, aParent);
  6128. if (i == length)
  6129. {
  6130. length = UInt16Math::Add(length, 8);
  6131. FrameDisplay * tmp = RecyclerNewPlus(scriptContext->GetRecycler(), length * sizeof(void*), FrameDisplay, length);
  6132. js_memcpy_s((char*)tmp + tmp->GetOffsetOfScopes(), tmp->GetLength() * sizeof(void *), (char*)pDisplay + pDisplay->GetOffsetOfScopes(), pDisplay->GetLength() * sizeof(void*));
  6133. pDisplay = tmp;
  6134. }
  6135. Var aParentWrapped = ToUnscopablesWrapperObject(aParent, scriptContext);
  6136. pDisplay->SetItem(i, aParentWrapped);
  6137. aChild = aParent;
  6138. i++;
  6139. }
  6140. Assert(i <= pDisplay->GetLength());
  6141. pDisplay->SetLength(i);
  6142. }
  6143. FrameDisplay * JavascriptOperators::OP_LdHandlerScope(Var argThis, ScriptContext* scriptContext)
  6144. {
  6145. JIT_HELPER_REENTRANT_HEADER(ScrObj_LdHandlerScope);
  6146. // The idea here is to build a stack of nested scopes in the form of a JS array.
  6147. //
  6148. // The scope stack for an event handler looks like this:
  6149. //
  6150. // implicit "this"
  6151. // implicit namespace parent scopes
  6152. // Put the implicit "this"
  6153. if (argThis != NULL)
  6154. {
  6155. RecyclableObject* hostObject = scriptContext->GetGlobalObject()->GetHostObject();
  6156. if (hostObject == nullptr)
  6157. {
  6158. hostObject = scriptContext->GetGlobalObject()->GetDirectHostObject();
  6159. }
  6160. if (hostObject != nullptr)
  6161. {
  6162. uint16 length = 7;
  6163. FrameDisplay *pDisplay =
  6164. RecyclerNewPlus(scriptContext->GetRecycler(), length * sizeof(void*), FrameDisplay, length);
  6165. BuildHandlerScope(argThis, hostObject, pDisplay, scriptContext);
  6166. return pDisplay;
  6167. }
  6168. }
  6169. return const_cast<FrameDisplay *>(&Js::NullFrameDisplay);
  6170. JIT_HELPER_END(ScrObj_LdHandlerScope);
  6171. }
  6172. FrameDisplay* JavascriptOperators::OP_LdFrameDisplay(void *argHead, void *argEnv, ScriptContext* scriptContext)
  6173. {
  6174. JIT_HELPER_NOT_REENTRANT_HEADER(ScrObj_LdFrameDisplay, reentrancylock, scriptContext->GetThreadContext());
  6175. // Build a display of nested frame objects.
  6176. // argHead is the current scope; argEnv is either the lone trailing scope or an array of scopes
  6177. // which we append to the new display.
  6178. // Note that there are cases in which a function with no local frame must construct a display to pass
  6179. // to the function(s) nested within it. In such a case, argHead will be a null object, and it's not
  6180. // strictly necessary to include it. But such cases are rare and not perf critical, so it's not
  6181. // worth the extra complexity to notify the nested functions that they can "skip" this slot in the
  6182. // frame display when they're loading scopes nested outside it.
  6183. FrameDisplay *pDisplay = nullptr;
  6184. FrameDisplay *envDisplay = (FrameDisplay*)argEnv;
  6185. uint16 length = UInt16Math::Add(envDisplay->GetLength(), 1);
  6186. pDisplay = RecyclerNewPlus(scriptContext->GetRecycler(), length * sizeof(void*), FrameDisplay, length);
  6187. for (uint16 j = 0; j < length - 1; j++)
  6188. {
  6189. pDisplay->SetItem(j + 1, envDisplay->GetItem(j));
  6190. }
  6191. pDisplay->SetItem(0, argHead);
  6192. return pDisplay;
  6193. JIT_HELPER_END(ScrObj_LdFrameDisplay);
  6194. }
  6195. FrameDisplay* JavascriptOperators::OP_LdFrameDisplayNoParent(void *argHead, ScriptContext* scriptContext)
  6196. {
  6197. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdFrameDisplayNoParent);
  6198. return OP_LdFrameDisplay(argHead, (void*)&NullFrameDisplay, scriptContext);
  6199. JIT_HELPER_END(ScrObj_LdFrameDisplayNoParent);
  6200. }
  6201. FrameDisplay* JavascriptOperators::OP_LdStrictFrameDisplay(void *argHead, void *argEnv, ScriptContext* scriptContext)
  6202. {
  6203. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdStrictFrameDisplay);
  6204. FrameDisplay * pDisplay = OP_LdFrameDisplay(argHead, argEnv, scriptContext);
  6205. pDisplay->SetStrictMode(true);
  6206. return pDisplay;
  6207. JIT_HELPER_END(ScrObj_LdStrictFrameDisplay);
  6208. }
  6209. FrameDisplay* JavascriptOperators::OP_LdStrictFrameDisplayNoParent(void *argHead, ScriptContext* scriptContext)
  6210. {
  6211. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdStrictFrameDisplayNoParent);
  6212. return OP_LdStrictFrameDisplay(argHead, (void*)&StrictNullFrameDisplay, scriptContext);
  6213. JIT_HELPER_END(ScrObj_LdStrictFrameDisplayNoParent);
  6214. }
  6215. FrameDisplay* JavascriptOperators::OP_LdInnerFrameDisplay(void *argHead, void *argEnv, ScriptContext* scriptContext)
  6216. {
  6217. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdInnerFrameDisplay);
  6218. CheckInnerFrameDisplayArgument(argHead);
  6219. return OP_LdFrameDisplay(argHead, argEnv, scriptContext);
  6220. JIT_HELPER_END(ScrObj_LdInnerFrameDisplay);
  6221. }
  6222. FrameDisplay* JavascriptOperators::OP_LdInnerFrameDisplayNoParent(void *argHead, ScriptContext* scriptContext)
  6223. {
  6224. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdInnerFrameDisplayNoParent);
  6225. CheckInnerFrameDisplayArgument(argHead);
  6226. return OP_LdFrameDisplayNoParent(argHead, scriptContext);
  6227. JIT_HELPER_END(ScrObj_LdInnerFrameDisplayNoParent);
  6228. }
  6229. FrameDisplay* JavascriptOperators::OP_LdStrictInnerFrameDisplay(void *argHead, void *argEnv, ScriptContext* scriptContext)
  6230. {
  6231. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdStrictInnerFrameDisplay);
  6232. CheckInnerFrameDisplayArgument(argHead);
  6233. return OP_LdStrictFrameDisplay(argHead, argEnv, scriptContext);
  6234. JIT_HELPER_END(ScrObj_LdStrictInnerFrameDisplay);
  6235. }
  6236. FrameDisplay* JavascriptOperators::OP_LdStrictInnerFrameDisplayNoParent(void *argHead, ScriptContext* scriptContext)
  6237. {
  6238. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScrObj_LdStrictInnerFrameDisplayNoParent);
  6239. CheckInnerFrameDisplayArgument(argHead);
  6240. return OP_LdStrictFrameDisplayNoParent(argHead, scriptContext);
  6241. JIT_HELPER_END(ScrObj_LdStrictInnerFrameDisplayNoParent);
  6242. }
  6243. void JavascriptOperators::CheckInnerFrameDisplayArgument(void *argHead)
  6244. {
  6245. if (ThreadContext::IsOnStack(argHead))
  6246. {
  6247. AssertMsg(false, "Illegal byte code: stack object as with scope");
  6248. Js::Throw::FatalInternalError();
  6249. }
  6250. if (!RecyclableObject::Is(argHead))
  6251. {
  6252. AssertMsg(false, "Illegal byte code: non-object as with scope");
  6253. Js::Throw::FatalInternalError();
  6254. }
  6255. }
  6256. Js::PropertyId JavascriptOperators::GetPropertyId(Var propertyName, ScriptContext* scriptContext)
  6257. {
  6258. PropertyRecord const * propertyRecord = nullptr;
  6259. JavascriptSymbol * symbol = JavascriptOperators::TryFromVar<Js::JavascriptSymbol>(propertyName);
  6260. if (symbol)
  6261. {
  6262. propertyRecord = symbol->GetValue();
  6263. }
  6264. else
  6265. {
  6266. JavascriptSymbolObject * symbolObject = JavascriptOperators::TryFromVar<JavascriptSymbolObject>(propertyName);
  6267. if (symbolObject)
  6268. {
  6269. propertyRecord = symbolObject->GetValue();
  6270. }
  6271. else
  6272. {
  6273. JavascriptString * indexStr = JavascriptConversion::ToString(propertyName, scriptContext);
  6274. scriptContext->GetOrAddPropertyRecord(indexStr, &propertyRecord);
  6275. }
  6276. }
  6277. return propertyRecord->GetPropertyId();
  6278. }
  6279. void JavascriptOperators::OP_InitSetter(Var object, PropertyId propertyId, Var setter)
  6280. {
  6281. AssertMsg(!TaggedNumber::Is(object), "SetMember on a non-object?");
  6282. RecyclableObject* recylableObject = RecyclableObject::FromVar(object);
  6283. JIT_HELPER_NOT_REENTRANT_HEADER(OP_InitSetter, reentrancylock, recylableObject->GetScriptContext()->GetThreadContext());
  6284. recylableObject->SetAccessors(propertyId, nullptr, setter);
  6285. JIT_HELPER_END(OP_InitSetter);
  6286. }
  6287. void JavascriptOperators::OP_InitClassMemberSet(Var object, PropertyId propertyId, Var setter)
  6288. {
  6289. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_InitClassMemberSet);
  6290. JIT_HELPER_SAME_ATTRIBUTES(Op_InitClassMemberSet, OP_InitSetter);
  6291. JavascriptOperators::OP_InitSetter(object, propertyId, setter);
  6292. RecyclableObject::FromVar(object)->SetAttributes(propertyId, PropertyClassMemberDefaults);
  6293. JIT_HELPER_END(Op_InitClassMemberSet);
  6294. }
  6295. Js::PropertyId JavascriptOperators::OP_InitElemSetter(Var object, Var elementName, Var setter, ScriptContext* scriptContext, PropertyOperationFlags flags)
  6296. {
  6297. JIT_HELPER_REENTRANT_HEADER(OP_InitElemSetter);
  6298. AssertMsg(!TaggedNumber::Is(object), "SetMember on a non-object?");
  6299. PropertyId propertyId = JavascriptOperators::GetPropertyId(elementName, scriptContext);
  6300. RecyclableObject::FromVar(object)->SetAccessors(propertyId, nullptr, setter);
  6301. return propertyId;
  6302. JIT_HELPER_END(OP_InitElemSetter);
  6303. }
  6304. Field(Var)* JavascriptOperators::OP_GetModuleExportSlotArrayAddress(uint moduleIndex, uint slotIndex, ScriptContextInfo* scriptContext)
  6305. {
  6306. return scriptContext->GetModuleExportSlotArrayAddress(moduleIndex, slotIndex);
  6307. }
  6308. Field(Var)* JavascriptOperators::OP_GetModuleExportSlotAddress(uint moduleIndex, uint slotIndex, ScriptContext* scriptContext)
  6309. {
  6310. Field(Var)* moduleRecordSlots = OP_GetModuleExportSlotArrayAddress(moduleIndex, slotIndex, scriptContext);
  6311. Assert(moduleRecordSlots != nullptr);
  6312. return &moduleRecordSlots[slotIndex];
  6313. }
  6314. Var JavascriptOperators::OP_LdModuleSlot(uint moduleIndex, uint slotIndex, ScriptContext* scriptContext)
  6315. {
  6316. Field(Var)* addr = OP_GetModuleExportSlotAddress(moduleIndex, slotIndex, scriptContext);
  6317. Assert(addr != nullptr);
  6318. return *addr;
  6319. }
  6320. void JavascriptOperators::OP_StModuleSlot(uint moduleIndex, uint slotIndex, Var value, ScriptContext* scriptContext)
  6321. {
  6322. Assert(value != nullptr);
  6323. Field(Var)* addr = OP_GetModuleExportSlotAddress(moduleIndex, slotIndex, scriptContext);
  6324. Assert(addr != nullptr);
  6325. *addr = value;
  6326. }
  6327. void JavascriptOperators::OP_InitClassMemberSetComputedName(Var object, Var elementName, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  6328. {
  6329. JIT_HELPER_REENTRANT_HEADER(Op_InitClassMemberSetComputedName);
  6330. Js::PropertyId propertyId = JavascriptOperators::OP_InitElemSetter(object, elementName, value, scriptContext);
  6331. RecyclableObject* instance = RecyclableObject::FromVar(object);
  6332. // instance will be a function if it is the class constructor (otherwise it would be an object)
  6333. if (JavascriptFunction::Is(instance) && Js::PropertyIds::prototype == propertyId)
  6334. {
  6335. // It is a TypeError to have a static member with a computed name that evaluates to 'prototype'
  6336. JavascriptError::ThrowTypeError(scriptContext, JSERR_ClassStaticMethodCannotBePrototype);
  6337. }
  6338. instance->SetAttributes(propertyId, PropertyClassMemberDefaults);
  6339. JIT_HELPER_END(Op_InitClassMemberSetComputedName);
  6340. }
  6341. BOOL JavascriptOperators::IsClassConstructor(Var instance)
  6342. {
  6343. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_IsClassConstructor);
  6344. JavascriptFunction * function = JavascriptOperators::TryFromVar<JavascriptFunction>(instance);
  6345. return function && (function->GetFunctionInfo()->IsClassConstructor() || (!function->IsScriptFunction() && !function->IsExternalFunction()));
  6346. JIT_HELPER_END(Op_IsClassConstructor);
  6347. }
  6348. BOOL JavascriptOperators::IsClassMethod(Var instance)
  6349. {
  6350. JavascriptFunction * function = JavascriptOperators::TryFromVar<JavascriptFunction>(instance);
  6351. return function && function->GetFunctionInfo()->IsClassMethod();
  6352. }
  6353. BOOL JavascriptOperators::IsBaseConstructorKind(Var instance)
  6354. {
  6355. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_IsBaseConstructorKind);
  6356. JavascriptFunction * function = JavascriptOperators::TryFromVar<JavascriptFunction>(instance);
  6357. return function && (function->GetFunctionInfo()->GetBaseConstructorKind());
  6358. JIT_HELPER_END(Op_IsBaseConstructorKind);
  6359. }
  6360. void JavascriptOperators::OP_InitGetter(Var object, PropertyId propertyId, Var getter)
  6361. {
  6362. AssertMsg(!TaggedNumber::Is(object), "GetMember on a non-object?");
  6363. RecyclableObject* recylableObject = RecyclableObject::FromVar(object);
  6364. JIT_HELPER_NOT_REENTRANT_HEADER(OP_InitGetter, reentrancylock, recylableObject->GetScriptContext()->GetThreadContext());
  6365. recylableObject->SetAccessors(propertyId, getter, nullptr);
  6366. JIT_HELPER_END(OP_InitGetter);
  6367. }
  6368. void JavascriptOperators::OP_InitClassMemberGet(Var object, PropertyId propertyId, Var getter)
  6369. {
  6370. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_InitClassMemberGet);
  6371. JIT_HELPER_SAME_ATTRIBUTES(Op_InitClassMemberGet, OP_InitGetter);
  6372. JavascriptOperators::OP_InitGetter(object, propertyId, getter);
  6373. RecyclableObject::FromVar(object)->SetAttributes(propertyId, PropertyClassMemberDefaults);
  6374. JIT_HELPER_END(Op_InitClassMemberGet);
  6375. }
  6376. Js::PropertyId JavascriptOperators::OP_InitElemGetter(Var object, Var elementName, Var getter, ScriptContext* scriptContext, PropertyOperationFlags flags)
  6377. {
  6378. JIT_HELPER_REENTRANT_HEADER(OP_InitElemGetter);
  6379. AssertMsg(!TaggedNumber::Is(object), "GetMember on a non-object?");
  6380. PropertyId propertyId = JavascriptOperators::GetPropertyId(elementName, scriptContext);
  6381. RecyclableObject::FromVar(object)->SetAccessors(propertyId, getter, nullptr);
  6382. return propertyId;
  6383. JIT_HELPER_END(OP_InitElemGetter);
  6384. }
  6385. void JavascriptOperators::OP_InitClassMemberGetComputedName(Var object, Var elementName, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  6386. {
  6387. JIT_HELPER_REENTRANT_HEADER(Op_InitClassMemberGetComputedName);
  6388. Js::PropertyId propertyId = JavascriptOperators::OP_InitElemGetter(object, elementName, value, scriptContext);
  6389. RecyclableObject* instance = RecyclableObject::FromVar(object);
  6390. // instance will be a function if it is the class constructor (otherwise it would be an object)
  6391. if (JavascriptFunction::Is(instance) && Js::PropertyIds::prototype == propertyId)
  6392. {
  6393. // It is a TypeError to have a static member with a computed name that evaluates to 'prototype'
  6394. JavascriptError::ThrowTypeError(scriptContext, JSERR_ClassStaticMethodCannotBePrototype);
  6395. }
  6396. instance->SetAttributes(propertyId, PropertyClassMemberDefaults);
  6397. JIT_HELPER_END(Op_InitClassMemberGetComputedName);
  6398. }
  6399. void JavascriptOperators::OP_InitComputedProperty(Var object, Var elementName, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  6400. {
  6401. JIT_HELPER_REENTRANT_HEADER(OP_InitComputedProperty);
  6402. PropertyId propertyId = JavascriptOperators::GetPropertyId(elementName, scriptContext);
  6403. RecyclableObject::FromVar(object)->InitProperty(propertyId, value, flags);
  6404. JIT_HELPER_END(OP_InitComputedProperty);
  6405. }
  6406. void JavascriptOperators::OP_InitClassMemberComputedName(Var object, Var elementName, Var value, ScriptContext* scriptContext, PropertyOperationFlags flags)
  6407. {
  6408. JIT_HELPER_REENTRANT_HEADER(Op_InitClassMemberComputedName);
  6409. PropertyId propertyId = JavascriptOperators::GetPropertyId(elementName, scriptContext);
  6410. RecyclableObject* instance = RecyclableObject::FromVar(object);
  6411. // instance will be a function if it is the class constructor (otherwise it would be an object)
  6412. if (JavascriptFunction::Is(instance) && Js::PropertyIds::prototype == propertyId)
  6413. {
  6414. // It is a TypeError to have a static member with a computed name that evaluates to 'prototype'
  6415. JavascriptError::ThrowTypeError(scriptContext, JSERR_ClassStaticMethodCannotBePrototype);
  6416. }
  6417. instance->SetPropertyWithAttributes(propertyId, value, PropertyClassMemberDefaults, NULL, flags);
  6418. JIT_HELPER_END(Op_InitClassMemberComputedName);
  6419. }
  6420. //
  6421. // Used by object literal {..., __proto__: ..., }.
  6422. //
  6423. void JavascriptOperators::OP_InitProto(Var instance, PropertyId propertyId, Var value)
  6424. {
  6425. AssertMsg(RecyclableObject::Is(instance), "__proto__ member on a non-object?");
  6426. Assert(propertyId == PropertyIds::__proto__);
  6427. RecyclableObject* object = RecyclableObject::FromVar(instance);
  6428. ScriptContext* scriptContext = object->GetScriptContext();
  6429. JIT_HELPER_NOT_REENTRANT_HEADER(OP_InitProto, reentrancylock, scriptContext->GetThreadContext());
  6430. // B.3.1 __proto___ Property Names in Object Initializers
  6431. //6.If propKey is the string value "__proto__" and if isComputedPropertyName(propKey) is false, then
  6432. // a.If Type(v) is either Object or Null, then
  6433. // i.Return the result of calling the [[SetInheritance]] internal method of object with argument propValue.
  6434. // b.Return NormalCompletion(empty).
  6435. if (JavascriptOperators::IsObjectOrNull(value))
  6436. {
  6437. JavascriptObject::ChangePrototype(object, RecyclableObject::FromVar(value), /*validate*/false, scriptContext);
  6438. }
  6439. JIT_HELPER_END(OP_InitProto);
  6440. }
  6441. Var JavascriptOperators::ConvertToUnmappedArguments(HeapArgumentsObject *argumentsObject,
  6442. uint32 paramCount,
  6443. Var *paramAddr,
  6444. DynamicObject* frameObject,
  6445. Js::PropertyIdArray *propIds,
  6446. uint32 formalsCount,
  6447. ScriptContext* scriptContext)
  6448. {
  6449. Var *paramIter = paramAddr;
  6450. uint32 i = 0;
  6451. for (paramIter = paramAddr + i; i < paramCount; i++, paramIter++)
  6452. {
  6453. JavascriptOperators::SetItem(argumentsObject, argumentsObject, i, *paramIter, scriptContext, PropertyOperation_None, /* skipPrototypeCheck = */ TRUE);
  6454. }
  6455. argumentsObject = argumentsObject->ConvertToUnmappedArgumentsObject();
  6456. // Now as the unmapping is done we need to fill those frame object with Undecl
  6457. for (i = 0; i < formalsCount; i++)
  6458. {
  6459. frameObject->SetSlot(SetSlotArguments(propIds != nullptr ? propIds->elements[i] : Js::Constants::NoProperty, i, scriptContext->GetLibrary()->GetUndeclBlockVar()));
  6460. }
  6461. return argumentsObject;
  6462. }
  6463. Var JavascriptOperators::LoadHeapArguments(JavascriptFunction *funcCallee, uint32 actualsCount, Var *paramAddr, Var frameObj, Var vArray, ScriptContext* scriptContext, bool nonSimpleParamList)
  6464. {
  6465. JIT_HELPER_NOT_REENTRANT_HEADER(Op_LoadHeapArguments, reentrancylock, scriptContext->GetThreadContext());
  6466. AssertMsg(actualsCount != (unsigned int)-1, "Loading the arguments object in the global function?");
  6467. // Create and initialize the Arguments object.
  6468. uint32 formalsCount = 0;
  6469. Js::PropertyIdArray *propIds = nullptr;
  6470. if (vArray != scriptContext->GetLibrary()->GetNull())
  6471. {
  6472. propIds = (Js::PropertyIdArray *)vArray;
  6473. formalsCount = propIds->count;
  6474. Assert(formalsCount != 0 && propIds != nullptr);
  6475. }
  6476. HeapArgumentsObject *argsObj = JavascriptOperators::CreateHeapArguments(funcCallee, actualsCount, formalsCount, frameObj, scriptContext);
  6477. return FillScopeObject(funcCallee, actualsCount, formalsCount, frameObj, paramAddr, propIds, argsObj, scriptContext, nonSimpleParamList, false);
  6478. JIT_HELPER_END(Op_LoadHeapArguments);
  6479. }
  6480. Var JavascriptOperators::LoadHeapArgsCached(JavascriptFunction *funcCallee, uint32 actualsCount, uint32 formalsCount, Var *paramAddr, Var frameObj, ScriptContext* scriptContext, bool nonSimpleParamList)
  6481. {
  6482. JIT_HELPER_NOT_REENTRANT_HEADER(Op_LoadHeapArgsCached, reentrancylock, scriptContext->GetThreadContext());
  6483. // Disregard the "this" param.
  6484. AssertMsg(actualsCount != (uint32)-1 && formalsCount != (uint32)-1,
  6485. "Loading the arguments object in the global function?");
  6486. HeapArgumentsObject *argsObj = JavascriptOperators::CreateHeapArguments(funcCallee, actualsCount, formalsCount, frameObj, scriptContext);
  6487. return FillScopeObject(funcCallee, actualsCount, formalsCount, frameObj, paramAddr, nullptr, argsObj, scriptContext, nonSimpleParamList, true);
  6488. JIT_HELPER_END(Op_LoadHeapArgsCached);
  6489. }
  6490. Var JavascriptOperators::FillScopeObject(JavascriptFunction *funcCallee, uint32 actualsCount, uint32 formalsCount, Var frameObj, Var * paramAddr,
  6491. Js::PropertyIdArray *propIds, HeapArgumentsObject * argsObj, ScriptContext * scriptContext, bool nonSimpleParamList, bool useCachedScope)
  6492. {
  6493. Assert(formalsCount == 0 || frameObj != nullptr);
  6494. // Transfer formal arguments (that were actually passed) from their ArgIn slots to the local frame object.
  6495. uint32 i;
  6496. Var *tmpAddr = paramAddr;
  6497. if (formalsCount != 0)
  6498. {
  6499. DynamicObject* frameObject = nullptr;
  6500. if (useCachedScope)
  6501. {
  6502. frameObject = DynamicObject::FromVar(frameObj);
  6503. __analysis_assume((uint32)frameObject->GetDynamicType()->GetTypeHandler()->GetSlotCapacity() >= formalsCount);
  6504. }
  6505. else
  6506. {
  6507. frameObject = (DynamicObject*)frameObj;
  6508. // No fixed fields for formal parameters of the arguments object. Also, mark all fields as initialized up-front, because
  6509. // we will set them directly using SetSlot below, so the type handler will not have a chance to mark them as initialized later.
  6510. // CONSIDER : When we delay type sharing until the second instance is created, pass an argument indicating we want the types
  6511. // 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
  6512. // is ready for storing values directly to slots.
  6513. DynamicType* newType = nullptr;
  6514. if (nonSimpleParamList)
  6515. {
  6516. bool skipLetAttrForArguments = ((JavascriptGeneratorFunction::Is(funcCallee) || JavascriptAsyncFunction::Is(funcCallee)) ?
  6517. JavascriptGeneratorFunction::FromVar(funcCallee)->GetGeneratorVirtualScriptFunction()->GetFunctionBody()->HasReferenceableBuiltInArguments()
  6518. : funcCallee->GetFunctionBody()->HasReferenceableBuiltInArguments());
  6519. if (skipLetAttrForArguments)
  6520. {
  6521. newType = PathTypeHandlerBase::CreateNewScopeObject<true>(scriptContext, frameObject->GetDynamicType(), propIds, PropertyLetDefaults);
  6522. }
  6523. else
  6524. {
  6525. newType = PathTypeHandlerBase::CreateNewScopeObject<false>(scriptContext, frameObject->GetDynamicType(), propIds, PropertyLetDefaults);
  6526. }
  6527. }
  6528. else
  6529. {
  6530. newType = PathTypeHandlerBase::CreateNewScopeObject<false>(scriptContext, frameObject->GetDynamicType(), propIds);
  6531. }
  6532. int oldSlotCapacity = frameObject->GetDynamicType()->GetTypeHandler()->GetSlotCapacity();
  6533. int newSlotCapacity = newType->GetTypeHandler()->GetSlotCapacity();
  6534. __analysis_assume((uint32)newSlotCapacity >= formalsCount);
  6535. frameObject->EnsureSlots(oldSlotCapacity, newSlotCapacity, scriptContext, newType->GetTypeHandler());
  6536. frameObject->ReplaceType(newType);
  6537. }
  6538. if (argsObj && nonSimpleParamList)
  6539. {
  6540. return ConvertToUnmappedArguments(argsObj, actualsCount, paramAddr, frameObject, propIds, formalsCount, scriptContext);
  6541. }
  6542. for (i = 0; i < formalsCount && i < actualsCount; i++, tmpAddr++)
  6543. {
  6544. frameObject->SetSlot(SetSlotArguments(propIds != nullptr? propIds->elements[i] : Constants::NoProperty, i, *tmpAddr));
  6545. }
  6546. if (i < formalsCount)
  6547. {
  6548. // The formals that weren't passed still need to be put in the frame object so that
  6549. // their names will be found. Initialize them to "undefined".
  6550. for (; i < formalsCount; i++)
  6551. {
  6552. frameObject->SetSlot(SetSlotArguments(propIds != nullptr? propIds->elements[i] : Constants::NoProperty, i, scriptContext->GetLibrary()->GetUndefined()));
  6553. }
  6554. }
  6555. }
  6556. if (argsObj != nullptr)
  6557. {
  6558. // Transfer the unnamed actual arguments, if any, to the Arguments object itself.
  6559. for (i = formalsCount, tmpAddr = paramAddr + i; i < actualsCount; i++, tmpAddr++)
  6560. {
  6561. // ES5 10.6.11: use [[DefineOwnProperty]] semantics (instead of [[Put]]):
  6562. // do not check whether property is non-writable/etc in the prototype.
  6563. // ES3 semantics is same.
  6564. JavascriptOperators::SetItem(argsObj, argsObj, i, *tmpAddr, scriptContext, PropertyOperation_None, /* skipPrototypeCheck = */ TRUE);
  6565. }
  6566. if (funcCallee->IsStrictMode())
  6567. {
  6568. // If the formals are let decls, then we just overwrote the frame object slots with
  6569. // Undecl sentinels, and we can use the original arguments that were passed to the HeapArgumentsObject.
  6570. return argsObj->ConvertToUnmappedArgumentsObject(!nonSimpleParamList);
  6571. }
  6572. }
  6573. return argsObj;
  6574. }
  6575. HeapArgumentsObject *JavascriptOperators::CreateHeapArguments(JavascriptFunction *funcCallee, uint32 actualsCount, uint32 formalsCount, Var frameObj, ScriptContext* scriptContext)
  6576. {
  6577. JavascriptLibrary *library = scriptContext->GetLibrary();
  6578. HeapArgumentsObject *argsObj = library->CreateHeapArguments(frameObj, formalsCount, !!funcCallee->IsStrictMode());
  6579. #if DBG
  6580. DynamicTypeHandler* typeHandler = argsObj->GetTypeHandler();
  6581. #endif
  6582. //
  6583. // Set the number of arguments of Arguments Object
  6584. //
  6585. argsObj->SetNumberOfArguments(actualsCount);
  6586. JavascriptOperators::SetProperty(argsObj, argsObj, PropertyIds::length, JavascriptNumber::ToVar(actualsCount, scriptContext), scriptContext);
  6587. JavascriptOperators::SetProperty(argsObj, argsObj, PropertyIds::_symbolIterator, library->EnsureArrayPrototypeValuesFunction(), scriptContext);
  6588. if (funcCallee->IsStrictMode())
  6589. {
  6590. JavascriptFunction* restrictedPropertyAccessor = library->GetThrowTypeErrorRestrictedPropertyAccessorFunction();
  6591. argsObj->SetAccessors(PropertyIds::callee, restrictedPropertyAccessor, restrictedPropertyAccessor, PropertyOperation_NonFixedValue);
  6592. }
  6593. else
  6594. {
  6595. JavascriptOperators::SetProperty(argsObj, argsObj, PropertyIds::callee,
  6596. StackScriptFunction::EnsureBoxed(BOX_PARAM(funcCallee, nullptr, _u("callee"))), scriptContext);
  6597. }
  6598. AssertMsg(argsObj->GetTypeHandler() == typeHandler || scriptContext->IsScriptContextInDebugMode(), "type handler should not transition because we initialized it correctly");
  6599. return argsObj;
  6600. }
  6601. Var JavascriptOperators::OP_NewScopeObject(ScriptContext* scriptContext)
  6602. {
  6603. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(OP_NewScopeObject);
  6604. return scriptContext->GetLibrary()->CreateActivationObject();
  6605. JIT_HELPER_END(OP_NewScopeObject);
  6606. }
  6607. Var JavascriptOperators::OP_NewScopeObjectWithFormals(ScriptContext* scriptContext, FunctionBody * calleeBody, bool nonSimpleParamList)
  6608. {
  6609. JIT_HELPER_NOT_REENTRANT_HEADER(OP_NewScopeObjectWithFormals, reentrancylock, scriptContext->GetThreadContext());
  6610. Js::ActivationObject * frameObject = (ActivationObject*)OP_NewScopeObject(scriptContext);
  6611. // No fixed fields for formal parameters of the arguments object. Also, mark all fields as initialized up-front, because
  6612. // we will set them directly using SetSlot below, so the type handler will not have a chance to mark them as initialized later.
  6613. // CONSIDER : When we delay type sharing until the second instance is created, pass an argument indicating we want the types
  6614. // 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
  6615. // is ready for storing values directly to slots.
  6616. DynamicType* newType = nullptr;
  6617. if (nonSimpleParamList)
  6618. {
  6619. if (calleeBody->HasReferenceableBuiltInArguments())
  6620. {
  6621. newType = PathTypeHandlerBase::CreateNewScopeObject<true>(scriptContext, frameObject->GetDynamicType(), calleeBody->GetFormalsPropIdArray(), PropertyLetDefaults);
  6622. }
  6623. else
  6624. {
  6625. newType = PathTypeHandlerBase::CreateNewScopeObject<false>(scriptContext, frameObject->GetDynamicType(), calleeBody->GetFormalsPropIdArray(), PropertyLetDefaults);
  6626. }
  6627. }
  6628. else
  6629. {
  6630. newType = PathTypeHandlerBase::CreateNewScopeObject<false>(scriptContext, frameObject->GetDynamicType(), calleeBody->GetFormalsPropIdArray());
  6631. }
  6632. int oldSlotCapacity = frameObject->GetDynamicType()->GetTypeHandler()->GetSlotCapacity();
  6633. int newSlotCapacity = newType->GetTypeHandler()->GetSlotCapacity();
  6634. frameObject->EnsureSlots(oldSlotCapacity, newSlotCapacity, scriptContext, newType->GetTypeHandler());
  6635. frameObject->ReplaceType(newType);
  6636. return frameObject;
  6637. JIT_HELPER_END(OP_NewScopeObjectWithFormals);
  6638. }
  6639. Field(Var)* JavascriptOperators::OP_NewScopeSlots(unsigned int size, ScriptContext *scriptContext, Var scope)
  6640. {
  6641. JIT_HELPER_NOT_REENTRANT_HEADER(OP_NewScopeSlots, reentrancylock, scriptContext->GetThreadContext());
  6642. Assert(size > ScopeSlots::FirstSlotIndex); // Should never see empty slot array
  6643. Field(Var)* slotArray = RecyclerNewArray(scriptContext->GetRecycler(), Field(Var), size); // last initialized slot contains reference to array of propertyIds, correspondent to objects in previous slots
  6644. uint count = size - ScopeSlots::FirstSlotIndex;
  6645. ScopeSlots slots(slotArray);
  6646. slots.SetCount(count);
  6647. AssertMsg(!FunctionBody::Is(scope), "Scope should only be FunctionInfo or DebuggerScope, not FunctionBody");
  6648. slots.SetScopeMetadata(scope);
  6649. Var undef = scriptContext->GetLibrary()->GetUndefined();
  6650. for (unsigned int i = 0; i < count; i++)
  6651. {
  6652. slots.Set(i, undef);
  6653. }
  6654. return slotArray;
  6655. JIT_HELPER_END(OP_NewScopeSlots);
  6656. }
  6657. Field(Var)* JavascriptOperators::OP_NewScopeSlotsWithoutPropIds(unsigned int count, int scopeIndex, ScriptContext *scriptContext, FunctionBody *functionBody)
  6658. {
  6659. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(OP_NewScopeSlotsWithoutPropIds);
  6660. DebuggerScope* scope = reinterpret_cast<DebuggerScope*>(Constants::FunctionBodyUnavailable);
  6661. if (scopeIndex != DebuggerScope::InvalidScopeIndex)
  6662. {
  6663. AssertMsg(functionBody->GetScopeObjectChain(), "A scope chain should always be created when there are new scope slots for blocks.");
  6664. scope = functionBody->GetScopeObjectChain()->pScopeChain->Item(scopeIndex);
  6665. }
  6666. return OP_NewScopeSlots(count, scriptContext, scope);
  6667. JIT_HELPER_END(OP_NewScopeSlotsWithoutPropIds);
  6668. }
  6669. Field(Var)* JavascriptOperators::OP_CloneScopeSlots(Field(Var) *slotArray, ScriptContext *scriptContext)
  6670. {
  6671. JIT_HELPER_NOT_REENTRANT_HEADER(OP_CloneInnerScopeSlots, reentrancylock, scriptContext->GetThreadContext());
  6672. ScopeSlots slots(slotArray);
  6673. uint size = ScopeSlots::FirstSlotIndex + static_cast<uint>(slots.GetCount());
  6674. Field(Var)* slotArrayClone = RecyclerNewArray(scriptContext->GetRecycler(), Field(Var), size);
  6675. CopyArray(slotArrayClone, size, slotArray, size);
  6676. return slotArrayClone;
  6677. JIT_HELPER_END(OP_CloneInnerScopeSlots);
  6678. }
  6679. Var JavascriptOperators::OP_NewPseudoScope(ScriptContext *scriptContext)
  6680. {
  6681. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(OP_NewPseudoScope);
  6682. return scriptContext->GetLibrary()->CreatePseudoActivationObject();
  6683. JIT_HELPER_END(OP_NewPseudoScope);
  6684. }
  6685. Var JavascriptOperators::OP_NewBlockScope(ScriptContext *scriptContext)
  6686. {
  6687. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(OP_NewBlockScope);
  6688. return scriptContext->GetLibrary()->CreateBlockActivationObject();
  6689. JIT_HELPER_END(OP_NewBlockScope);
  6690. }
  6691. Var JavascriptOperators::OP_CloneBlockScope(BlockActivationObject *blockScope, ScriptContext *scriptContext)
  6692. {
  6693. JIT_HELPER_NOT_REENTRANT_HEADER(OP_CloneBlockScope, reentrancylock, scriptContext->GetThreadContext());
  6694. return blockScope->Clone(scriptContext);
  6695. JIT_HELPER_END(OP_CloneBlockScope);
  6696. }
  6697. Var JavascriptOperators::OP_IsInst(Var instance, Var aClass, ScriptContext* scriptContext, IsInstInlineCache* inlineCache)
  6698. {
  6699. JIT_HELPER_REENTRANT_HEADER(ScrObj_OP_IsInst);
  6700. if (!RecyclableObject::Is(aClass))
  6701. {
  6702. JavascriptError::ThrowTypeError(scriptContext, JSERR_Operand_Invalid_NeedFunction, _u("instanceof"));
  6703. }
  6704. RecyclableObject* constructor = RecyclableObject::FromVar(aClass);
  6705. if (scriptContext->GetConfig()->IsES6HasInstanceEnabled())
  6706. {
  6707. Var instOfHandler = JavascriptOperators::GetPropertyNoCache(constructor,
  6708. PropertyIds::_symbolHasInstance, scriptContext);
  6709. if (JavascriptOperators::IsUndefinedObject(instOfHandler)
  6710. || instOfHandler == scriptContext->GetBuiltInLibraryFunction(JavascriptFunction::EntryInfo::SymbolHasInstance.GetOriginalEntryPoint()))
  6711. {
  6712. return JavascriptBoolean::ToVar(constructor->HasInstance(instance, scriptContext, inlineCache), scriptContext);
  6713. }
  6714. else
  6715. {
  6716. if (!JavascriptConversion::IsCallable(instOfHandler))
  6717. {
  6718. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, _u("Symbol[Symbol.hasInstance]"));
  6719. }
  6720. ThreadContext * threadContext = scriptContext->GetThreadContext();
  6721. RecyclableObject *instFunc = RecyclableObject::FromVar(instOfHandler);
  6722. Var result = threadContext->ExecuteImplicitCall(instFunc, ImplicitCall_Accessor, [=]()->Js::Var
  6723. {
  6724. return CALL_FUNCTION(scriptContext->GetThreadContext(), instFunc, CallInfo(CallFlags_Value, 2), constructor, instance);
  6725. });
  6726. return JavascriptBoolean::ToVar(JavascriptConversion::ToBoolean(result, scriptContext) ? TRUE : FALSE, scriptContext);
  6727. }
  6728. }
  6729. else
  6730. {
  6731. return JavascriptBoolean::ToVar(constructor->HasInstance(instance, scriptContext, inlineCache), scriptContext);
  6732. }
  6733. JIT_HELPER_END(ScrObj_OP_IsInst);
  6734. }
  6735. void JavascriptOperators::OP_InitClass(Var constructor, Var extends, ScriptContext * scriptContext)
  6736. {
  6737. JIT_HELPER_REENTRANT_HEADER(OP_InitClass);
  6738. if (JavascriptOperators::GetTypeId(constructor) != Js::TypeId::TypeIds_Function)
  6739. {
  6740. JavascriptError::ThrowTypeError(scriptContext, JSERR_Operand_Invalid_NeedFunction, _u("class"));
  6741. }
  6742. RecyclableObject * ctor = RecyclableObject::FromVar(constructor);
  6743. if (extends)
  6744. {
  6745. switch (JavascriptOperators::GetTypeId(extends))
  6746. {
  6747. case Js::TypeId::TypeIds_Null:
  6748. {
  6749. Var ctorProto = JavascriptOperators::GetProperty(constructor, ctor, Js::PropertyIds::prototype, scriptContext);
  6750. RecyclableObject * ctorProtoObj = RecyclableObject::FromVar(ctorProto);
  6751. ctorProtoObj->SetPrototype(RecyclableObject::FromVar(extends));
  6752. ctorProtoObj->EnsureProperty(Js::PropertyIds::constructor);
  6753. ctorProtoObj->SetEnumerable(Js::PropertyIds::constructor, FALSE);
  6754. break;
  6755. }
  6756. default:
  6757. {
  6758. if (!RecyclableObject::Is(extends))
  6759. {
  6760. JavascriptError::ThrowTypeError(scriptContext, JSERR_ErrorOnNew);
  6761. }
  6762. RecyclableObject * extendsObj = RecyclableObject::FromVar(extends);
  6763. if (!JavascriptOperators::IsConstructor(extendsObj))
  6764. {
  6765. JavascriptError::ThrowTypeError(scriptContext, JSERR_ErrorOnNew);
  6766. }
  6767. if (!extendsObj->HasProperty(Js::PropertyIds::prototype))
  6768. {
  6769. JavascriptError::ThrowTypeError(scriptContext, JSERR_InvalidPrototype);
  6770. }
  6771. Var extendsProto = JavascriptOperators::GetPropertyNoCache(extends, extendsObj, Js::PropertyIds::prototype, scriptContext);
  6772. uint extendsProtoTypeId = JavascriptOperators::GetTypeId(extendsProto);
  6773. if (extendsProtoTypeId <= Js::TypeId::TypeIds_LastJavascriptPrimitiveType && extendsProtoTypeId != Js::TypeId::TypeIds_Null)
  6774. {
  6775. JavascriptError::ThrowTypeError(scriptContext, JSERR_InvalidPrototype);
  6776. }
  6777. Var ctorProto = JavascriptOperators::GetPropertyNoCache(constructor, ctor, Js::PropertyIds::prototype, scriptContext);
  6778. RecyclableObject * ctorProtoObj = RecyclableObject::FromVar(ctorProto);
  6779. ctorProtoObj->SetPrototype(RecyclableObject::FromVar(extendsProto));
  6780. ctorProtoObj->EnsureProperty(Js::PropertyIds::constructor);
  6781. ctorProtoObj->SetEnumerable(Js::PropertyIds::constructor, FALSE);
  6782. Var protoCtor = JavascriptOperators::GetPropertyNoCache(ctorProto, ctorProtoObj, Js::PropertyIds::constructor, scriptContext);
  6783. RecyclableObject * protoCtorObj = RecyclableObject::FromVar(protoCtor);
  6784. protoCtorObj->SetPrototype(extendsObj);
  6785. break;
  6786. }
  6787. }
  6788. }
  6789. Var proto = JavascriptOperators::GetProperty(constructor, ctor, Js::PropertyIds::prototype, scriptContext);
  6790. JavascriptOperators::OP_SetHomeObj(constructor, proto);
  6791. JIT_HELPER_END(OP_InitClass);
  6792. }
  6793. void JavascriptOperators::OP_LoadUndefinedToElement(Var instance, PropertyId propertyId)
  6794. {
  6795. JIT_HELPER_NOT_REENTRANT_HEADER(Op_LdElemUndef, reentrancylock, RecyclableObject::FromVar(instance)->GetScriptContext()->GetThreadContext());
  6796. AssertMsg(!TaggedNumber::Is(instance), "Invalid scope/root object");
  6797. JavascriptOperators::EnsureProperty(instance, propertyId);
  6798. JIT_HELPER_END(Op_LdElemUndef);
  6799. }
  6800. void JavascriptOperators::OP_LoadUndefinedToElementScoped(FrameDisplay *pScope, PropertyId propertyId, Var defaultInstance, ScriptContext* scriptContext)
  6801. {
  6802. JIT_HELPER_NOT_REENTRANT_HEADER(Op_LdElemUndefScoped, reentrancylock, scriptContext->GetThreadContext());
  6803. int i;
  6804. int length = pScope->GetLength();
  6805. Var argInstance;
  6806. for (i = 0; i < length; i++)
  6807. {
  6808. argInstance = pScope->GetItem(i);
  6809. if (JavascriptOperators::EnsureProperty(argInstance, propertyId))
  6810. {
  6811. return;
  6812. }
  6813. }
  6814. if (!JavascriptOperators::HasOwnPropertyNoHostObject(defaultInstance, propertyId))
  6815. {
  6816. // CONSIDER : Consider adding pre-initialization support to activation objects.
  6817. JavascriptOperators::OP_InitPropertyScoped(pScope, propertyId, scriptContext->GetLibrary()->GetUndefined(), defaultInstance, scriptContext);
  6818. }
  6819. JIT_HELPER_END(Op_LdElemUndefScoped);
  6820. }
  6821. void JavascriptOperators::OP_LoadUndefinedToElementDynamic(Var instance, PropertyId propertyId, ScriptContext *scriptContext)
  6822. {
  6823. JIT_HELPER_NOT_REENTRANT_HEADER(Op_LdElemUndefDynamic, reentrancylock, scriptContext->GetThreadContext());
  6824. if (!JavascriptOperators::HasOwnPropertyNoHostObject(instance, propertyId))
  6825. {
  6826. RecyclableObject::FromVar(instance)->InitPropertyScoped(propertyId, scriptContext->GetLibrary()->GetUndefined());
  6827. }
  6828. JIT_HELPER_END(Op_LdElemUndefDynamic);
  6829. }
  6830. BOOL JavascriptOperators::EnsureProperty(Var instance, PropertyId propertyId)
  6831. {
  6832. RecyclableObject *obj = RecyclableObject::FromVar(instance);
  6833. return (obj && obj->EnsureProperty(propertyId));
  6834. }
  6835. void JavascriptOperators::OP_EnsureNoRootProperty(Var instance, PropertyId propertyId)
  6836. {
  6837. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_EnsureNoRootProperty);
  6838. Assert(RootObjectBase::Is(instance));
  6839. RootObjectBase *obj = RootObjectBase::FromVar(instance);
  6840. obj->EnsureNoProperty(propertyId);
  6841. JIT_HELPER_END(Op_EnsureNoRootProperty);
  6842. }
  6843. void JavascriptOperators::OP_EnsureNoRootRedeclProperty(Var instance, PropertyId propertyId)
  6844. {
  6845. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_EnsureNoRootRedeclProperty);
  6846. Assert(RootObjectBase::Is(instance));
  6847. RecyclableObject *obj = RecyclableObject::FromVar(instance);
  6848. obj->EnsureNoRedeclProperty(propertyId);
  6849. JIT_HELPER_END(Op_EnsureNoRootRedeclProperty);
  6850. }
  6851. void JavascriptOperators::OP_ScopedEnsureNoRedeclProperty(FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance)
  6852. {
  6853. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_EnsureNoRedeclPropertyScoped);
  6854. int i;
  6855. int length = pDisplay->GetLength();
  6856. RecyclableObject *object;
  6857. for (i = 0; i < length; i++)
  6858. {
  6859. object = RecyclableObject::FromVar(pDisplay->GetItem(i));
  6860. if (object->EnsureNoRedeclProperty(propertyId))
  6861. {
  6862. return;
  6863. }
  6864. }
  6865. object = RecyclableObject::FromVar(defaultInstance);
  6866. object->EnsureNoRedeclProperty(propertyId);
  6867. JIT_HELPER_END(Op_EnsureNoRedeclPropertyScoped);
  6868. }
  6869. Var JavascriptOperators::IsIn(Var argProperty, Var instance, ScriptContext* scriptContext)
  6870. {
  6871. JIT_HELPER_REENTRANT_HEADER(Op_IsIn);
  6872. // Note that the fact that we haven't seen a given name before doesn't mean that the instance doesn't
  6873. if (!IsObject(instance))
  6874. {
  6875. JavascriptError::ThrowTypeError(scriptContext, JSERR_Operand_Invalid_NeedObject, _u("in"));
  6876. }
  6877. RecyclableObject* object = RecyclableObject::FromVar(instance);
  6878. BOOL result;
  6879. PropertyRecord const * propertyRecord = nullptr;
  6880. uint32 index;
  6881. IndexType indexType;
  6882. // Fast path for JavascriptSymbols and PropertyStrings
  6883. RecyclableObject* cacheOwner;
  6884. PropertyRecordUsageCache* propertyRecordUsageCache;
  6885. if (GetPropertyRecordUsageCache(argProperty, scriptContext, &propertyRecordUsageCache, &cacheOwner))
  6886. {
  6887. Var value;
  6888. propertyRecord = propertyRecordUsageCache->GetPropertyRecord();
  6889. if (!propertyRecord->IsNumeric())
  6890. {
  6891. PropertyValueInfo info;
  6892. if (propertyRecordUsageCache->TryGetPropertyFromCache<false /* OwnPropertyOnly */, true /* OutputExistence */, false /* ReturnOperationInfo */>(instance, object, &value, scriptContext, &info, cacheOwner, nullptr))
  6893. {
  6894. Assert(JavascriptBoolean::Is(value));
  6895. return value;
  6896. }
  6897. result = JavascriptOperators::GetPropertyWPCache<true /* OutputExistence */>(instance, object, propertyRecordUsageCache->GetPropertyRecord()->GetPropertyId(), &value, scriptContext, &info);
  6898. Assert(value == JavascriptBoolean::ToVar(result, scriptContext));
  6899. return value;
  6900. }
  6901. // We don't cache numeric property lookups, so fall through to the IndexType_Number case
  6902. index = propertyRecord->GetNumericValue();
  6903. indexType = IndexType_Number;
  6904. }
  6905. else
  6906. {
  6907. indexType = GetIndexType(argProperty, scriptContext, &index, &propertyRecord, true);
  6908. }
  6909. if (indexType == IndexType_Number)
  6910. {
  6911. result = JavascriptOperators::HasItem(object, index);
  6912. }
  6913. else
  6914. {
  6915. result = JavascriptOperators::HasProperty(object, propertyRecord->GetPropertyId());
  6916. #ifdef TELEMETRY_JSO
  6917. {
  6918. Assert(indexType != Js::IndexType_JavascriptString);
  6919. if (indexType == Js::IndexType_PropertyId)
  6920. {
  6921. scriptContext->GetTelemetry().GetOpcodeTelemetry().IsIn(instance, propertyId, result != 0);
  6922. }
  6923. }
  6924. #endif
  6925. }
  6926. return JavascriptBoolean::ToVar(result, scriptContext);
  6927. JIT_HELPER_END(Op_IsIn);
  6928. }
  6929. template <bool IsFromFullJit, class TInlineCache>
  6930. inline Var JavascriptOperators::PatchGetValue(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  6931. {
  6932. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetValue);
  6933. return PatchGetValueWithThisPtr<IsFromFullJit, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, instance);
  6934. JIT_HELPER_END(Op_PatchGetValue);
  6935. }
  6936. JIT_HELPER_TEMPLATE(Op_PatchGetValue, Op_PatchGetValuePolymorphic)
  6937. template <bool IsFromFullJit, class TInlineCache>
  6938. __forceinline Var JavascriptOperators::PatchGetValueWithThisPtr(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var thisInstance)
  6939. {
  6940. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetValueWithThisPtr);
  6941. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  6942. Assert(Js::JavascriptStackWalker::ValidateTopJitFrame(scriptContext));
  6943. RecyclableObject* object = nullptr;
  6944. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  6945. {
  6946. if (scriptContext->GetThreadContext()->RecordImplicitException())
  6947. {
  6948. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined,
  6949. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  6950. }
  6951. else
  6952. {
  6953. return scriptContext->GetLibrary()->GetUndefined();
  6954. }
  6955. }
  6956. PropertyValueInfo info;
  6957. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  6958. Var value;
  6959. if (CacheOperators::TryGetProperty<true, true, true, true, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  6960. instance, false, object, propertyId, &value, scriptContext, nullptr, &info))
  6961. {
  6962. return value;
  6963. }
  6964. #if DBG_DUMP
  6965. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  6966. {
  6967. CacheOperators::TraceCache(inlineCache, _u("PatchGetValue"), propertyId, scriptContext, object);
  6968. }
  6969. #endif
  6970. return JavascriptOperators::GetProperty(thisInstance, object, propertyId, scriptContext, &info);
  6971. JIT_HELPER_END(Op_PatchGetValueWithThisPtr);
  6972. }
  6973. JIT_HELPER_TEMPLATE(Op_PatchGetValueWithThisPtr, Op_PatchGetValuePolymorphicWithThisPtr)
  6974. template Var JavascriptOperators::PatchGetValue<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  6975. template Var JavascriptOperators::PatchGetValue<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  6976. template Var JavascriptOperators::PatchGetValue<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  6977. template Var JavascriptOperators::PatchGetValue<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  6978. template Var JavascriptOperators::PatchGetValueWithThisPtr<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var thisInstance);
  6979. template Var JavascriptOperators::PatchGetValueWithThisPtr<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var thisInstance);
  6980. template Var JavascriptOperators::PatchGetValueWithThisPtr<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var thisInstance);
  6981. template Var JavascriptOperators::PatchGetValueWithThisPtr<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var thisInstance);
  6982. template <bool IsFromFullJit, class TInlineCache>
  6983. Var JavascriptOperators::PatchGetValueForTypeOf(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  6984. {
  6985. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetValueForTypeOf);
  6986. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  6987. Assert(Js::JavascriptStackWalker::ValidateTopJitFrame(scriptContext));
  6988. RecyclableObject* object = nullptr;
  6989. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  6990. {
  6991. if (scriptContext->GetThreadContext()->RecordImplicitException())
  6992. {
  6993. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined,
  6994. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  6995. }
  6996. else
  6997. {
  6998. return scriptContext->GetLibrary()->GetUndefined();
  6999. }
  7000. }
  7001. PropertyValueInfo info;
  7002. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7003. Var value;
  7004. if (CacheOperators::TryGetProperty<true, true, true, true, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7005. instance, false, object, propertyId, &value, scriptContext, nullptr, &info))
  7006. {
  7007. return value;
  7008. }
  7009. #if DBG_DUMP
  7010. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7011. {
  7012. CacheOperators::TraceCache(inlineCache, _u("PatchGetValueForTypeOf"), propertyId, scriptContext, object);
  7013. }
  7014. #endif
  7015. Var prop = nullptr;
  7016. BEGIN_TYPEOF_ERROR_HANDLER(scriptContext);
  7017. prop = JavascriptOperators::GetProperty(instance, object, propertyId, scriptContext, &info);
  7018. END_TYPEOF_ERROR_HANDLER(scriptContext, prop);
  7019. return prop;
  7020. JIT_HELPER_END(Op_PatchGetValueForTypeOf);
  7021. }
  7022. JIT_HELPER_TEMPLATE(Op_PatchGetValueForTypeOf, Op_PatchGetValuePolymorphicForTypeOf)
  7023. template Var JavascriptOperators::PatchGetValueForTypeOf<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7024. template Var JavascriptOperators::PatchGetValueForTypeOf<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7025. template Var JavascriptOperators::PatchGetValueForTypeOf<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7026. template Var JavascriptOperators::PatchGetValueForTypeOf<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7027. Var JavascriptOperators::PatchGetValueUsingSpecifiedInlineCache(InlineCache * inlineCache, Var instance, RecyclableObject * object, PropertyId propertyId, ScriptContext* scriptContext)
  7028. {
  7029. PropertyValueInfo info;
  7030. PropertyValueInfo::SetCacheInfo(&info, inlineCache);
  7031. Var value;
  7032. if (CacheOperators::TryGetProperty<true, true, true, true, false, true, !InlineCache::IsPolymorphic, InlineCache::IsPolymorphic, false, false>(
  7033. instance, false, object, propertyId, &value, scriptContext, nullptr, &info))
  7034. {
  7035. return value;
  7036. }
  7037. #if DBG_DUMP
  7038. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7039. {
  7040. CacheOperators::TraceCache(inlineCache, _u("PatchGetValue"), propertyId, scriptContext, object);
  7041. }
  7042. #endif
  7043. return JavascriptOperators::GetProperty(instance, object, propertyId, scriptContext, &info);
  7044. }
  7045. Var JavascriptOperators::PatchGetValueNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7046. {
  7047. return PatchGetValueWithThisPtrNoFastPath(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, instance);
  7048. }
  7049. Var JavascriptOperators::PatchGetValueWithThisPtrNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var thisInstance)
  7050. {
  7051. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7052. RecyclableObject* object = nullptr;
  7053. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  7054. {
  7055. if (scriptContext->GetThreadContext()->RecordImplicitException())
  7056. {
  7057. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined,
  7058. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  7059. }
  7060. else
  7061. {
  7062. return scriptContext->GetLibrary()->GetUndefined();
  7063. }
  7064. }
  7065. PropertyValueInfo info;
  7066. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  7067. return JavascriptOperators::GetProperty(thisInstance, object, propertyId, scriptContext, &info);
  7068. }
  7069. template <bool IsFromFullJit, class TInlineCache>
  7070. inline Var JavascriptOperators::PatchGetRootValue(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId)
  7071. {
  7072. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetRootValue);
  7073. AssertMsg(RootObjectBase::Is(object), "Root must be a global object!");
  7074. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7075. PropertyValueInfo info;
  7076. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7077. Var value;
  7078. if (CacheOperators::TryGetProperty<true, true, true, false, true, false, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7079. object, true, object, propertyId, &value, scriptContext, nullptr, &info))
  7080. {
  7081. return value;
  7082. }
  7083. #if DBG_DUMP
  7084. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7085. {
  7086. CacheOperators::TraceCache(inlineCache, _u("PatchGetRootValue"), propertyId, scriptContext, object);
  7087. }
  7088. #endif
  7089. return JavascriptOperators::OP_GetRootProperty(object, propertyId, &info, scriptContext);
  7090. JIT_HELPER_END(Op_PatchGetRootValue);
  7091. }
  7092. JIT_HELPER_TEMPLATE(Op_PatchGetRootValue, Op_PatchGetRootValuePolymorphic)
  7093. template Var JavascriptOperators::PatchGetRootValue<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7094. template Var JavascriptOperators::PatchGetRootValue<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7095. template Var JavascriptOperators::PatchGetRootValue<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7096. template Var JavascriptOperators::PatchGetRootValue<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7097. template <bool IsFromFullJit, class TInlineCache>
  7098. Var JavascriptOperators::PatchGetRootValueForTypeOf(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId)
  7099. {
  7100. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetRootValueForTypeOf);
  7101. AssertMsg(RootObjectBase::Is(object), "Root must be a global object!");
  7102. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7103. PropertyValueInfo info;
  7104. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7105. Var value = nullptr;
  7106. if (CacheOperators::TryGetProperty<true, true, true, false, true, false, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7107. object, true, object, propertyId, &value, scriptContext, nullptr, &info))
  7108. {
  7109. return value;
  7110. }
  7111. #if DBG_DUMP
  7112. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7113. {
  7114. CacheOperators::TraceCache(inlineCache, _u("PatchGetRootValueForTypeOf"), propertyId, scriptContext, object);
  7115. }
  7116. #endif
  7117. value = nullptr;
  7118. BEGIN_TYPEOF_ERROR_HANDLER(scriptContext);
  7119. if (JavascriptOperators::GetRootProperty(RecyclableObject::FromVar(object), propertyId, &value, scriptContext, &info))
  7120. {
  7121. if (scriptContext->IsUndeclBlockVar(value))
  7122. {
  7123. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UseBeforeDeclaration);
  7124. }
  7125. return value;
  7126. }
  7127. END_TYPEOF_ERROR_HANDLER(scriptContext, value);
  7128. value = scriptContext->GetLibrary()->GetUndefined();
  7129. return value;
  7130. JIT_HELPER_END(Op_PatchGetRootValueForTypeOf);
  7131. }
  7132. JIT_HELPER_TEMPLATE(Op_PatchGetRootValueForTypeOf, Op_PatchGetRootValuePolymorphicForTypeOf)
  7133. template Var JavascriptOperators::PatchGetRootValueForTypeOf<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7134. template Var JavascriptOperators::PatchGetRootValueForTypeOf<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7135. template Var JavascriptOperators::PatchGetRootValueForTypeOf<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7136. template Var JavascriptOperators::PatchGetRootValueForTypeOf<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject * object, PropertyId propertyId);
  7137. Var JavascriptOperators::PatchGetRootValueNoFastPath_Var(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7138. {
  7139. return
  7140. PatchGetRootValueNoFastPath(
  7141. functionBody,
  7142. inlineCache,
  7143. inlineCacheIndex,
  7144. DynamicObject::FromVar(instance),
  7145. propertyId);
  7146. }
  7147. Var JavascriptOperators::PatchGetRootValueNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId)
  7148. {
  7149. AssertMsg(RootObjectBase::Is(object), "Root must be a global object!");
  7150. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7151. PropertyValueInfo info;
  7152. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  7153. return JavascriptOperators::OP_GetRootProperty(object, propertyId, &info, scriptContext);
  7154. }
  7155. template <bool IsFromFullJit, class TInlineCache>
  7156. inline Var JavascriptOperators::PatchGetPropertyScoped(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance)
  7157. {
  7158. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetPropertyScoped);
  7159. // Get the property, using a scope stack rather than an individual instance.
  7160. // Walk the stack until we find an instance that has the property.
  7161. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7162. uint16 length = pDisplay->GetLength();
  7163. PropertyValueInfo info;
  7164. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7165. for (uint16 i = 0; i < length; i++)
  7166. {
  7167. RecyclableObject* object = RecyclableObject::UnsafeFromVar(pDisplay->GetItem(i));
  7168. Var value;
  7169. if (CacheOperators::TryGetProperty<true, true, true, false, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7170. object, false, object, propertyId, &value, scriptContext, nullptr, &info))
  7171. {
  7172. return value;
  7173. }
  7174. #if DBG_DUMP
  7175. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7176. {
  7177. CacheOperators::TraceCache(inlineCache, _u("PatchGetPropertyScoped"), propertyId, scriptContext, object);
  7178. }
  7179. #endif
  7180. if (JavascriptOperators::GetProperty(object, propertyId, &value, scriptContext, &info))
  7181. {
  7182. if (scriptContext->IsUndeclBlockVar(value) && propertyId != PropertyIds::_this)
  7183. {
  7184. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UseBeforeDeclaration);
  7185. }
  7186. return value;
  7187. }
  7188. }
  7189. // There is no root decl for 'this', we should instead load the global 'this' value.
  7190. if (propertyId == PropertyIds::_this)
  7191. {
  7192. Var varNull = OP_LdNull(scriptContext);
  7193. return JavascriptOperators::OP_GetThis(varNull, functionBody->GetModuleID(), scriptContext);
  7194. }
  7195. else if (propertyId == PropertyIds::_super)
  7196. {
  7197. JavascriptError::ThrowReferenceError(scriptContext, JSERR_BadSuperReference);
  7198. }
  7199. // No one in the scope stack has the property, so get it from the default instance provided by the caller.
  7200. Var value = JavascriptOperators::PatchGetRootValue<IsFromFullJit>(functionBody, inlineCache, inlineCacheIndex, DynamicObject::FromVar(defaultInstance), propertyId);
  7201. if (scriptContext->IsUndeclBlockVar(value))
  7202. {
  7203. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UseBeforeDeclaration);
  7204. }
  7205. return value;
  7206. JIT_HELPER_END(Op_PatchGetPropertyScoped);
  7207. }
  7208. template Var JavascriptOperators::PatchGetPropertyScoped<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7209. template Var JavascriptOperators::PatchGetPropertyScoped<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7210. template Var JavascriptOperators::PatchGetPropertyScoped<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7211. template Var JavascriptOperators::PatchGetPropertyScoped<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7212. template <bool IsFromFullJit, class TInlineCache>
  7213. Var JavascriptOperators::PatchGetPropertyForTypeOfScoped(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance)
  7214. {
  7215. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetPropertyForTypeOfScoped);
  7216. Var value = nullptr;
  7217. ScriptContext *scriptContext = functionBody->GetScriptContext();
  7218. BEGIN_TYPEOF_ERROR_HANDLER(scriptContext);
  7219. value = JavascriptOperators::PatchGetPropertyScoped<IsFromFullJit, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, pDisplay, propertyId, defaultInstance);
  7220. END_TYPEOF_ERROR_HANDLER(scriptContext, value)
  7221. return value;
  7222. JIT_HELPER_END(Op_PatchGetPropertyForTypeOfScoped);
  7223. }
  7224. template Var JavascriptOperators::PatchGetPropertyForTypeOfScoped<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7225. template Var JavascriptOperators::PatchGetPropertyForTypeOfScoped<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7226. template Var JavascriptOperators::PatchGetPropertyForTypeOfScoped<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7227. template Var JavascriptOperators::PatchGetPropertyForTypeOfScoped<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, FrameDisplay *pDisplay, PropertyId propertyId, Var defaultInstance);
  7228. template <bool IsFromFullJit, class TInlineCache>
  7229. inline Var JavascriptOperators::PatchGetMethod(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7230. {
  7231. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetMethod);
  7232. Assert(inlineCache != nullptr);
  7233. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7234. RecyclableObject* object = nullptr;
  7235. #if ENABLE_COPYONACCESS_ARRAY
  7236. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  7237. #endif
  7238. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  7239. {
  7240. // Don't error if we disabled implicit calls
  7241. if (scriptContext->GetThreadContext()->RecordImplicitException())
  7242. {
  7243. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined,
  7244. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  7245. }
  7246. else
  7247. {
  7248. #ifdef TELEMETRY_JSO
  7249. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  7250. {
  7251. // `successful` will be true as PatchGetMethod throws an exception if not found.
  7252. scriptContext->GetTelemetry().GetOpcodeTelemetry().GetMethodProperty(object, propertyId, value, /*successful:*/false);
  7253. }
  7254. #endif
  7255. return scriptContext->GetLibrary()->GetUndefined();
  7256. }
  7257. }
  7258. PropertyValueInfo info;
  7259. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7260. Var value;
  7261. if (CacheOperators::TryGetProperty<true, true, true, false, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7262. instance, false, object, propertyId, &value, scriptContext, nullptr, &info))
  7263. {
  7264. return value;
  7265. }
  7266. #if DBG_DUMP
  7267. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7268. {
  7269. CacheOperators::TraceCache(inlineCache, _u("PatchGetMethod"), propertyId, scriptContext, object);
  7270. }
  7271. #endif
  7272. value = Js::JavascriptOperators::PatchGetMethodFromObject(instance, object, propertyId, &info, scriptContext, false);
  7273. #ifdef TELEMETRY_JSO
  7274. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  7275. {
  7276. // `successful` will be true as PatchGetMethod throws an exception if not found.
  7277. scriptContext->GetTelemetry().GetOpcodeTelemetry().GetMethodProperty(object, propertyId, value, /*successful:*/true);
  7278. }
  7279. #endif
  7280. return value;
  7281. JIT_HELPER_END(Op_PatchGetMethod);
  7282. }
  7283. JIT_HELPER_TEMPLATE(Op_PatchGetMethod, Op_PatchGetMethodPolymorphic)
  7284. template Var JavascriptOperators::PatchGetMethod<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7285. template Var JavascriptOperators::PatchGetMethod<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7286. template Var JavascriptOperators::PatchGetMethod<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7287. template Var JavascriptOperators::PatchGetMethod<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7288. template <bool IsFromFullJit, class TInlineCache>
  7289. inline Var JavascriptOperators::PatchGetRootMethod(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId)
  7290. {
  7291. JIT_HELPER_REENTRANT_HEADER(Op_PatchGetRootMethod);
  7292. Assert(inlineCache != nullptr);
  7293. AssertMsg(RootObjectBase::Is(object), "Root must be a global object!");
  7294. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7295. PropertyValueInfo info;
  7296. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7297. Var value;
  7298. if (CacheOperators::TryGetProperty<true, true, true, false, true, false, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7299. object, true, object, propertyId, &value, scriptContext, nullptr, &info))
  7300. {
  7301. return value;
  7302. }
  7303. #if DBG_DUMP
  7304. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7305. {
  7306. CacheOperators::TraceCache(inlineCache, _u("PatchGetRootMethod"), propertyId, scriptContext, object);
  7307. }
  7308. #endif
  7309. value = Js::JavascriptOperators::PatchGetMethodFromObject(object, object, propertyId, &info, scriptContext, true);
  7310. #ifdef TELEMETRY_JSO
  7311. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  7312. {
  7313. // `successful` will be true as PatchGetMethod throws an exception if not found.
  7314. scriptContext->GetTelemetry().GetOpcodeTelemetry().GetMethodProperty(object, propertyId, value, /*successful:*/ true);
  7315. }
  7316. #endif
  7317. return value;
  7318. JIT_HELPER_END(Op_PatchGetRootMethod);
  7319. }
  7320. JIT_HELPER_TEMPLATE(Op_PatchGetRootMethod, Op_PatchGetRootMethodPolymorphic)
  7321. template Var JavascriptOperators::PatchGetRootMethod<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId);
  7322. template Var JavascriptOperators::PatchGetRootMethod<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId);
  7323. template Var JavascriptOperators::PatchGetRootMethod<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId);
  7324. template Var JavascriptOperators::PatchGetRootMethod<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId);
  7325. template <bool IsFromFullJit, class TInlineCache>
  7326. inline Var JavascriptOperators::PatchScopedGetMethod(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7327. {
  7328. JIT_HELPER_REENTRANT_HEADER(Op_ScopedGetMethod);
  7329. Assert(inlineCache != nullptr);
  7330. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7331. RecyclableObject* object = nullptr;
  7332. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  7333. {
  7334. // Don't error if we disabled implicit calls
  7335. if (scriptContext->GetThreadContext()->RecordImplicitException())
  7336. {
  7337. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined,
  7338. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  7339. }
  7340. else
  7341. {
  7342. return scriptContext->GetLibrary()->GetUndefined();
  7343. }
  7344. }
  7345. PropertyValueInfo info;
  7346. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7347. const bool isRoot = RootObjectBase::Is(object);
  7348. Var value;
  7349. if (CacheOperators::TryGetProperty<true, true, true, false, true, false, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false, false>(
  7350. instance, isRoot, object, propertyId, &value, scriptContext, nullptr, &info))
  7351. {
  7352. return value;
  7353. }
  7354. #if DBG_DUMP
  7355. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7356. {
  7357. CacheOperators::TraceCache(inlineCache, _u("PatchGetMethod"), propertyId, scriptContext, object);
  7358. }
  7359. #endif
  7360. return Js::JavascriptOperators::PatchGetMethodFromObject(instance, object, propertyId, &info, scriptContext, isRoot);
  7361. JIT_HELPER_END(Op_ScopedGetMethod);
  7362. }
  7363. JIT_HELPER_TEMPLATE(Op_ScopedGetMethod, Op_ScopedGetMethodPolymorphic)
  7364. template Var JavascriptOperators::PatchScopedGetMethod<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7365. template Var JavascriptOperators::PatchScopedGetMethod<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7366. template Var JavascriptOperators::PatchScopedGetMethod<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7367. template Var JavascriptOperators::PatchScopedGetMethod<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId);
  7368. Var JavascriptOperators::PatchGetMethodNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7369. {
  7370. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7371. RecyclableObject* object = nullptr;
  7372. if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
  7373. {
  7374. // Don't error if we disabled implicit calls
  7375. if (scriptContext->GetThreadContext()->RecordImplicitException())
  7376. {
  7377. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined,
  7378. scriptContext->GetPropertyName(propertyId)->GetBuffer());
  7379. }
  7380. else
  7381. {
  7382. return scriptContext->GetLibrary()->GetUndefined();
  7383. }
  7384. }
  7385. PropertyValueInfo info;
  7386. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  7387. return Js::JavascriptOperators::PatchGetMethodFromObject(instance, object, propertyId, &info, scriptContext, false);
  7388. }
  7389. Var JavascriptOperators::PatchGetRootMethodNoFastPath_Var(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId)
  7390. {
  7391. return
  7392. PatchGetRootMethodNoFastPath(
  7393. functionBody,
  7394. inlineCache,
  7395. inlineCacheIndex,
  7396. DynamicObject::FromVar(instance),
  7397. propertyId);
  7398. }
  7399. Var JavascriptOperators::PatchGetRootMethodNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, DynamicObject* object, PropertyId propertyId)
  7400. {
  7401. AssertMsg(RootObjectBase::Is(object), "Root must be a global object!");
  7402. PropertyValueInfo info;
  7403. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  7404. return Js::JavascriptOperators::PatchGetMethodFromObject(object, object, propertyId, &info, functionBody->GetScriptContext(), true);
  7405. }
  7406. Var JavascriptOperators::PatchGetMethodFromObject(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, PropertyValueInfo * info, ScriptContext* scriptContext, bool isRootLd)
  7407. {
  7408. Assert(IsPropertyObject(propertyObject));
  7409. Var value = nullptr;
  7410. BOOL foundValue = FALSE;
  7411. if (isRootLd)
  7412. {
  7413. RootObjectBase* rootObject = RootObjectBase::FromVar(instance);
  7414. foundValue = JavascriptOperators::GetRootPropertyReference(rootObject, propertyId, &value, scriptContext, info);
  7415. }
  7416. else
  7417. {
  7418. foundValue = JavascriptOperators::GetPropertyReference(instance, propertyObject, propertyId, &value, scriptContext, info);
  7419. }
  7420. if (!foundValue)
  7421. {
  7422. // Don't error if we disabled implicit calls
  7423. if (scriptContext->GetThreadContext()->RecordImplicitException())
  7424. {
  7425. const char16* propertyName = scriptContext->GetPropertyName(propertyId)->GetBuffer();
  7426. value = scriptContext->GetLibrary()->GetUndefined();
  7427. JavascriptFunction * caller = NULL;
  7428. if (JavascriptStackWalker::GetCaller(&caller, scriptContext))
  7429. {
  7430. FunctionBody * callerBody = caller->GetFunctionBody();
  7431. if (callerBody && callerBody->GetUtf8SourceInfo()->GetIsXDomain())
  7432. {
  7433. propertyName = NULL;
  7434. }
  7435. }
  7436. // Prior to version 12 we had mistakenly immediately thrown an error for property reference method calls
  7437. // (i.e. <expr>.foo() form) when the target object is the global object. The spec says that a GetValue
  7438. // on a reference should throw if the reference is unresolved, of which a property reference can never be,
  7439. // however it can be unresolved in the case of an identifier expression, e.g. foo() with no qualification.
  7440. // Such a case would come down to the global object if foo was undefined, hence the check for root object,
  7441. // except that it should have been a check for isRootLd to be correct.
  7442. //
  7443. // // (at global scope)
  7444. // foo(x());
  7445. //
  7446. // should throw an error before evaluating x() if foo is not defined, but
  7447. //
  7448. // // (at global scope)
  7449. // this.foo(x());
  7450. //
  7451. // should evaluate x() before throwing an error if foo is not a property on the global object.
  7452. // Maintain old behavior prior to version 12.
  7453. bool isPropertyReference = !isRootLd;
  7454. if (!isPropertyReference)
  7455. {
  7456. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UndefVariable, propertyName);
  7457. }
  7458. else
  7459. {
  7460. // 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
  7461. // first (#3). Postpone throwing error to invoke time.
  7462. value = ThrowErrorObject::CreateThrowTypeErrorObject(scriptContext, VBSERR_OLENoPropOrMethod, propertyName);
  7463. }
  7464. }
  7465. }
  7466. return value;
  7467. }
  7468. template <bool IsFromFullJit, class TInlineCache>
  7469. inline void JavascriptOperators::PatchPutValue(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7470. {
  7471. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValue);
  7472. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValue, Op_PatchPutValueWithThisPtr);
  7473. PatchPutValueWithThisPtr<IsFromFullJit, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, instance, flags);
  7474. JIT_HELPER_END(Op_PatchPutValue);
  7475. }
  7476. JIT_HELPER_TEMPLATE(Op_PatchPutValue, Op_PatchPutValuePolymorphic)
  7477. template <bool IsFromFullJit, class TInlineCache>
  7478. inline void JavascriptOperators::PatchPutValueWithThisPtr(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags)
  7479. {
  7480. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueWithThisPtr);
  7481. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7482. if (TaggedNumber::Is(instance))
  7483. {
  7484. JavascriptOperators::SetPropertyOnTaggedNumber(instance, nullptr, propertyId, newValue, scriptContext, flags);
  7485. return;
  7486. }
  7487. #if ENABLE_COPYONACCESS_ARRAY
  7488. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  7489. #endif
  7490. RecyclableObject* object = RecyclableObject::FromVar(instance);
  7491. PropertyValueInfo info;
  7492. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7493. if (CacheOperators::TrySetProperty<true, true, true, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false>(
  7494. object, false, propertyId, newValue, scriptContext, flags, nullptr, &info))
  7495. {
  7496. return;
  7497. }
  7498. #if DBG_DUMP
  7499. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7500. {
  7501. CacheOperators::TraceCache(inlineCache, _u("PatchPutValue"), propertyId, scriptContext, object);
  7502. }
  7503. #endif
  7504. ImplicitCallFlags prevImplicitCallFlags = ImplicitCall_None;
  7505. ImplicitCallFlags currImplicitCallFlags = ImplicitCall_None;
  7506. bool hasThisOnlyStatements = functionBody->GetHasOnlyThisStmts();
  7507. if (hasThisOnlyStatements)
  7508. {
  7509. prevImplicitCallFlags = CacheAndClearImplicitBit(scriptContext);
  7510. }
  7511. if (!JavascriptOperators::OP_SetProperty(object, propertyId, newValue, scriptContext, &info, flags, thisInstance))
  7512. {
  7513. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  7514. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  7515. }
  7516. if (hasThisOnlyStatements)
  7517. {
  7518. currImplicitCallFlags = CheckAndUpdateFunctionBodyWithImplicitFlag(functionBody);
  7519. RestoreImplicitFlag(scriptContext, prevImplicitCallFlags, currImplicitCallFlags);
  7520. }
  7521. JIT_HELPER_END(Op_PatchPutValueWithThisPtr);
  7522. }
  7523. JIT_HELPER_TEMPLATE(Op_PatchPutValueWithThisPtr, Op_PatchPutValueWithThisPtrPolymorphic)
  7524. template void JavascriptOperators::PatchPutValue<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7525. template void JavascriptOperators::PatchPutValue<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7526. template void JavascriptOperators::PatchPutValue<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7527. template void JavascriptOperators::PatchPutValue<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7528. template <bool IsFromFullJit, class TInlineCache>
  7529. inline void JavascriptOperators::PatchPutRootValue(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7530. {
  7531. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutRootValue);
  7532. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7533. RecyclableObject* object = RecyclableObject::FromVar(instance);
  7534. PropertyValueInfo info;
  7535. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7536. if (CacheOperators::TrySetProperty<true, true, true, true, false, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false>(
  7537. object, true, propertyId, newValue, scriptContext, flags, nullptr, &info))
  7538. {
  7539. return;
  7540. }
  7541. #if DBG_DUMP
  7542. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7543. {
  7544. CacheOperators::TraceCache(inlineCache, _u("PatchPutRootValue"), propertyId, scriptContext, object);
  7545. }
  7546. #endif
  7547. ImplicitCallFlags prevImplicitCallFlags = ImplicitCall_None;
  7548. ImplicitCallFlags currImplicitCallFlags = ImplicitCall_None;
  7549. bool hasThisOnlyStatements = functionBody->GetHasOnlyThisStmts();
  7550. if (hasThisOnlyStatements)
  7551. {
  7552. prevImplicitCallFlags = CacheAndClearImplicitBit(scriptContext);
  7553. }
  7554. if (!JavascriptOperators::SetRootProperty(object, propertyId, newValue, &info, scriptContext, flags))
  7555. {
  7556. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  7557. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  7558. }
  7559. if (hasThisOnlyStatements)
  7560. {
  7561. currImplicitCallFlags = CheckAndUpdateFunctionBodyWithImplicitFlag(functionBody);
  7562. RestoreImplicitFlag(scriptContext, prevImplicitCallFlags, currImplicitCallFlags);
  7563. }
  7564. JIT_HELPER_END(Op_PatchPutRootValue);
  7565. }
  7566. JIT_HELPER_TEMPLATE(Op_PatchPutRootValue, Op_PatchPutRootValuePolymorphic)
  7567. template void JavascriptOperators::PatchPutRootValue<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7568. template void JavascriptOperators::PatchPutRootValue<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7569. template void JavascriptOperators::PatchPutRootValue<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7570. template void JavascriptOperators::PatchPutRootValue<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7571. template <bool IsFromFullJit, class TInlineCache>
  7572. inline void JavascriptOperators::PatchPutValueNoLocalFastPath(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7573. {
  7574. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueNoLocalFastPath);
  7575. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValueNoLocalFastPath, Op_PatchPutValueWithThisPtrNoLocalFastPath);
  7576. PatchPutValueWithThisPtrNoLocalFastPath<IsFromFullJit, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, instance, flags);
  7577. JIT_HELPER_END(Op_PatchPutValueNoLocalFastPath);
  7578. }
  7579. JIT_HELPER_TEMPLATE(Op_PatchPutValueNoLocalFastPath, Op_PatchPutValueNoLocalFastPathPolymorphic)
  7580. template void JavascriptOperators::PatchPutValueNoLocalFastPath<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7581. template void JavascriptOperators::PatchPutValueNoLocalFastPath<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7582. template void JavascriptOperators::PatchPutValueNoLocalFastPath<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7583. template void JavascriptOperators::PatchPutValueNoLocalFastPath<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7584. template <bool IsFromFullJit, class TInlineCache>
  7585. inline void JavascriptOperators::PatchPutValueWithThisPtrNoLocalFastPath(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags)
  7586. {
  7587. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueWithThisPtrNoLocalFastPath);
  7588. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7589. if (TaggedNumber::Is(instance))
  7590. {
  7591. JavascriptOperators::SetPropertyOnTaggedNumber(instance,
  7592. nullptr,
  7593. propertyId,
  7594. newValue,
  7595. scriptContext,
  7596. flags);
  7597. return;
  7598. }
  7599. #if ENABLE_COPYONACCESS_ARRAY
  7600. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  7601. #endif
  7602. RecyclableObject *object = RecyclableObject::UnsafeFromVar(instance);
  7603. PropertyValueInfo info;
  7604. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7605. if (CacheOperators::TrySetProperty<!TInlineCache::IsPolymorphic, true, true, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false>(
  7606. object, false, propertyId, newValue, scriptContext, flags, nullptr, &info))
  7607. {
  7608. return;
  7609. }
  7610. #if DBG_DUMP
  7611. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7612. {
  7613. CacheOperators::TraceCache(inlineCache, _u("PatchPutValueNoLocalFastPath"), propertyId, scriptContext, object);
  7614. }
  7615. #endif
  7616. ImplicitCallFlags prevImplicitCallFlags = ImplicitCall_None;
  7617. ImplicitCallFlags currImplicitCallFlags = ImplicitCall_None;
  7618. bool hasThisOnlyStatements = functionBody->GetHasOnlyThisStmts();
  7619. if (hasThisOnlyStatements)
  7620. {
  7621. prevImplicitCallFlags = CacheAndClearImplicitBit(scriptContext);
  7622. }
  7623. if (!JavascriptOperators::OP_SetProperty(instance, propertyId, newValue, scriptContext, &info, flags, thisInstance))
  7624. {
  7625. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  7626. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  7627. }
  7628. if (hasThisOnlyStatements)
  7629. {
  7630. currImplicitCallFlags = CheckAndUpdateFunctionBodyWithImplicitFlag(functionBody);
  7631. RestoreImplicitFlag(scriptContext, prevImplicitCallFlags, currImplicitCallFlags);
  7632. }
  7633. JIT_HELPER_END(Op_PatchPutValueWithThisPtrNoLocalFastPath);
  7634. }
  7635. JIT_HELPER_TEMPLATE(Op_PatchPutValueWithThisPtrNoLocalFastPath, Op_PatchPutValueWithThisPtrNoLocalFastPathPolymorphic)
  7636. 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);
  7637. 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);
  7638. 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);
  7639. 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);
  7640. template <bool IsFromFullJit, class TInlineCache>
  7641. inline void JavascriptOperators::PatchPutRootValueNoLocalFastPath(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7642. {
  7643. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutRootValueNoLocalFastPath);
  7644. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7645. RecyclableObject *object = RecyclableObject::FromVar(instance);
  7646. PropertyValueInfo info;
  7647. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7648. if (CacheOperators::TrySetProperty<!TInlineCache::IsPolymorphic, true, true, true, false, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false>(
  7649. object, true, propertyId, newValue, scriptContext, flags, nullptr, &info))
  7650. {
  7651. return;
  7652. }
  7653. #if DBG_DUMP
  7654. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7655. {
  7656. CacheOperators::TraceCache(inlineCache, _u("PatchPutRootValueNoLocalFastPath"), propertyId, scriptContext, object);
  7657. }
  7658. #endif
  7659. ImplicitCallFlags prevImplicitCallFlags = ImplicitCall_None;
  7660. ImplicitCallFlags currImplicitCallFlags = ImplicitCall_None;
  7661. bool hasThisOnlyStatements = functionBody->GetHasOnlyThisStmts();
  7662. if (hasThisOnlyStatements)
  7663. {
  7664. prevImplicitCallFlags = CacheAndClearImplicitBit(scriptContext);
  7665. }
  7666. if (!JavascriptOperators::SetRootProperty(object, propertyId, newValue, &info, scriptContext, flags))
  7667. {
  7668. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  7669. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  7670. }
  7671. if (hasThisOnlyStatements)
  7672. {
  7673. currImplicitCallFlags = CheckAndUpdateFunctionBodyWithImplicitFlag(functionBody);
  7674. RestoreImplicitFlag(scriptContext, prevImplicitCallFlags, currImplicitCallFlags);
  7675. }
  7676. JIT_HELPER_END(Op_PatchPutRootValueNoLocalFastPath);
  7677. }
  7678. JIT_HELPER_TEMPLATE(Op_PatchPutRootValueNoLocalFastPath, Op_PatchPutRootValueNoLocalFastPathPolymorphic)
  7679. template void JavascriptOperators::PatchPutRootValueNoLocalFastPath<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7680. template void JavascriptOperators::PatchPutRootValueNoLocalFastPath<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7681. template void JavascriptOperators::PatchPutRootValueNoLocalFastPath<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7682. template void JavascriptOperators::PatchPutRootValueNoLocalFastPath<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7683. void JavascriptOperators::PatchPutValueNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7684. {
  7685. PatchPutValueWithThisPtrNoFastPath(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, instance, flags);
  7686. }
  7687. void JavascriptOperators::PatchPutValueWithThisPtrNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags)
  7688. {
  7689. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7690. if (TaggedNumber::Is(instance))
  7691. {
  7692. JavascriptOperators::SetPropertyOnTaggedNumber(instance, nullptr, propertyId, newValue, scriptContext, flags);
  7693. return;
  7694. }
  7695. RecyclableObject* object = RecyclableObject::FromVar(instance);
  7696. PropertyValueInfo info;
  7697. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  7698. if (!JavascriptOperators::OP_SetProperty(object, propertyId, newValue, scriptContext, &info, flags, thisInstance))
  7699. {
  7700. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  7701. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  7702. }
  7703. }
  7704. void JavascriptOperators::PatchPutRootValueNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7705. {
  7706. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7707. RecyclableObject* object = RecyclableObject::FromVar(instance);
  7708. PropertyValueInfo info;
  7709. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  7710. if (!JavascriptOperators::SetRootProperty(object, propertyId, newValue, &info, scriptContext, flags))
  7711. {
  7712. // Add implicit call flags, to bail out if field copy prop may propagate the wrong value.
  7713. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
  7714. }
  7715. }
  7716. template <class TInlineCache>
  7717. inline bool JavascriptOperators::PatchPutValueCantChangeType(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7718. {
  7719. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueCantChangeType);
  7720. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValueCantChangeType, Op_PatchPutValue);
  7721. Type * oldType = DynamicObject::Is(instance) ? DynamicObject::FromVar(instance)->GetType() : nullptr;
  7722. PatchPutValueWithThisPtr<true, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, instance, flags);
  7723. return (oldType != nullptr && oldType != DynamicObject::FromVar(instance)->GetType());
  7724. JIT_HELPER_END(Op_PatchPutValueCantChangeType);
  7725. }
  7726. JIT_HELPER_TEMPLATE(Op_PatchPutValueCantChangeType, Op_PatchPutValuePolymorphicCantChangeType);
  7727. template bool JavascriptOperators::PatchPutValueCantChangeType<InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7728. template bool JavascriptOperators::PatchPutValueCantChangeType<PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7729. template <class TInlineCache>
  7730. inline bool JavascriptOperators::PatchPutValueWithThisPtrCantChangeType(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags)
  7731. {
  7732. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueWithThisPtrCantChangeType);
  7733. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValueWithThisPtrCantChangeType, Op_PatchPutValueWithThisPtr);
  7734. Type * oldType = DynamicObject::Is(instance) ? DynamicObject::FromVar(instance)->GetType() : nullptr;
  7735. PatchPutValueWithThisPtr<true, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, thisInstance, flags);
  7736. return (oldType != nullptr && oldType != DynamicObject::FromVar(instance)->GetType());
  7737. JIT_HELPER_END(Op_PatchPutValueWithThisPtrCantChangeType);
  7738. }
  7739. JIT_HELPER_TEMPLATE(Op_PatchPutValueWithThisPtrCantChangeType, Op_PatchPutValueWithThisPtrPolymorphicCantChangeType);
  7740. template bool JavascriptOperators::PatchPutValueWithThisPtrCantChangeType<InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags);
  7741. template bool JavascriptOperators::PatchPutValueWithThisPtrCantChangeType<PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags);
  7742. template <class TInlineCache>
  7743. inline bool JavascriptOperators::PatchPutValueNoLocalFastPathCantChangeType(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7744. {
  7745. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueNoLocalFastPathCantChangeType);
  7746. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValueNoLocalFastPathCantChangeType, Op_PatchPutValueNoLocalFastPath);
  7747. Type * oldType = DynamicObject::Is(instance) ? DynamicObject::FromVar(instance)->GetType() : nullptr;
  7748. PatchPutValueWithThisPtrNoLocalFastPath<true, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, instance, flags);
  7749. return (oldType != nullptr && oldType != DynamicObject::FromVar(instance)->GetType());
  7750. JIT_HELPER_END(Op_PatchPutValueNoLocalFastPathCantChangeType);
  7751. }
  7752. JIT_HELPER_TEMPLATE(Op_PatchPutValueNoLocalFastPathCantChangeType, Op_PatchPutValueNoLocalFastPathPolymorphicCantChangeType);
  7753. template bool JavascriptOperators::PatchPutValueNoLocalFastPathCantChangeType<InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7754. template bool JavascriptOperators::PatchPutValueNoLocalFastPathCantChangeType<PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7755. template <class TInlineCache>
  7756. inline bool JavascriptOperators::PatchPutValueWithThisPtrNoLocalFastPathCantChangeType(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags)
  7757. {
  7758. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueWithThisPtrNoLocalFastPathCantChangeType);
  7759. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValueWithThisPtrNoLocalFastPathCantChangeType, Op_PatchPutValueWithThisPtrNoLocalFastPath);
  7760. Type * oldType = DynamicObject::Is(instance) ? DynamicObject::FromVar(instance)->GetType() : nullptr;
  7761. PatchPutValueWithThisPtrNoLocalFastPath<true, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, thisInstance, flags);
  7762. return (oldType != nullptr && oldType != DynamicObject::FromVar(instance)->GetType());
  7763. JIT_HELPER_END(Op_PatchPutValueWithThisPtrNoLocalFastPathCantChangeType);
  7764. }
  7765. JIT_HELPER_TEMPLATE(Op_PatchPutValueWithThisPtrNoLocalFastPathCantChangeType, Op_PatchPutValueWithThisPtrNoLocalFastPathPolymorphicCantChangeType);
  7766. template bool JavascriptOperators::PatchPutValueWithThisPtrNoLocalFastPathCantChangeType<InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags);
  7767. template bool JavascriptOperators::PatchPutValueWithThisPtrNoLocalFastPathCantChangeType<PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags);
  7768. template <class TInlineCache>
  7769. inline bool JavascriptOperators::PatchInitValueCantChangeType(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue)
  7770. {
  7771. JIT_HELPER_REENTRANT_HEADER(Op_PatchInitValueCantChangeType);
  7772. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchInitValueCantChangeType, Op_PatchInitValue);
  7773. Type * oldType = DynamicObject::Is(object) ? DynamicObject::FromVar(object)->GetType() : nullptr;
  7774. PatchInitValue<true, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, object, propertyId, newValue);
  7775. return (oldType != nullptr && oldType != DynamicObject::FromVar(object)->GetType());
  7776. JIT_HELPER_END(Op_PatchInitValueCantChangeType);
  7777. }
  7778. JIT_HELPER_TEMPLATE(Op_PatchInitValueCantChangeType, Op_PatchInitValuePolymorphicCantChangeType);
  7779. template bool JavascriptOperators::PatchInitValueCantChangeType<InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue);
  7780. template bool JavascriptOperators::PatchInitValueCantChangeType<PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue);
  7781. template <class TInlineCache>
  7782. inline bool JavascriptOperators::PatchPutValueCheckLayout(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7783. {
  7784. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueCheckLayout);
  7785. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValueCheckLayout, Op_PatchPutValue);
  7786. DynamicTypeHandler * oldTypeHandler = DynamicObject::Is(instance) ? DynamicObject::FromVar(instance)->GetTypeHandler() : nullptr;
  7787. PatchPutValueWithThisPtr<true, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, instance, flags);
  7788. return (oldTypeHandler != nullptr && LayoutChanged(DynamicObject::FromVar(instance), oldTypeHandler));
  7789. JIT_HELPER_END(Op_PatchPutValueCheckLayout);
  7790. }
  7791. JIT_HELPER_TEMPLATE(Op_PatchPutValueCheckLayout, Op_PatchPutValuePolymorphicCheckLayout);
  7792. template bool JavascriptOperators::PatchPutValueCheckLayout<InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7793. template bool JavascriptOperators::PatchPutValueCheckLayout<PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7794. template <class TInlineCache>
  7795. inline bool JavascriptOperators::PatchPutValueWithThisPtrCheckLayout(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags)
  7796. {
  7797. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueWithThisPtrCheckLayout);
  7798. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValueWithThisPtrCheckLayout, Op_PatchPutValueWithThisPtr);
  7799. DynamicTypeHandler * oldTypeHandler = DynamicObject::Is(instance) ? DynamicObject::FromVar(instance)->GetTypeHandler() : nullptr;
  7800. PatchPutValueWithThisPtr<true, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, thisInstance, flags);
  7801. return (oldTypeHandler != nullptr && LayoutChanged(DynamicObject::FromVar(instance), oldTypeHandler));
  7802. JIT_HELPER_END(Op_PatchPutValueWithThisPtrCheckLayout);
  7803. }
  7804. JIT_HELPER_TEMPLATE(Op_PatchPutValueWithThisPtrCheckLayout, Op_PatchPutValueWithThisPtrPolymorphicCheckLayout);
  7805. template bool JavascriptOperators::PatchPutValueWithThisPtrCheckLayout<InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags);
  7806. template bool JavascriptOperators::PatchPutValueWithThisPtrCheckLayout<PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags);
  7807. template <class TInlineCache>
  7808. inline bool JavascriptOperators::PatchPutValueNoLocalFastPathCheckLayout(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags)
  7809. {
  7810. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueNoLocalFastPathCheckLayout);
  7811. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValueNoLocalFastPathCheckLayout, Op_PatchPutValueNoLocalFastPath);
  7812. DynamicTypeHandler * oldTypeHandler = DynamicObject::Is(instance) ? DynamicObject::FromVar(instance)->GetTypeHandler() : nullptr;
  7813. PatchPutValueWithThisPtrNoLocalFastPath<true, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, instance, flags);
  7814. return (oldTypeHandler != nullptr && LayoutChanged(DynamicObject::FromVar(instance), oldTypeHandler));
  7815. JIT_HELPER_END(Op_PatchPutValueNoLocalFastPathCheckLayout);
  7816. }
  7817. JIT_HELPER_TEMPLATE(Op_PatchPutValueNoLocalFastPathCheckLayout, Op_PatchPutValueNoLocalFastPathPolymorphicCheckLayout);
  7818. template bool JavascriptOperators::PatchPutValueNoLocalFastPathCheckLayout<InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7819. template bool JavascriptOperators::PatchPutValueNoLocalFastPathCheckLayout<PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, PropertyOperationFlags flags);
  7820. template <class TInlineCache>
  7821. inline bool JavascriptOperators::PatchPutValueWithThisPtrNoLocalFastPathCheckLayout(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags)
  7822. {
  7823. JIT_HELPER_REENTRANT_HEADER(Op_PatchPutValueWithThisPtrNoLocalFastPathCheckLayout);
  7824. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchPutValueWithThisPtrNoLocalFastPathCheckLayout, Op_PatchPutValueWithThisPtrNoLocalFastPath);
  7825. DynamicTypeHandler * oldTypeHandler = DynamicObject::Is(instance) ? DynamicObject::FromVar(instance)->GetTypeHandler() : nullptr;
  7826. PatchPutValueWithThisPtrNoLocalFastPath<true, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, instance, propertyId, newValue, thisInstance, flags);
  7827. return (oldTypeHandler != nullptr && LayoutChanged(DynamicObject::FromVar(instance), oldTypeHandler));
  7828. JIT_HELPER_END(Op_PatchPutValueWithThisPtrNoLocalFastPathCheckLayout);
  7829. }
  7830. JIT_HELPER_TEMPLATE(Op_PatchPutValueWithThisPtrNoLocalFastPathCheckLayout, Op_PatchPutValueWithThisPtrNoLocalFastPathPolymorphicCheckLayout);
  7831. template bool JavascriptOperators::PatchPutValueWithThisPtrNoLocalFastPathCheckLayout<InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags);
  7832. template bool JavascriptOperators::PatchPutValueWithThisPtrNoLocalFastPathCheckLayout<PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, Var instance, PropertyId propertyId, Var newValue, Var thisInstance, PropertyOperationFlags flags);
  7833. template <class TInlineCache>
  7834. inline bool JavascriptOperators::PatchInitValueCheckLayout(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue)
  7835. {
  7836. JIT_HELPER_REENTRANT_HEADER(Op_PatchInitValueCheckLayout);
  7837. JIT_HELPER_SAME_ATTRIBUTES(Op_PatchInitValueCheckLayout, Op_PatchInitValue);
  7838. DynamicTypeHandler * oldTypeHandler = DynamicObject::Is(object) ? DynamicObject::FromVar(object)->GetTypeHandler() : nullptr;
  7839. PatchInitValue<true, TInlineCache>(functionBody, inlineCache, inlineCacheIndex, object, propertyId, newValue);
  7840. return (oldTypeHandler != nullptr && LayoutChanged(DynamicObject::FromVar(object), oldTypeHandler));
  7841. JIT_HELPER_END(Op_PatchInitValueCheckLayout);
  7842. }
  7843. JIT_HELPER_TEMPLATE(Op_PatchInitValueCheckLayout, Op_PatchInitValuePolymorphicCheckLayout);
  7844. template bool JavascriptOperators::PatchInitValueCheckLayout<InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue);
  7845. template bool JavascriptOperators::PatchInitValueCheckLayout<PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue);
  7846. bool JavascriptOperators::LayoutChanged(DynamicObject *const instance, DynamicTypeHandler *const oldTypeHandler)
  7847. {
  7848. DynamicTypeHandler * newTypeHandler = instance->GetTypeHandler();
  7849. return (oldTypeHandler != newTypeHandler &&
  7850. (oldTypeHandler->GetInlineSlotCapacity() != newTypeHandler->GetInlineSlotCapacity() ||
  7851. oldTypeHandler->GetOffsetOfInlineSlots() != newTypeHandler->GetOffsetOfInlineSlots()));
  7852. }
  7853. template <bool IsFromFullJit, class TInlineCache>
  7854. inline void JavascriptOperators::PatchInitValue(FunctionBody *const functionBody, TInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue)
  7855. {
  7856. JIT_HELPER_REENTRANT_HEADER(Op_PatchInitValue);
  7857. ScriptContext *const scriptContext = functionBody->GetScriptContext();
  7858. const PropertyOperationFlags flags = newValue == NULL ? PropertyOperation_SpecialValue : PropertyOperation_None;
  7859. PropertyValueInfo info;
  7860. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, !IsFromFullJit);
  7861. if (CacheOperators::TrySetProperty<true, true, false, true, true, !TInlineCache::IsPolymorphic, TInlineCache::IsPolymorphic, false>(
  7862. object, false, propertyId, newValue, scriptContext, flags, nullptr, &info))
  7863. {
  7864. return;
  7865. }
  7866. #if DBG_DUMP
  7867. if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
  7868. {
  7869. CacheOperators::TraceCache(inlineCache, _u("PatchInitValue"), propertyId, scriptContext, object);
  7870. }
  7871. #endif
  7872. Type *typeWithoutProperty = object->GetType();
  7873. if (functionBody->IsEval())
  7874. {
  7875. if (object->InitPropertyInEval(propertyId, newValue, flags, &info))
  7876. {
  7877. CacheOperators::CachePropertyWrite(object, false, typeWithoutProperty, propertyId, &info, scriptContext);
  7878. return;
  7879. }
  7880. }
  7881. // Ideally the lowerer would emit a call to the right flavor of PatchInitValue, so that we can ensure that we only
  7882. // ever initialize to NULL in the right cases. But the backend uses the StFld opcode for initialization, and it
  7883. // would be cumbersome to thread the different helper calls all the way down
  7884. if (object->InitProperty(propertyId, newValue, flags, &info))
  7885. {
  7886. CacheOperators::CachePropertyWrite(object, false, typeWithoutProperty, propertyId, &info, scriptContext);
  7887. }
  7888. JIT_HELPER_END(Op_PatchInitValue);
  7889. }
  7890. JIT_HELPER_TEMPLATE(Op_PatchInitValue, Op_PatchInitValuePolymorphic)
  7891. template void JavascriptOperators::PatchInitValue<false, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue);
  7892. template void JavascriptOperators::PatchInitValue<true, InlineCache>(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue);
  7893. template void JavascriptOperators::PatchInitValue<false, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue);
  7894. template void JavascriptOperators::PatchInitValue<true, PolymorphicInlineCache>(FunctionBody *const functionBody, PolymorphicInlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue);
  7895. void JavascriptOperators::PatchInitValueNoFastPath(FunctionBody *const functionBody, InlineCache *const inlineCache, const InlineCacheIndex inlineCacheIndex, RecyclableObject* object, PropertyId propertyId, Var newValue)
  7896. {
  7897. PropertyValueInfo info;
  7898. PropertyValueInfo::SetCacheInfo(&info, functionBody, inlineCache, inlineCacheIndex, true);
  7899. Type *typeWithoutProperty = object->GetType();
  7900. if (functionBody->IsEval())
  7901. {
  7902. if (object->InitPropertyInEval(propertyId, newValue, PropertyOperation_None, &info))
  7903. {
  7904. CacheOperators::CachePropertyWrite(object, false, typeWithoutProperty, propertyId, &info, functionBody->GetScriptContext());
  7905. return;
  7906. }
  7907. }
  7908. if (object->InitProperty(propertyId, newValue, PropertyOperation_None, &info))
  7909. {
  7910. CacheOperators::CachePropertyWrite(object, false, typeWithoutProperty, propertyId, &info, functionBody->GetScriptContext());
  7911. }
  7912. }
  7913. void JavascriptOperators::GetPropertyIdForInt(uint64 value, ScriptContext* scriptContext, PropertyRecord const ** propertyRecord)
  7914. {
  7915. char16 buffer[20];
  7916. ::_ui64tow_s(value, buffer, sizeof(buffer)/sizeof(char16), 10);
  7917. scriptContext->GetOrAddPropertyRecord(buffer, JavascriptString::GetBufferLength(buffer), propertyRecord);
  7918. }
  7919. void JavascriptOperators::GetPropertyIdForInt(uint32 value, ScriptContext* scriptContext, PropertyRecord const ** propertyRecord)
  7920. {
  7921. GetPropertyIdForInt(static_cast<uint64>(value), scriptContext, propertyRecord);
  7922. }
  7923. Var JavascriptOperators::FromPropertyDescriptor(const PropertyDescriptor& descriptor, ScriptContext* scriptContext)
  7924. {
  7925. DynamicObject* object = scriptContext->GetLibrary()->CreateObject();
  7926. // ES5 Section 8.10.4 specifies the order for adding these properties.
  7927. if (descriptor.IsDataDescriptor())
  7928. {
  7929. if (descriptor.ValueSpecified())
  7930. {
  7931. JavascriptOperators::InitProperty(object, PropertyIds::value, descriptor.GetValue());
  7932. }
  7933. JavascriptOperators::InitProperty(object, PropertyIds::writable, JavascriptBoolean::ToVar(descriptor.IsWritable(),scriptContext));
  7934. }
  7935. else if (descriptor.IsAccessorDescriptor())
  7936. {
  7937. JavascriptOperators::InitProperty(object, PropertyIds::get, JavascriptOperators::CanonicalizeAccessor(descriptor.GetGetter(), scriptContext));
  7938. JavascriptOperators::InitProperty(object, PropertyIds::set, JavascriptOperators::CanonicalizeAccessor(descriptor.GetSetter(), scriptContext));
  7939. }
  7940. if (descriptor.EnumerableSpecified())
  7941. {
  7942. JavascriptOperators::InitProperty(object, PropertyIds::enumerable, JavascriptBoolean::ToVar(descriptor.IsEnumerable(), scriptContext));
  7943. }
  7944. if (descriptor.ConfigurableSpecified())
  7945. {
  7946. JavascriptOperators::InitProperty(object, PropertyIds::configurable, JavascriptBoolean::ToVar(descriptor.IsConfigurable(), scriptContext));
  7947. }
  7948. return object;
  7949. }
  7950. // ES5 8.12.9 [[DefineOwnProperty]].
  7951. // Return value:
  7952. // - TRUE = success.
  7953. // - FALSE (can throw depending on throwOnError parameter) = unsuccessful.
  7954. BOOL JavascriptOperators::DefineOwnPropertyDescriptor(RecyclableObject* obj, PropertyId propId, const PropertyDescriptor& descriptor, bool throwOnError, ScriptContext* scriptContext)
  7955. {
  7956. Assert(obj);
  7957. Assert(scriptContext);
  7958. if (JavascriptProxy::Is(obj))
  7959. {
  7960. return JavascriptProxy::DefineOwnPropertyDescriptor(obj, propId, descriptor, throwOnError, scriptContext);
  7961. }
  7962. PropertyDescriptor currentDescriptor;
  7963. BOOL isCurrentDescriptorDefined = JavascriptOperators::GetOwnPropertyDescriptor(obj, propId, scriptContext, &currentDescriptor);
  7964. bool isExtensible = !!obj->IsExtensible();
  7965. return ValidateAndApplyPropertyDescriptor<true>(obj, propId, descriptor, isCurrentDescriptorDefined ? &currentDescriptor : nullptr, isExtensible, throwOnError, scriptContext);
  7966. }
  7967. BOOL JavascriptOperators::IsCompatiblePropertyDescriptor(const PropertyDescriptor& descriptor, PropertyDescriptor* currentDescriptor, bool isExtensible, bool throwOnError, ScriptContext* scriptContext)
  7968. {
  7969. return ValidateAndApplyPropertyDescriptor<false>(nullptr, Constants::NoProperty, descriptor, currentDescriptor, isExtensible, throwOnError, scriptContext);
  7970. }
  7971. template<bool needToSetProperty>
  7972. BOOL JavascriptOperators::ValidateAndApplyPropertyDescriptor(RecyclableObject* obj, PropertyId propId, const PropertyDescriptor& descriptor,
  7973. PropertyDescriptor* currentDescriptor, bool isExtensible, bool throwOnError, ScriptContext* scriptContext)
  7974. {
  7975. Var defaultDataValue = scriptContext->GetLibrary()->GetUndefined();
  7976. Var defaultAccessorValue = scriptContext->GetLibrary()->GetDefaultAccessorFunction();
  7977. if (currentDescriptor == nullptr)
  7978. {
  7979. if (!isExtensible) // ES5 8.12.9.3.
  7980. {
  7981. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotExtensible, propId);
  7982. }
  7983. else // ES5 8.12.9.4.
  7984. {
  7985. if (needToSetProperty)
  7986. {
  7987. if (descriptor.IsGenericDescriptor() || descriptor.IsDataDescriptor())
  7988. {
  7989. // ES5 8.12.9.4a: Create an own data property named P of object O whose [[Value]], [[Writable]],
  7990. // [[Enumerable]] and [[Configurable]] attribute values are described by Desc.
  7991. // If the value of an attribute field of Desc is absent, the attribute of the newly created property
  7992. // is set to its default value.
  7993. PropertyDescriptor filledDescriptor = FillMissingPropertyDescriptorFields<false>(descriptor, scriptContext);
  7994. BOOL tempResult = obj->SetPropertyWithAttributes(propId, filledDescriptor.GetValue(), filledDescriptor.GetAttributes(), nullptr);
  7995. if (!obj->IsExternal() && !tempResult)
  7996. {
  7997. Assert(TypedArrayBase::Is(obj)); // typed array returns false when canonical numeric index is not integer or out of range
  7998. return FALSE;
  7999. }
  8000. }
  8001. else
  8002. {
  8003. // ES5 8.12.9.4b: Create an own accessor property named P of object O whose [[Get]], [[Set]], [[Enumerable]]
  8004. // and [[Configurable]] attribute values are described by Desc. If the value of an attribute field of Desc is absent,
  8005. // the attribute of the newly created property is set to its default value.
  8006. Assert(descriptor.IsAccessorDescriptor());
  8007. PropertyDescriptor filledDescriptor = FillMissingPropertyDescriptorFields<true>(descriptor, scriptContext);
  8008. BOOL isSetAccessorsSuccess = obj->SetAccessors(propId, filledDescriptor.GetGetter(), filledDescriptor.GetSetter());
  8009. // It is valid for some objects to not-support getters and setters, specifically, for projection of an ABI method
  8010. // (CustomExternalObject => MapWithStringKey) which SetAccessors returns VBSErr_ActionNotSupported.
  8011. // But for non-external objects SetAccessors should succeed.
  8012. Assert(isSetAccessorsSuccess || obj->IsExternal());
  8013. // If SetAccessors failed, the property wasn't created, so no need to change the attributes.
  8014. if (isSetAccessorsSuccess)
  8015. {
  8016. JavascriptOperators::SetAttributes(obj, propId, filledDescriptor, true); // use 'force' as default attributes in type system are different from ES5.
  8017. }
  8018. }
  8019. }
  8020. return TRUE;
  8021. }
  8022. }
  8023. // ES5 8.12.9.5: Return true, if every field in Desc is absent.
  8024. if (!descriptor.ConfigurableSpecified() && !descriptor.EnumerableSpecified() && !descriptor.WritableSpecified() &&
  8025. !descriptor.ValueSpecified() && !descriptor.GetterSpecified() && !descriptor.SetterSpecified())
  8026. {
  8027. return TRUE;
  8028. }
  8029. // 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
  8030. // as the corresponding field in current when compared using the SameValue algorithm (9.12).
  8031. PropertyDescriptor filledDescriptor = descriptor.IsAccessorDescriptor() ? FillMissingPropertyDescriptorFields<true>(descriptor, scriptContext)
  8032. : FillMissingPropertyDescriptorFields<false>(descriptor, scriptContext);
  8033. if (JavascriptOperators::AreSamePropertyDescriptors(&filledDescriptor, currentDescriptor, scriptContext))
  8034. {
  8035. return TRUE;
  8036. }
  8037. if (!currentDescriptor->IsConfigurable()) // ES5 8.12.9.7.
  8038. {
  8039. if (descriptor.ConfigurableSpecified() && descriptor.IsConfigurable())
  8040. {
  8041. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotConfigurable, propId);
  8042. }
  8043. if (descriptor.EnumerableSpecified() && descriptor.IsEnumerable() != currentDescriptor->IsEnumerable())
  8044. {
  8045. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotConfigurable, propId);
  8046. }
  8047. }
  8048. // Whether to merge attributes from tempDescriptor into descriptor to keep original values
  8049. // of some attributes from the object/use tempDescriptor for SetAttributes, or just use descriptor.
  8050. // This is optimization to avoid 2 calls to SetAttributes.
  8051. bool mergeDescriptors = false;
  8052. // Whether to call SetAttributes with 'force' flag which forces setting all attributes
  8053. // rather than only specified or which have true values.
  8054. // This is to make sure that the object has correct attributes, as default values in the object are not for ES5.
  8055. bool forceSetAttributes = false;
  8056. PropertyDescriptor tempDescriptor;
  8057. // ES5 8.12.9.8: If IsGenericDescriptor(Desc) is true, then no further validation is required.
  8058. if (!descriptor.IsGenericDescriptor())
  8059. {
  8060. if (currentDescriptor->IsDataDescriptor() != descriptor.IsDataDescriptor())
  8061. {
  8062. // ES5 8.12.9.9: Else, if IsDataDescriptor(current) and IsDataDescriptor(Desc) have different results...
  8063. if (!currentDescriptor->IsConfigurable())
  8064. {
  8065. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotConfigurable, propId);
  8066. }
  8067. if (needToSetProperty)
  8068. {
  8069. if (currentDescriptor->IsDataDescriptor())
  8070. {
  8071. // ES5 8.12.9.9.b: Convert the property named P of object O from a data property to an accessor property.
  8072. // Preserve the existing values of the converted property's [[Configurable]] and [[Enumerable]] attributes
  8073. // and set the rest of the property's attributes to their default values.
  8074. PropertyAttributes preserveFromObject = currentDescriptor->GetAttributes() & (PropertyConfigurable | PropertyEnumerable);
  8075. BOOL isSetAccessorsSuccess = obj->SetAccessors(propId, defaultAccessorValue, defaultAccessorValue);
  8076. // It is valid for some objects to not-support getters and setters, specifically, for projection of an ABI method
  8077. // (CustomExternalObject => MapWithStringKey) which SetAccessors returns VBSErr_ActionNotSupported.
  8078. // But for non-external objects SetAccessors should succeed.
  8079. Assert(isSetAccessorsSuccess || obj->IsExternal());
  8080. if (isSetAccessorsSuccess)
  8081. {
  8082. tempDescriptor.SetAttributes(preserveFromObject, PropertyConfigurable | PropertyEnumerable);
  8083. forceSetAttributes = true; // use SetAttrbiutes with 'force' as default attributes in type system are different from ES5.
  8084. mergeDescriptors = true;
  8085. }
  8086. }
  8087. else
  8088. {
  8089. // ES5 8.12.9.9.c: Convert the property named P of object O from an accessor property to a data property.
  8090. // Preserve the existing values of the converted property's [[Configurable]] and [[Enumerable]] attributes
  8091. // and set the rest of the property's attributes to their default values.
  8092. // Note: avoid using SetProperty/SetPropertyWithAttributes here because they has undesired side-effects:
  8093. // it calls previous setter and in some cases of attribute values throws.
  8094. // To walk around, call DeleteProperty and then AddProperty.
  8095. PropertyAttributes preserveFromObject = currentDescriptor->GetAttributes() & (PropertyConfigurable | PropertyEnumerable);
  8096. tempDescriptor.SetAttributes(preserveFromObject, PropertyConfigurable | PropertyEnumerable);
  8097. tempDescriptor.MergeFrom(descriptor); // Update only fields specified in 'descriptor'.
  8098. Var descriptorValue = descriptor.ValueSpecified() ? descriptor.GetValue() : defaultDataValue;
  8099. // Note: HostDispath'es implementation of DeleteProperty currently throws E_NOTIMPL.
  8100. obj->DeleteProperty(propId, PropertyOperation_None);
  8101. BOOL tempResult = obj->SetPropertyWithAttributes(propId, descriptorValue, tempDescriptor.GetAttributes(), NULL, PropertyOperation_Force);
  8102. Assert(tempResult);
  8103. // At this time we already set value and attributes to desired values,
  8104. // thus we can skip step ES5 8.12.9.12 and simply return true.
  8105. return TRUE;
  8106. }
  8107. }
  8108. }
  8109. else if (currentDescriptor->IsDataDescriptor() && descriptor.IsDataDescriptor())
  8110. {
  8111. // ES5 8.12.9.10: Else, if IsDataDescriptor(current) and IsDataDescriptor(Desc) are both true...
  8112. if (!currentDescriptor->IsConfigurable())
  8113. {
  8114. if (!currentDescriptor->IsWritable())
  8115. {
  8116. if (descriptor.WritableSpecified() && descriptor.IsWritable()) // ES5 8.12.9.10.a.i
  8117. {
  8118. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotConfigurable, propId);
  8119. }
  8120. else if (descriptor.ValueSpecified() &&
  8121. !JavascriptConversion::SameValue(descriptor.GetValue(), currentDescriptor->GetValue())) // ES5 8.12.9.10.a.ii
  8122. {
  8123. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotWritable, propId);
  8124. }
  8125. }
  8126. }
  8127. // ES5 8.12.9.10.b: else, the [[Configurable]] field of current is true, so any change is acceptable.
  8128. }
  8129. else
  8130. {
  8131. // ES5 8.12.9.11: Else, IsAccessorDescriptor(current) and IsAccessorDescriptor(Desc) are both true, so...
  8132. Assert(currentDescriptor->IsAccessorDescriptor() && descriptor.IsAccessorDescriptor());
  8133. if (!currentDescriptor->IsConfigurable())
  8134. {
  8135. if ((descriptor.SetterSpecified() &&
  8136. !JavascriptConversion::SameValue(
  8137. JavascriptOperators::CanonicalizeAccessor(descriptor.GetSetter(), scriptContext),
  8138. JavascriptOperators::CanonicalizeAccessor(currentDescriptor->GetSetter(), scriptContext))) ||
  8139. (descriptor.GetterSpecified() &&
  8140. !JavascriptConversion::SameValue(
  8141. JavascriptOperators::CanonicalizeAccessor(descriptor.GetGetter(), scriptContext),
  8142. JavascriptOperators::CanonicalizeAccessor(currentDescriptor->GetGetter(), scriptContext))))
  8143. {
  8144. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotConfigurable, propId);
  8145. }
  8146. }
  8147. }
  8148. // This part is only for non-generic descriptors:
  8149. // ES5 8.12.9.12: For each attribute field of Desc that is present,
  8150. // set the correspondingly named attribute of the property named P of object O to the value of the field.
  8151. if (descriptor.IsDataDescriptor())
  8152. {
  8153. if (descriptor.ValueSpecified() && needToSetProperty)
  8154. {
  8155. // Set just the value by passing the current attributes of the property.
  8156. // If the property's attributes are also changing (perhaps becoming non-writable),
  8157. // this will be taken care of in the call to JavascriptOperators::SetAttributes below.
  8158. // Built-in Function.prototype properties 'length', 'arguments', and 'caller' are special cases.
  8159. BOOL tempResult = obj->SetPropertyWithAttributes(propId, descriptor.GetValue(), currentDescriptor->GetAttributes(), nullptr);
  8160. AssertMsg(tempResult || JavascriptFunction::IsBuiltinProperty(obj, propId), "If you hit this assert, most likely there is something wrong with the object/type.");
  8161. }
  8162. }
  8163. else if (descriptor.IsAccessorDescriptor() && needToSetProperty)
  8164. {
  8165. Assert(descriptor.GetterSpecified() || descriptor.SetterSpecified());
  8166. Var oldGetter = defaultAccessorValue, oldSetter = defaultAccessorValue;
  8167. if (!descriptor.GetterSpecified() || !descriptor.SetterSpecified())
  8168. {
  8169. // Unless both getter and setter are specified, make sure we don't overwrite old accessor.
  8170. #pragma prefast(suppress:6031, "We defaulted oldGetter and oldSetter already, so ignoring the return value here is safe")
  8171. obj->GetAccessors(propId, &oldGetter, &oldSetter, scriptContext);
  8172. }
  8173. Var getter = descriptor.GetterSpecified() ? descriptor.GetGetter() : oldGetter;
  8174. Var setter = descriptor.SetterSpecified() ? descriptor.GetSetter() : oldSetter;
  8175. obj->SetAccessors(propId, getter, setter);
  8176. }
  8177. } // if (!descriptor.IsGenericDescriptor())
  8178. // Continue for all descriptors including generic:
  8179. // ES5 8.12.9.12: For each attribute field of Desc that is present,
  8180. // set the correspondingly named attribute of the property named P of object O to the value of the field.
  8181. if (needToSetProperty)
  8182. {
  8183. if (mergeDescriptors)
  8184. {
  8185. tempDescriptor.MergeFrom(descriptor);
  8186. JavascriptOperators::SetAttributes(obj, propId, tempDescriptor, forceSetAttributes);
  8187. }
  8188. else
  8189. {
  8190. JavascriptOperators::SetAttributes(obj, propId, descriptor, forceSetAttributes);
  8191. }
  8192. }
  8193. return TRUE;
  8194. }
  8195. template <bool isAccessor>
  8196. PropertyDescriptor JavascriptOperators::FillMissingPropertyDescriptorFields(PropertyDescriptor descriptor, ScriptContext* scriptContext)
  8197. {
  8198. PropertyDescriptor newDescriptor;
  8199. const PropertyDescriptor* defaultDescriptor = scriptContext->GetLibrary()->GetDefaultPropertyDescriptor();
  8200. if (isAccessor)
  8201. {
  8202. newDescriptor.SetGetter(descriptor.GetterSpecified() ? descriptor.GetGetter() : defaultDescriptor->GetGetter());
  8203. newDescriptor.SetSetter(descriptor.SetterSpecified() ? descriptor.GetSetter() : defaultDescriptor->GetSetter());
  8204. }
  8205. else
  8206. {
  8207. newDescriptor.SetValue(descriptor.ValueSpecified() ? descriptor.GetValue() : defaultDescriptor->GetValue());
  8208. newDescriptor.SetWritable(descriptor.WritableSpecified() ? descriptor.IsWritable() : defaultDescriptor->IsWritable());
  8209. }
  8210. newDescriptor.SetConfigurable(descriptor.ConfigurableSpecified() ? descriptor.IsConfigurable() : defaultDescriptor->IsConfigurable());
  8211. newDescriptor.SetEnumerable(descriptor.EnumerableSpecified() ? descriptor.IsEnumerable() : defaultDescriptor->IsEnumerable());
  8212. return newDescriptor;
  8213. }
  8214. // ES5: 15.4.5.1
  8215. BOOL JavascriptOperators::DefineOwnPropertyForArray(JavascriptArray* arr, PropertyId propId, const PropertyDescriptor& descriptor, bool throwOnError, ScriptContext* scriptContext)
  8216. {
  8217. if (propId == PropertyIds::length)
  8218. {
  8219. if (!descriptor.ValueSpecified())
  8220. {
  8221. return DefineOwnPropertyDescriptor(arr, PropertyIds::length, descriptor, throwOnError, scriptContext);
  8222. }
  8223. PropertyDescriptor newLenDesc = descriptor;
  8224. uint32 newLen = ES5Array::ToLengthValue(descriptor.GetValue(), scriptContext);
  8225. newLenDesc.SetValue(JavascriptNumber::ToVar(newLen, scriptContext));
  8226. uint32 oldLen = arr->GetLength();
  8227. if (newLen >= oldLen)
  8228. {
  8229. return DefineOwnPropertyDescriptor(arr, PropertyIds::length, newLenDesc, throwOnError, scriptContext);
  8230. }
  8231. BOOL oldLenWritable = arr->IsWritable(PropertyIds::length);
  8232. if (!oldLenWritable)
  8233. {
  8234. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_NotWritable, propId);
  8235. }
  8236. bool newWritable = (!newLenDesc.WritableSpecified() || newLenDesc.IsWritable());
  8237. if (!newWritable)
  8238. {
  8239. // Need to defer setting writable to false in case any elements cannot be deleted
  8240. newLenDesc.SetWritable(true);
  8241. }
  8242. BOOL succeeded = DefineOwnPropertyDescriptor(arr, PropertyIds::length, newLenDesc, throwOnError, scriptContext);
  8243. //
  8244. // Our SetProperty(length) is also responsible to trim elements. When succeeded is
  8245. //
  8246. // false:
  8247. // * length attributes rejected
  8248. // * elements not touched
  8249. // true:
  8250. // * length attributes are set successfully
  8251. // * elements trimming may be either completed or incompleted, length value is correct
  8252. //
  8253. // * Strict mode TODO: Currently SetProperty(length) does not throw. If that throws, we need
  8254. // to update here to set correct newWritable even on exception.
  8255. //
  8256. if (!succeeded)
  8257. {
  8258. return false;
  8259. }
  8260. if (!newWritable) // Now set requested newWritable.
  8261. {
  8262. PropertyDescriptor newWritableDesc;
  8263. newWritableDesc.SetWritable(false);
  8264. DefineOwnPropertyDescriptor(arr, PropertyIds::length, newWritableDesc, false, scriptContext);
  8265. }
  8266. if (arr->GetLength() > newLen) // Delete incompleted
  8267. {
  8268. // Since SetProperty(length) not throwing, we'll reject here
  8269. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_Default, propId);
  8270. }
  8271. return true;
  8272. }
  8273. uint32 index;
  8274. if (scriptContext->IsNumericPropertyId(propId, &index))
  8275. {
  8276. if (index >= arr->GetLength() && !arr->IsWritable(PropertyIds::length))
  8277. {
  8278. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_LengthNotWritable, propId);
  8279. }
  8280. BOOL succeeded = DefineOwnPropertyDescriptor(arr, propId, descriptor, false, scriptContext);
  8281. if (!succeeded)
  8282. {
  8283. return Reject(throwOnError, scriptContext, JSERR_DefineProperty_Default, propId);
  8284. }
  8285. // Out SetItem takes care of growing "length". we are done.
  8286. return true;
  8287. }
  8288. return DefineOwnPropertyDescriptor(arr, propId, descriptor, throwOnError, scriptContext);
  8289. }
  8290. BOOL JavascriptOperators::SetPropertyDescriptor(RecyclableObject* object, PropertyId propId, const PropertyDescriptor& descriptor)
  8291. {
  8292. if (descriptor.ValueSpecified())
  8293. {
  8294. ScriptContext* requestContext = object->GetScriptContext(); // Real requestContext?
  8295. JavascriptOperators::SetProperty(object, object, propId, descriptor.GetValue(), requestContext);
  8296. }
  8297. else if (descriptor.GetterSpecified() || descriptor.SetterSpecified())
  8298. {
  8299. JavascriptOperators::SetAccessors(object, propId, descriptor.GetGetter(), descriptor.GetSetter());
  8300. }
  8301. if (descriptor.EnumerableSpecified())
  8302. {
  8303. object->SetEnumerable(propId, descriptor.IsEnumerable());
  8304. }
  8305. if (descriptor.ConfigurableSpecified())
  8306. {
  8307. object->SetConfigurable(propId, descriptor.IsConfigurable());
  8308. }
  8309. if (descriptor.WritableSpecified())
  8310. {
  8311. object->SetWritable(propId, descriptor.IsWritable());
  8312. }
  8313. return true;
  8314. }
  8315. BOOL JavascriptOperators::ToPropertyDescriptorForProxyObjects(Var propertySpec, PropertyDescriptor* descriptor, ScriptContext* scriptContext)
  8316. {
  8317. if (!JavascriptOperators::IsObject(propertySpec))
  8318. {
  8319. return FALSE;
  8320. }
  8321. Var value;
  8322. RecyclableObject* propertySpecObj = RecyclableObject::FromVar(propertySpec);
  8323. if (JavascriptOperators::HasProperty(propertySpecObj, PropertyIds::enumerable) == TRUE)
  8324. {
  8325. if (JavascriptOperators::GetProperty(propertySpecObj, PropertyIds::enumerable, &value, scriptContext))
  8326. {
  8327. descriptor->SetEnumerable(JavascriptConversion::ToBoolean(value, scriptContext) ? true : false);
  8328. }
  8329. else
  8330. {
  8331. // The proxy said we have the property, so we try to read the property and get the default value.
  8332. descriptor->SetEnumerable(false);
  8333. }
  8334. }
  8335. if (JavascriptOperators::HasProperty(propertySpecObj, PropertyIds::configurable) == TRUE)
  8336. {
  8337. if (JavascriptOperators::GetProperty(propertySpecObj, PropertyIds::configurable, &value, scriptContext))
  8338. {
  8339. descriptor->SetConfigurable(JavascriptConversion::ToBoolean(value, scriptContext) ? true : false);
  8340. }
  8341. else
  8342. {
  8343. // The proxy said we have the property, so we try to read the property and get the default value.
  8344. descriptor->SetConfigurable(false);
  8345. }
  8346. }
  8347. if (JavascriptOperators::HasProperty(propertySpecObj, PropertyIds::value) == TRUE)
  8348. {
  8349. if (JavascriptOperators::GetProperty(propertySpecObj, PropertyIds::value, &value, scriptContext))
  8350. {
  8351. descriptor->SetValue(value);
  8352. }
  8353. else
  8354. {
  8355. // The proxy said we have the property, so we try to read the property and get the default value.
  8356. descriptor->SetValue(scriptContext->GetLibrary()->GetUndefined());
  8357. }
  8358. }
  8359. if (JavascriptOperators::HasProperty(propertySpecObj, PropertyIds::writable) == TRUE)
  8360. {
  8361. if (JavascriptOperators::GetProperty(propertySpecObj, PropertyIds::writable, &value, scriptContext))
  8362. {
  8363. descriptor->SetWritable(JavascriptConversion::ToBoolean(value, scriptContext) ? true : false);
  8364. }
  8365. else
  8366. {
  8367. // The proxy said we have the property, so we try to read the property and get the default value.
  8368. descriptor->SetWritable(false);
  8369. }
  8370. }
  8371. if (JavascriptOperators::HasProperty(propertySpecObj, PropertyIds::get) == TRUE)
  8372. {
  8373. if (JavascriptOperators::GetProperty(propertySpecObj, PropertyIds::get, &value, scriptContext))
  8374. {
  8375. if (JavascriptOperators::GetTypeId(value) != TypeIds_Undefined && (false == JavascriptConversion::IsCallable(value)))
  8376. {
  8377. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, scriptContext->GetPropertyName(PropertyIds::get)->GetBuffer());
  8378. }
  8379. descriptor->SetGetter(value);
  8380. }
  8381. else
  8382. {
  8383. // The proxy said we have the property, so we try to read the property and get the default value.
  8384. descriptor->SetGetter(scriptContext->GetLibrary()->GetUndefined());
  8385. }
  8386. }
  8387. if (JavascriptOperators::HasProperty(propertySpecObj, PropertyIds::set) == TRUE)
  8388. {
  8389. if (JavascriptOperators::GetProperty(propertySpecObj, PropertyIds::set, &value, scriptContext))
  8390. {
  8391. if (JavascriptOperators::GetTypeId(value) != TypeIds_Undefined && (false == JavascriptConversion::IsCallable(value)))
  8392. {
  8393. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, scriptContext->GetPropertyName(PropertyIds::set)->GetBuffer());
  8394. }
  8395. descriptor->SetSetter(value);
  8396. }
  8397. else
  8398. {
  8399. // The proxy said we have the property, so we try to read the property and get the default value.
  8400. descriptor->SetSetter(scriptContext->GetLibrary()->GetUndefined());
  8401. }
  8402. }
  8403. return TRUE;
  8404. }
  8405. BOOL JavascriptOperators::ToPropertyDescriptorForGenericObjects(Var propertySpec, PropertyDescriptor* descriptor, ScriptContext* scriptContext)
  8406. {
  8407. if (!JavascriptOperators::IsObject(propertySpec))
  8408. {
  8409. return FALSE;
  8410. }
  8411. Var value;
  8412. RecyclableObject* propertySpecObj = RecyclableObject::FromVar(propertySpec);
  8413. if (JavascriptOperators::GetPropertyNoCache(propertySpecObj, PropertyIds::enumerable, &value, scriptContext))
  8414. {
  8415. descriptor->SetEnumerable(JavascriptConversion::ToBoolean(value, scriptContext) ? true : false);
  8416. }
  8417. if (JavascriptOperators::GetPropertyNoCache(propertySpecObj, PropertyIds::configurable, &value, scriptContext))
  8418. {
  8419. descriptor->SetConfigurable(JavascriptConversion::ToBoolean(value, scriptContext) ? true : false);
  8420. }
  8421. if (JavascriptOperators::GetPropertyNoCache(propertySpecObj, PropertyIds::value, &value, scriptContext))
  8422. {
  8423. descriptor->SetValue(value);
  8424. }
  8425. if (JavascriptOperators::GetPropertyNoCache(propertySpecObj, PropertyIds::writable, &value, scriptContext))
  8426. {
  8427. descriptor->SetWritable(JavascriptConversion::ToBoolean(value, scriptContext) ? true : false);
  8428. }
  8429. if (JavascriptOperators::GetPropertyNoCache(propertySpecObj, PropertyIds::get, &value, scriptContext))
  8430. {
  8431. if (JavascriptOperators::GetTypeId(value) != TypeIds_Undefined && (false == JavascriptConversion::IsCallable(value)))
  8432. {
  8433. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, scriptContext->GetPropertyName(PropertyIds::get)->GetBuffer());
  8434. }
  8435. descriptor->SetGetter(value);
  8436. }
  8437. if (JavascriptOperators::GetPropertyNoCache(propertySpecObj, PropertyIds::set, &value, scriptContext))
  8438. {
  8439. if (JavascriptOperators::GetTypeId(value) != TypeIds_Undefined && (false == JavascriptConversion::IsCallable(value)))
  8440. {
  8441. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, scriptContext->GetPropertyName(PropertyIds::set)->GetBuffer());
  8442. }
  8443. descriptor->SetSetter(value);
  8444. }
  8445. return TRUE;
  8446. }
  8447. BOOL JavascriptOperators::ToPropertyDescriptor(Var propertySpec, PropertyDescriptor* descriptor, ScriptContext* scriptContext)
  8448. {
  8449. if (JavascriptProxy::Is(propertySpec) || (
  8450. RecyclableObject::Is(propertySpec) &&
  8451. JavascriptOperators::CheckIfPrototypeChainContainsProxyObject(RecyclableObject::FromVar(propertySpec)->GetPrototype())))
  8452. {
  8453. if (ToPropertyDescriptorForProxyObjects(propertySpec, descriptor, scriptContext) == FALSE)
  8454. {
  8455. return FALSE;
  8456. }
  8457. }
  8458. else
  8459. {
  8460. if (ToPropertyDescriptorForGenericObjects(propertySpec, descriptor, scriptContext) == FALSE)
  8461. {
  8462. return FALSE;
  8463. }
  8464. }
  8465. if (descriptor->GetterSpecified() || descriptor->SetterSpecified())
  8466. {
  8467. if (descriptor->ValueSpecified())
  8468. {
  8469. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotHaveAccessorsAndValue);
  8470. }
  8471. if (descriptor->WritableSpecified())
  8472. {
  8473. int32 hCode = descriptor->IsWritable() ? JSERR_InvalidAttributeTrue : JSERR_InvalidAttributeFalse;
  8474. JavascriptError::ThrowTypeError(scriptContext, hCode, _u("writable"));
  8475. }
  8476. }
  8477. descriptor->SetOriginal(propertySpec);
  8478. return TRUE;
  8479. }
  8480. void JavascriptOperators::CompletePropertyDescriptor(PropertyDescriptor* resultDescriptor, PropertyDescriptor* likeDescriptor, ScriptContext* requestContext)
  8481. {
  8482. const PropertyDescriptor* likePropertyDescriptor = likeDescriptor;
  8483. // 1. Assert: LikeDesc is either a Property Descriptor or undefined.
  8484. // 2. ReturnIfAbrupt(Desc).
  8485. // 3. Assert : Desc is a Property Descriptor
  8486. // 4. If LikeDesc is undefined, then set LikeDesc to Record{ [[Value]]: undefined, [[Writable]] : false, [[Get]] : undefined, [[Set]] : undefined, [[Enumerable]] : false, [[Configurable]] : false }.
  8487. if (likePropertyDescriptor == nullptr)
  8488. {
  8489. likePropertyDescriptor = requestContext->GetLibrary()->GetDefaultPropertyDescriptor();
  8490. }
  8491. // 5. If either IsGenericDescriptor(Desc) or IsDataDescriptor(Desc) is true, then
  8492. if (resultDescriptor->IsDataDescriptor() || resultDescriptor->IsGenericDescriptor())
  8493. {
  8494. // a.If Desc does not have a[[Value]] field, then set Desc.[[Value]] to LikeDesc.[[Value]].
  8495. // b.If Desc does not have a[[Writable]] field, then set Desc.[[Writable]] to LikeDesc.[[Writable]].
  8496. if (!resultDescriptor->ValueSpecified())
  8497. {
  8498. resultDescriptor->SetValue(likePropertyDescriptor->GetValue());
  8499. }
  8500. if (!resultDescriptor->WritableSpecified())
  8501. {
  8502. resultDescriptor->SetWritable(likePropertyDescriptor->IsWritable());
  8503. }
  8504. }
  8505. else
  8506. {
  8507. // 6. Else,
  8508. // a.If Desc does not have a[[Get]] field, then set Desc.[[Get]] to LikeDesc.[[Get]].
  8509. // b.If Desc does not have a[[Set]] field, then set Desc.[[Set]] to LikeDesc.[[Set]].
  8510. if (!resultDescriptor->GetterSpecified())
  8511. {
  8512. resultDescriptor->SetGetter(likePropertyDescriptor->GetGetter());
  8513. }
  8514. if (!resultDescriptor->SetterSpecified())
  8515. {
  8516. resultDescriptor->SetSetter(likePropertyDescriptor->GetSetter());
  8517. }
  8518. }
  8519. // 7. If Desc does not have an[[Enumerable]] field, then set Desc.[[Enumerable]] to LikeDesc.[[Enumerable]].
  8520. // 8. If Desc does not have a[[Configurable]] field, then set Desc.[[Configurable]] to LikeDesc.[[Configurable]].
  8521. // 9. Return Desc.
  8522. if (!resultDescriptor->EnumerableSpecified())
  8523. {
  8524. resultDescriptor->SetEnumerable(likePropertyDescriptor->IsEnumerable());
  8525. }
  8526. if (!resultDescriptor->ConfigurableSpecified())
  8527. {
  8528. resultDescriptor->SetConfigurable(likePropertyDescriptor->IsConfigurable());
  8529. }
  8530. }
  8531. // Conformance to: ES5 8.6.1.
  8532. // Set attributes on the object as provided by property descriptor.
  8533. // If force parameter is true, we force SetAttributes call even if none of the attributes are defined by the descriptor.
  8534. // NOTE: does not set [[Get]], [Set]], [[Value]]
  8535. void JavascriptOperators::SetAttributes(RecyclableObject* object, PropertyId propId, const PropertyDescriptor& descriptor, bool force)
  8536. {
  8537. Assert(object);
  8538. BOOL isWritable = FALSE;
  8539. if (descriptor.IsDataDescriptor())
  8540. {
  8541. isWritable = descriptor.WritableSpecified() ? descriptor.IsWritable() : FALSE;
  8542. }
  8543. else if (descriptor.IsAccessorDescriptor())
  8544. {
  8545. // The reason is that JavascriptOperators::OP_SetProperty checks for RecyclableObject::FromVar(instance)->IsWritableOrAccessor(propertyId),
  8546. // which should in fact check for 'is writable or accessor' but since there is no GetAttributes, we can't do that efficiently.
  8547. isWritable = TRUE;
  8548. }
  8549. // CONSIDER: call object->SetAttributes which is much more efficient as that's 1 call instead of 3.
  8550. // Can't do that now as object->SetAttributes doesn't provide a way which attributes to modify and which not.
  8551. if (force || descriptor.ConfigurableSpecified())
  8552. {
  8553. object->SetConfigurable(propId, descriptor.ConfigurableSpecified() ? descriptor.IsConfigurable() : FALSE);
  8554. }
  8555. if (force || descriptor.EnumerableSpecified())
  8556. {
  8557. object->SetEnumerable(propId, descriptor.EnumerableSpecified() ? descriptor.IsEnumerable() : FALSE);
  8558. }
  8559. if (force || descriptor.WritableSpecified() || isWritable)
  8560. {
  8561. object->SetWritable(propId, isWritable);
  8562. }
  8563. }
  8564. void JavascriptOperators::OP_ClearAttributes(Var instance, PropertyId propertyId)
  8565. {
  8566. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(OP_ClearAttributes);
  8567. Assert(instance);
  8568. if (RecyclableObject::Is(instance))
  8569. {
  8570. RecyclableObject* obj = RecyclableObject::FromVar(instance);
  8571. obj->SetAttributes(propertyId, PropertyNone);
  8572. }
  8573. JIT_HELPER_END(OP_ClearAttributes);
  8574. }
  8575. BOOL JavascriptOperators::Reject(bool throwOnError, ScriptContext* scriptContext, int32 errorCode, PropertyId propertyId)
  8576. {
  8577. Assert(scriptContext);
  8578. if (throwOnError)
  8579. {
  8580. JavascriptError::ThrowTypeError(scriptContext, errorCode, scriptContext->GetThreadContext()->GetPropertyName(propertyId)->GetBuffer());
  8581. }
  8582. return FALSE;
  8583. }
  8584. bool JavascriptOperators::AreSamePropertyDescriptors(const PropertyDescriptor* x, const PropertyDescriptor* y, ScriptContext* scriptContext)
  8585. {
  8586. Assert(scriptContext);
  8587. if (x->ConfigurableSpecified() != y->ConfigurableSpecified() || x->IsConfigurable() != y->IsConfigurable() ||
  8588. x->EnumerableSpecified() != y->EnumerableSpecified() || x->IsEnumerable() != y->IsEnumerable())
  8589. {
  8590. return false;
  8591. }
  8592. if (x->IsDataDescriptor())
  8593. {
  8594. if (!y->IsDataDescriptor() || x->WritableSpecified() != y->WritableSpecified() || x->IsWritable() != y->IsWritable())
  8595. {
  8596. return false;
  8597. }
  8598. if (x->ValueSpecified())
  8599. {
  8600. if (!y->ValueSpecified() || !JavascriptConversion::SameValue(x->GetValue(), y->GetValue()))
  8601. {
  8602. return false;
  8603. }
  8604. }
  8605. }
  8606. else if (x->IsAccessorDescriptor())
  8607. {
  8608. if (!y->IsAccessorDescriptor())
  8609. {
  8610. return false;
  8611. }
  8612. if (x->GetterSpecified())
  8613. {
  8614. if (!y->GetterSpecified() || !JavascriptConversion::SameValue(
  8615. JavascriptOperators::CanonicalizeAccessor(x->GetGetter(), scriptContext),
  8616. JavascriptOperators::CanonicalizeAccessor(y->GetGetter(), scriptContext)))
  8617. {
  8618. return false;
  8619. }
  8620. }
  8621. if (x->SetterSpecified())
  8622. {
  8623. if (!y->SetterSpecified() || !JavascriptConversion::SameValue(
  8624. JavascriptOperators::CanonicalizeAccessor(x->GetSetter(), scriptContext),
  8625. JavascriptOperators::CanonicalizeAccessor(y->GetSetter(), scriptContext)))
  8626. {
  8627. return false;
  8628. }
  8629. }
  8630. }
  8631. return true;
  8632. }
  8633. // Check if an accessor is undefined (null or defaultAccessor)
  8634. bool JavascriptOperators::IsUndefinedAccessor(Var accessor, ScriptContext* scriptContext)
  8635. {
  8636. return nullptr == accessor || scriptContext->GetLibrary()->GetDefaultAccessorFunction() == accessor;
  8637. }
  8638. // Converts default accessor to undefined.
  8639. // Can be used when comparing accessors.
  8640. Var JavascriptOperators::CanonicalizeAccessor(Var accessor, ScriptContext* scriptContext)
  8641. {
  8642. Assert(scriptContext);
  8643. if (IsUndefinedAccessor(accessor, scriptContext))
  8644. {
  8645. return scriptContext->GetLibrary()->GetUndefined();
  8646. }
  8647. return accessor;
  8648. }
  8649. Var JavascriptOperators::DefaultAccessor(RecyclableObject* function, CallInfo callInfo, ...)
  8650. {
  8651. return function->GetLibrary()->GetUndefined();
  8652. }
  8653. void FrameDisplay::SetItem(uint index, void* item)
  8654. {
  8655. AssertMsg(index < this->length, "Invalid frame display access");
  8656. scopes[index] = item;
  8657. }
  8658. void *FrameDisplay::GetItem(uint index)
  8659. {
  8660. AssertMsg(index < this->length, "Invalid frame display access");
  8661. return scopes[index];
  8662. }
  8663. // Grab the "this" pointer, mapping a root object to its associated host object.
  8664. Var JavascriptOperators::RootToThisObject(const Var object, ScriptContext* scriptContext)
  8665. {
  8666. Js::Var thisVar = object;
  8667. TypeId typeId = Js::JavascriptOperators::GetTypeId(thisVar);
  8668. switch (typeId)
  8669. {
  8670. case Js::TypeIds_GlobalObject:
  8671. return ((Js::GlobalObject*)thisVar)->ToThis();
  8672. case Js::TypeIds_ModuleRoot:
  8673. return Js::JavascriptOperators::GetThisFromModuleRoot(thisVar);
  8674. default:
  8675. if (typeId == scriptContext->GetDirectHostTypeId())
  8676. {
  8677. return ((RecyclableObject*)thisVar)->GetLibrary()->GetGlobalObject()->ToThis();
  8678. }
  8679. }
  8680. return thisVar;
  8681. }
  8682. Var JavascriptOperators::CallGetter(RecyclableObject * const function, Var const object, ScriptContext * requestContext)
  8683. {
  8684. #if ENABLE_TTD
  8685. if(function->GetScriptContext()->ShouldSuppressGetterInvocationForDebuggerEvaluation())
  8686. {
  8687. return requestContext->GetLibrary()->GetUndefined();
  8688. }
  8689. #endif
  8690. ScriptContext * scriptContext = function->GetScriptContext();
  8691. ThreadContext * threadContext = scriptContext->GetThreadContext();
  8692. return threadContext->ExecuteImplicitCall(function, ImplicitCall_Accessor, [=]() -> Js::Var
  8693. {
  8694. // Stack object should have a pre-op bail on implicit call. We shouldn't see them here.
  8695. // Stack numbers are ok, as we will call ToObject to wrap it in a number object anyway
  8696. // See JavascriptOperators::GetThisHelper
  8697. Assert(JavascriptOperators::GetTypeId(object) == TypeIds_Integer ||
  8698. JavascriptOperators::GetTypeId(object) == TypeIds_Number ||
  8699. threadContext->HasNoSideEffect(function) ||
  8700. !ThreadContext::IsOnStack(object));
  8701. // Verify that the scriptcontext is alive before firing getter/setter
  8702. if (!scriptContext->VerifyAlive(!function->IsExternal(), requestContext))
  8703. {
  8704. return nullptr;
  8705. }
  8706. CallFlags flags = CallFlags_Value;
  8707. Var thisVar = RootToThisObject(object, scriptContext);
  8708. RecyclableObject* marshalledFunction = RecyclableObject::UnsafeFromVar(
  8709. CrossSite::MarshalVar(requestContext, function, scriptContext));
  8710. Var result = CALL_ENTRYPOINT(threadContext, marshalledFunction->GetEntryPoint(), function, CallInfo(flags, 1), thisVar);
  8711. result = CrossSite::MarshalVar(requestContext, result);
  8712. // Set implicit call flags so we bail out if we're trying to propagate the value forward, e.g., from a compare. Subsequent calls
  8713. // to the getter may produce different results.
  8714. threadContext->AddImplicitCallFlags(ImplicitCall_Accessor);
  8715. return result;
  8716. });
  8717. }
  8718. void JavascriptOperators::CallSetter(RecyclableObject * const function, Var const object, Var const value, ScriptContext * requestContext)
  8719. {
  8720. ScriptContext * scriptContext = function->GetScriptContext();
  8721. ThreadContext * threadContext = scriptContext->GetThreadContext();
  8722. threadContext->ExecuteImplicitCall(function, ImplicitCall_Accessor, [=]() -> Js::Var
  8723. {
  8724. // Stack object should have a pre-op bail on implicit call. We shouldn't see them here.
  8725. // Stack numbers are ok, as we will call ToObject to wrap it in a number object anyway
  8726. // See JavascriptOperators::GetThisHelper
  8727. Assert(JavascriptOperators::GetTypeId(object) == TypeIds_Integer ||
  8728. JavascriptOperators::GetTypeId(object) == TypeIds_Number || !ThreadContext::IsOnStack(object));
  8729. // Verify that the scriptcontext is alive before firing getter/setter
  8730. if (!scriptContext->VerifyAlive(!function->IsExternal(), requestContext))
  8731. {
  8732. return nullptr;
  8733. }
  8734. CallFlags flags = CallFlags_Value;
  8735. Var putValue = value;
  8736. // CONSIDER: Have requestContext everywhere, even in the setProperty related codepath.
  8737. if (requestContext)
  8738. {
  8739. putValue = CrossSite::MarshalVar(requestContext, value);
  8740. }
  8741. Var thisVar = RootToThisObject(object, scriptContext);
  8742. RecyclableObject* marshalledFunction = function;
  8743. if (requestContext)
  8744. {
  8745. marshalledFunction = RecyclableObject::UnsafeFromVar(CrossSite::MarshalVar(requestContext, function, function->GetScriptContext()));
  8746. }
  8747. Var result = CALL_ENTRYPOINT(threadContext, marshalledFunction->GetEntryPoint(), function, CallInfo(flags, 2), thisVar, putValue);
  8748. Assert(result);
  8749. // Set implicit call flags so we bail out if we're trying to propagate the stored value forward. We can't count on the getter/setter
  8750. // to produce the stored value on a LdFld.
  8751. threadContext->AddImplicitCallFlags(ImplicitCall_Accessor);
  8752. return nullptr;
  8753. });
  8754. }
  8755. void * JavascriptOperators::AllocMemForVarArray(size_t size, Recycler* recycler)
  8756. {
  8757. TRACK_ALLOC_INFO(recycler, Js::Var, Recycler, 0, (size_t)(size / sizeof(Js::Var)));
  8758. return recycler->AllocZero(size);
  8759. }
  8760. #if !FLOATVAR
  8761. void * JavascriptOperators::AllocUninitializedNumber(Js::RecyclerJavascriptNumberAllocator * allocator)
  8762. {
  8763. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(AllocUninitializedNumber);
  8764. TRACK_ALLOC_INFO(allocator->GetRecycler(), Js::JavascriptNumber, Recycler, 0, (size_t)-1);
  8765. return allocator->Alloc(sizeof(Js::JavascriptNumber));
  8766. JIT_HELPER_END(AllocUninitializedNumber);
  8767. }
  8768. #endif
  8769. void JavascriptOperators::ScriptAbort()
  8770. {
  8771. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(ScriptAbort);
  8772. throw ScriptAbortException();
  8773. JIT_HELPER_END(ScriptAbort);
  8774. }
  8775. JavascriptString * JavascriptOperators::Concat3(Var aLeft, Var aCenter, Var aRight, ScriptContext * scriptContext)
  8776. {
  8777. // Make sure we do the conversion in order from left to right
  8778. JavascriptString * strLeft = JavascriptConversion::ToPrimitiveString(aLeft, scriptContext);
  8779. JavascriptString * strCenter = JavascriptConversion::ToPrimitiveString(aCenter, scriptContext);
  8780. JavascriptString * strRight = JavascriptConversion::ToPrimitiveString(aRight, scriptContext);
  8781. return JavascriptString::Concat3(strLeft, strCenter, strRight);
  8782. }
  8783. JavascriptString *
  8784. JavascriptOperators::NewConcatStrMulti(Var a1, Var a2, uint count, ScriptContext * scriptContext)
  8785. {
  8786. // Make sure we do the conversion in order
  8787. JavascriptString * str1 = JavascriptConversion::ToPrimitiveString(a1, scriptContext);
  8788. JavascriptString * str2 = JavascriptConversion::ToPrimitiveString(a2, scriptContext);
  8789. return ConcatStringMulti::New(count, str1, str2, scriptContext);
  8790. }
  8791. void
  8792. JavascriptOperators::SetConcatStrMultiItem(Var concatStr, Var str, uint index, ScriptContext * scriptContext)
  8793. {
  8794. ConcatStringMulti::FromVar(concatStr)->SetItem(index,
  8795. JavascriptConversion::ToPrimitiveString(str, scriptContext));
  8796. }
  8797. void
  8798. JavascriptOperators::SetConcatStrMultiItem2(Var concatStr, Var str1, Var str2, uint index, ScriptContext * scriptContext)
  8799. {
  8800. ConcatStringMulti * cs = ConcatStringMulti::FromVar(concatStr);
  8801. cs->SetItem(index, JavascriptConversion::ToPrimitiveString(str1, scriptContext));
  8802. cs->SetItem(index + 1, JavascriptConversion::ToPrimitiveString(str2, scriptContext));
  8803. }
  8804. void JavascriptOperators::OP_SetComputedNameVar(Var method, Var computedNameVar)
  8805. {
  8806. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(SetComputedNameVar);
  8807. ScriptFunctionBase *scriptFunction = ScriptFunctionBase::FromVar(method);
  8808. scriptFunction->SetComputedNameVar(computedNameVar);
  8809. JIT_HELPER_END(SetComputedNameVar);
  8810. }
  8811. void JavascriptOperators::OP_SetHomeObj(Var method, Var homeObj)
  8812. {
  8813. ScriptFunctionBase *scriptFunction = ScriptFunctionBase::FromVar(method);
  8814. JIT_HELPER_NOT_REENTRANT_HEADER(SetHomeObj, reentrancylock, scriptFunction->GetScriptContext()->GetThreadContext());
  8815. scriptFunction->SetHomeObj(homeObj);
  8816. JIT_HELPER_END(SetHomeObj);
  8817. }
  8818. Var JavascriptOperators::OP_LdHomeObj(Var scriptFunction, ScriptContext * scriptContext)
  8819. {
  8820. JIT_HELPER_NOT_REENTRANT_HEADER(LdHomeObj, reentrancylock, scriptContext->GetThreadContext());
  8821. // Ensure this is not a stack ScriptFunction
  8822. if (!ScriptFunction::Is(scriptFunction) || ThreadContext::IsOnStack(scriptFunction))
  8823. {
  8824. return scriptContext->GetLibrary()->GetUndefined();
  8825. }
  8826. ScriptFunction *instance = ScriptFunction::UnsafeFromVar(scriptFunction);
  8827. // We keep a reference to the current class rather than its super prototype
  8828. // since the prototype could change.
  8829. Var homeObj = instance->GetHomeObj();
  8830. return (homeObj != nullptr) ? homeObj : scriptContext->GetLibrary()->GetUndefined();
  8831. JIT_HELPER_END(LdHomeObj);
  8832. }
  8833. Var JavascriptOperators::OP_LdHomeObjProto(Var homeObj, ScriptContext* scriptContext)
  8834. {
  8835. JIT_HELPER_NOT_REENTRANT_HEADER(LdHomeObjProto, reentrancylock, scriptContext->GetThreadContext());
  8836. if (homeObj == nullptr || !RecyclableObject::Is(homeObj))
  8837. {
  8838. return scriptContext->GetLibrary()->GetUndefined();
  8839. }
  8840. RecyclableObject *thisObjPrototype = RecyclableObject::FromVar(homeObj);
  8841. TypeId typeId = thisObjPrototype->GetTypeId();
  8842. if (typeId == TypeIds_Null || typeId == TypeIds_Undefined)
  8843. {
  8844. JavascriptError::ThrowReferenceError(scriptContext, JSERR_BadSuperReference);
  8845. }
  8846. Assert(thisObjPrototype != nullptr);
  8847. RecyclableObject *superBase = thisObjPrototype->GetPrototype();
  8848. if (superBase == nullptr || !RecyclableObject::Is(superBase))
  8849. {
  8850. return scriptContext->GetLibrary()->GetUndefined();
  8851. }
  8852. return superBase;
  8853. JIT_HELPER_END(LdHomeObjProto);
  8854. }
  8855. Var JavascriptOperators::OP_LdFuncObj(Var scriptFunction, ScriptContext * scriptContext)
  8856. {
  8857. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(LdFuncObj);
  8858. // use self as value of [[FunctionObject]] - this is true only for constructors
  8859. Assert(RecyclableObject::Is(scriptFunction));
  8860. return scriptFunction;
  8861. JIT_HELPER_END(LdFuncObj);
  8862. }
  8863. Var JavascriptOperators::OP_LdFuncObjProto(Var funcObj, ScriptContext* scriptContext)
  8864. {
  8865. JIT_HELPER_NOT_REENTRANT_HEADER(LdFuncObjProto, reentrancylock, scriptContext->GetThreadContext());
  8866. RecyclableObject *superCtor = RecyclableObject::FromVar(funcObj)->GetPrototype();
  8867. if (superCtor == nullptr || !IsConstructor(superCtor))
  8868. {
  8869. JavascriptError::ThrowTypeError(scriptContext, JSERR_NotAConstructor);
  8870. }
  8871. return superCtor;
  8872. JIT_HELPER_END(LdFuncObjProto);
  8873. }
  8874. Var JavascriptOperators::OP_ImportCall(__in JavascriptFunction *function, __in Var specifier, __in ScriptContext* scriptContext)
  8875. {
  8876. JIT_HELPER_REENTRANT_HEADER(ImportCall);
  8877. ModuleRecordBase *moduleRecordBase = nullptr;
  8878. SourceTextModuleRecord *moduleRecord = nullptr;
  8879. FunctionBody* parentFuncBody = function->GetFunctionBody();
  8880. JavascriptString *specifierString = nullptr;
  8881. try
  8882. {
  8883. specifierString = JavascriptConversion::ToString(specifier, scriptContext);
  8884. }
  8885. catch (const JavascriptException &err)
  8886. {
  8887. Var errorObject = err.GetAndClear()->GetThrownObject(scriptContext);
  8888. AssertMsg(errorObject != nullptr, "OP_ImportCall: null error object thrown by ToString(specifier)");
  8889. if (errorObject != nullptr)
  8890. {
  8891. return SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, errorObject, scriptContext);
  8892. }
  8893. Throw::InternalError();
  8894. }
  8895. DWORD_PTR dwReferencingSourceContext = parentFuncBody->GetHostSourceContext();
  8896. if (!parentFuncBody->IsES6ModuleCode() && dwReferencingSourceContext == Js::Constants::NoHostSourceContext)
  8897. {
  8898. // import() called from eval
  8899. if (parentFuncBody->GetUtf8SourceInfo()->GetCallerUtf8SourceInfo() == nullptr)
  8900. {
  8901. JavascriptError *error = scriptContext->GetLibrary()->CreateError();
  8902. JavascriptError::SetErrorMessageProperties(error, E_FAIL, _u("Unable to locate active script or module that calls import()"), scriptContext);
  8903. return SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, error, scriptContext);
  8904. }
  8905. dwReferencingSourceContext = parentFuncBody->GetUtf8SourceInfo()->GetCallerUtf8SourceInfo()->GetSourceContextInfo()->dwHostSourceContext;
  8906. if (dwReferencingSourceContext == Js::Constants::NoHostSourceContext)
  8907. {
  8908. // Walk the call stack if caller function is neither module code nor having host source context
  8909. JavascriptFunction* caller = nullptr;
  8910. Js::JavascriptStackWalker walker(scriptContext);
  8911. walker.GetCaller(&caller);
  8912. do
  8913. {
  8914. if (walker.GetCaller(&caller) && caller != nullptr && caller->IsScriptFunction())
  8915. {
  8916. parentFuncBody = caller->GetFunctionBody();
  8917. dwReferencingSourceContext = parentFuncBody->GetHostSourceContext();
  8918. }
  8919. else
  8920. {
  8921. JavascriptError *error = scriptContext->GetLibrary()->CreateError();
  8922. JavascriptError::SetErrorMessageProperties(error, E_FAIL, _u("Unable to locate active script or module that calls import()"), scriptContext);
  8923. return SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, error, scriptContext);
  8924. }
  8925. } while (!parentFuncBody->IsES6ModuleCode() && dwReferencingSourceContext == Js::Constants::NoHostSourceContext);
  8926. }
  8927. }
  8928. LPCOLESTR moduleName = specifierString->GetSz();
  8929. HRESULT hr = 0;
  8930. if (parentFuncBody->IsES6ModuleCode())
  8931. {
  8932. SourceTextModuleRecord *referenceModuleRecord = parentFuncBody->GetScriptContext()->GetLibrary()->GetModuleRecord(parentFuncBody->GetModuleID());
  8933. BEGIN_LEAVE_SCRIPT(scriptContext);
  8934. BEGIN_TRANSLATE_TO_HRESULT(static_cast<ExceptionType>(ExceptionType_OutOfMemory | ExceptionType_StackOverflow));
  8935. hr = scriptContext->GetHostScriptContext()->FetchImportedModule(referenceModuleRecord, moduleName, &moduleRecordBase);
  8936. END_TRANSLATE_EXCEPTION_TO_HRESULT(hr);
  8937. END_LEAVE_SCRIPT(scriptContext);
  8938. }
  8939. else
  8940. {
  8941. Assert(dwReferencingSourceContext != Js::Constants::NoHostSourceContext);
  8942. BEGIN_LEAVE_SCRIPT(scriptContext);
  8943. BEGIN_TRANSLATE_TO_HRESULT(static_cast<ExceptionType>(ExceptionType_OutOfMemory | ExceptionType_StackOverflow));
  8944. hr = scriptContext->GetHostScriptContext()->FetchImportedModuleFromScript(dwReferencingSourceContext, moduleName, &moduleRecordBase);
  8945. END_TRANSLATE_EXCEPTION_TO_HRESULT(hr);
  8946. END_LEAVE_SCRIPT(scriptContext);
  8947. }
  8948. if (FAILED(hr))
  8949. {
  8950. // We cannot just use the buffer in the specifier string - need to make a copy here.
  8951. size_t length = wcslen(moduleName);
  8952. char16* allocatedString = RecyclerNewArrayLeaf(scriptContext->GetRecycler(), char16, length + 1);
  8953. wmemcpy_s(allocatedString, length + 1, moduleName, length);
  8954. allocatedString[length] = _u('\0');
  8955. Js::JavascriptError *error = scriptContext->GetLibrary()->CreateURIError();
  8956. JavascriptError::SetErrorMessageProperties(error, hr, allocatedString, scriptContext);
  8957. return SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, error, scriptContext);
  8958. }
  8959. moduleRecord = SourceTextModuleRecord::FromHost(moduleRecordBase);
  8960. if (moduleRecord->GetErrorObject() != nullptr)
  8961. {
  8962. return SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(false, moduleRecord->GetErrorObject(), scriptContext, moduleRecord);
  8963. }
  8964. else if (moduleRecord->WasEvaluated())
  8965. {
  8966. return SourceTextModuleRecord::ResolveOrRejectDynamicImportPromise(true, moduleRecord->GetNamespace(), scriptContext, moduleRecord);
  8967. }
  8968. return moduleRecord->PostProcessDynamicModuleImport();
  8969. JIT_HELPER_END(ImportCall);
  8970. }
  8971. Var JavascriptOperators::OP_ResumeYield(ResumeYieldData* yieldData, RecyclableObject* iterator)
  8972. {
  8973. JIT_HELPER_REENTRANT_HEADER(ResumeYield);
  8974. bool isNext = yieldData->exceptionObj == nullptr;
  8975. bool isThrow = !isNext && !yieldData->exceptionObj->IsGeneratorReturnException();
  8976. if (iterator != nullptr) // yield*
  8977. {
  8978. ScriptContext* scriptContext = iterator->GetScriptContext();
  8979. PropertyId propertyId = isNext ? PropertyIds::next : isThrow ? PropertyIds::throw_ : PropertyIds::return_;
  8980. Var prop = JavascriptOperators::GetProperty(iterator, propertyId, scriptContext);
  8981. if (!isNext && JavascriptOperators::IsUndefinedOrNull(prop))
  8982. {
  8983. if (isThrow)
  8984. {
  8985. // 5.b.iii.2
  8986. // NOTE: If iterator does not have a throw method, this throw is going to terminate the yield* loop.
  8987. // But first we need to give iterator a chance to clean up.
  8988. prop = JavascriptOperators::GetProperty(iterator, PropertyIds::return_, scriptContext);
  8989. if (!JavascriptOperators::IsUndefinedOrNull(prop))
  8990. {
  8991. if (!JavascriptConversion::IsCallable(prop))
  8992. {
  8993. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, _u("return"));
  8994. }
  8995. Var result = nullptr;
  8996. RecyclableObject* method = RecyclableObject::FromVar(prop);
  8997. BEGIN_SAFE_REENTRANT_CALL(scriptContext->GetThreadContext())
  8998. {
  8999. Var args[] = { iterator, yieldData->data };
  9000. CallInfo callInfo(CallFlags_Value, _countof(args));
  9001. result = JavascriptFunction::CallFunction<true>(method, method->GetEntryPoint(), Arguments(callInfo, args));
  9002. }
  9003. END_SAFE_REENTRANT_CALL
  9004. if (!JavascriptOperators::IsObject(result))
  9005. {
  9006. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject);
  9007. }
  9008. }
  9009. // 5.b.iii.3
  9010. // NOTE: The next step throws a TypeError to indicate that there was a yield* protocol violation:
  9011. // iterator does not have a throw method.
  9012. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, _u("throw"));
  9013. }
  9014. // Do not use ThrowExceptionObject for return() API exceptions since these exceptions are not real exceptions
  9015. JavascriptExceptionOperators::DoThrow(yieldData->exceptionObj, scriptContext);
  9016. }
  9017. if (!JavascriptConversion::IsCallable(prop))
  9018. {
  9019. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, isNext ? _u("next") : isThrow ? _u("throw") : _u("return"));
  9020. }
  9021. RecyclableObject* method = RecyclableObject::FromVar(prop);
  9022. Var result = scriptContext->GetThreadContext()->ExecuteImplicitCall(method, Js::ImplicitCall_Accessor, [=]()->Js::Var
  9023. {
  9024. Var args[] = { iterator, yieldData->data };
  9025. CallInfo callInfo(CallFlags_Value, _countof(args));
  9026. return JavascriptFunction::CallFunction<true>(method, method->GetEntryPoint(), Arguments(callInfo, args));
  9027. });
  9028. if (!JavascriptOperators::IsObject(result))
  9029. {
  9030. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject);
  9031. }
  9032. if (isThrow || isNext)
  9033. {
  9034. // 5.b.ii.2
  9035. // NOTE: Exceptions from the inner iterator throw method are propagated.
  9036. // Normal completions from an inner throw method are processed similarly to an inner next.
  9037. return result;
  9038. }
  9039. RecyclableObject* obj = RecyclableObject::FromVar(result);
  9040. Var done = JavascriptOperators::GetProperty(obj, PropertyIds::done, scriptContext);
  9041. if (done == iterator->GetLibrary()->GetTrue())
  9042. {
  9043. Var value = JavascriptOperators::GetProperty(obj, PropertyIds::value, scriptContext);
  9044. yieldData->exceptionObj->SetThrownObject(value);
  9045. // Do not use ThrowExceptionObject for return() API exceptions since these exceptions are not real exceptions
  9046. JavascriptExceptionOperators::DoThrow(yieldData->exceptionObj, scriptContext);
  9047. }
  9048. return result;
  9049. }
  9050. // 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.
  9051. if (isNext)
  9052. {
  9053. return yieldData->data;
  9054. }
  9055. if (isThrow)
  9056. {
  9057. // Use ThrowExceptionObject() to get debugger support for breaking on throw
  9058. JavascriptExceptionOperators::ThrowExceptionObject(yieldData->exceptionObj, yieldData->exceptionObj->GetScriptContext(), true);
  9059. }
  9060. // CONSIDER: Using an exception to carry the return value and force finally code to execute is a bit of a janky
  9061. // solution since we have to override the value here in the case of yield* expressions. It works but is there
  9062. // a more elegant way?
  9063. //
  9064. // Instead what if ResumeYield was a "set Dst then optionally branch" opcode, that could also throw? Then we could
  9065. // avoid using a special exception entirely with byte code something like this:
  9066. //
  9067. // ;; Ry is the yieldData
  9068. //
  9069. // ResumeYield Rx Ry $returnPathLabel
  9070. // ... code like normal
  9071. // $returnPathLabel:
  9072. // Ld_A R0 Rx
  9073. // Br $exitFinallyAndReturn
  9074. //
  9075. // This would probably give better performance for the common case of calling next() on generators since we wouldn't
  9076. // have to wrap the call to the generator code in a try catch.
  9077. // Do not use ThrowExceptionObject for return() API exceptions since these exceptions are not real exceptions
  9078. JavascriptExceptionOperators::DoThrow(yieldData->exceptionObj, yieldData->exceptionObj->GetScriptContext());
  9079. JIT_HELPER_END(ResumeYield);
  9080. }
  9081. Js::Var
  9082. JavascriptOperators::BoxStackInstance(Js::Var instance, ScriptContext * scriptContext, bool allowStackFunction, bool deepCopy)
  9083. {
  9084. if (!ThreadContext::IsOnStack(instance) || (allowStackFunction && !TaggedNumber::Is(instance) && (*(int*)instance & 1)))
  9085. {
  9086. return instance;
  9087. }
  9088. TypeId typeId = JavascriptOperators::GetTypeId(instance);
  9089. switch (typeId)
  9090. {
  9091. case Js::TypeIds_Number:
  9092. #if !FLOATVAR
  9093. return JavascriptNumber::BoxStackInstance(instance, scriptContext);
  9094. #endif
  9095. // fall-through
  9096. case Js::TypeIds_Integer:
  9097. return instance;
  9098. case Js::TypeIds_RegEx:
  9099. return JavascriptRegExp::BoxStackInstance(JavascriptRegExp::FromVar(instance), deepCopy);
  9100. case Js::TypeIds_Object:
  9101. return DynamicObject::BoxStackInstance(DynamicObject::FromVar(instance), deepCopy);
  9102. case Js::TypeIds_Array:
  9103. return JavascriptArray::BoxStackInstance(JavascriptArray::UnsafeFromVar(instance), deepCopy);
  9104. case Js::TypeIds_NativeIntArray:
  9105. return JavascriptNativeIntArray::BoxStackInstance(JavascriptNativeIntArray::UnsafeFromVar(instance), deepCopy);
  9106. case Js::TypeIds_NativeFloatArray:
  9107. return JavascriptNativeFloatArray::BoxStackInstance(JavascriptNativeFloatArray::UnsafeFromVar(instance), deepCopy);
  9108. case Js::TypeIds_Function:
  9109. Assert(allowStackFunction);
  9110. // Stack functions are deal with not mar mark them, but by nested function escape analysis
  9111. // in the front end. No need to box here.
  9112. return instance;
  9113. #if ENABLE_COPYONACCESS_ARRAY
  9114. case Js::TypeIds_CopyOnAccessNativeIntArray:
  9115. Assert(false);
  9116. // fall-through
  9117. #endif
  9118. default:
  9119. Assert(false);
  9120. return instance;
  9121. };
  9122. }
  9123. ImplicitCallFlags
  9124. JavascriptOperators::CacheAndClearImplicitBit(ScriptContext* scriptContext)
  9125. {
  9126. ImplicitCallFlags prevImplicitCallFlags = scriptContext->GetThreadContext()->GetImplicitCallFlags();
  9127. scriptContext->GetThreadContext()->ClearImplicitCallFlags();
  9128. return prevImplicitCallFlags;
  9129. }
  9130. ImplicitCallFlags
  9131. JavascriptOperators::CheckAndUpdateFunctionBodyWithImplicitFlag(FunctionBody* functionBody)
  9132. {
  9133. ScriptContext* scriptContext = functionBody->GetScriptContext();
  9134. ImplicitCallFlags currImplicitCallFlags = scriptContext->GetThreadContext()->GetImplicitCallFlags();
  9135. if ((currImplicitCallFlags > ImplicitCall_None))
  9136. {
  9137. functionBody->SetHasOnlyThisStmts(false);
  9138. }
  9139. return currImplicitCallFlags;
  9140. }
  9141. void
  9142. JavascriptOperators::RestoreImplicitFlag(ScriptContext* scriptContext, ImplicitCallFlags prevImplicitCallFlags, ImplicitCallFlags currImplicitCallFlags)
  9143. {
  9144. scriptContext->GetThreadContext()->SetImplicitCallFlags((ImplicitCallFlags)(prevImplicitCallFlags | currImplicitCallFlags));
  9145. }
  9146. FunctionProxy*
  9147. JavascriptOperators::GetDeferredDeserializedFunctionProxy(JavascriptFunction* func)
  9148. {
  9149. FunctionProxy* proxy = func->GetFunctionProxy();
  9150. Assert(proxy->GetFunctionInfo()->GetFunctionProxy() != proxy);
  9151. return proxy;
  9152. }
  9153. template <>
  9154. Js::Var JavascriptOperators::GetElementAtIndex(Js::JavascriptArray* arrayObject, UINT index, Js::ScriptContext* scriptContext)
  9155. {
  9156. Js::Var result;
  9157. if (Js::JavascriptOperators::OP_GetElementI_ArrayFastPath(arrayObject, index, &result, scriptContext))
  9158. {
  9159. return result;
  9160. }
  9161. return scriptContext->GetMissingItemResult();
  9162. }
  9163. template<>
  9164. Js::Var JavascriptOperators::GetElementAtIndex(Js::JavascriptNativeIntArray* arrayObject, UINT index, Js::ScriptContext* scriptContext)
  9165. {
  9166. Js::Var result;
  9167. if (Js::JavascriptOperators::OP_GetElementI_ArrayFastPath(arrayObject, index, &result, scriptContext))
  9168. {
  9169. return result;
  9170. }
  9171. return scriptContext->GetMissingItemResult();
  9172. }
  9173. template<>
  9174. Js::Var JavascriptOperators::GetElementAtIndex(Js::JavascriptNativeFloatArray* arrayObject, UINT index, Js::ScriptContext* scriptContext)
  9175. {
  9176. Js::Var result;
  9177. if (Js::JavascriptOperators::OP_GetElementI_ArrayFastPath(arrayObject, index, &result, scriptContext))
  9178. {
  9179. return result;
  9180. }
  9181. return scriptContext->GetMissingItemResult();
  9182. }
  9183. template<>
  9184. Js::Var JavascriptOperators::GetElementAtIndex(Js::Var* arrayObject, UINT index, Js::ScriptContext* scriptContext)
  9185. {
  9186. return Js::JavascriptOperators::OP_GetElementI_Int32(*arrayObject, index, scriptContext);
  9187. }
  9188. template<typename T>
  9189. void JavascriptOperators::ObjectToNativeArray(T* arrayObject,
  9190. JsNativeValueType valueType,
  9191. __in UINT length,
  9192. __in UINT elementSize,
  9193. __out_bcount(length*elementSize) byte* buffer,
  9194. Js::ScriptContext* scriptContext)
  9195. {
  9196. Var element;
  9197. uint64 allocSize = UInt32Math::Mul(length, elementSize);
  9198. // TODO:further fast path the call for things like IntArray convert to int, floatarray convert to float etc.
  9199. // such that we don't need boxing.
  9200. switch (valueType)
  9201. {
  9202. case JsInt8Type:
  9203. AnalysisAssert(elementSize == sizeof(int8));
  9204. for (UINT i = 0; i < length; i++)
  9205. {
  9206. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9207. AnalysisAssert((i + 1) * sizeof(int8) <= allocSize);
  9208. #pragma prefast(suppress:22102)
  9209. ((int8*)buffer)[i] = Js::JavascriptConversion::ToInt8(element, scriptContext);
  9210. }
  9211. break;
  9212. case JsUint8Type:
  9213. AnalysisAssert(elementSize == sizeof(uint8));
  9214. for (UINT i = 0; i < length; i++)
  9215. {
  9216. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9217. AnalysisAssert((i + 1) * sizeof(uint8) <= allocSize);
  9218. ((uint8*)buffer)[i] = Js::JavascriptConversion::ToUInt8(element, scriptContext);
  9219. }
  9220. break;
  9221. case JsInt16Type:
  9222. AnalysisAssert(elementSize == sizeof(int16));
  9223. for (UINT i = 0; i < length; i++)
  9224. {
  9225. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9226. AnalysisAssert((i + 1) * sizeof(int16) <= allocSize);
  9227. ((int16*)buffer)[i] = Js::JavascriptConversion::ToInt16(element, scriptContext);
  9228. }
  9229. break;
  9230. case JsUint16Type:
  9231. AnalysisAssert(elementSize == sizeof(uint16));
  9232. for (UINT i = 0; i < length; i++)
  9233. {
  9234. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9235. AnalysisAssert((i + 1) * sizeof(uint16) <= allocSize);
  9236. ((uint16*)buffer)[i] = Js::JavascriptConversion::ToUInt16(element, scriptContext);
  9237. }
  9238. break;
  9239. case JsInt32Type:
  9240. AnalysisAssert(elementSize == sizeof(int32));
  9241. for (UINT i = 0; i < length; i++)
  9242. {
  9243. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9244. AnalysisAssert((i + 1) * sizeof(int32) <= allocSize);
  9245. ((int32*)buffer)[i] = Js::JavascriptConversion::ToInt32(element, scriptContext);
  9246. }
  9247. break;
  9248. case JsUint32Type:
  9249. AnalysisAssert(elementSize == sizeof(uint32));
  9250. for (UINT i = 0; i < length; i++)
  9251. {
  9252. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9253. AnalysisAssert((i + 1) * sizeof(uint32) <= allocSize);
  9254. ((uint32*)buffer)[i] = Js::JavascriptConversion::ToUInt32(element, scriptContext);
  9255. }
  9256. break;
  9257. case JsInt64Type:
  9258. AnalysisAssert(elementSize == sizeof(int64));
  9259. for (UINT i = 0; i < length; i++)
  9260. {
  9261. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9262. AnalysisAssert((i + 1) * sizeof(int64) <= allocSize);
  9263. ((int64*)buffer)[i] = Js::JavascriptConversion::ToInt64(element, scriptContext);
  9264. }
  9265. break;
  9266. case JsUint64Type:
  9267. AnalysisAssert(elementSize == sizeof(uint64));
  9268. for (UINT i = 0; i < length; i++)
  9269. {
  9270. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9271. AnalysisAssert((i + 1) * sizeof(uint64) <= allocSize);
  9272. ((uint64*)buffer)[i] = Js::JavascriptConversion::ToUInt64(element, scriptContext);
  9273. }
  9274. break;
  9275. case JsFloatType:
  9276. AnalysisAssert(elementSize == sizeof(float));
  9277. for (UINT i = 0; i < length; i++)
  9278. {
  9279. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9280. AnalysisAssert((i + 1) * sizeof(float) <= allocSize);
  9281. ((float*)buffer)[i] = Js::JavascriptConversion::ToFloat(element, scriptContext);
  9282. }
  9283. break;
  9284. case JsDoubleType:
  9285. AnalysisAssert(elementSize == sizeof(double));
  9286. for (UINT i = 0; i < length; i++)
  9287. {
  9288. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9289. AnalysisAssert((i + 1) * sizeof(double) <= allocSize);
  9290. ((double*)buffer)[i] = Js::JavascriptConversion::ToNumber(element, scriptContext);
  9291. }
  9292. break;
  9293. case JsNativeStringType:
  9294. AnalysisAssert(elementSize == sizeof(JsNativeString));
  9295. for (UINT i = 0; i < length; i++)
  9296. {
  9297. element = GetElementAtIndex(arrayObject, i, scriptContext);
  9298. AnalysisAssert((i + 1) * sizeof(JsNativeString) <= allocSize);
  9299. Js::JavascriptString* string = Js::JavascriptConversion::ToString(element, scriptContext);
  9300. (((JsNativeString*)buffer)[i]).str = string->GetSz();
  9301. (((JsNativeString*)buffer)[i]).length = string->GetLength();
  9302. }
  9303. break;
  9304. default:
  9305. Assert(FALSE);
  9306. }
  9307. }
  9308. void JavascriptOperators::VarToNativeArray(Var arrayObject,
  9309. JsNativeValueType valueType,
  9310. __in UINT length,
  9311. __in UINT elementSize,
  9312. __out_bcount(length*elementSize) byte* buffer,
  9313. Js::ScriptContext* scriptContext)
  9314. {
  9315. Js::DynamicObject* dynamicObject = DynamicObject::FromVar(arrayObject);
  9316. if (dynamicObject->IsCrossSiteObject() || Js::TaggedInt::IsOverflow(length))
  9317. {
  9318. Js::JavascriptOperators::ObjectToNativeArray(&arrayObject, valueType, length, elementSize, buffer, scriptContext);
  9319. }
  9320. else
  9321. {
  9322. #if ENABLE_COPYONACCESS_ARRAY
  9323. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(arrayObject);
  9324. #endif
  9325. switch (Js::JavascriptOperators::GetTypeId(arrayObject))
  9326. {
  9327. case TypeIds_Array:
  9328. Js::JavascriptOperators::ObjectToNativeArray(Js::JavascriptArray::UnsafeFromVar(arrayObject), valueType, length, elementSize, buffer, scriptContext);
  9329. break;
  9330. case TypeIds_NativeFloatArray:
  9331. Js::JavascriptOperators::ObjectToNativeArray(Js::JavascriptNativeFloatArray::UnsafeFromVar(arrayObject), valueType, length, elementSize, buffer, scriptContext);
  9332. break;
  9333. case TypeIds_NativeIntArray:
  9334. Js::JavascriptOperators::ObjectToNativeArray(Js::JavascriptNativeIntArray::UnsafeFromVar(arrayObject), valueType, length, elementSize, buffer, scriptContext);
  9335. break;
  9336. // We can have more specialized template if needed.
  9337. default:
  9338. Js::JavascriptOperators::ObjectToNativeArray(&arrayObject, valueType, length, elementSize, buffer, scriptContext);
  9339. }
  9340. }
  9341. }
  9342. // SpeciesConstructor abstract operation as described in ES6.0 Section 7.3.20
  9343. RecyclableObject* JavascriptOperators::SpeciesConstructor(_In_ RecyclableObject* object, _In_ JavascriptFunction* defaultConstructor, _In_ ScriptContext* scriptContext)
  9344. {
  9345. //1.Assert: Type(O) is Object.
  9346. Assert(JavascriptOperators::IsObject(object));
  9347. //2.Let C be Get(O, "constructor").
  9348. //3.ReturnIfAbrupt(C).
  9349. Var constructor = JavascriptOperators::GetProperty(object, PropertyIds::constructor, scriptContext);
  9350. if (scriptContext->GetConfig()->IsES6SpeciesEnabled())
  9351. {
  9352. //4.If C is undefined, return defaultConstructor.
  9353. if (JavascriptOperators::IsUndefinedObject(constructor))
  9354. {
  9355. return defaultConstructor;
  9356. }
  9357. //5.If Type(C) is not Object, throw a TypeError exception.
  9358. if (!JavascriptOperators::IsObject(constructor))
  9359. {
  9360. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject, _u("[constructor]"));
  9361. }
  9362. //6.Let S be Get(C, @@species).
  9363. //7.ReturnIfAbrupt(S).
  9364. Var species = nullptr;
  9365. if (!JavascriptOperators::GetProperty(RecyclableObject::FromVar(constructor),
  9366. PropertyIds::_symbolSpecies, &species, scriptContext)
  9367. || JavascriptOperators::IsUndefinedOrNull(species))
  9368. {
  9369. //8.If S is either undefined or null, return defaultConstructor.
  9370. return defaultConstructor;
  9371. }
  9372. constructor = species;
  9373. }
  9374. //9.If IsConstructor(S) is true, return S.
  9375. RecyclableObject* constructorObj = JavascriptOperators::TryFromVar<RecyclableObject>(constructor);
  9376. if (constructorObj && JavascriptOperators::IsConstructor(constructorObj))
  9377. {
  9378. return constructorObj;
  9379. }
  9380. //10.Throw a TypeError exception.
  9381. JavascriptError::ThrowTypeError(scriptContext, JSERR_NotAConstructor, _u("constructor[Symbol.species]"));
  9382. }
  9383. BOOL JavascriptOperators::GreaterEqual(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9384. {
  9385. JIT_HELPER_REENTRANT_HEADER(Op_GreaterEqual);
  9386. if (TaggedInt::Is(aLeft))
  9387. {
  9388. if (TaggedInt::Is(aRight))
  9389. {
  9390. // Works whether it is TaggedInt31 or TaggedInt32
  9391. return ::Math::PointerCastToIntegralTruncate<int>(aLeft) >= ::Math::PointerCastToIntegralTruncate<int>(aRight);
  9392. }
  9393. if (JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9394. {
  9395. return TaggedInt::ToDouble(aLeft) >= JavascriptNumber::GetValue(aRight);
  9396. }
  9397. }
  9398. else if (TaggedInt::Is(aRight))
  9399. {
  9400. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft))
  9401. {
  9402. return JavascriptNumber::GetValue(aLeft) >= TaggedInt::ToDouble(aRight);
  9403. }
  9404. }
  9405. else
  9406. {
  9407. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft) && JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9408. {
  9409. return JavascriptNumber::GetValue(aLeft) >= JavascriptNumber::GetValue(aRight);
  9410. }
  9411. }
  9412. return !RelationalComparisonHelper(aLeft, aRight, scriptContext, true, true);
  9413. JIT_HELPER_END(Op_GreaterEqual);
  9414. }
  9415. BOOL JavascriptOperators::LessEqual(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9416. {
  9417. JIT_HELPER_REENTRANT_HEADER(Op_LessEqual);
  9418. if (TaggedInt::Is(aLeft))
  9419. {
  9420. if (TaggedInt::Is(aRight))
  9421. {
  9422. // Works whether it is TaggedInt31 or TaggedInt32
  9423. return ::Math::PointerCastToIntegralTruncate<int>(aLeft) <= ::Math::PointerCastToIntegralTruncate<int>(aRight);
  9424. }
  9425. if (JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9426. {
  9427. return TaggedInt::ToDouble(aLeft) <= JavascriptNumber::GetValue(aRight);
  9428. }
  9429. }
  9430. else if (TaggedInt::Is(aRight))
  9431. {
  9432. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft))
  9433. {
  9434. return JavascriptNumber::GetValue(aLeft) <= TaggedInt::ToDouble(aRight);
  9435. }
  9436. }
  9437. else
  9438. {
  9439. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft) && JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9440. {
  9441. return JavascriptNumber::GetValue(aLeft) <= JavascriptNumber::GetValue(aRight);
  9442. }
  9443. }
  9444. return !RelationalComparisonHelper(aRight, aLeft, scriptContext, false, true);
  9445. JIT_HELPER_END(Op_LessEqual);
  9446. }
  9447. BOOL JavascriptOperators::NotEqual(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9448. {
  9449. JIT_HELPER_REENTRANT_HEADER(Op_NotEqual);
  9450. JIT_HELPER_SAME_ATTRIBUTES(Op_NotEqual, Op_Equal);
  9451. //
  9452. // TODO: Change to use Abstract Equality Comparison Algorithm (ES3.0: S11.9.3):
  9453. // - Evaluate left, then right, operands to preserve correct evaluation order.
  9454. // - Call algorithm, potentially reversing arguments.
  9455. //
  9456. return !Equal(aLeft, aRight, scriptContext);
  9457. JIT_HELPER_END(Op_NotEqual);
  9458. }
  9459. // NotStrictEqual() returns whether the two vars have strict equality, as
  9460. // described in (ES3.0: S11.9.5, S11.9.6).
  9461. BOOL JavascriptOperators::NotStrictEqual(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9462. {
  9463. JIT_HELPER_REENTRANT_HEADER(Op_NotStrictEqual);
  9464. JIT_HELPER_SAME_ATTRIBUTES(Op_NotStrictEqual, Op_StrictEqual);
  9465. return !StrictEqual(aLeft, aRight, scriptContext);
  9466. JIT_HELPER_END(Op_NotStrictEqual);
  9467. }
  9468. bool JavascriptOperators::CheckIfObjectAndPrototypeChainHasOnlyWritableDataProperties(_In_ RecyclableObject* object)
  9469. {
  9470. return object->GetLibrary()->GetTypesWithOnlyWritablePropertyProtoChainCache()->Check(object);
  9471. }
  9472. bool JavascriptOperators::CheckIfPrototypeChainHasOnlyWritableDataProperties(_In_ RecyclableObject* prototype)
  9473. {
  9474. return prototype->GetLibrary()->GetTypesWithOnlyWritablePropertyProtoChainCache()->CheckProtoChain(prototype);
  9475. }
  9476. bool JavascriptOperators::CheckIfObjectAndProtoChainHasNoSpecialProperties(_In_ RecyclableObject* object)
  9477. {
  9478. return object->GetLibrary()->GetTypesWithNoSpecialPropertyProtoChainCache()->Check(object);
  9479. }
  9480. // Checks to see if the specified object (which should be a prototype object)
  9481. // contains a proxy anywhere in the prototype chain.
  9482. bool JavascriptOperators::CheckIfPrototypeChainContainsProxyObject(RecyclableObject* prototype)
  9483. {
  9484. if (prototype == nullptr)
  9485. {
  9486. return false;
  9487. }
  9488. Assert(JavascriptOperators::IsObjectOrNull(prototype));
  9489. while (prototype->GetTypeId() != TypeIds_Null)
  9490. {
  9491. if (prototype->GetTypeId() == TypeIds_Proxy)
  9492. {
  9493. return true;
  9494. }
  9495. prototype = prototype->GetPrototype();
  9496. }
  9497. return false;
  9498. }
  9499. BOOL JavascriptOperators::Equal(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9500. {
  9501. JIT_HELPER_REENTRANT_HEADER(Op_Equal);
  9502. JIT_HELPER_SAME_ATTRIBUTES(Op_Equal, Op_Equal_Full);
  9503. if (aLeft == aRight)
  9504. {
  9505. if (TaggedInt::Is(aLeft) || JavascriptObject::Is(aLeft))
  9506. {
  9507. return true;
  9508. }
  9509. else
  9510. {
  9511. return Equal_Full(aLeft, aRight, scriptContext);
  9512. }
  9513. }
  9514. if (JavascriptString::Is(aLeft) && JavascriptString::Is(aRight))
  9515. {
  9516. JavascriptString* left = (JavascriptString*)aLeft;
  9517. JavascriptString* right = (JavascriptString*)aRight;
  9518. if (left->GetLength() == right->GetLength())
  9519. {
  9520. if (left->UnsafeGetBuffer() != NULL && right->UnsafeGetBuffer() != NULL)
  9521. {
  9522. if (left->GetLength() == 1)
  9523. {
  9524. return left->UnsafeGetBuffer()[0] == right->UnsafeGetBuffer()[0];
  9525. }
  9526. return memcmp(left->UnsafeGetBuffer(), right->UnsafeGetBuffer(), left->GetLength() * sizeof(left->UnsafeGetBuffer()[0])) == 0;
  9527. }
  9528. // fall through to Equal_Full
  9529. }
  9530. else
  9531. {
  9532. return false;
  9533. }
  9534. }
  9535. return Equal_Full(aLeft, aRight, scriptContext);
  9536. JIT_HELPER_END(Op_Equal);
  9537. }
  9538. BOOL JavascriptOperators::Greater(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9539. {
  9540. JIT_HELPER_REENTRANT_HEADER(Op_Greater);
  9541. if (TaggedInt::Is(aLeft))
  9542. {
  9543. if (TaggedInt::Is(aRight))
  9544. {
  9545. // Works whether it is TaggedInt31 or TaggedInt32
  9546. return ::Math::PointerCastToIntegralTruncate<int>(aLeft) > ::Math::PointerCastToIntegralTruncate<int>(aRight);
  9547. }
  9548. if (JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9549. {
  9550. return TaggedInt::ToDouble(aLeft) > JavascriptNumber::GetValue(aRight);
  9551. }
  9552. }
  9553. else if (TaggedInt::Is(aRight))
  9554. {
  9555. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft))
  9556. {
  9557. return JavascriptNumber::GetValue(aLeft) > TaggedInt::ToDouble(aRight);
  9558. }
  9559. }
  9560. else
  9561. {
  9562. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft) && JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9563. {
  9564. return JavascriptNumber::GetValue(aLeft) > JavascriptNumber::GetValue(aRight);
  9565. }
  9566. }
  9567. return Greater_Full(aLeft, aRight, scriptContext);
  9568. JIT_HELPER_END(Op_Greater);
  9569. }
  9570. BOOL JavascriptOperators::Less(Var aLeft, Var aRight, ScriptContext* scriptContext)
  9571. {
  9572. JIT_HELPER_REENTRANT_HEADER(Op_Less);
  9573. if (TaggedInt::Is(aLeft))
  9574. {
  9575. if (TaggedInt::Is(aRight))
  9576. {
  9577. // Works whether it is TaggedInt31 or TaggedInt32
  9578. return ::Math::PointerCastToIntegralTruncate<int>(aLeft) < ::Math::PointerCastToIntegralTruncate<int>(aRight);
  9579. }
  9580. if (JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9581. {
  9582. return TaggedInt::ToDouble(aLeft) < JavascriptNumber::GetValue(aRight);
  9583. }
  9584. }
  9585. else if (TaggedInt::Is(aRight))
  9586. {
  9587. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft))
  9588. {
  9589. return JavascriptNumber::GetValue(aLeft) < TaggedInt::ToDouble(aRight);
  9590. }
  9591. }
  9592. else
  9593. {
  9594. if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft) && JavascriptNumber::Is_NoTaggedIntCheck(aRight))
  9595. {
  9596. return JavascriptNumber::GetValue(aLeft) < JavascriptNumber::GetValue(aRight);
  9597. }
  9598. }
  9599. return Less_Full(aLeft, aRight, scriptContext);
  9600. JIT_HELPER_END(Op_Less);
  9601. }
  9602. RecyclableObject* JavascriptOperators::ToObject(Var aRight, ScriptContext* scriptContext)
  9603. {
  9604. JIT_HELPER_NOT_REENTRANT_HEADER(Op_ConvObject, reentrancylock, scriptContext->GetThreadContext());
  9605. RecyclableObject* object = nullptr;
  9606. if (FALSE == JavascriptConversion::ToObject(aRight, scriptContext, &object))
  9607. {
  9608. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject /* TODO-ERROR: get arg name - aValue */);
  9609. }
  9610. return object;
  9611. JIT_HELPER_END(Op_ConvObject);
  9612. }
  9613. Var JavascriptOperators::ToUnscopablesWrapperObject(Var aRight, ScriptContext* scriptContext)
  9614. {
  9615. JIT_HELPER_NOT_REENTRANT_HEADER(Op_NewUnscopablesWrapperObject, reentrancylock, scriptContext->GetThreadContext());
  9616. RecyclableObject* object = RecyclableObject::FromVar(aRight);
  9617. UnscopablesWrapperObject* withWrapper = RecyclerNew(scriptContext->GetRecycler(), UnscopablesWrapperObject, object, scriptContext->GetLibrary()->GetWithType());
  9618. return withWrapper;
  9619. JIT_HELPER_END(Op_NewUnscopablesWrapperObject);
  9620. }
  9621. Var JavascriptOperators::ToNumber(Var aRight, ScriptContext* scriptContext)
  9622. {
  9623. JIT_HELPER_REENTRANT_HEADER(Op_ConvNumber_Full);
  9624. if (TaggedInt::Is(aRight) || (JavascriptNumber::Is_NoTaggedIntCheck(aRight)))
  9625. {
  9626. return aRight;
  9627. }
  9628. return JavascriptNumber::ToVarIntCheck(JavascriptConversion::ToNumber_Full(aRight, scriptContext), scriptContext);
  9629. JIT_HELPER_END(Op_ConvNumber_Full);
  9630. }
  9631. BOOL JavascriptOperators::IsObject(_In_ RecyclableObject* instance)
  9632. {
  9633. return GetTypeId(instance) > TypeIds_LastJavascriptPrimitiveType;
  9634. }
  9635. BOOL JavascriptOperators::IsObject(_In_ Var instance)
  9636. {
  9637. JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_IsObject);
  9638. return GetTypeId(instance) > TypeIds_LastJavascriptPrimitiveType;
  9639. JIT_HELPER_END(Op_IsObject);
  9640. }
  9641. BOOL JavascriptOperators::IsObjectType(TypeId typeId)
  9642. {
  9643. return typeId > TypeIds_LastJavascriptPrimitiveType;
  9644. }
  9645. BOOL JavascriptOperators::IsExposedType(TypeId typeId)
  9646. {
  9647. return typeId <= TypeIds_LastTrueJavascriptObjectType && typeId != TypeIds_HostDispatch;
  9648. }
  9649. BOOL JavascriptOperators::IsObjectOrNull(Var instance)
  9650. {
  9651. TypeId typeId = GetTypeId(instance);
  9652. return IsObjectType(typeId) || typeId == TypeIds_Null;
  9653. }
  9654. BOOL JavascriptOperators::IsUndefined(_In_ RecyclableObject* instance)
  9655. {
  9656. return JavascriptOperators::GetTypeId(instance) == TypeIds_Undefined;
  9657. }
  9658. BOOL JavascriptOperators::IsUndefined(Var instance)
  9659. {
  9660. return JavascriptOperators::GetTypeId(instance) == TypeIds_Undefined;
  9661. }
  9662. BOOL JavascriptOperators::IsUndefinedOrNullType(TypeId typeId)
  9663. {
  9664. return typeId <= TypeIds_UndefinedOrNull;
  9665. }
  9666. BOOL JavascriptOperators::IsUndefinedOrNull(Var instance)
  9667. {
  9668. return IsUndefinedOrNullType(JavascriptOperators::GetTypeId(instance));
  9669. }
  9670. BOOL JavascriptOperators::IsUndefinedOrNull(RecyclableObject* instance)
  9671. {
  9672. return JavascriptOperators::IsUndefinedOrNullType(instance->GetTypeId());
  9673. }
  9674. BOOL JavascriptOperators::IsUndefinedOrNull(Var instance, ScriptContext* scriptContext)
  9675. {
  9676. JavascriptLibrary* library = scriptContext->GetLibrary();
  9677. return IsUndefinedObject(instance, library) || IsNull(instance, library);
  9678. }
  9679. BOOL JavascriptOperators::IsUndefinedOrNull(Var instance, JavascriptLibrary* library)
  9680. {
  9681. return IsUndefinedObject(instance, library) || IsNull(instance, library);
  9682. }
  9683. BOOL JavascriptOperators::IsNull(Var instance)
  9684. {
  9685. return JavascriptOperators::GetTypeId(instance) == TypeIds_Null;
  9686. }
  9687. BOOL JavascriptOperators::IsNull(Var instance, ScriptContext* scriptContext)
  9688. {
  9689. return JavascriptOperators::IsNull(instance, scriptContext->GetLibrary());
  9690. }
  9691. BOOL JavascriptOperators::IsNull(Var instance, JavascriptLibrary* library)
  9692. {
  9693. Assert(!RecyclableObject::Is(instance) ? TRUE : ((RecyclableObject*)instance)->GetScriptContext()->GetLibrary() == library );
  9694. return library->GetNull() == instance;
  9695. }
  9696. BOOL JavascriptOperators::IsNull(RecyclableObject* instance)
  9697. {
  9698. return instance->GetType()->GetTypeId() == TypeIds_Null;
  9699. }
  9700. BOOL JavascriptOperators::IsSpecialObjectType(TypeId typeId)
  9701. {
  9702. return typeId > TypeIds_LastTrueJavascriptObjectType;
  9703. }
  9704. BOOL JavascriptOperators::IsUndefinedObject(Var instance)
  9705. {
  9706. return JavascriptOperators::GetTypeId(instance) == TypeIds_Undefined;
  9707. }
  9708. BOOL JavascriptOperators::IsUndefinedObject(RecyclableObject* instance)
  9709. {
  9710. return instance->GetType()->GetTypeId() == TypeIds_Undefined;
  9711. }
  9712. BOOL JavascriptOperators::IsUndefinedObject(Var instance, RecyclableObject *libraryUndefined)
  9713. {
  9714. Assert(JavascriptOperators::IsUndefinedObject(libraryUndefined));
  9715. AssertMsg((instance == libraryUndefined)
  9716. == JavascriptOperators::IsUndefinedObject(instance), "Wrong ScriptContext?");
  9717. return instance == libraryUndefined;
  9718. }
  9719. BOOL JavascriptOperators::IsUndefinedObject(Var instance, ScriptContext *scriptContext)
  9720. {
  9721. return JavascriptOperators::IsUndefinedObject(instance, scriptContext->GetLibrary());
  9722. }
  9723. BOOL JavascriptOperators::IsUndefinedObject(Var instance, JavascriptLibrary* library)
  9724. {
  9725. Assert(!RecyclableObject::Is(instance) ? TRUE : ((RecyclableObject*)instance)->GetScriptContext()->GetLibrary() == library );
  9726. return JavascriptOperators::IsUndefinedObject(instance, library->GetUndefined());
  9727. }
  9728. BOOL JavascriptOperators::IsAnyNumberValue(Var instance)
  9729. {
  9730. TypeId typeId = GetTypeId(instance);
  9731. return TypeIds_FirstNumberType <= typeId && typeId <= TypeIds_LastNumberType;
  9732. }
  9733. // GetIterator as described in ES6.0 (draft 22) Section 7.4.1
  9734. RecyclableObject* JavascriptOperators::GetIterator(Var iterable, ScriptContext* scriptContext, bool optional)
  9735. {
  9736. RecyclableObject* iterableObj = JavascriptOperators::ToObject(iterable, scriptContext);
  9737. return JavascriptOperators::GetIterator(iterableObj, scriptContext, optional);
  9738. }
  9739. RecyclableObject* JavascriptOperators::GetIteratorFunction(Var iterable, ScriptContext* scriptContext, bool optional)
  9740. {
  9741. RecyclableObject* iterableObj = JavascriptOperators::ToObject(iterable, scriptContext);
  9742. return JavascriptOperators::GetIteratorFunction(iterableObj, scriptContext, optional);
  9743. }
  9744. RecyclableObject* JavascriptOperators::GetIteratorFunction(RecyclableObject* instance, ScriptContext * scriptContext, bool optional)
  9745. {
  9746. Var func = JavascriptOperators::GetPropertyNoCache(instance, PropertyIds::_symbolIterator, scriptContext);
  9747. if (optional && JavascriptOperators::IsUndefinedOrNull(func))
  9748. {
  9749. return nullptr;
  9750. }
  9751. if (!JavascriptConversion::IsCallable(func))
  9752. {
  9753. JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction);
  9754. }
  9755. RecyclableObject* function = RecyclableObject::FromVar(func);
  9756. return function;
  9757. }
  9758. RecyclableObject* JavascriptOperators::GetIterator(RecyclableObject* instance, ScriptContext * scriptContext, bool optional)
  9759. {
  9760. RecyclableObject* function = GetIteratorFunction(instance, scriptContext, optional);
  9761. if (function == nullptr)
  9762. {
  9763. Assert(optional);
  9764. return nullptr;
  9765. }
  9766. Var iterator = scriptContext->GetThreadContext()->ExecuteImplicitCall(function, Js::ImplicitCall_Accessor, [=]()->Js::Var
  9767. {
  9768. return CALL_FUNCTION(scriptContext->GetThreadContext(), function, CallInfo(Js::CallFlags_Value, 1), instance);
  9769. });
  9770. if (!JavascriptOperators::IsObject(iterator))
  9771. {
  9772. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject);
  9773. }
  9774. return RecyclableObject::FromVar(iterator);
  9775. }
  9776. void JavascriptOperators::IteratorClose(RecyclableObject* iterator, ScriptContext* scriptContext)
  9777. {
  9778. try
  9779. {
  9780. Var func = JavascriptOperators::GetProperty(iterator, PropertyIds::return_, scriptContext);
  9781. if (JavascriptConversion::IsCallable(func))
  9782. {
  9783. RecyclableObject* callable = RecyclableObject::FromVar(func);
  9784. scriptContext->GetThreadContext()->ExecuteImplicitCall(callable, ImplicitCall_Accessor, [=]()->Var
  9785. {
  9786. Js::Var args[] = { iterator };
  9787. Js::CallInfo callInfo(Js::CallFlags_Value, _countof(args));
  9788. return JavascriptFunction::CallFunction<true>(callable, callable->GetEntryPoint(), Js::Arguments(callInfo, args));
  9789. });
  9790. }
  9791. }
  9792. catch (const JavascriptException& err)
  9793. {
  9794. err.GetAndClear(); // discard exception object
  9795. // We have arrived in this function due to AbruptCompletion (which is an exception), so we don't need to
  9796. // propagate the exception of calling return function
  9797. }
  9798. }
  9799. // IteratorNext as described in ES6.0 (draft 22) Section 7.4.2
  9800. RecyclableObject* JavascriptOperators::IteratorNext(RecyclableObject* iterator, ScriptContext* scriptContext, Var value)
  9801. {
  9802. Var func = JavascriptOperators::GetPropertyNoCache(iterator, PropertyIds::next, scriptContext);
  9803. ThreadContext *threadContext = scriptContext->GetThreadContext();
  9804. if (!JavascriptConversion::IsCallable(func))
  9805. {
  9806. if (!threadContext->RecordImplicitException())
  9807. {
  9808. return scriptContext->GetLibrary()->GetUndefined();
  9809. }
  9810. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedFunction);
  9811. }
  9812. RecyclableObject* callable = RecyclableObject::FromVar(func);
  9813. Var result = threadContext->ExecuteImplicitCall(callable, ImplicitCall_Accessor, [=]() -> Var
  9814. {
  9815. Js::Var args[] = { iterator, value };
  9816. Js::CallInfo callInfo(Js::CallFlags_Value, _countof(args) + (value == nullptr ? -1 : 0));
  9817. return JavascriptFunction::CallFunction<true>(callable, callable->GetEntryPoint(), Arguments(callInfo, args));
  9818. });
  9819. if (!JavascriptOperators::IsObject(result))
  9820. {
  9821. if (!threadContext->RecordImplicitException())
  9822. {
  9823. return scriptContext->GetLibrary()->GetUndefined();
  9824. }
  9825. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject);
  9826. }
  9827. return RecyclableObject::FromVar(result);
  9828. }
  9829. // IteratorComplete as described in ES6.0 (draft 22) Section 7.4.3
  9830. bool JavascriptOperators::IteratorComplete(RecyclableObject* iterResult, ScriptContext* scriptContext)
  9831. {
  9832. Var done = JavascriptOperators::GetPropertyNoCache(iterResult, Js::PropertyIds::done, scriptContext);
  9833. return JavascriptConversion::ToBool(done, scriptContext);
  9834. }
  9835. // IteratorValue as described in ES6.0 (draft 22) Section 7.4.4
  9836. Var JavascriptOperators::IteratorValue(RecyclableObject* iterResult, ScriptContext* scriptContext)
  9837. {
  9838. return JavascriptOperators::GetPropertyNoCache(iterResult, Js::PropertyIds::value, scriptContext);
  9839. }
  9840. // IteratorStep as described in ES6.0 (draft 22) Section 7.4.5
  9841. bool JavascriptOperators::IteratorStep(RecyclableObject* iterator, ScriptContext* scriptContext, RecyclableObject** result)
  9842. {
  9843. Assert(result);
  9844. *result = JavascriptOperators::IteratorNext(iterator, scriptContext);
  9845. return !JavascriptOperators::IteratorComplete(*result, scriptContext);
  9846. }
  9847. bool JavascriptOperators::IteratorStepAndValue(RecyclableObject* iterator, ScriptContext* scriptContext, Var* resultValue)
  9848. {
  9849. // CONSIDER: Fast-pathing for iterators that are built-ins?
  9850. RecyclableObject* result = JavascriptOperators::IteratorNext(iterator, scriptContext);
  9851. if (!JavascriptOperators::IteratorComplete(result, scriptContext))
  9852. {
  9853. *resultValue = JavascriptOperators::IteratorValue(result, scriptContext);
  9854. return true;
  9855. }
  9856. return false;
  9857. }
  9858. RecyclableObject* JavascriptOperators::CreateFromConstructor(RecyclableObject* constructor, ScriptContext* scriptContext)
  9859. {
  9860. // Create a regular object and set the internal proto from the constructor
  9861. return JavascriptOperators::OrdinaryCreateFromConstructor(constructor, scriptContext->GetLibrary()->CreateObject(), nullptr, scriptContext);
  9862. }
  9863. RecyclableObject* JavascriptOperators::OrdinaryCreateFromConstructor(RecyclableObject* constructor, RecyclableObject* obj, DynamicObject* intrinsicProto, ScriptContext* scriptContext)
  9864. {
  9865. // There isn't a good way for us to add internal properties to objects in Chakra.
  9866. // Thus, caller should take care to create obj with the correct internal properties.
  9867. Var proto = JavascriptOperators::GetPropertyNoCache(constructor, Js::PropertyIds::prototype, scriptContext);
  9868. // If constructor.prototype is an object, we should use that as the [[Prototype]] for our obj.
  9869. // Else, we set the [[Prototype]] internal slot of obj to %intrinsicProto% - which should be the default.
  9870. if (JavascriptOperators::IsObjectType(JavascriptOperators::GetTypeId(proto)) &&
  9871. DynamicObject::FromVar(proto) != intrinsicProto)
  9872. {
  9873. JavascriptObject::ChangePrototype(obj, RecyclableObject::FromVar(proto), /*validate*/true, scriptContext);
  9874. }
  9875. return obj;
  9876. }
  9877. Var JavascriptOperators::GetProperty(RecyclableObject* instance, PropertyId propertyId, ScriptContext* requestContext, PropertyValueInfo* info)
  9878. {
  9879. return JavascriptOperators::GetProperty(instance, instance, propertyId, requestContext, info);
  9880. }
  9881. BOOL JavascriptOperators::GetProperty(RecyclableObject* instance, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  9882. {
  9883. return JavascriptOperators::GetProperty(instance, instance, propertyId, value, requestContext, info);
  9884. }
  9885. Var JavascriptOperators::GetProperty(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, ScriptContext* requestContext, PropertyValueInfo* info)
  9886. {
  9887. Var value;
  9888. if (JavascriptOperators::GetProperty(instance, propertyObject, propertyId, &value, requestContext, info))
  9889. {
  9890. return value;
  9891. }
  9892. return requestContext->GetMissingPropertyResult();
  9893. }
  9894. Var JavascriptOperators::GetPropertyNoCache(RecyclableObject* instance, PropertyId propertyId, ScriptContext* requestContext)
  9895. {
  9896. return JavascriptOperators::GetPropertyNoCache(instance, instance, propertyId, requestContext);
  9897. }
  9898. Var JavascriptOperators::GetPropertyNoCache(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, ScriptContext* requestContext)
  9899. {
  9900. Var value;
  9901. JavascriptOperators::GetProperty_InternalSimple(instance, propertyObject, propertyId, &value, requestContext);
  9902. return value;
  9903. }
  9904. BOOL JavascriptOperators::GetPropertyNoCache(RecyclableObject* instance, PropertyId propertyId, Var* value, ScriptContext* requestContext)
  9905. {
  9906. return JavascriptOperators::GetPropertyNoCache(instance, instance, propertyId, value, requestContext);
  9907. }
  9908. BOOL JavascriptOperators::GetPropertyNoCache(Var instance, RecyclableObject* propertyObject, PropertyId propertyId, Var* value, ScriptContext* requestContext)
  9909. {
  9910. return JavascriptOperators::GetProperty_InternalSimple(instance, propertyObject, propertyId, value, requestContext);
  9911. }
  9912. Var JavascriptOperators::GetRootProperty(RecyclableObject* instance, PropertyId propertyId, ScriptContext* requestContext, PropertyValueInfo* info)
  9913. {
  9914. Var value;
  9915. if (JavascriptOperators::GetRootProperty(instance, propertyId, &value, requestContext, info))
  9916. {
  9917. return value;
  9918. }
  9919. return requestContext->GetMissingPropertyResult();
  9920. }
  9921. BOOL JavascriptOperators::GetPropertyReference(RecyclableObject *instance, PropertyId propertyId, Var* value, ScriptContext* requestContext, PropertyValueInfo* info)
  9922. {
  9923. return JavascriptOperators::GetPropertyReference(instance, instance, propertyId, value, requestContext, info);
  9924. }
  9925. Var JavascriptOperators::GetItem(RecyclableObject* instance, uint32 index, ScriptContext* requestContext)
  9926. {
  9927. Var value;
  9928. if (GetItem(instance, index, &value, requestContext))
  9929. {
  9930. return value;
  9931. }
  9932. return requestContext->GetMissingItemResult();
  9933. }
  9934. Var JavascriptOperators::GetItem(RecyclableObject* instance, uint64 index, ScriptContext* requestContext)
  9935. {
  9936. Var value;
  9937. if (GetItem(instance, index, &value, requestContext))
  9938. {
  9939. return value;
  9940. }
  9941. return requestContext->GetMissingItemResult();
  9942. }
  9943. BOOL JavascriptOperators::GetItem(RecyclableObject* instance, uint64 index, Var* value, ScriptContext* requestContext)
  9944. {
  9945. if (index < JavascriptArray::InvalidIndex)
  9946. {
  9947. // In case index fits in uint32, we can avoid the (slower) big-index path
  9948. return GetItem(instance, static_cast<uint32>(index), value, requestContext);
  9949. }
  9950. PropertyRecord const * propertyRecord = nullptr;
  9951. JavascriptOperators::GetPropertyIdForInt(index, requestContext, &propertyRecord);
  9952. return JavascriptOperators::GetProperty(instance, propertyRecord->GetPropertyId(), value, requestContext);
  9953. }
  9954. BOOL JavascriptOperators::GetItem(RecyclableObject* instance, uint32 index, Var* value, ScriptContext* requestContext)
  9955. {
  9956. return JavascriptOperators::GetItem(instance, instance, index, value, requestContext);
  9957. }
  9958. BOOL JavascriptOperators::GetItemReference(RecyclableObject* instance, uint32 index, Var* value, ScriptContext* requestContext)
  9959. {
  9960. return GetItemReference(instance, instance, index, value, requestContext);
  9961. }
  9962. BOOL JavascriptOperators::CheckPrototypesForAccessorOrNonWritableProperty(RecyclableObject* instance, PropertyId propertyId, Var* setterValue, DescriptorFlags* flags, PropertyValueInfo* info, ScriptContext* scriptContext)
  9963. {
  9964. if (propertyId == Js::PropertyIds::__proto__)
  9965. {
  9966. return CheckPrototypesForAccessorOrNonWritablePropertyCore<PropertyId, false, false>(instance, propertyId, setterValue, flags, info, scriptContext);
  9967. }
  9968. else
  9969. {
  9970. return CheckPrototypesForAccessorOrNonWritablePropertyCore<PropertyId, true, false>(instance, propertyId, setterValue, flags, info, scriptContext);
  9971. }
  9972. }
  9973. BOOL JavascriptOperators::CheckPrototypesForAccessorOrNonWritableRootProperty(RecyclableObject* instance, PropertyId propertyId, Var* setterValue, DescriptorFlags* flags, PropertyValueInfo* info, ScriptContext* scriptContext)
  9974. {
  9975. if (propertyId == Js::PropertyIds::__proto__)
  9976. {
  9977. return CheckPrototypesForAccessorOrNonWritablePropertyCore<PropertyId, false, true>(instance, propertyId, setterValue, flags, info, scriptContext);
  9978. }
  9979. else
  9980. {
  9981. return CheckPrototypesForAccessorOrNonWritablePropertyCore<PropertyId, true, true>(instance, propertyId, setterValue, flags, info, scriptContext);
  9982. }
  9983. }
  9984. BOOL JavascriptOperators::CheckPrototypesForAccessorOrNonWritableProperty(RecyclableObject* instance, JavascriptString* propertyNameString, Var* setterValue, DescriptorFlags* flags, PropertyValueInfo* info, ScriptContext* scriptContext)
  9985. {
  9986. Js::PropertyRecord const * localPropertyRecord;
  9987. propertyNameString->GetPropertyRecord(&localPropertyRecord);
  9988. PropertyId propertyId = localPropertyRecord->GetPropertyId();
  9989. return CheckPrototypesForAccessorOrNonWritableProperty(instance, propertyId, setterValue, flags, info, scriptContext);
  9990. }
  9991. BOOL JavascriptOperators::SetProperty(Var instance, RecyclableObject* object, PropertyId propertyId, Var newValue, ScriptContext* requestContext, PropertyOperationFlags propertyOperationFlags)
  9992. {
  9993. PropertyValueInfo info;
  9994. return JavascriptOperators::SetProperty(instance, object, propertyId, newValue, &info, requestContext, propertyOperationFlags);
  9995. }
  9996. BOOL JavascriptOperators::TryConvertToUInt32(const char16* str, int length, uint32* intVal)
  9997. {
  9998. return NumberUtilities::TryConvertToUInt32(str, length, intVal);
  9999. }
  10000. template <typename TPropertyKey>
  10001. DescriptorFlags JavascriptOperators::GetRootSetter(RecyclableObject* instance, TPropertyKey propertyKey, Var *setterValue, PropertyValueInfo* info, ScriptContext* requestContext)
  10002. {
  10003. // This is provided only so that CheckPrototypesForAccessorOrNonWritablePropertyCore will compile.
  10004. // It will never be called.
  10005. Throw::FatalInternalError();
  10006. }
  10007. template <>
  10008. inline DescriptorFlags JavascriptOperators::GetRootSetter(RecyclableObject* instance, PropertyId propertyId, Var *setterValue, PropertyValueInfo* info, ScriptContext* requestContext)
  10009. {
  10010. AssertMsg(JavascriptOperators::GetTypeId(instance) == TypeIds_GlobalObject
  10011. || JavascriptOperators::GetTypeId(instance) == TypeIds_ModuleRoot,
  10012. "Root must be a global object!");
  10013. RootObjectBase* rootObject = static_cast<RootObjectBase*>(instance);
  10014. return rootObject->GetRootSetter(propertyId, setterValue, info, requestContext);
  10015. }
  10016. // Helper to fetch @@species from a constructor object
  10017. Var JavascriptOperators::GetSpecies(RecyclableObject* constructor, ScriptContext* scriptContext)
  10018. {
  10019. if (scriptContext->GetConfig()->IsES6SpeciesEnabled())
  10020. {
  10021. Var species = nullptr;
  10022. // Let S be Get(C, @@species)
  10023. if (JavascriptOperators::GetProperty(constructor, PropertyIds::_symbolSpecies, &species, scriptContext)
  10024. && !JavascriptOperators::IsUndefinedOrNull(species))
  10025. {
  10026. // If S is neither undefined nor null, let C be S
  10027. return species;
  10028. }
  10029. }
  10030. return constructor;
  10031. }