JavascriptLibrary.cpp 400 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft Corporation and contributors. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "RuntimeLibraryPch.h"
  6. #include "Library/JSON.h"
  7. #include "Types/MissingPropertyTypeHandler.h"
  8. #include "Types/NullTypeHandler.h"
  9. #include "Types/SimpleTypeHandler.h"
  10. #include "Types/DeferredTypeHandler.h"
  11. #include "Types/PathTypeHandler.h"
  12. #include "Types/PropertyIndexRanges.h"
  13. #include "Types/SimpleDictionaryPropertyDescriptor.h"
  14. #include "Types/SimpleDictionaryTypeHandler.h"
  15. #include "Library/ForInObjectEnumerator.h"
  16. #include "Library/EngineInterfaceObject.h"
  17. #include "Library/IntlEngineInterfaceExtensionObject.h"
  18. #ifdef ENABLE_JS_BUILTINS
  19. #include "Library/JsBuiltInEngineInterfaceExtensionObject.h"
  20. #endif
  21. #include "Library/ThrowErrorObject.h"
  22. #include "Library/StackScriptFunction.h"
  23. namespace Js
  24. {
  25. SimplePropertyDescriptor const JavascriptLibrary::SharedFunctionPropertyDescriptors[2] =
  26. {
  27. SimplePropertyDescriptor(NO_WRITE_BARRIER_TAG(BuiltInPropertyRecords::prototype), PropertyWritable),
  28. SimplePropertyDescriptor(NO_WRITE_BARRIER_TAG(BuiltInPropertyRecords::name), PropertyConfigurable)
  29. };
  30. SimplePropertyDescriptor const JavascriptLibrary::SharedIdMappedFunctionPropertyDescriptors[2] =
  31. {
  32. SimplePropertyDescriptor(NO_WRITE_BARRIER_TAG(BuiltInPropertyRecords::prototype), PropertyNone),
  33. SimplePropertyDescriptor(NO_WRITE_BARRIER_TAG(BuiltInPropertyRecords::name), PropertyConfigurable)
  34. };
  35. SimplePropertyDescriptor const JavascriptLibrary::FunctionWithLengthAndNameTypeDescriptors[2] =
  36. {
  37. SimplePropertyDescriptor(NO_WRITE_BARRIER_TAG(BuiltInPropertyRecords::length), PropertyConfigurable),
  38. SimplePropertyDescriptor(NO_WRITE_BARRIER_TAG(BuiltInPropertyRecords::name), PropertyConfigurable)
  39. };
  40. SimplePropertyDescriptor const JavascriptLibrary::ModuleNamespaceTypeDescriptors[1] =
  41. {
  42. SimplePropertyDescriptor(NO_WRITE_BARRIER_TAG(BuiltInPropertyRecords::_symbolToStringTag), PropertyNone)
  43. };
  44. SimpleTypeHandler<1> JavascriptLibrary::SharedPrototypeTypeHandler(NO_WRITE_BARRIER_TAG(BuiltInPropertyRecords::constructor), PropertyWritable | PropertyConfigurable, PropertyTypesWritableDataOnly, 4, sizeof(DynamicObject));
  45. SimpleTypeHandler<1> JavascriptLibrary::SharedFunctionWithoutPrototypeTypeHandler(NO_WRITE_BARRIER_TAG(BuiltInPropertyRecords::name), PropertyConfigurable);
  46. SimpleTypeHandler<1> JavascriptLibrary::SharedFunctionWithPrototypeTypeHandlerV11(NO_WRITE_BARRIER_TAG(BuiltInPropertyRecords::prototype), PropertyWritable);
  47. SimpleTypeHandler<2> JavascriptLibrary::SharedFunctionWithPrototypeTypeHandler(NO_WRITE_BARRIER_TAG(SharedFunctionPropertyDescriptors));
  48. SimpleTypeHandler<2> JavascriptLibrary::SharedIdMappedFunctionWithPrototypeTypeHandler(NO_WRITE_BARRIER_TAG(SharedIdMappedFunctionPropertyDescriptors));
  49. SimpleTypeHandler<1> JavascriptLibrary::SharedFunctionWithConfigurableLengthTypeHandler(NO_WRITE_BARRIER_TAG(BuiltInPropertyRecords::length), PropertyConfigurable);
  50. SimpleTypeHandler<1> JavascriptLibrary::SharedFunctionWithLengthTypeHandler(NO_WRITE_BARRIER_TAG(BuiltInPropertyRecords::length));
  51. SimpleTypeHandler<2> JavascriptLibrary::SharedFunctionWithLengthAndNameTypeHandler(NO_WRITE_BARRIER_TAG(FunctionWithLengthAndNameTypeDescriptors));
  52. SimpleTypeHandler<1> JavascriptLibrary::SharedNamespaceSymbolTypeHandler(NO_WRITE_BARRIER_TAG(ModuleNamespaceTypeDescriptors));
  53. MissingPropertyTypeHandler JavascriptLibrary::MissingPropertyHolderTypeHandler;
  54. SimplePropertyDescriptor const JavascriptLibrary::HeapArgumentsPropertyDescriptors[3] =
  55. {
  56. SimplePropertyDescriptor(NO_WRITE_BARRIER_TAG(BuiltInPropertyRecords::length), PropertyConfigurable | PropertyWritable),
  57. SimplePropertyDescriptor(NO_WRITE_BARRIER_TAG(BuiltInPropertyRecords::callee), PropertyConfigurable | PropertyWritable),
  58. SimplePropertyDescriptor(NO_WRITE_BARRIER_TAG(BuiltInPropertyRecords::_symbolIterator), PropertyConfigurable | PropertyWritable)
  59. };
  60. SimplePropertyDescriptor const JavascriptLibrary::FunctionWithLengthAndPrototypeTypeDescriptors[2] =
  61. {
  62. SimplePropertyDescriptor(NO_WRITE_BARRIER_TAG(BuiltInPropertyRecords::prototype), PropertyNone),
  63. SimplePropertyDescriptor(NO_WRITE_BARRIER_TAG(BuiltInPropertyRecords::length), PropertyConfigurable)
  64. };
  65. void JavascriptLibrary::Initialize(ScriptContext* scriptContext, GlobalObject * globalObject)
  66. {
  67. PROBE_STACK(scriptContext, Js::Constants::MinStackDefault);
  68. #ifdef PROFILE_EXEC
  69. scriptContext->ProfileBegin(Js::LibInitPhase);
  70. #endif
  71. this->scriptContext = scriptContext;
  72. this->recycler = scriptContext->GetRecycler();
  73. this->undeclBlockVarSentinel = RecyclerNew(recycler, UndeclaredBlockVariable, StaticType::New(scriptContext, TypeIds_Null, nullptr, nullptr));
  74. typesEnsuredToHaveOnlyWritableDataPropertiesInItAndPrototypeChain = RecyclerNew(recycler, JsUtil::List<Type *>, recycler);
  75. // Library is not zero-initialized. memset the memory occupied by builtinFunctions array to 0.
  76. ClearArray(builtinFunctions, BuiltinFunction::Count);
  77. // Note: InitializePrototypes and InitializeTypes must be called first.
  78. InitializePrototypes();
  79. InitializeTypes();
  80. InitializeGlobal(globalObject);
  81. InitializeComplexThings();
  82. InitializeStaticValues();
  83. PrecalculateArrayAllocationBuckets();
  84. this->cache.toStringTagCache = ScriptContextPolymorphicInlineCache::New(32, this);
  85. #if ENABLE_COPYONACCESS_ARRAY
  86. if (!PHASE_OFF1(CopyOnAccessArrayPhase))
  87. {
  88. this->cacheForCopyOnAccessArraySegments = RecyclerNewZ(this->recycler, CacheForCopyOnAccessArraySegments);
  89. }
  90. #endif
  91. #ifdef PROFILE_EXEC
  92. scriptContext->ProfileEnd(Js::LibInitPhase);
  93. #endif
  94. }
  95. void JavascriptLibrary::Uninitialize()
  96. {
  97. this->globalObject = nullptr;
  98. }
  99. void JavascriptLibrary::InitializePrototypes()
  100. {
  101. Recycler* recycler = this->GetRecycler();
  102. // Recycler macros below expect that their arguments will not throw when they're evaluated.
  103. // We allocate a lot of types for the built-in prototype objects which we need to store temporarily.
  104. DynamicType* tempDynamicType = nullptr;
  105. Type* tempType = StaticType::New(scriptContext, TypeIds_Null, nullptr, nullptr);
  106. nullValue = RecyclerNew(recycler, RecyclableObject, tempType);
  107. nullValue->GetType()->SetHasSpecialPrototype(true);
  108. this->rootPath = TypePath::New(recycler);
  109. // The prototype property of the object prototype is null.
  110. objectPrototype = ObjectPrototypeObject::New(recycler,
  111. DynamicType::New(scriptContext, TypeIds_Object, nullValue, nullptr,
  112. DeferredTypeHandler<InitializeObjectPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  113. constructorPrototypeObjectType = DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  114. &SharedPrototypeTypeHandler, true, true);
  115. constructorPrototypeObjectType->SetHasNoEnumerableProperties(true);
  116. arrayBufferPrototype = DynamicObject::New(recycler,
  117. DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  118. DeferredTypeHandler<InitializeArrayBufferPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  119. if (scriptContext->GetConfig()->IsESSharedArrayBufferEnabled())
  120. {
  121. sharedArrayBufferPrototype = DynamicObject::New(recycler,
  122. DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  123. DeferredTypeHandler<InitializeSharedArrayBufferPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  124. }
  125. else
  126. {
  127. sharedArrayBufferPrototype = nullptr;
  128. }
  129. dataViewPrototype = DynamicObject::New(recycler,
  130. DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  131. DeferredTypeHandler<InitializeDataViewPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  132. typedArrayPrototype = DynamicObject::New(recycler,
  133. DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  134. DeferredTypeHandler<InitializeTypedArrayPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  135. Int8ArrayPrototype = DynamicObject::New(recycler,
  136. DynamicType::New(scriptContext, TypeIds_Object, typedArrayPrototype, nullptr,
  137. DeferredTypeHandler<InitializeInt8ArrayPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  138. Uint8ArrayPrototype = DynamicObject::New(recycler,
  139. DynamicType::New(scriptContext, TypeIds_Object, typedArrayPrototype, nullptr,
  140. DeferredTypeHandler<InitializeUint8ArrayPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  141. // If ES6 TypedArrays are enabled, we have Khronos Interop mode enabled
  142. Uint8ClampedArrayPrototype = DynamicObject::New(recycler,
  143. DynamicType::New(scriptContext, TypeIds_Object, typedArrayPrototype, nullptr,
  144. DeferredTypeHandler<InitializeUint8ClampedArrayPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  145. Int16ArrayPrototype = DynamicObject::New(recycler,
  146. DynamicType::New(scriptContext, TypeIds_Object, typedArrayPrototype, nullptr,
  147. DeferredTypeHandler<InitializeInt16ArrayPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  148. Uint16ArrayPrototype = DynamicObject::New(recycler,
  149. DynamicType::New(scriptContext, TypeIds_Object, typedArrayPrototype, nullptr,
  150. DeferredTypeHandler<InitializeUint16ArrayPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  151. Int32ArrayPrototype = DynamicObject::New(recycler,
  152. DynamicType::New(scriptContext, TypeIds_Object, typedArrayPrototype, nullptr,
  153. DeferredTypeHandler<InitializeInt32ArrayPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  154. Uint32ArrayPrototype = DynamicObject::New(recycler,
  155. DynamicType::New(scriptContext, TypeIds_Object, typedArrayPrototype, nullptr,
  156. DeferredTypeHandler<InitializeUint32ArrayPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  157. Float32ArrayPrototype = DynamicObject::New(recycler,
  158. DynamicType::New(scriptContext, TypeIds_Object, typedArrayPrototype, nullptr,
  159. DeferredTypeHandler<InitializeFloat32ArrayPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  160. Float64ArrayPrototype = DynamicObject::New(recycler,
  161. DynamicType::New(scriptContext, TypeIds_Object, typedArrayPrototype, nullptr,
  162. DeferredTypeHandler<InitializeFloat64ArrayPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  163. Int64ArrayPrototype = DynamicObject::New(recycler,
  164. DynamicType::New(scriptContext, TypeIds_Object, typedArrayPrototype, nullptr,
  165. DeferredTypeHandler<InitializeInt64ArrayPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  166. Uint64ArrayPrototype = DynamicObject::New(recycler,
  167. DynamicType::New(scriptContext, TypeIds_Object, typedArrayPrototype, nullptr,
  168. DeferredTypeHandler<InitializeUint64ArrayPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  169. BoolArrayPrototype = DynamicObject::New(recycler,
  170. DynamicType::New(scriptContext, TypeIds_Object, typedArrayPrototype, nullptr,
  171. DeferredTypeHandler<InitializeBoolArrayPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  172. CharArrayPrototype = DynamicObject::New(recycler,
  173. DynamicType::New(scriptContext, TypeIds_Object, typedArrayPrototype, nullptr,
  174. DeferredTypeHandler<InitializeCharArrayPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  175. arrayPrototype = JavascriptArray::New<Var, JavascriptArray, 0>(0,
  176. DynamicType::New(scriptContext, TypeIds_Array, objectPrototype, nullptr,
  177. DeferredTypeHandler<InitializeArrayPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()), recycler);
  178. tempDynamicType = DynamicType::New(scriptContext, TypeIds_BooleanObject, objectPrototype, nullptr,
  179. DeferredTypeHandler<InitializeBooleanPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance());
  180. booleanPrototype = RecyclerNew(recycler, JavascriptBooleanObject, nullptr, tempDynamicType);
  181. tempDynamicType = DynamicType::New(scriptContext, TypeIds_NumberObject, objectPrototype, nullptr,
  182. DeferredTypeHandler<InitializeNumberPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance());
  183. numberPrototype = RecyclerNew(recycler, JavascriptNumberObject, TaggedInt::ToVarUnchecked(0), tempDynamicType);
  184. tempDynamicType = DynamicType::New(scriptContext, TypeIds_StringObject, objectPrototype, nullptr,
  185. DeferredTypeHandler<InitializeStringPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance());
  186. stringPrototype = RecyclerNew(recycler, JavascriptStringObject, nullptr, tempDynamicType);
  187. if (scriptContext->GetConfig()->IsES6PrototypeChain())
  188. {
  189. datePrototype = DynamicObject::New(recycler,
  190. DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  191. DeferredTypeHandler<InitializeDatePrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  192. regexPrototype = DynamicObject::New(recycler,
  193. DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  194. DeferredTypeHandler<InitializeRegexPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  195. errorPrototype = DynamicObject::New(recycler,
  196. DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  197. DeferredTypeHandler<InitializeErrorPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  198. }
  199. else
  200. {
  201. double initDateValue = JavascriptNumber::NaN;
  202. tempDynamicType = DynamicType::New(scriptContext, TypeIds_Date, objectPrototype, nullptr,
  203. DeferredTypeHandler<InitializeDatePrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance());
  204. datePrototype = RecyclerNewZ(recycler, JavascriptDate, initDateValue, tempDynamicType);
  205. tempDynamicType = DynamicType::New(scriptContext, TypeIds_Error, objectPrototype, nullptr,
  206. DeferredTypeHandler<InitializeErrorPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance());
  207. errorPrototype = RecyclerNew(this->GetRecycler(), JavascriptError, tempDynamicType, /*isExternalError*/FALSE, /*isPrototype*/TRUE);
  208. }
  209. #define INIT_ERROR_PROTO(field, initFunc) \
  210. if (scriptContext->GetConfig()->IsES6PrototypeChain()) \
  211. { \
  212. field = DynamicObject::New(recycler, \
  213. DynamicType::New(scriptContext, TypeIds_Object, errorPrototype, nullptr, \
  214. DeferredTypeHandler<initFunc, DefaultDeferredTypeFilter, true>::GetDefaultInstance())); \
  215. } \
  216. else \
  217. { \
  218. tempDynamicType = DynamicType::New(scriptContext, TypeIds_Error, errorPrototype, nullptr, \
  219. DeferredTypeHandler<initFunc, DefaultDeferredTypeFilter, true>::GetDefaultInstance()); \
  220. field = RecyclerNew(this->GetRecycler(), JavascriptError, tempDynamicType, /*isExternalError*/FALSE, /*isPrototype*/TRUE); \
  221. }
  222. INIT_ERROR_PROTO(evalErrorPrototype, InitializeEvalErrorPrototype);
  223. INIT_ERROR_PROTO(rangeErrorPrototype, InitializeRangeErrorPrototype);
  224. INIT_ERROR_PROTO(referenceErrorPrototype, InitializeReferenceErrorPrototype);
  225. INIT_ERROR_PROTO(syntaxErrorPrototype, InitializeSyntaxErrorPrototype);
  226. INIT_ERROR_PROTO(typeErrorPrototype, InitializeTypeErrorPrototype);
  227. INIT_ERROR_PROTO(uriErrorPrototype, InitializeURIErrorPrototype);
  228. #ifdef ENABLE_WASM
  229. if (CONFIG_FLAG(Wasm) && !PHASE_OFF1(Js::WasmPhase))
  230. {
  231. INIT_ERROR_PROTO(webAssemblyCompileErrorPrototype, InitializeWebAssemblyCompileErrorPrototype);
  232. INIT_ERROR_PROTO(webAssemblyRuntimeErrorPrototype, InitializeWebAssemblyRuntimeErrorPrototype);
  233. INIT_ERROR_PROTO(webAssemblyLinkErrorPrototype, InitializeWebAssemblyLinkErrorPrototype);
  234. }
  235. #endif
  236. #undef INIT_ERROR_PROTO
  237. tempDynamicType = DynamicType::New(scriptContext, TypeIds_Function, objectPrototype, JavascriptFunction::PrototypeEntryPoint,
  238. DeferredTypeHandler<InitializeFunctionPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance());
  239. functionPrototype = RecyclerNew(recycler, JavascriptFunction, tempDynamicType, &JavascriptFunction::EntryInfo::PrototypeEntryPoint);
  240. promisePrototype = nullptr;
  241. generatorFunctionPrototype = nullptr;
  242. generatorPrototype = nullptr;
  243. asyncFunctionPrototype = nullptr;
  244. symbolPrototype = DynamicObject::New(recycler,
  245. DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  246. DeferredTypeHandler<InitializeSymbolPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  247. mapPrototype = DynamicObject::New(recycler,
  248. DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  249. DeferredTypeHandler<InitializeMapPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  250. setPrototype = DynamicObject::New(recycler,
  251. DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  252. DeferredTypeHandler<InitializeSetPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  253. weakMapPrototype = DynamicObject::New(recycler,
  254. DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  255. DeferredTypeHandler<InitializeWeakMapPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  256. weakSetPrototype = DynamicObject::New(recycler,
  257. DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  258. DeferredTypeHandler<InitializeWeakSetPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  259. iteratorPrototype = DynamicObject::New(recycler,
  260. DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  261. DeferredTypeHandler<InitializeIteratorPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  262. if (!scriptContext->IsJsBuiltInEnabled())
  263. {
  264. arrayIteratorPrototype = DynamicObject::New(recycler,
  265. DynamicType::New(scriptContext, TypeIds_Object, iteratorPrototype, nullptr,
  266. DeferredTypeHandler<InitializeArrayIteratorPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  267. }
  268. mapIteratorPrototype = DynamicObject::New(recycler,
  269. DynamicType::New(scriptContext, TypeIds_Object, iteratorPrototype, nullptr,
  270. DeferredTypeHandler<InitializeMapIteratorPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  271. setIteratorPrototype = DynamicObject::New(recycler,
  272. DynamicType::New(scriptContext, TypeIds_Object, iteratorPrototype, nullptr,
  273. DeferredTypeHandler<InitializeSetIteratorPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  274. stringIteratorPrototype = DynamicObject::New(recycler,
  275. DynamicType::New(scriptContext, TypeIds_Object, iteratorPrototype, nullptr,
  276. DeferredTypeHandler<InitializeStringIteratorPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  277. #ifdef ENABLE_WASM
  278. if (CONFIG_FLAG(Wasm) && !PHASE_OFF1(Js::WasmPhase))
  279. {
  280. webAssemblyMemoryPrototype = DynamicObject::New(recycler,
  281. DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  282. DeferredTypeHandler<InitializeWebAssemblyMemoryPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  283. webAssemblyModulePrototype = DynamicObject::New(recycler,
  284. DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  285. DeferredTypeHandler<InitializeWebAssemblyModulePrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  286. webAssemblyInstancePrototype = DynamicObject::New(recycler,
  287. DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  288. DeferredTypeHandler<InitializeWebAssemblyInstancePrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  289. webAssemblyTablePrototype = DynamicObject::New(recycler,
  290. DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  291. DeferredTypeHandler<InitializeWebAssemblyTablePrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  292. }
  293. #endif
  294. if(scriptContext->GetConfig()->IsES6PromiseEnabled())
  295. {
  296. promisePrototype = DynamicObject::New(recycler,
  297. DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  298. DeferredTypeHandler<InitializePromisePrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  299. }
  300. if(scriptContext->GetConfig()->IsES6GeneratorsEnabled())
  301. {
  302. generatorFunctionPrototype = DynamicObject::New(recycler,
  303. DynamicType::New(scriptContext, TypeIds_Object, functionPrototype, nullptr,
  304. DeferredTypeHandler<InitializeGeneratorFunctionPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  305. generatorPrototype = DynamicObject::New(recycler,
  306. DynamicType::New(scriptContext, TypeIds_Object, iteratorPrototype, nullptr,
  307. DeferredTypeHandler<InitializeGeneratorPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  308. }
  309. if(scriptContext->GetConfig()->IsES7AsyncAndAwaitEnabled())
  310. {
  311. asyncFunctionPrototype = DynamicObject::New(recycler,
  312. DynamicType::New(scriptContext, TypeIds_Object, functionPrototype, nullptr,
  313. DeferredTypeHandler<InitializeAsyncFunctionPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  314. }
  315. }
  316. void JavascriptLibrary::InitializeTypes()
  317. {
  318. Recycler* recycler = this->GetRecycler();
  319. ScriptConfiguration const *config = scriptContext->GetConfig();
  320. enumeratorType = StaticType::New(scriptContext, TypeIds_Enumerator, objectPrototype, nullptr);
  321. // Initialize Array/Argument types
  322. heapArgumentsType = DynamicType::New(scriptContext, TypeIds_Arguments, objectPrototype, nullptr,
  323. SimpleDictionaryTypeHandler::New(scriptContext, HeapArgumentsPropertyDescriptors, _countof(HeapArgumentsPropertyDescriptors), 0, 0, true, true), true, true);
  324. #define INIT_SIMPLE_TYPE(field, typeId, prototype) \
  325. field = DynamicType::New(scriptContext, typeId, prototype, nullptr, \
  326. PathTypeHandlerNoAttr::New(scriptContext, this->GetRootPath(), 0, 0, 0, true, true), true, true)
  327. INIT_SIMPLE_TYPE(activationObjectType, TypeIds_ActivationObject, nullValue);
  328. INIT_SIMPLE_TYPE(arrayType, TypeIds_Array, arrayPrototype);
  329. INIT_SIMPLE_TYPE(nativeIntArrayType, TypeIds_NativeIntArray, arrayPrototype);
  330. INIT_SIMPLE_TYPE(nativeFloatArrayType, TypeIds_NativeFloatArray, arrayPrototype);
  331. INIT_SIMPLE_TYPE(arrayBufferType, TypeIds_ArrayBuffer, arrayBufferPrototype);
  332. #if ENABLE_COPYONACCESS_ARRAY
  333. INIT_SIMPLE_TYPE(copyOnAccessNativeIntArrayType, TypeIds_CopyOnAccessNativeIntArray, arrayPrototype);
  334. #endif
  335. if (scriptContext->GetConfig()->IsESSharedArrayBufferEnabled())
  336. {
  337. INIT_SIMPLE_TYPE(sharedArrayBufferType, TypeIds_SharedArrayBuffer, sharedArrayBufferPrototype);
  338. }
  339. else
  340. {
  341. sharedArrayBufferType = nullptr;
  342. }
  343. INIT_SIMPLE_TYPE(dataViewType, TypeIds_DataView, dataViewPrototype);
  344. INIT_SIMPLE_TYPE(int8ArrayType, TypeIds_Int8Array, Int8ArrayPrototype);
  345. INIT_SIMPLE_TYPE(uint8ArrayType, TypeIds_Uint8Array, Uint8ArrayPrototype);
  346. INIT_SIMPLE_TYPE(uint8ClampedArrayType, TypeIds_Uint8ClampedArray, Uint8ClampedArrayPrototype);
  347. INIT_SIMPLE_TYPE(int16ArrayType, TypeIds_Int16Array, Int16ArrayPrototype);
  348. INIT_SIMPLE_TYPE(uint16ArrayType, TypeIds_Uint16Array, Uint16ArrayPrototype);
  349. INIT_SIMPLE_TYPE(int32ArrayType, TypeIds_Int32Array, Int32ArrayPrototype);
  350. INIT_SIMPLE_TYPE(uint32ArrayType, TypeIds_Uint32Array, Uint32ArrayPrototype);
  351. INIT_SIMPLE_TYPE(float32ArrayType, TypeIds_Float32Array, Float32ArrayPrototype);
  352. INIT_SIMPLE_TYPE(float64ArrayType, TypeIds_Float64Array, Float64ArrayPrototype);
  353. INIT_SIMPLE_TYPE(int64ArrayType, TypeIds_Int64Array, Int64ArrayPrototype);
  354. INIT_SIMPLE_TYPE(uint64ArrayType, TypeIds_Uint64Array, Uint64ArrayPrototype);
  355. INIT_SIMPLE_TYPE(boolArrayType, TypeIds_BoolArray, BoolArrayPrototype);
  356. INIT_SIMPLE_TYPE(charArrayType, TypeIds_CharArray, CharArrayPrototype);
  357. INIT_SIMPLE_TYPE(errorType, TypeIds_Error, errorPrototype);
  358. INIT_SIMPLE_TYPE(evalErrorType, TypeIds_Error, evalErrorPrototype);
  359. INIT_SIMPLE_TYPE(rangeErrorType, TypeIds_Error, rangeErrorPrototype);
  360. INIT_SIMPLE_TYPE(referenceErrorType, TypeIds_Error, referenceErrorPrototype);
  361. INIT_SIMPLE_TYPE(syntaxErrorType, TypeIds_Error, syntaxErrorPrototype);
  362. INIT_SIMPLE_TYPE(typeErrorType, TypeIds_Error, typeErrorPrototype);
  363. INIT_SIMPLE_TYPE(uriErrorType, TypeIds_Error, uriErrorPrototype);
  364. #ifdef ENABLE_WASM
  365. if (CONFIG_FLAG(Wasm) && !PHASE_OFF1(Js::WasmPhase))
  366. {
  367. INIT_SIMPLE_TYPE(webAssemblyCompileErrorType, TypeIds_Error, webAssemblyCompileErrorPrototype);
  368. INIT_SIMPLE_TYPE(webAssemblyRuntimeErrorType, TypeIds_Error, webAssemblyRuntimeErrorPrototype);
  369. INIT_SIMPLE_TYPE(webAssemblyLinkErrorType, TypeIds_Error, webAssemblyLinkErrorPrototype);
  370. }
  371. #endif
  372. #undef INIT_SIMPLE_TYPE
  373. withType = nullptr;
  374. proxyType = nullptr;
  375. promiseType = nullptr;
  376. moduleNamespaceType = nullptr;
  377. // Initialize boolean types
  378. booleanTypeStatic = StaticType::New(scriptContext, TypeIds_Boolean, booleanPrototype, nullptr);
  379. booleanTypeDynamic = DynamicType::New(scriptContext, TypeIds_BooleanObject, booleanPrototype, nullptr, NullTypeHandler<false>::GetDefaultInstance(), true, true);
  380. // Initialize symbol types
  381. symbolTypeStatic = StaticType::New(scriptContext, TypeIds_Symbol, symbolPrototype, nullptr);
  382. symbolTypeDynamic = DynamicType::New(scriptContext, TypeIds_SymbolObject, symbolPrototype, nullptr, NullTypeHandler<false>::GetDefaultInstance(), true, true);
  383. if (config->IsES6UnscopablesEnabled())
  384. {
  385. withType = StaticType::New(scriptContext, TypeIds_WithScopeObject, GetNull(), nullptr);
  386. }
  387. if (config->IsES6SpreadEnabled())
  388. {
  389. SpreadArgumentType = DynamicType::New(scriptContext, TypeIds_SpreadArgument, GetNull(), nullptr, NullTypeHandler<false>::GetDefaultInstance(), true, true);
  390. }
  391. if (config->IsES6ProxyEnabled())
  392. {
  393. // proxy's prototype is not actually used. once a proxy is used, the GetType()->GetPrototype() is not used in instanceOf style usage as they are trapped.
  394. // We can use GetType()->GetPrototype() in [[get]], [[set]], and [[hasProperty]] to force the prototype walk to stop at prototype so we don't need to
  395. // continue prototype chain walk after proxy.
  396. proxyType = DynamicType::New(scriptContext, TypeIds_Proxy, GetNull(), nullptr, NullTypeHandler<false>::GetDefaultInstance(), true, true);
  397. }
  398. if (config->IsES6PromiseEnabled())
  399. {
  400. promiseType = DynamicType::New(scriptContext, TypeIds_Promise, promisePrototype, nullptr, NullTypeHandler<false>::GetDefaultInstance(), true, true);
  401. }
  402. if (config->IsES6ModuleEnabled())
  403. {
  404. moduleNamespaceType = DynamicType::New(scriptContext, TypeIds_ModuleNamespace, nullValue, nullptr, &SharedNamespaceSymbolTypeHandler);
  405. moduleNamespaceType->ShareType();
  406. }
  407. // Initialize Date types
  408. dateType = DynamicType::New(scriptContext, TypeIds_Date, datePrototype, nullptr,
  409. PathTypeHandlerNoAttr::New(scriptContext, this->GetRootPath(), 0, 0, 0, true, true), true, true);
  410. variantDateType = StaticType::New(scriptContext, TypeIds_VariantDate, nullValue, nullptr);
  411. anonymousFunctionTypeHandler = NullTypeHandler<false>::GetDefaultInstance();
  412. anonymousFunctionWithPrototypeTypeHandler = &SharedFunctionWithPrototypeTypeHandlerV11;
  413. // Initialize function types
  414. functionTypeHandler = &SharedFunctionWithoutPrototypeTypeHandler;
  415. functionWithPrototypeTypeHandler = &SharedFunctionWithPrototypeTypeHandler;
  416. functionWithPrototypeTypeHandler->SetHasKnownSlot0();
  417. externalFunctionWithDeferredPrototypeType = CreateDeferredPrototypeFunctionTypeNoProfileThunk(JavascriptExternalFunction::ExternalFunctionThunk, true /*isShared*/);
  418. externalFunctionWithLengthAndDeferredPrototypeType = CreateDeferredPrototypeFunctionTypeNoProfileThunk(JavascriptExternalFunction::ExternalFunctionThunk, true /*isShared*/, /* isLengthAvailable */ true);
  419. wrappedFunctionWithDeferredPrototypeType = CreateDeferredPrototypeFunctionTypeNoProfileThunk(JavascriptExternalFunction::WrappedFunctionThunk, true /*isShared*/);
  420. stdCallFunctionWithDeferredPrototypeType = CreateDeferredPrototypeFunctionTypeNoProfileThunk(JavascriptExternalFunction::StdCallExternalFunctionThunk, true /*isShared*/);
  421. idMappedFunctionWithPrototypeType = DynamicType::New(scriptContext, TypeIds_Function, functionPrototype, JavascriptExternalFunction::ExternalFunctionThunk,
  422. &SharedIdMappedFunctionWithPrototypeTypeHandler, true, true);
  423. externalConstructorFunctionWithDeferredPrototypeType = DynamicType::New(scriptContext, TypeIds_Function, functionPrototype, JavascriptExternalFunction::ExternalFunctionThunk,
  424. Js::DeferredTypeHandler<Js::JavascriptExternalFunction::DeferredConstructorInitializer>::GetDefaultInstance(), true, true);
  425. defaultExternalConstructorFunctionWithDeferredPrototypeType = DynamicType::New(scriptContext, TypeIds_Function, functionPrototype, JavascriptExternalFunction::DefaultExternalFunctionThunk,
  426. Js::DeferredTypeHandler<Js::JavascriptExternalFunction::DeferredConstructorInitializer>::GetDefaultInstance(), true, true);
  427. boundFunctionType = DynamicType::New(scriptContext, TypeIds_Function, functionPrototype, BoundFunction::NewInstance,
  428. GetDeferredFunctionTypeHandler(), true, true);
  429. crossSiteDeferredPrototypeFunctionType = CreateDeferredPrototypeFunctionTypeNoProfileThunk(
  430. scriptContext->CurrentCrossSiteThunk, true /*isShared*/);
  431. crossSiteIdMappedFunctionWithPrototypeType = DynamicType::New(scriptContext, TypeIds_Function, functionPrototype, scriptContext->CurrentCrossSiteThunk,
  432. &SharedIdMappedFunctionWithPrototypeTypeHandler, true, true);
  433. crossSiteExternalConstructFunctionWithPrototypeType = DynamicType::New(scriptContext, TypeIds_Function, functionPrototype, scriptContext->CurrentCrossSiteThunk,
  434. Js::DeferredTypeHandler<Js::JavascriptExternalFunction::DeferredConstructorInitializer>::GetDefaultInstance(), true, true);
  435. // Initialize Number types
  436. numberTypeStatic = StaticType::New(scriptContext, TypeIds_Number, numberPrototype, nullptr);
  437. int64NumberTypeStatic = StaticType::New(scriptContext, TypeIds_Int64Number, numberPrototype, nullptr);
  438. uint64NumberTypeStatic = StaticType::New(scriptContext, TypeIds_UInt64Number, numberPrototype, nullptr);
  439. numberTypeDynamic = DynamicType::New(scriptContext, TypeIds_NumberObject, numberPrototype, nullptr, NullTypeHandler<false>::GetDefaultInstance(), true, true);
  440. #ifdef ENABLE_WASM
  441. if (CONFIG_FLAG(Wasm) && !PHASE_OFF1(Js::WasmPhase))
  442. {
  443. webAssemblyModuleType = DynamicType::New(scriptContext, TypeIds_WebAssemblyModule, webAssemblyModulePrototype, nullptr, NullTypeHandler<false>::GetDefaultInstance(), true, true);
  444. webAssemblyInstanceType = DynamicType::New(scriptContext, TypeIds_WebAssemblyInstance, webAssemblyInstancePrototype, nullptr, NullTypeHandler<false>::GetDefaultInstance(), true, true);
  445. webAssemblyMemoryType = DynamicType::New(scriptContext, TypeIds_WebAssemblyMemory, webAssemblyMemoryPrototype, nullptr, NullTypeHandler<false>::GetDefaultInstance(), true, true);
  446. webAssemblyTableType = DynamicType::New(scriptContext, TypeIds_WebAssemblyTable, webAssemblyTablePrototype, nullptr, NullTypeHandler<false>::GetDefaultInstance(), true, true);
  447. }
  448. #endif
  449. // Initialize Object types
  450. for (int16 i = 0; i < PreInitializedObjectTypeCount; i++)
  451. {
  452. PathTypeHandlerNoAttr * typeHandler =
  453. PathTypeHandlerNoAttr::New(
  454. scriptContext,
  455. this->GetRootPath(),
  456. 0,
  457. i * InlineSlotCountIncrement,
  458. sizeof(DynamicObject),
  459. true,
  460. true);
  461. typeHandler->SetIsInlineSlotCapacityLocked();
  462. objectTypes[i] = DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr, typeHandler, true, true);
  463. }
  464. for (int16 i = 0; i < PreInitializedObjectTypeCount; i++)
  465. {
  466. PathTypeHandlerNoAttr * typeHandler =
  467. PathTypeHandlerNoAttr::New(
  468. scriptContext,
  469. this->GetRootPath(),
  470. 0,
  471. DynamicTypeHandler::GetObjectHeaderInlinableSlotCapacity() + i * InlineSlotCountIncrement,
  472. DynamicTypeHandler::GetOffsetOfObjectHeaderInlineSlots(),
  473. true,
  474. true);
  475. typeHandler->SetIsInlineSlotCapacityLocked();
  476. objectHeaderInlinedTypes[i] =
  477. DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr, typeHandler, true, true);
  478. }
  479. PathTypeHandlerNoAttr * typeHandler = PathTypeHandlerNoAttr::New(scriptContext, this->GetRootPath(), 0, 0, 0, true, true);
  480. nullPrototypeObjectType = DynamicType::New(scriptContext, TypeIds_Object, nullValue, nullptr, typeHandler, true, true);
  481. // Initialize regex types
  482. TypePath *const regexResultPath = TypePath::New(recycler);
  483. regexResultPath->Add(BuiltInPropertyRecords::input);
  484. regexResultPath->Add(BuiltInPropertyRecords::index);
  485. regexResultType = DynamicType::New(scriptContext, TypeIds_Array, arrayPrototype, nullptr,
  486. PathTypeHandlerNoAttr::New(scriptContext, regexResultPath, regexResultPath->GetPathLength(), JavascriptRegularExpressionResult::InlineSlotCount, sizeof(JavascriptArray), true, true), true, true);
  487. // Initialize string types
  488. // static type is handled under StringCache.h
  489. stringTypeDynamic = DynamicType::New(scriptContext, TypeIds_StringObject, stringPrototype, nullptr, NullTypeHandler<false>::GetDefaultInstance(), true, true);
  490. // Initialize Throw error object type
  491. throwErrorObjectType = StaticType::New(scriptContext, TypeIds_Undefined, nullValue, ThrowErrorObject::DefaultEntryPoint);
  492. mapType = DynamicType::New(scriptContext, TypeIds_Map, mapPrototype, nullptr,
  493. PathTypeHandlerNoAttr::New(scriptContext, this->GetRootPath(), 0, 0, 0, true, true), true, true);
  494. setType = DynamicType::New(scriptContext, TypeIds_Set, setPrototype, nullptr,
  495. PathTypeHandlerNoAttr::New(scriptContext, this->GetRootPath(), 0, 0, 0, true, true), true, true);
  496. weakMapType = DynamicType::New(scriptContext, TypeIds_WeakMap, weakMapPrototype, nullptr,
  497. PathTypeHandlerNoAttr::New(scriptContext, this->GetRootPath(), 0, 0, 0, true, true), true, true);
  498. weakSetType = DynamicType::New(scriptContext, TypeIds_WeakSet, weakSetPrototype, nullptr,
  499. PathTypeHandlerNoAttr::New(scriptContext, this->GetRootPath(), 0, 0, 0, true, true), true, true);
  500. TypePath *const iteratorResultPath = TypePath::New(recycler);
  501. iteratorResultPath->Add(BuiltInPropertyRecords::value);
  502. iteratorResultPath->Add(BuiltInPropertyRecords::done);
  503. iteratorResultType = DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  504. PathTypeHandlerNoAttr::New(scriptContext, iteratorResultPath, iteratorResultPath->GetPathLength(), 2, sizeof(DynamicObject), true, true), true, true);
  505. arrayIteratorType = DynamicType::New(scriptContext, TypeIds_ArrayIterator, arrayIteratorPrototype, nullptr,
  506. PathTypeHandlerNoAttr::New(scriptContext, this->GetRootPath(), 0, 0, 0, true, true), true, true);
  507. mapIteratorType = DynamicType::New(scriptContext, TypeIds_MapIterator, mapIteratorPrototype, nullptr,
  508. PathTypeHandlerNoAttr::New(scriptContext, this->GetRootPath(), 0, 0, 0, true, true), true, true);
  509. setIteratorType = DynamicType::New(scriptContext, TypeIds_SetIterator, setIteratorPrototype, nullptr,
  510. PathTypeHandlerNoAttr::New(scriptContext, this->GetRootPath(), 0, 0, 0, true, true), true, true);
  511. stringIteratorType = DynamicType::New(scriptContext, TypeIds_StringIterator, stringIteratorPrototype, nullptr,
  512. PathTypeHandlerNoAttr::New(scriptContext, this->GetRootPath(), 0, 0, 0, true, true), true, true);
  513. listIteratorType = DynamicType::New(scriptContext, TypeIds_ListIterator, iteratorPrototype, nullptr,
  514. PathTypeHandlerNoAttr::New(scriptContext, this->GetRootPath(), 0, 0, 0, true, true), true, true);
  515. if (config->IsES6GeneratorsEnabled())
  516. {
  517. generatorConstructorPrototypeObjectType = DynamicType::New(scriptContext, TypeIds_Object, generatorPrototype, nullptr,
  518. NullTypeHandler<false>::GetDefaultInstance(), true, true);
  519. generatorConstructorPrototypeObjectType->SetHasNoEnumerableProperties(true);
  520. }
  521. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  522. debugDisposableObjectType = DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  523. PathTypeHandlerNoAttr::New(scriptContext, this->GetRootPath(), 0, 0, 0, true, true), true, true);
  524. debugFuncExecutorInDisposeObjectType = DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  525. PathTypeHandlerNoAttr::New(scriptContext, this->GetRootPath(), 0, 0, 0, true, true), true, true);
  526. #endif
  527. }
  528. bool JavascriptLibrary::InitializeGeneratorFunction(DynamicObject *instance, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  529. {
  530. JavascriptGeneratorFunction *function = JavascriptGeneratorFunction::FromVar(instance);
  531. bool isAnonymousFunction = function->IsAnonymousFunction();
  532. JavascriptLibrary* javascriptLibrary = function->GetType()->GetLibrary();
  533. typeHandler->ConvertFunction(function, isAnonymousFunction ? javascriptLibrary->anonymousFunctionWithPrototypeTypeHandler : javascriptLibrary->functionWithPrototypeTypeHandler);
  534. function->SetPropertyWithAttributes(PropertyIds::prototype, javascriptLibrary->CreateGeneratorConstructorPrototypeObject(), PropertyWritable, nullptr);
  535. if (!isAnonymousFunction)
  536. {
  537. JavascriptString * functionName = nullptr;
  538. DebugOnly(bool status = ) ((Js::JavascriptFunction*)function)->GetFunctionName(&functionName);
  539. Assert(status);
  540. function->SetPropertyWithAttributes(PropertyIds::name,functionName, PropertyConfigurable, nullptr);
  541. }
  542. return true;
  543. }
  544. bool JavascriptLibrary::InitializeAsyncFunction(DynamicObject *function, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  545. {
  546. // Async function instances do not have a prototype property as they are not constructable
  547. typeHandler->Convert(function, mode, 1);
  548. if (!JavascriptAsyncFunction::FromVar(function)->IsAnonymousFunction())
  549. {
  550. JavascriptString * functionName = nullptr;
  551. DebugOnly(bool status = ) ((Js::JavascriptFunction*)function)->GetFunctionName(&functionName);
  552. Assert(status);
  553. function->SetPropertyWithAttributes(PropertyIds::name, functionName, PropertyConfigurable, nullptr);
  554. }
  555. return true;
  556. }
  557. /* static */
  558. void JavascriptLibrary::PrecalculateArrayAllocationBuckets()
  559. {
  560. JavascriptArray::EnsureCalculationOfAllocationBuckets<Js::JavascriptNativeIntArray>();
  561. JavascriptArray::EnsureCalculationOfAllocationBuckets<Js::JavascriptNativeFloatArray>();
  562. JavascriptArray::EnsureCalculationOfAllocationBuckets<Js::JavascriptArray>();
  563. }
  564. template<bool addPrototype>
  565. bool JavascriptLibrary::InitializeFunction(DynamicObject *instance, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  566. {
  567. JavascriptFunction * function = JavascriptFunction::FromVar(instance);
  568. JavascriptLibrary* javascriptLibrary = function->GetType()->GetLibrary();
  569. ScriptFunction *scriptFunction = nullptr;
  570. bool useAnonymous = false;
  571. if (ScriptFunction::Is(function))
  572. {
  573. scriptFunction = Js::ScriptFunction::FromVar(function);
  574. useAnonymous = scriptFunction->IsAnonymousFunction();
  575. }
  576. if (!addPrototype)
  577. {
  578. Assert(!useAnonymous);
  579. typeHandler->ConvertFunction(function, javascriptLibrary->functionTypeHandler);
  580. }
  581. else
  582. {
  583. typeHandler->ConvertFunction(function, useAnonymous ? javascriptLibrary->anonymousFunctionWithPrototypeTypeHandler : javascriptLibrary->functionWithPrototypeTypeHandler);
  584. DynamicObject *protoObject = javascriptLibrary->CreateConstructorPrototypeObject(function);
  585. if (scriptFunction && scriptFunction->GetFunctionInfo()->IsClassConstructor())
  586. {
  587. function->SetPropertyWithAttributes(PropertyIds::prototype, protoObject, PropertyNone, nullptr);
  588. }
  589. else
  590. {
  591. function->SetProperty(PropertyIds::prototype, protoObject, PropertyOperation_None, nullptr);
  592. }
  593. }
  594. if(scriptFunction && (useAnonymous || scriptFunction->GetFunctionProxy()->EnsureDeserialized()->GetIsStaticNameFunction()))
  595. {
  596. return true;
  597. }
  598. JavascriptString * functionName = nullptr;
  599. if (((Js::JavascriptFunction*)function)->GetFunctionName(&functionName))
  600. {
  601. function->SetPropertyWithAttributes(PropertyIds::name, functionName, PropertyConfigurable, nullptr);
  602. }
  603. return true;
  604. }
  605. DynamicType* JavascriptLibrary::GetErrorType(ErrorTypeEnum typeToFind) const
  606. {
  607. switch (typeToFind)
  608. {
  609. case kjstError:
  610. return GetErrorType();
  611. case kjstEvalError:
  612. return GetEvalErrorType();
  613. case kjstRangeError:
  614. return GetRangeErrorType();
  615. case kjstReferenceError:
  616. return GetReferenceErrorType();
  617. case kjstSyntaxError:
  618. return GetSyntaxErrorType();
  619. case kjstTypeError:
  620. return GetTypeErrorType();
  621. case kjstURIError:
  622. return GetURIErrorType();
  623. case kjstWebAssemblyCompileError:
  624. return GetWebAssemblyCompileErrorType();
  625. case kjstWebAssemblyRuntimeError:
  626. return GetWebAssemblyRuntimeErrorType();
  627. case kjstWebAssemblyLinkError:
  628. return GetWebAssemblyLinkErrorType();
  629. }
  630. return nullptr;
  631. }
  632. template<bool isNameAvailable, bool isPrototypeAvailable, bool isLengthAvailable = false>
  633. class InitializeFunctionDeferredTypeHandlerFilter
  634. {
  635. public:
  636. static bool HasFilter() { return true; }
  637. static bool HasProperty(PropertyId propertyId)
  638. {
  639. switch (propertyId)
  640. {
  641. case PropertyIds::prototype:
  642. return isPrototypeAvailable;
  643. case PropertyIds::name:
  644. return isNameAvailable;
  645. case PropertyIds::length:
  646. return isLengthAvailable;
  647. }
  648. return false;
  649. }
  650. };
  651. template<bool isNameAvailable, bool isPrototypeAvailable, bool isLengthAvailable>
  652. DynamicTypeHandler * JavascriptLibrary::GetDeferredFunctionTypeHandlerBase()
  653. {
  654. return DeferredTypeHandler<InitializeFunction<isPrototypeAvailable>, InitializeFunctionDeferredTypeHandlerFilter<isNameAvailable, isPrototypeAvailable, isLengthAvailable>>::GetDefaultInstance();
  655. }
  656. template<bool isNameAvailable, bool isPrototypeAvailable>
  657. DynamicTypeHandler * JavascriptLibrary::GetDeferredGeneratorFunctionTypeHandlerBase()
  658. {
  659. return DeferredTypeHandler<InitializeGeneratorFunction, InitializeFunctionDeferredTypeHandlerFilter<isNameAvailable, isPrototypeAvailable>>::GetDefaultInstance();
  660. }
  661. template<bool isNameAvailable>
  662. DynamicTypeHandler * JavascriptLibrary::GetDeferredAsyncFunctionTypeHandlerBase()
  663. {
  664. // Async functions do not have the prototype property
  665. return DeferredTypeHandler<InitializeAsyncFunction, InitializeFunctionDeferredTypeHandlerFilter<isNameAvailable, /* isPrototypeAvailable */ false>>::GetDefaultInstance();
  666. }
  667. DynamicTypeHandler * JavascriptLibrary::GetDeferredAnonymousPrototypeGeneratorFunctionTypeHandler()
  668. {
  669. return JavascriptLibrary::GetDeferredGeneratorFunctionTypeHandlerBase</*isNameAvailable*/ false>();
  670. }
  671. DynamicTypeHandler * JavascriptLibrary::GetDeferredAnonymousPrototypeAsyncFunctionTypeHandler()
  672. {
  673. return JavascriptLibrary::GetDeferredAsyncFunctionTypeHandlerBase</*isNameAvailable*/ false>();
  674. }
  675. DynamicTypeHandler * JavascriptLibrary::GetDeferredPrototypeGeneratorFunctionTypeHandler(ScriptContext* scriptContext)
  676. {
  677. return JavascriptLibrary::GetDeferredGeneratorFunctionTypeHandlerBase</*isNameAvailable*/ true>();
  678. }
  679. DynamicTypeHandler * JavascriptLibrary::GetDeferredPrototypeAsyncFunctionTypeHandler(ScriptContext* scriptContext)
  680. {
  681. return JavascriptLibrary::GetDeferredAsyncFunctionTypeHandlerBase</*isNameAvailable*/ true>();
  682. }
  683. DynamicTypeHandler * JavascriptLibrary::GetDeferredAnonymousPrototypeFunctionTypeHandler()
  684. {
  685. return JavascriptLibrary::GetDeferredFunctionTypeHandlerBase</*isNameAvailable*/ false>();
  686. }
  687. DynamicTypeHandler * JavascriptLibrary::GetDeferredPrototypeFunctionTypeHandler(ScriptContext* scriptContext)
  688. {
  689. return JavascriptLibrary::GetDeferredFunctionTypeHandlerBase</* isNameAvailable */ true>();
  690. }
  691. DynamicTypeHandler * JavascriptLibrary::GetDeferredPrototypeFunctionWithLengthTypeHandler(ScriptContext* scriptContext)
  692. {
  693. return DeferredTypeHandler<Js::JavascriptExternalFunction::DeferredLengthInitializer, InitializeFunctionDeferredTypeHandlerFilter</* isNameAvailable */ true, /* isPrototypeAvailable */ true, /* isLengthAvailable */ true>>::GetDefaultInstance();
  694. }
  695. DynamicTypeHandler * JavascriptLibrary::GetDeferredAnonymousFunctionTypeHandler()
  696. {
  697. return anonymousFunctionTypeHandler;
  698. }
  699. DynamicTypeHandler * JavascriptLibrary::GetDeferredFunctionTypeHandler()
  700. {
  701. return GetDeferredFunctionTypeHandlerBase</*isNameAvailable*/ true, /*isPrototypeAvailable*/ false>();
  702. }
  703. DynamicTypeHandler * JavascriptLibrary::ScriptFunctionTypeHandler(bool noPrototypeProperty, bool isAnonymousFunction)
  704. {
  705. DynamicTypeHandler * scriptFunctionTypeHandler = nullptr;
  706. if (noPrototypeProperty)
  707. {
  708. scriptFunctionTypeHandler = isAnonymousFunction ?
  709. this->GetDeferredAnonymousFunctionTypeHandler() :
  710. this->GetDeferredFunctionTypeHandler();
  711. }
  712. else
  713. {
  714. scriptFunctionTypeHandler = isAnonymousFunction ?
  715. JavascriptLibrary::GetDeferredAnonymousPrototypeFunctionTypeHandler() :
  716. JavascriptLibrary::GetDeferredPrototypeFunctionTypeHandler(scriptContext);
  717. }
  718. return scriptFunctionTypeHandler;
  719. }
  720. DynamicType * JavascriptLibrary::CreateDeferredPrototypeGeneratorFunctionType(JavascriptMethod entrypoint, bool isAnonymousFunction, bool isShared)
  721. {
  722. return DynamicType::New(scriptContext, TypeIds_Function, generatorFunctionPrototype, entrypoint,
  723. isAnonymousFunction ? GetDeferredAnonymousPrototypeGeneratorFunctionTypeHandler() : GetDeferredPrototypeGeneratorFunctionTypeHandler(scriptContext), isShared, isShared);
  724. }
  725. DynamicType * JavascriptLibrary::CreateDeferredPrototypeAsyncFunctionType(JavascriptMethod entrypoint, bool isAnonymousFunction, bool isShared)
  726. {
  727. return DynamicType::New(scriptContext, TypeIds_Function, asyncFunctionPrototype, entrypoint,
  728. isAnonymousFunction ? GetDeferredAnonymousPrototypeAsyncFunctionTypeHandler() : GetDeferredPrototypeAsyncFunctionTypeHandler(scriptContext), isShared, isShared);
  729. }
  730. DynamicType * JavascriptLibrary::CreateDeferredPrototypeFunctionType(JavascriptMethod entrypoint)
  731. {
  732. return CreateDeferredPrototypeFunctionTypeNoProfileThunk(this->inDispatchProfileMode ? ProfileEntryThunk : entrypoint);
  733. }
  734. DynamicType * JavascriptLibrary::CreateDeferredPrototypeFunctionTypeNoProfileThunk(JavascriptMethod entrypoint, bool isShared, bool isLengthAvailable)
  735. {
  736. // Note: the lack of TypeHandler switching here based on the isAnonymousFunction flag is intentional.
  737. // We can't switch shared typeHandlers and RuntimeFunctions do not produce script code for us to know if a function is Anonymous.
  738. // As a result we may have an issue where hasProperty would say you have a name property but getProperty returns undefined
  739. return DynamicType::New(scriptContext, TypeIds_Function, functionPrototype, entrypoint,
  740. isLengthAvailable ? GetDeferredPrototypeFunctionWithLengthTypeHandler(scriptContext) : GetDeferredPrototypeFunctionTypeHandler(scriptContext),
  741. isShared, isShared);
  742. }
  743. DynamicType * JavascriptLibrary::CreateFunctionType(JavascriptMethod entrypoint, RecyclableObject* prototype)
  744. {
  745. if (prototype == nullptr)
  746. {
  747. prototype = functionPrototype;
  748. }
  749. return DynamicType::New(scriptContext, TypeIds_Function, prototype, entrypoint,
  750. GetDeferredFunctionTypeHandler(), false, false);
  751. }
  752. DynamicType * JavascriptLibrary::CreateFunctionWithConfigurableLengthType(FunctionInfo * functionInfo)
  753. {
  754. return CreateFunctionWithConfigurableLengthType(this->GetFunctionPrototype(), functionInfo);
  755. }
  756. DynamicType * JavascriptLibrary::CreateFunctionWithLengthType(FunctionInfo * functionInfo)
  757. {
  758. return CreateFunctionWithLengthType(this->GetFunctionPrototype(), functionInfo);
  759. }
  760. DynamicType * JavascriptLibrary::CreateFunctionWithLengthAndNameType(FunctionInfo * functionInfo)
  761. {
  762. return CreateFunctionWithLengthAndNameType(this->GetFunctionPrototype(), functionInfo);
  763. }
  764. DynamicType * JavascriptLibrary::CreateFunctionWithLengthAndPrototypeType(FunctionInfo * functionInfo)
  765. {
  766. return CreateFunctionWithLengthAndPrototypeType(this->GetFunctionPrototype(), functionInfo);
  767. }
  768. DynamicType * JavascriptLibrary::CreateFunctionWithConfigurableLengthType(DynamicObject * prototype, FunctionInfo * functionInfo)
  769. {
  770. Assert(!functionInfo->HasBody());
  771. return DynamicType::New(scriptContext, TypeIds_Function, prototype,
  772. this->inProfileMode? ProfileEntryThunk : functionInfo->GetOriginalEntryPoint(),
  773. &SharedFunctionWithConfigurableLengthTypeHandler);
  774. }
  775. DynamicType * JavascriptLibrary::CreateFunctionWithLengthType(DynamicObject * prototype, FunctionInfo * functionInfo)
  776. {
  777. Assert(!functionInfo->HasBody());
  778. return DynamicType::New(scriptContext, TypeIds_Function, prototype,
  779. this->inProfileMode? ProfileEntryThunk : functionInfo->GetOriginalEntryPoint(),
  780. &SharedFunctionWithLengthTypeHandler);
  781. }
  782. DynamicType * JavascriptLibrary::CreateFunctionWithLengthAndNameType(DynamicObject * prototype, FunctionInfo * functionInfo)
  783. {
  784. Assert(!functionInfo->HasBody());
  785. return DynamicType::New(scriptContext, TypeIds_Function, prototype,
  786. this->inProfileMode ? ProfileEntryThunk : functionInfo->GetOriginalEntryPoint(),
  787. &SharedFunctionWithLengthAndNameTypeHandler);
  788. }
  789. DynamicType * JavascriptLibrary::CreateFunctionWithLengthAndPrototypeType(DynamicObject * prototype, FunctionInfo * functionInfo)
  790. {
  791. Assert(!functionInfo->HasBody());
  792. return DynamicType::New(scriptContext, TypeIds_Function, prototype,
  793. this->inProfileMode? ProfileEntryThunk : functionInfo->GetOriginalEntryPoint(),
  794. SimpleDictionaryTypeHandler::New(scriptContext, FunctionWithLengthAndPrototypeTypeDescriptors, _countof(FunctionWithLengthAndPrototypeTypeDescriptors), 0, 0));
  795. }
  796. void JavascriptLibrary::InitializeProperties(ThreadContext * threadContext)
  797. {
  798. if ( threadContext->GetMaxPropertyId() < PropertyIds::_countJSOnlyProperty )
  799. {
  800. threadContext->UncheckedAddBuiltInPropertyId();
  801. }
  802. }
  803. DynamicObject * JavascriptLibraryBase::GetObjectPrototype()
  804. {
  805. return GetObjectPrototypeObject();
  806. }
  807. void JavascriptLibraryBase::Finalize(bool isShutdown)
  808. {
  809. if (scriptContext)
  810. {
  811. // Clear the weak reference dictionary so we don't need to clean them
  812. // during PostCollectCallBack before Dispose deleting the script context.
  813. scriptContext->ResetWeakReferenceDictionaryList();
  814. scriptContext->SetIsFinalized();
  815. scriptContext->MarkForClose();
  816. if (scriptContext->IsRegistered())
  817. {
  818. scriptContext->GetThreadContext()->UnregisterScriptContext(scriptContext);
  819. }
  820. }
  821. }
  822. void JavascriptLibraryBase::Dispose(bool isShutdown)
  823. {
  824. if (scriptContext)
  825. {
  826. HeapDelete(scriptContext);
  827. scriptContext = nullptr;
  828. }
  829. }
  830. void JavascriptLibrary::Finalize(bool isShutdown)
  831. {
  832. __super::Finalize(isShutdown);
  833. this->SetFakeGlobalFuncForUndefer(nullptr);
  834. if (this->referencedPropertyRecords != nullptr)
  835. {
  836. RECYCLER_PERF_COUNTER_SUB(PropertyRecordBindReference, this->referencedPropertyRecords->Count());
  837. }
  838. }
  839. void JavascriptLibrary::InitializeGlobal(GlobalObject * globalObject)
  840. {
  841. RecyclableObject* globalObjectPrototype = GetObjectPrototype();
  842. globalObject->SetPrototype(globalObjectPrototype);
  843. Recycler* recycler = this->GetRecycler();
  844. StaticType* staticString = StaticType::New(scriptContext, TypeIds_String, stringPrototype, nullptr);
  845. stringCache.Initialize(scriptContext, staticString);
  846. pi = JavascriptNumber::New(Math::PI, scriptContext);
  847. nan = JavascriptNumber::New(JavascriptNumber::NaN, scriptContext);
  848. negativeInfinite = JavascriptNumber::New(JavascriptNumber::NEGATIVE_INFINITY, scriptContext);
  849. positiveInfinite = JavascriptNumber::New(JavascriptNumber::POSITIVE_INFINITY, scriptContext);
  850. minValue = JavascriptNumber::New(JavascriptNumber::MIN_VALUE, scriptContext);
  851. maxValue = JavascriptNumber::New(JavascriptNumber::MAX_VALUE, scriptContext);
  852. negativeZero = JavascriptNumber::New(JavascriptNumber::NEG_ZERO, scriptContext);
  853. Type* type = StaticType::New(scriptContext, TypeIds_Undefined, nullValue, nullptr);
  854. undefinedValue = RecyclerNew(recycler, RecyclableObject, type);
  855. DynamicType* dynamicType = DynamicType::New(scriptContext, TypeIds_Object, nullValue, nullptr, &MissingPropertyHolderTypeHandler);
  856. missingPropertyHolder = RecyclerNewPlus(recycler, sizeof(Var), DynamicObject, dynamicType);
  857. MissingPropertyTypeHandler::SetUndefinedPropertySlot(missingPropertyHolder);
  858. emptyString = CreateEmptyString(); // Must be created before other calls to CreateString
  859. nullString = CreateEmptyString(); // Must be distinct from emptyString (for the DOM)
  860. promiseResolveFunction = nullptr;
  861. promiseThenFunction = nullptr;
  862. generatorNextFunction = nullptr;
  863. generatorThrowFunction = nullptr;
  864. jsonStringifyFunction = nullptr;
  865. objectFreezeFunction = nullptr;
  866. symbolHasInstance = CreateSymbol(BuiltInPropertyRecords::_symbolHasInstance);
  867. symbolIsConcatSpreadable = CreateSymbol(BuiltInPropertyRecords::_symbolIsConcatSpreadable);
  868. symbolIterator = CreateSymbol(BuiltInPropertyRecords::_symbolIterator);
  869. symbolSpecies = CreateSymbol(BuiltInPropertyRecords::_symbolSpecies);
  870. symbolToPrimitive = CreateSymbol(BuiltInPropertyRecords::_symbolToPrimitive);
  871. symbolToStringTag = CreateSymbol(BuiltInPropertyRecords::_symbolToStringTag);
  872. symbolUnscopables = CreateSymbol(BuiltInPropertyRecords::_symbolUnscopables);
  873. if (scriptContext->GetConfig()->IsES6RegExSymbolsEnabled())
  874. {
  875. symbolMatch = CreateSymbol(BuiltInPropertyRecords::_symbolMatch);
  876. symbolReplace = CreateSymbol(BuiltInPropertyRecords::_symbolReplace);
  877. symbolSearch = CreateSymbol(BuiltInPropertyRecords::_symbolSearch);
  878. symbolSplit = CreateSymbol(BuiltInPropertyRecords::_symbolSplit);
  879. }
  880. else
  881. {
  882. symbolMatch = nullptr;
  883. symbolReplace = nullptr;
  884. symbolSearch = nullptr;
  885. symbolSplit = nullptr;
  886. }
  887. debuggerDeadZoneBlockVariableString = CreateStringFromCppLiteral(_u("[Uninitialized block variable]"));
  888. defaultAccessorFunction = CreateNonProfiledFunction(&JavascriptOperators::EntryInfo::DefaultAccessor);
  889. if (scriptContext->GetConfig()->IsErrorStackTraceEnabled())
  890. {
  891. stackTraceAccessorFunction = CreateNonProfiledFunction(&JavascriptExceptionOperators::EntryInfo::StackTraceAccessor);
  892. stackTraceAccessorFunction->SetPropertyWithAttributes(PropertyIds::length, TaggedInt::ToVarUnchecked(0), PropertyConfigurable, nullptr);
  893. }
  894. throwTypeErrorRestrictedPropertyAccessorFunction = CreateNonProfiledFunction(&JavascriptExceptionOperators::EntryInfo::ThrowTypeErrorRestrictedPropertyAccessor);
  895. throwTypeErrorRestrictedPropertyAccessorFunction->SetPropertyWithAttributes(PropertyIds::length, TaggedInt::ToVarUnchecked(0), PropertyConfigurable, nullptr);
  896. __proto__getterFunction = CreateNonProfiledFunction(&ObjectPrototypeObject::EntryInfo::__proto__getter);
  897. __proto__getterFunction->SetPropertyWithAttributes(PropertyIds::length, TaggedInt::ToVarUnchecked(0), PropertyConfigurable, nullptr);
  898. __proto__setterFunction = CreateNonProfiledFunction(&ObjectPrototypeObject::EntryInfo::__proto__setter);
  899. __proto__setterFunction->SetPropertyWithAttributes(PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable, nullptr);
  900. if (scriptContext->GetConfig()->IsES6PromiseEnabled())
  901. {
  902. identityFunction = CreateNonProfiledFunction(&JavascriptPromise::EntryInfo::Identity);
  903. identityFunction->SetPropertyWithAttributes(PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable, nullptr);
  904. throwerFunction = CreateNonProfiledFunction(&JavascriptPromise::EntryInfo::Thrower);
  905. throwerFunction->SetPropertyWithAttributes(PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable, nullptr);
  906. }
  907. booleanTrue = RecyclerNew(recycler, JavascriptBoolean, true, booleanTypeStatic);
  908. booleanFalse = RecyclerNew(recycler, JavascriptBoolean, false, booleanTypeStatic);
  909. isPRNGSeeded = false;
  910. randSeed0 = 0;
  911. randSeed1 = 0;
  912. AddMember(globalObject, PropertyIds::NaN, nan, PropertyNone);
  913. AddMember(globalObject, PropertyIds::Infinity, positiveInfinite, PropertyNone);
  914. AddMember(globalObject, PropertyIds::undefined, undefinedValue, PropertyNone);
  915. // Note: Any global function added/removed/changed here should also be updated in JavascriptLibrary::ProfilerRegisterBuiltinFunctions
  916. // so that the new functions show up in the profiler too.
  917. Field(JavascriptFunction*)* builtinFuncs = this->GetBuiltinFunctions();
  918. evalFunctionObject = AddFunctionToLibraryObject(globalObject, PropertyIds::eval, &GlobalObject::EntryInfo::Eval, 1);
  919. parseIntFunctionObject = AddFunctionToLibraryObject(globalObject, PropertyIds::parseInt, &GlobalObject::EntryInfo::ParseInt, 2);
  920. builtinFuncs[BuiltinFunction::GlobalObject_ParseInt] = parseIntFunctionObject;
  921. parseFloatFunctionObject = AddFunctionToLibraryObject(globalObject, PropertyIds::parseFloat, &GlobalObject::EntryInfo::ParseFloat, 1);
  922. AddFunctionToLibraryObject(globalObject, PropertyIds::isNaN, &GlobalObject::EntryInfo::IsNaN, 1);
  923. AddFunctionToLibraryObject(globalObject, PropertyIds::isFinite, &GlobalObject::EntryInfo::IsFinite, 1);
  924. AddFunctionToLibraryObject(globalObject, PropertyIds::decodeURI, &GlobalObject::EntryInfo::DecodeURI, 1);
  925. AddFunctionToLibraryObject(globalObject, PropertyIds::decodeURIComponent, &GlobalObject::EntryInfo::DecodeURIComponent, 1);
  926. AddFunctionToLibraryObject(globalObject, PropertyIds::encodeURI, &GlobalObject::EntryInfo::EncodeURI, 1);
  927. AddFunctionToLibraryObject(globalObject, PropertyIds::encodeURIComponent, &GlobalObject::EntryInfo::EncodeURIComponent, 1);
  928. AddFunctionToLibraryObject(globalObject, PropertyIds::escape, &GlobalObject::EntryInfo::Escape, 1);
  929. AddFunctionToLibraryObject(globalObject, PropertyIds::unescape, &GlobalObject::EntryInfo::UnEscape, 1);
  930. // for backward compat reasons in non-core builds add CollectGarbage function even if it would do nothing later
  931. #ifdef _CHAKRACOREBUILD
  932. if (scriptContext->GetConfig()->IsCollectGarbageEnabled()
  933. #ifdef ENABLE_PROJECTION
  934. || scriptContext->GetConfig()->GetHostType() == HostType::HostTypeApplication
  935. || scriptContext->GetConfig()->GetHostType() == HostType::HostTypeWebview
  936. #endif
  937. )
  938. #endif
  939. {
  940. AddFunctionToLibraryObject(globalObject, PropertyIds::CollectGarbage, &GlobalObject::EntryInfo::CollectGarbage, 0);
  941. }
  942. #if ENABLE_TTD
  943. if(scriptContext->GetThreadContext()->IsRuntimeInTTDMode())
  944. {
  945. //
  946. //TODO: when we formalize our telemetry library in JS land we will want to move these to a seperate Debug or Telemetry object instead of cluttering the global object
  947. //
  948. AddFunctionToLibraryObjectWithPropertyName(globalObject, _u("telemetryLog"), &GlobalObject::EntryInfo::TelemetryLog, 3);
  949. AddFunctionToLibraryObjectWithPropertyName(globalObject, _u("enabledDiagnosticsTrace"), &GlobalObject::EntryInfo::EnabledDiagnosticsTrace, 1);
  950. AddFunctionToLibraryObjectWithPropertyName(globalObject, _u("emitTTDLog"), &GlobalObject::EntryInfo::EmitTTDLog, 2);
  951. }
  952. #endif
  953. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  954. AddFunctionToLibraryObjectWithPropertyName(globalObject, _u("chWriteTraceEvent"), &GlobalObject::EntryInfo::ChWriteTraceEvent, 1);
  955. #endif
  956. #ifdef IR_VIEWER
  957. if (Js::Configuration::Global.flags.IsEnabled(Js::IRViewerFlag))
  958. {
  959. AddFunctionToLibraryObjectWithPropertyName(globalObject, _u("parseIR"), &GlobalObject::EntryInfo::ParseIR, 1);
  960. AddFunctionToLibraryObjectWithPropertyName(globalObject, _u("functionList"), &GlobalObject::EntryInfo::FunctionList, 1);
  961. AddFunctionToLibraryObjectWithPropertyName(globalObject, _u("rejitFunction"), &GlobalObject::EntryInfo::RejitFunction, 2);
  962. }
  963. #endif /* IR_VIEWER */
  964. DebugOnly(CheckRegisteredBuiltIns(builtinFuncs, scriptContext));
  965. builtInConstructorCache = RecyclerNew(this->GetRecycler(), ConstructorCache);
  966. builtInConstructorCache->PopulateForSkipDefaultNewObject(this->GetScriptContext());
  967. objectConstructor = CreateBuiltinConstructor(&JavascriptObject::EntryInfo::NewInstance,
  968. DeferredTypeHandler<InitializeObjectConstructor>::GetDefaultInstance());
  969. AddFunction(globalObject, PropertyIds::Object, objectConstructor);
  970. arrayConstructor = CreateBuiltinConstructor(&JavascriptArray::EntryInfo::NewInstance,
  971. DeferredTypeHandler<InitializeArrayConstructor>::GetDefaultInstance());
  972. SetArrayObjectHasUserDefinedSpecies(false);
  973. AddFunction(globalObject, PropertyIds::Array, arrayConstructor);
  974. booleanConstructor = CreateBuiltinConstructor(&JavascriptBoolean::EntryInfo::NewInstance,
  975. DeferredTypeHandler<InitializeBooleanConstructor>::GetDefaultInstance());
  976. AddFunction(globalObject, PropertyIds::Boolean, booleanConstructor);
  977. symbolConstructor = nullptr;
  978. proxyConstructor = nullptr;
  979. promiseConstructor = nullptr;
  980. reflectObject = nullptr;
  981. debugEval = nullptr;
  982. getStackTrace = nullptr;
  983. #ifdef EDIT_AND_CONTINUE
  984. editSource = nullptr;
  985. #endif
  986. symbolConstructor = CreateBuiltinConstructor(&JavascriptSymbol::EntryInfo::NewInstance,
  987. DeferredTypeHandler<InitializeSymbolConstructor>::GetDefaultInstance());
  988. AddFunction(globalObject, PropertyIds::Symbol, symbolConstructor);
  989. if (scriptContext->GetConfig()->IsES6ProxyEnabled())
  990. {
  991. proxyConstructor = CreateBuiltinConstructor(&JavascriptProxy::EntryInfo::NewInstance,
  992. DeferredTypeHandler<InitializeProxyConstructor>::GetDefaultInstance());
  993. AddFunction(globalObject, PropertyIds::Proxy, proxyConstructor);
  994. reflectObject = DynamicObject::New(recycler,
  995. DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  996. DeferredTypeHandler<InitializeReflectObject>::GetDefaultInstance()));
  997. AddMember(globalObject, PropertyIds::Reflect, reflectObject);
  998. }
  999. if (scriptContext->GetConfig()->IsES6PromiseEnabled())
  1000. {
  1001. promiseConstructor = CreateBuiltinConstructor(&JavascriptPromise::EntryInfo::NewInstance,
  1002. DeferredTypeHandler<InitializePromiseConstructor>::GetDefaultInstance());
  1003. AddFunction(globalObject, PropertyIds::Promise, promiseConstructor);
  1004. }
  1005. dateConstructor = CreateBuiltinConstructor(&JavascriptDate::EntryInfo::NewInstance,
  1006. DeferredTypeHandler<InitializeDateConstructor>::GetDefaultInstance());
  1007. AddFunction(globalObject, PropertyIds::Date, dateConstructor);
  1008. functionConstructor = CreateBuiltinConstructor(&JavascriptFunction::EntryInfo::NewInstance,
  1009. DeferredTypeHandler<InitializeFunctionConstructor, DefaultDeferredTypeFilter, true>::GetDefaultInstance());
  1010. AddFunction(globalObject, PropertyIds::Function, functionConstructor);
  1011. mathObject = DynamicObject::New(recycler,
  1012. DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  1013. DeferredTypeHandler<InitializeMathObject>::GetDefaultInstance()));
  1014. AddMember(globalObject, PropertyIds::Math, mathObject);
  1015. debugObject = nullptr;
  1016. numberConstructor = CreateBuiltinConstructor(&JavascriptNumber::EntryInfo::NewInstance,
  1017. DeferredTypeHandler<InitializeNumberConstructor>::GetDefaultInstance());
  1018. AddFunction(globalObject, PropertyIds::Number, numberConstructor);
  1019. stringConstructor = CreateBuiltinConstructor(&JavascriptString::EntryInfo::NewInstance,
  1020. DeferredTypeHandler<InitializeStringConstructor>::GetDefaultInstance());
  1021. AddFunction(globalObject, PropertyIds::String, stringConstructor);
  1022. regexConstructorType = DynamicType::New(scriptContext, TypeIds_Function, functionPrototype, JavascriptRegExp::NewInstance,
  1023. DeferredTypeHandler<InitializeRegexConstructor>::GetDefaultInstance());
  1024. regexConstructor = RecyclerNewEnumClass(recycler, EnumFunctionClass, JavascriptRegExpConstructor, regexConstructorType);
  1025. AddFunction(globalObject, PropertyIds::RegExp, regexConstructor);
  1026. arrayBufferConstructor = CreateBuiltinConstructor(&ArrayBuffer::EntryInfo::NewInstance,
  1027. DeferredTypeHandler<InitializeArrayBufferConstructor>::GetDefaultInstance());
  1028. AddFunction(globalObject, PropertyIds::ArrayBuffer, arrayBufferConstructor);
  1029. dataViewConstructor = CreateBuiltinConstructor(&DataView::EntryInfo::NewInstance,
  1030. DeferredTypeHandler<InitializeDataViewConstructor>::GetDefaultInstance());
  1031. AddFunction(globalObject, PropertyIds::DataView, dataViewConstructor);
  1032. typedArrayConstructor = CreateBuiltinConstructor(&TypedArrayBase::EntryInfo::NewInstance,
  1033. DeferredTypeHandler<InitializeTypedArrayConstructor, DefaultDeferredTypeFilter, true>::GetDefaultInstance(),
  1034. functionPrototype);
  1035. Int8ArrayConstructor = CreateBuiltinConstructor(&Int8Array::EntryInfo::NewInstance,
  1036. DeferredTypeHandler<InitializeInt8ArrayConstructor>::GetDefaultInstance(),
  1037. typedArrayConstructor);
  1038. AddFunction(globalObject, PropertyIds::Int8Array, Int8ArrayConstructor);
  1039. Uint8ArrayConstructor = CreateBuiltinConstructor(&Uint8Array::EntryInfo::NewInstance,
  1040. DeferredTypeHandler<InitializeUint8ArrayConstructor>::GetDefaultInstance(),
  1041. typedArrayConstructor);
  1042. AddFunction(globalObject, PropertyIds::Uint8Array, Uint8ArrayConstructor);
  1043. Uint8ClampedArrayConstructor = CreateBuiltinConstructor(&Uint8ClampedArray::EntryInfo::NewInstance,
  1044. DeferredTypeHandler<InitializeUint8ClampedArrayConstructor>::GetDefaultInstance(),
  1045. typedArrayConstructor);
  1046. AddFunction(globalObject, PropertyIds::Uint8ClampedArray, Uint8ClampedArrayConstructor);
  1047. Int16ArrayConstructor = CreateBuiltinConstructor(&Int16Array::EntryInfo::NewInstance,
  1048. DeferredTypeHandler<InitializeInt16ArrayConstructor>::GetDefaultInstance(),
  1049. typedArrayConstructor);
  1050. AddFunction(globalObject, PropertyIds::Int16Array, Int16ArrayConstructor);
  1051. Uint16ArrayConstructor = CreateBuiltinConstructor(&Uint16Array::EntryInfo::NewInstance,
  1052. DeferredTypeHandler<InitializeUint16ArrayConstructor>::GetDefaultInstance(),
  1053. typedArrayConstructor);
  1054. AddFunction(globalObject, PropertyIds::Uint16Array, Uint16ArrayConstructor);
  1055. Int32ArrayConstructor = CreateBuiltinConstructor(&Int32Array::EntryInfo::NewInstance,
  1056. DeferredTypeHandler<InitializeInt32ArrayConstructor>::GetDefaultInstance(),
  1057. typedArrayConstructor);
  1058. AddFunction(globalObject, PropertyIds::Int32Array, Int32ArrayConstructor);
  1059. Uint32ArrayConstructor = CreateBuiltinConstructor(&Uint32Array::EntryInfo::NewInstance,
  1060. DeferredTypeHandler<InitializeUint32ArrayConstructor>::GetDefaultInstance(),
  1061. typedArrayConstructor);
  1062. AddFunction(globalObject, PropertyIds::Uint32Array, Uint32ArrayConstructor);
  1063. Float32ArrayConstructor = CreateBuiltinConstructor(&Float32Array::EntryInfo::NewInstance,
  1064. DeferredTypeHandler<InitializeFloat32ArrayConstructor>::GetDefaultInstance(),
  1065. typedArrayConstructor);
  1066. AddFunction(globalObject, PropertyIds::Float32Array, Float32ArrayConstructor);
  1067. Float64ArrayConstructor = CreateBuiltinConstructor(&Float64Array::EntryInfo::NewInstance,
  1068. DeferredTypeHandler<InitializeFloat64ArrayConstructor>::GetDefaultInstance(),
  1069. typedArrayConstructor);
  1070. AddFunction(globalObject, PropertyIds::Float64Array, Float64ArrayConstructor);
  1071. if (scriptContext->GetConfig()->IsESSharedArrayBufferEnabled())
  1072. {
  1073. sharedArrayBufferConstructor = CreateBuiltinConstructor(&SharedArrayBuffer::EntryInfo::NewInstance,
  1074. DeferredTypeHandler<InitializeSharedArrayBufferConstructor>::GetDefaultInstance());
  1075. AddFunction(globalObject, PropertyIds::SharedArrayBuffer, sharedArrayBufferConstructor);
  1076. atomicsObject = DynamicObject::New(recycler,
  1077. DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  1078. DeferredTypeHandler<InitializeAtomicsObject>::GetDefaultInstance()));
  1079. AddMember(globalObject, PropertyIds::Atomics, atomicsObject);
  1080. }
  1081. else
  1082. {
  1083. sharedArrayBufferConstructor = nullptr;
  1084. atomicsObject = nullptr;
  1085. }
  1086. JSONObject = DynamicObject::New(recycler,
  1087. DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  1088. DeferredTypeHandler<InitializeJSONObject>::GetDefaultInstance()));
  1089. AddMember(globalObject, PropertyIds::JSON, JSONObject);
  1090. #ifdef ENABLE_INTL_OBJECT
  1091. if (scriptContext->IsIntlEnabled())
  1092. {
  1093. IntlObject = DynamicObject::New(recycler,
  1094. DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  1095. DeferredTypeHandler<InitializeIntlObject>::GetDefaultInstance()));
  1096. AddMember(globalObject, PropertyIds::Intl, IntlObject);
  1097. }
  1098. else
  1099. {
  1100. IntlObject = nullptr;
  1101. }
  1102. #endif
  1103. #if defined(ENABLE_INTL_OBJECT) || defined(ENABLE_JS_BUILTINS) || defined(ENABLE_PROJECTION)
  1104. engineInterfaceObject = EngineInterfaceObject::New(recycler,
  1105. DynamicType::New(scriptContext, TypeIds_EngineInterfaceObject, objectPrototype, nullptr,
  1106. DeferredTypeHandler<InitializeEngineInterfaceObject>::GetDefaultInstance()));
  1107. #ifdef ENABLE_INTL_OBJECT
  1108. IntlEngineInterfaceExtensionObject* intlExtension = RecyclerNew(recycler, IntlEngineInterfaceExtensionObject, scriptContext);
  1109. engineInterfaceObject->SetEngineExtension(EngineInterfaceExtensionKind_Intl, intlExtension);
  1110. #endif
  1111. #ifdef ENABLE_JS_BUILTINS
  1112. chakraLibraryObject = DynamicObject::New(recycler,
  1113. DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  1114. DeferredTypeHandler<InitializeChakraLibraryObject>::GetDefaultInstance()));
  1115. if (CONFIG_FLAG(LdChakraLib)) {
  1116. AddMember(globalObject, PropertyIds::__chakraLibrary, chakraLibraryObject);
  1117. }
  1118. JsBuiltInEngineInterfaceExtensionObject* builtInExtension = RecyclerNew(recycler, JsBuiltInEngineInterfaceExtensionObject, scriptContext);
  1119. engineInterfaceObject->SetEngineExtension(EngineInterfaceExtensionKind_JsBuiltIn, builtInExtension);
  1120. this->isArrayFunction = this->DefaultCreateFunction(&JavascriptArray::EntryInfo::IsArray, 1, nullptr, nullptr, PropertyIds::isArray);
  1121. #endif
  1122. #endif
  1123. mapConstructor = CreateBuiltinConstructor(&JavascriptMap::EntryInfo::NewInstance,
  1124. DeferredTypeHandler<InitializeMapConstructor>::GetDefaultInstance());
  1125. AddFunction(globalObject, PropertyIds::Map, mapConstructor);
  1126. setConstructor = CreateBuiltinConstructor(&JavascriptSet::EntryInfo::NewInstance,
  1127. DeferredTypeHandler<InitializeSetConstructor>::GetDefaultInstance());
  1128. AddFunction(globalObject, PropertyIds::Set, setConstructor);
  1129. weakMapConstructor = CreateBuiltinConstructor(&JavascriptWeakMap::EntryInfo::NewInstance,
  1130. DeferredTypeHandler<InitializeWeakMapConstructor>::GetDefaultInstance());
  1131. AddFunction(globalObject, PropertyIds::WeakMap, weakMapConstructor);
  1132. weakSetConstructor = CreateBuiltinConstructor(&JavascriptWeakSet::EntryInfo::NewInstance,
  1133. DeferredTypeHandler<InitializeWeakSetConstructor>::GetDefaultInstance());
  1134. AddFunction(globalObject, PropertyIds::WeakSet, weakSetConstructor);
  1135. generatorFunctionConstructor = nullptr;
  1136. if (scriptContext->GetConfig()->IsES6GeneratorsEnabled())
  1137. {
  1138. generatorFunctionConstructor = CreateBuiltinConstructor(&JavascriptGeneratorFunction::EntryInfo::NewInstance,
  1139. DeferredTypeHandler<InitializeGeneratorFunctionConstructor>::GetDefaultInstance(),
  1140. functionConstructor);
  1141. // GeneratorFunction is not a global property by ES6 spec so don't add it to the global object
  1142. }
  1143. asyncFunctionConstructor = nullptr;
  1144. if (scriptContext->GetConfig()->IsES7AsyncAndAwaitEnabled())
  1145. {
  1146. asyncFunctionConstructor = CreateBuiltinConstructor(&JavascriptFunction::EntryInfo::NewAsyncFunctionInstance,
  1147. DeferredTypeHandler<InitializeAsyncFunctionConstructor>::GetDefaultInstance(),
  1148. functionConstructor);
  1149. // AsyncFunction is not a global property by ES7 spec so don't add it to the global object
  1150. }
  1151. errorConstructor = CreateBuiltinConstructor(&JavascriptError::EntryInfo::NewErrorInstance,
  1152. DeferredTypeHandler<InitializeErrorConstructor>::GetDefaultInstance());
  1153. AddFunction(globalObject, PropertyIds::Error, errorConstructor);
  1154. RuntimeFunction* nativeErrorPrototype = nullptr;
  1155. if (scriptContext->GetConfig()->IsES6PrototypeChain())
  1156. {
  1157. nativeErrorPrototype = errorConstructor;
  1158. }
  1159. evalErrorConstructor = CreateBuiltinConstructor(&JavascriptError::EntryInfo::NewEvalErrorInstance,
  1160. DeferredTypeHandler<InitializeEvalErrorConstructor>::GetDefaultInstance(),
  1161. nativeErrorPrototype);
  1162. AddFunction(globalObject, PropertyIds::EvalError, evalErrorConstructor);
  1163. rangeErrorConstructor = CreateBuiltinConstructor(&JavascriptError::EntryInfo::NewRangeErrorInstance,
  1164. DeferredTypeHandler<InitializeRangeErrorConstructor>::GetDefaultInstance(),
  1165. nativeErrorPrototype);
  1166. AddFunction(globalObject, PropertyIds::RangeError, rangeErrorConstructor);
  1167. referenceErrorConstructor = CreateBuiltinConstructor(&JavascriptError::EntryInfo::NewReferenceErrorInstance,
  1168. DeferredTypeHandler<InitializeReferenceErrorConstructor>::GetDefaultInstance(),
  1169. nativeErrorPrototype);
  1170. AddFunction(globalObject, PropertyIds::ReferenceError, referenceErrorConstructor);
  1171. syntaxErrorConstructor = CreateBuiltinConstructor(&JavascriptError::EntryInfo::NewSyntaxErrorInstance,
  1172. DeferredTypeHandler<InitializeSyntaxErrorConstructor>::GetDefaultInstance(),
  1173. nativeErrorPrototype);
  1174. AddFunction(globalObject, PropertyIds::SyntaxError, syntaxErrorConstructor);
  1175. typeErrorConstructor = CreateBuiltinConstructor(&JavascriptError::EntryInfo::NewTypeErrorInstance,
  1176. DeferredTypeHandler<InitializeTypeErrorConstructor>::GetDefaultInstance(),
  1177. nativeErrorPrototype);
  1178. AddFunction(globalObject, PropertyIds::TypeError, typeErrorConstructor);
  1179. uriErrorConstructor = CreateBuiltinConstructor(&JavascriptError::EntryInfo::NewURIErrorInstance,
  1180. DeferredTypeHandler<InitializeURIErrorConstructor>::GetDefaultInstance(),
  1181. nativeErrorPrototype);
  1182. AddFunction(globalObject, PropertyIds::URIError, uriErrorConstructor);
  1183. #ifdef ENABLE_WASM
  1184. if (CONFIG_FLAG(Wasm) && !PHASE_OFF1(Js::WasmPhase))
  1185. {
  1186. webAssemblyCompileFunction = nullptr;
  1187. // new WebAssembly object
  1188. webAssemblyObject = DynamicObject::New(recycler,
  1189. DynamicType::New(scriptContext, TypeIds_Object, objectPrototype, nullptr,
  1190. DeferredTypeHandler<InitializeWebAssemblyObject>::GetDefaultInstance()));
  1191. AddMember(globalObject, PropertyIds::WebAssembly, webAssemblyObject);
  1192. webAssemblyCompileErrorConstructor = CreateBuiltinConstructor(&JavascriptError::EntryInfo::NewWebAssemblyCompileErrorInstance,
  1193. DeferredTypeHandler<InitializeWebAssemblyCompileErrorConstructor>::GetDefaultInstance(),
  1194. nativeErrorPrototype);
  1195. webAssemblyRuntimeErrorConstructor = CreateBuiltinConstructor(&JavascriptError::EntryInfo::NewWebAssemblyRuntimeErrorInstance,
  1196. DeferredTypeHandler<InitializeWebAssemblyRuntimeErrorConstructor>::GetDefaultInstance(),
  1197. nativeErrorPrototype);
  1198. webAssemblyLinkErrorConstructor = CreateBuiltinConstructor(&JavascriptError::EntryInfo::NewWebAssemblyLinkErrorInstance,
  1199. DeferredTypeHandler<InitializeWebAssemblyLinkErrorConstructor>::GetDefaultInstance(),
  1200. nativeErrorPrototype);
  1201. webAssemblyInstanceConstructor = CreateBuiltinConstructor(&WebAssemblyInstance::EntryInfo::NewInstance,
  1202. DeferredTypeHandler<InitializeWebAssemblyInstanceConstructor>::GetDefaultInstance(), webAssemblyInstancePrototype);
  1203. webAssemblyModuleConstructor = CreateBuiltinConstructor(&WebAssemblyModule::EntryInfo::NewInstance,
  1204. DeferredTypeHandler<InitializeWebAssemblyModuleConstructor>::GetDefaultInstance(), webAssemblyModulePrototype);
  1205. webAssemblyMemoryConstructor = CreateBuiltinConstructor(&WebAssemblyMemory::EntryInfo::NewInstance,
  1206. DeferredTypeHandler<InitializeWebAssemblyMemoryConstructor>::GetDefaultInstance(), webAssemblyMemoryConstructor);
  1207. webAssemblyTableConstructor = CreateBuiltinConstructor(&WebAssemblyTable::EntryInfo::NewInstance,
  1208. DeferredTypeHandler<InitializeWebAssemblyTableConstructor>::GetDefaultInstance(), webAssemblyTableConstructor);
  1209. }
  1210. #endif
  1211. }
  1212. void JavascriptLibrary::EnsureDebugObject(DynamicObject* newDebugObject)
  1213. {
  1214. Assert(!debugObject);
  1215. if (!debugObject)
  1216. {
  1217. this->debugObject = newDebugObject;
  1218. AddMember(globalObject, PropertyIds::Debug, debugObject);
  1219. }
  1220. }
  1221. void JavascriptLibrary::SetDebugObjectNonUserAccessor(FunctionInfo *funcGetter, FunctionInfo *funcSetter)
  1222. {
  1223. Assert(funcGetter);
  1224. Assert(funcSetter);
  1225. debugObjectNonUserGetterFunction = CreateNonProfiledFunction(funcGetter);
  1226. debugObjectNonUserGetterFunction->SetPropertyWithAttributes(PropertyIds::length, TaggedInt::ToVarUnchecked(0), PropertyConfigurable, nullptr);
  1227. debugObjectNonUserSetterFunction = CreateNonProfiledFunction(funcSetter);
  1228. debugObjectNonUserSetterFunction->SetPropertyWithAttributes(PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable, nullptr);
  1229. }
  1230. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  1231. void JavascriptLibrary::SetDebugObjectFaultInjectionCookieGetterAccessor(FunctionInfo *funcGetter, FunctionInfo *funcSetter)
  1232. {
  1233. Assert(funcGetter);
  1234. Assert(funcSetter);
  1235. debugObjectFaultInjectionCookieGetterFunction = CreateNonProfiledFunction(funcGetter);
  1236. debugObjectFaultInjectionCookieGetterFunction->SetPropertyWithAttributes(PropertyIds::length, TaggedInt::ToVarUnchecked(0), PropertyConfigurable, nullptr);
  1237. debugObjectFaultInjectionCookieSetterFunction = CreateNonProfiledFunction(funcSetter);
  1238. debugObjectFaultInjectionCookieSetterFunction->SetPropertyWithAttributes(PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable, nullptr);
  1239. }
  1240. #endif
  1241. void JavascriptLibrary::SetDebugObjectDebugModeAccessor(FunctionInfo *funcGetter)
  1242. {
  1243. Assert(funcGetter);
  1244. debugObjectDebugModeGetterFunction = CreateNonProfiledFunction(funcGetter);
  1245. debugObjectDebugModeGetterFunction->SetPropertyWithAttributes(PropertyIds::length, TaggedInt::ToVarUnchecked(0), PropertyConfigurable, nullptr);
  1246. }
  1247. bool JavascriptLibrary::InitializeArrayConstructor(DynamicObject* arrayConstructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  1248. {
  1249. typeHandler->Convert(arrayConstructor, mode, 6);
  1250. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterArray
  1251. // so that the update is in sync with profiler
  1252. ScriptContext* scriptContext = arrayConstructor->GetScriptContext();
  1253. JavascriptLibrary* library = arrayConstructor->GetLibrary();
  1254. Field(JavascriptFunction*)* builtinFuncs = library->GetBuiltinFunctions();
  1255. library->AddMember(arrayConstructor, PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable);
  1256. library->AddMember(arrayConstructor, PropertyIds::prototype, scriptContext->GetLibrary()->arrayPrototype, PropertyNone);
  1257. library->AddSpeciesAccessorsToLibraryObject(arrayConstructor, &JavascriptArray::EntryInfo::GetterSymbolSpecies);
  1258. library->AddMember(arrayConstructor, PropertyIds::name, scriptContext->GetPropertyString(PropertyIds::Array), PropertyConfigurable);
  1259. #ifdef ENABLE_JS_BUILTINS
  1260. builtinFuncs[BuiltinFunction::JavascriptArray_IsArray] = library->isArrayFunction;
  1261. library->AddMember(arrayConstructor, PropertyIds::isArray, library->isArrayFunction);
  1262. #else
  1263. library->AddFunctionToLibraryObject(arrayConstructor, PropertyIds::isArray, &JavascriptArray::EntryInfo::IsArray, 1);
  1264. #endif
  1265. library->AddFunctionToLibraryObject(arrayConstructor, PropertyIds::from, &JavascriptArray::EntryInfo::From, 1);
  1266. library->AddFunctionToLibraryObject(arrayConstructor, PropertyIds::of, &JavascriptArray::EntryInfo::Of, 0);
  1267. DebugOnly(CheckRegisteredBuiltIns(builtinFuncs, scriptContext));
  1268. arrayConstructor->SetHasNoEnumerableProperties(true);
  1269. return true;
  1270. }
  1271. bool JavascriptLibrary::IsDefaultArrayValuesFunction(RecyclableObject * function, ScriptContext *scriptContext)
  1272. {
  1273. #ifdef ENABLE_JS_BUILTINS
  1274. if (scriptContext->IsJsBuiltInEnabled())
  1275. {
  1276. scriptContext->GetLibrary()->EnsureBuiltInEngineIsReady();
  1277. return JavascriptFunction::Is(function) && JavascriptFunction::FromVar(function)->IsJsBuiltIn();
  1278. }
  1279. #endif
  1280. JavascriptMethod method = function->GetEntryPoint();
  1281. return method == JavascriptArray::EntryInfo::Values.GetOriginalEntryPoint();
  1282. }
  1283. JavascriptFunction* JavascriptLibrary::EnsureArrayPrototypeForEachFunction()
  1284. {
  1285. if (arrayPrototypeForEachFunction == nullptr)
  1286. {
  1287. arrayPrototypeForEachFunction = DefaultCreateFunction(&JavascriptArray::EntryInfo::ForEach, 1, nullptr, nullptr, PropertyIds::forEach);
  1288. }
  1289. return arrayPrototypeForEachFunction;
  1290. }
  1291. JavascriptFunction* JavascriptLibrary::EnsureArrayPrototypeKeysFunction()
  1292. {
  1293. if (arrayPrototypeKeysFunction == nullptr)
  1294. {
  1295. #ifndef ENABLE_JS_BUILTINS
  1296. arrayPrototypeKeysFunction = DefaultCreateFunction(&JavascriptArray::EntryInfo::Keys, 0, nullptr, nullptr, PropertyIds::keys);
  1297. #else
  1298. if (!scriptContext->IsJsBuiltInEnabled())
  1299. {
  1300. arrayPrototypeKeysFunction = DefaultCreateFunction(&JavascriptArray::EntryInfo::Keys, 0, nullptr, nullptr, PropertyIds::keys);
  1301. }
  1302. else
  1303. {
  1304. this->EnsureBuiltInEngineIsReady();
  1305. }
  1306. #endif
  1307. }
  1308. return arrayPrototypeKeysFunction;
  1309. }
  1310. JavascriptFunction* JavascriptLibrary::EnsureArrayPrototypeValuesFunction()
  1311. {
  1312. if (arrayPrototypeValuesFunction == nullptr)
  1313. {
  1314. #ifndef ENABLE_JS_BUILTINS
  1315. arrayPrototypeValuesFunction = DefaultCreateFunction(&JavascriptArray::EntryInfo::Values, 0, nullptr, nullptr, PropertyIds::values);
  1316. #else
  1317. if (!scriptContext->IsJsBuiltInEnabled())
  1318. {
  1319. arrayPrototypeValuesFunction = DefaultCreateFunction(&JavascriptArray::EntryInfo::Values, 0, nullptr, nullptr, PropertyIds::values);
  1320. }
  1321. else
  1322. {
  1323. this->EnsureBuiltInEngineIsReady();
  1324. }
  1325. #endif
  1326. }
  1327. return arrayPrototypeValuesFunction;
  1328. }
  1329. JavascriptFunction* JavascriptLibrary::EnsureArrayPrototypeEntriesFunction()
  1330. {
  1331. if (arrayPrototypeEntriesFunction == nullptr)
  1332. {
  1333. #ifndef ENABLE_JS_BUILTINS
  1334. arrayPrototypeEntriesFunction = DefaultCreateFunction(&JavascriptArray::EntryInfo::Entries, 0, nullptr, nullptr, PropertyIds::entries);
  1335. #else
  1336. if (!scriptContext->IsJsBuiltInEnabled())
  1337. {
  1338. arrayPrototypeEntriesFunction = DefaultCreateFunction(&JavascriptArray::EntryInfo::Entries, 0, nullptr, nullptr, PropertyIds::entries);
  1339. }
  1340. else
  1341. {
  1342. this->EnsureBuiltInEngineIsReady();
  1343. }
  1344. #endif
  1345. }
  1346. return arrayPrototypeEntriesFunction;
  1347. }
  1348. bool JavascriptLibrary::InitializeArrayPrototype(DynamicObject* arrayPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  1349. {
  1350. typeHandler->Convert(arrayPrototype, mode, 24);
  1351. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterArray
  1352. // so that the update is in sync with profiler
  1353. ScriptContext* scriptContext = arrayPrototype->GetScriptContext();
  1354. JavascriptLibrary* library = arrayPrototype->GetLibrary();
  1355. library->AddMember(arrayPrototype, PropertyIds::constructor, library->arrayConstructor);
  1356. Field(JavascriptFunction*)* builtinFuncs = library->GetBuiltinFunctions();
  1357. builtinFuncs[BuiltinFunction::JavascriptArray_Push] = library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::push, &JavascriptArray::EntryInfo::Push, 1);
  1358. builtinFuncs[BuiltinFunction::JavascriptArray_Concat] = library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::concat, &JavascriptArray::EntryInfo::Concat, 1);
  1359. builtinFuncs[BuiltinFunction::JavascriptArray_Join] = library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::join, &JavascriptArray::EntryInfo::Join, 1);
  1360. builtinFuncs[BuiltinFunction::JavascriptArray_Pop] = library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::pop, &JavascriptArray::EntryInfo::Pop, 0);
  1361. builtinFuncs[BuiltinFunction::JavascriptArray_Reverse] = library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::reverse, &JavascriptArray::EntryInfo::Reverse, 0);
  1362. builtinFuncs[BuiltinFunction::JavascriptArray_Shift] = library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::shift, &JavascriptArray::EntryInfo::Shift, 0);
  1363. builtinFuncs[BuiltinFunction::JavascriptArray_Slice] = library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::slice, &JavascriptArray::EntryInfo::Slice, 2);
  1364. /* No inlining Array_Sort */ library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::sort, &JavascriptArray::EntryInfo::Sort, 1);
  1365. builtinFuncs[BuiltinFunction::JavascriptArray_Splice] = library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::splice, &JavascriptArray::EntryInfo::Splice, 2);
  1366. // The toString and toLocaleString properties are shared between Array.prototype and %TypedArray%.prototype.
  1367. // Whichever prototype is initialized first will create the functions, the other should just load the existing function objects.
  1368. if (library->arrayPrototypeToStringFunction == nullptr)
  1369. {
  1370. Assert(library->arrayPrototypeToLocaleStringFunction == nullptr);
  1371. library->arrayPrototypeToLocaleStringFunction = /* No inlining Array_ToLocaleString */ library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::toLocaleString, &JavascriptArray::EntryInfo::ToLocaleString, 0);
  1372. library->arrayPrototypeToStringFunction = /* No inlining Array_ToString */ library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::toString, &JavascriptArray::EntryInfo::ToString, 0);
  1373. }
  1374. else
  1375. {
  1376. Assert(library->arrayPrototypeToLocaleStringFunction);
  1377. library->AddMember(arrayPrototype, PropertyIds::toLocaleString, library->arrayPrototypeToLocaleStringFunction);
  1378. library->AddMember(arrayPrototype, PropertyIds::toString, library->arrayPrototypeToStringFunction);
  1379. }
  1380. builtinFuncs[BuiltinFunction::JavascriptArray_Unshift] = library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::unshift, &JavascriptArray::EntryInfo::Unshift, 1);
  1381. builtinFuncs[BuiltinFunction::JavascriptArray_IndexOf] = library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::indexOf, &JavascriptArray::EntryInfo::IndexOf, 1);
  1382. /* No inlining Array_Every */ library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::every, &JavascriptArray::EntryInfo::Every, 1);
  1383. /* No inlining Array_Filter */ library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::filter, &JavascriptArray::EntryInfo::Filter, 1);
  1384. /* No inlining Array_ForEach */
  1385. library->AddMember(arrayPrototype, PropertyIds::forEach, library->EnsureArrayPrototypeForEachFunction());
  1386. builtinFuncs[BuiltinFunction::JavascriptArray_LastIndexOf] = library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::lastIndexOf, &JavascriptArray::EntryInfo::LastIndexOf, 1);
  1387. /* No inlining Array_Map */ library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::map, &JavascriptArray::EntryInfo::Map, 1);
  1388. /* No inlining Array_Reduce */ library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::reduce, &JavascriptArray::EntryInfo::Reduce, 1);
  1389. /* No inlining Array_ReduceRight */ library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::reduceRight, &JavascriptArray::EntryInfo::ReduceRight, 1);
  1390. /* No inlining Array_Some */ library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::some, &JavascriptArray::EntryInfo::Some, 1);
  1391. if (scriptContext->GetConfig()->IsES6StringExtensionsEnabled()) // This is not a typo, Array.prototype.find and .findIndex are part of the ES6 Improved String APIs feature
  1392. {
  1393. /* No inlining Array_Find */ library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::find, &JavascriptArray::EntryInfo::Find, 1);
  1394. /* No inlining Array_FindIndex */ library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::findIndex, &JavascriptArray::EntryInfo::FindIndex, 1);
  1395. }
  1396. #ifndef ENABLE_JS_BUILTINS
  1397. /* No inlining Array_Entries */
  1398. library->AddMember(arrayPrototype, PropertyIds::entries, library->EnsureArrayPrototypeEntriesFunction());
  1399. /* No inlining Array_Keys */
  1400. library->AddMember(arrayPrototype, PropertyIds::keys, library->EnsureArrayPrototypeKeysFunction());
  1401. JavascriptFunction *values = library->EnsureArrayPrototypeValuesFunction();
  1402. /* No inlining Array_Values */ library->AddMember(arrayPrototype, PropertyIds::values, values);
  1403. /* No inlining Array_SymbolIterator */ library->AddMember(arrayPrototype, PropertyIds::_symbolIterator, values);
  1404. #else
  1405. if (!scriptContext->IsJsBuiltInEnabled())
  1406. {
  1407. /* No inlining Array_Entries */
  1408. library->AddMember(arrayPrototype, PropertyIds::entries, library->EnsureArrayPrototypeEntriesFunction());
  1409. /* No inlining Array_Keys */
  1410. library->AddMember(arrayPrototype, PropertyIds::keys, library->EnsureArrayPrototypeKeysFunction());
  1411. JavascriptFunction *values = library->EnsureArrayPrototypeValuesFunction();
  1412. /* No inlining Array_Values */ library->AddMember(arrayPrototype, PropertyIds::values, values);
  1413. /* No inlining Array_SymbolIterator */ library->AddMember(arrayPrototype, PropertyIds::_symbolIterator, values);
  1414. }
  1415. else
  1416. {
  1417. library->EnsureBuiltInEngineIsReady();
  1418. }
  1419. #endif
  1420. if (scriptContext->GetConfig()->IsES6UnscopablesEnabled())
  1421. {
  1422. DynamicType* dynamicType = DynamicType::New(scriptContext, TypeIds_Object, library->nullValue, nullptr, NullTypeHandler<false>::GetDefaultInstance(), false);
  1423. DynamicObject* unscopablesList = DynamicObject::New(library->GetRecycler(), dynamicType);
  1424. unscopablesList->SetProperty(PropertyIds::find, JavascriptBoolean::ToVar(true, scriptContext), PropertyOperation_None, nullptr);
  1425. unscopablesList->SetProperty(PropertyIds::findIndex, JavascriptBoolean::ToVar(true, scriptContext), PropertyOperation_None, nullptr);
  1426. unscopablesList->SetProperty(PropertyIds::fill, JavascriptBoolean::ToVar(true, scriptContext), PropertyOperation_None, nullptr);
  1427. unscopablesList->SetProperty(PropertyIds::copyWithin, JavascriptBoolean::ToVar(true, scriptContext), PropertyOperation_None, nullptr);
  1428. unscopablesList->SetProperty(PropertyIds::entries, JavascriptBoolean::ToVar(true, scriptContext), PropertyOperation_None, nullptr);
  1429. unscopablesList->SetProperty(PropertyIds::includes, JavascriptBoolean::ToVar(true, scriptContext), PropertyOperation_None, nullptr);
  1430. unscopablesList->SetProperty(PropertyIds::keys, JavascriptBoolean::ToVar(true, scriptContext), PropertyOperation_None, nullptr);
  1431. unscopablesList->SetProperty(PropertyIds::values, JavascriptBoolean::ToVar(true, scriptContext), PropertyOperation_None, nullptr);
  1432. library->AddMember(arrayPrototype, PropertyIds::_symbolUnscopables, unscopablesList, PropertyConfigurable);
  1433. }
  1434. /* No inlining Array_Fill */ library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::fill, &JavascriptArray::EntryInfo::Fill, 1);
  1435. /* No inlining Array_CopyWithin */ library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::copyWithin, &JavascriptArray::EntryInfo::CopyWithin, 2);
  1436. builtinFuncs[BuiltinFunction::JavascriptArray_Includes] = library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::includes, &JavascriptArray::EntryInfo::Includes, 1);
  1437. DebugOnly(CheckRegisteredBuiltIns(builtinFuncs, scriptContext));
  1438. arrayPrototype->SetHasNoEnumerableProperties(true);
  1439. return true;
  1440. }
  1441. bool JavascriptLibrary::InitializeSharedArrayBufferConstructor(DynamicObject* sharedArrayBufferConstructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  1442. {
  1443. typeHandler->Convert(sharedArrayBufferConstructor, mode, 4);
  1444. ScriptContext* scriptContext = sharedArrayBufferConstructor->GetScriptContext();
  1445. JavascriptLibrary* library = sharedArrayBufferConstructor->GetLibrary();
  1446. library->AddMember(sharedArrayBufferConstructor, PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable);
  1447. library->AddMember(sharedArrayBufferConstructor, PropertyIds::prototype, scriptContext->GetLibrary()->sharedArrayBufferPrototype, PropertyNone);
  1448. library->AddSpeciesAccessorsToLibraryObject(sharedArrayBufferConstructor, &SharedArrayBuffer::EntryInfo::GetterSymbolSpecies);
  1449. library->AddMember(sharedArrayBufferConstructor, PropertyIds::name, scriptContext->GetPropertyString(PropertyIds::SharedArrayBuffer), PropertyConfigurable);
  1450. sharedArrayBufferConstructor->SetHasNoEnumerableProperties(true);
  1451. return true;
  1452. }
  1453. bool JavascriptLibrary::InitializeSharedArrayBufferPrototype(DynamicObject* sharedArrayBufferPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  1454. {
  1455. typeHandler->Convert(sharedArrayBufferPrototype, mode, 4);
  1456. ScriptContext* scriptContext = sharedArrayBufferPrototype->GetScriptContext();
  1457. JavascriptLibrary* library = sharedArrayBufferPrototype->GetLibrary();
  1458. library->AddMember(sharedArrayBufferPrototype, PropertyIds::constructor, library->sharedArrayBufferConstructor);
  1459. library->AddFunctionToLibraryObject(sharedArrayBufferPrototype, PropertyIds::slice, &SharedArrayBuffer::EntryInfo::Slice, 2);
  1460. library->AddAccessorsToLibraryObject(sharedArrayBufferPrototype, PropertyIds::byteLength, &SharedArrayBuffer::EntryInfo::GetterByteLength, nullptr);
  1461. if (scriptContext->GetConfig()->IsES6ToStringTagEnabled())
  1462. {
  1463. library->AddMember(sharedArrayBufferPrototype, PropertyIds::_symbolToStringTag, library->CreateStringFromCppLiteral(_u("SharedArrayBuffer")), PropertyConfigurable);
  1464. }
  1465. sharedArrayBufferPrototype->SetHasNoEnumerableProperties(true);
  1466. return true;
  1467. }
  1468. bool JavascriptLibrary::InitializeAtomicsObject(DynamicObject* atomicsObject, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  1469. {
  1470. typeHandler->Convert(atomicsObject, mode, 12);
  1471. JavascriptLibrary* library = atomicsObject->GetLibrary();
  1472. library->AddFunctionToLibraryObject(atomicsObject, PropertyIds::add, &AtomicsObject::EntryInfo::Add, 3);
  1473. library->AddFunctionToLibraryObject(atomicsObject, PropertyIds::and_, &AtomicsObject::EntryInfo::And, 3);
  1474. library->AddFunctionToLibraryObject(atomicsObject, PropertyIds::compareExchange, &AtomicsObject::EntryInfo::CompareExchange, 4);
  1475. library->AddFunctionToLibraryObject(atomicsObject, PropertyIds::exchange, &AtomicsObject::EntryInfo::Exchange, 3);
  1476. library->AddFunctionToLibraryObject(atomicsObject, PropertyIds::isLockFree, &AtomicsObject::EntryInfo::IsLockFree, 1);
  1477. library->AddFunctionToLibraryObject(atomicsObject, PropertyIds::load, &AtomicsObject::EntryInfo::Load, 2);
  1478. library->AddFunctionToLibraryObject(atomicsObject, PropertyIds::or_, &AtomicsObject::EntryInfo::Or, 3);
  1479. library->AddFunctionToLibraryObject(atomicsObject, PropertyIds::store, &AtomicsObject::EntryInfo::Store, 3);
  1480. library->AddFunctionToLibraryObject(atomicsObject, PropertyIds::sub, &AtomicsObject::EntryInfo::Sub, 3);
  1481. library->AddFunctionToLibraryObject(atomicsObject, PropertyIds::wait, &AtomicsObject::EntryInfo::Wait, 4);
  1482. library->AddFunctionToLibraryObject(atomicsObject, PropertyIds::wake, &AtomicsObject::EntryInfo::Wake, 3);
  1483. library->AddFunctionToLibraryObject(atomicsObject, PropertyIds::xor_, &AtomicsObject::EntryInfo::Xor, 3);
  1484. if (atomicsObject->GetScriptContext()->GetConfig()->IsES6ToStringTagEnabled())
  1485. {
  1486. library->AddMember(atomicsObject, PropertyIds::_symbolToStringTag, library->CreateStringFromCppLiteral(_u("Atomics")), PropertyConfigurable);
  1487. }
  1488. atomicsObject->SetHasNoEnumerableProperties(true);
  1489. return true;
  1490. }
  1491. bool JavascriptLibrary::InitializeArrayBufferConstructor(DynamicObject* arrayBufferConstructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  1492. {
  1493. typeHandler->Convert(arrayBufferConstructor, mode, 4);
  1494. ScriptContext* scriptContext = arrayBufferConstructor->GetScriptContext();
  1495. JavascriptLibrary* library = arrayBufferConstructor->GetLibrary();
  1496. library->AddMember(arrayBufferConstructor, PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable);
  1497. library->AddMember(arrayBufferConstructor, PropertyIds::prototype, scriptContext->GetLibrary()->arrayBufferPrototype, PropertyNone);
  1498. library->AddSpeciesAccessorsToLibraryObject(arrayBufferConstructor, &ArrayBuffer::EntryInfo::GetterSymbolSpecies);
  1499. library->AddMember(arrayBufferConstructor, PropertyIds::name, scriptContext->GetPropertyString(PropertyIds::ArrayBuffer), PropertyConfigurable);
  1500. library->AddFunctionToLibraryObject(arrayBufferConstructor, PropertyIds::isView, &ArrayBuffer::EntryInfo::IsView, 1);
  1501. #if ENABLE_DEBUG_CONFIG_OPTIONS
  1502. library->AddFunctionToLibraryObject(arrayBufferConstructor, PropertyIds::detach, &ArrayBuffer::EntryInfo::Detach, 1);
  1503. #endif
  1504. arrayBufferConstructor->SetHasNoEnumerableProperties(true);
  1505. return true;
  1506. }
  1507. bool JavascriptLibrary::InitializeArrayBufferPrototype(DynamicObject* arrayBufferPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  1508. {
  1509. typeHandler->Convert(arrayBufferPrototype, mode, 2);
  1510. ScriptContext* scriptContext = arrayBufferPrototype->GetScriptContext();
  1511. JavascriptLibrary* library = arrayBufferPrototype->GetLibrary();
  1512. library->AddMember(arrayBufferPrototype, PropertyIds::constructor, library->arrayBufferConstructor);
  1513. library->AddFunctionToLibraryObject(arrayBufferPrototype, PropertyIds::slice, &ArrayBuffer::EntryInfo::Slice, 2);
  1514. library->AddAccessorsToLibraryObject(arrayBufferPrototype, PropertyIds::byteLength, &ArrayBuffer::EntryInfo::GetterByteLength, nullptr);
  1515. if (scriptContext->GetConfig()->IsES6ToStringTagEnabled())
  1516. {
  1517. library->AddMember(arrayBufferPrototype, PropertyIds::_symbolToStringTag, library->CreateStringFromCppLiteral(_u("ArrayBuffer")), PropertyConfigurable);
  1518. }
  1519. arrayBufferPrototype->SetHasNoEnumerableProperties(true);
  1520. return true;
  1521. }
  1522. bool JavascriptLibrary::InitializeDataViewConstructor(DynamicObject* dataViewConstructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  1523. {
  1524. typeHandler->Convert(dataViewConstructor, mode, 3);
  1525. ScriptContext* scriptContext = dataViewConstructor->GetScriptContext();
  1526. JavascriptLibrary* library = dataViewConstructor->GetLibrary();
  1527. library->AddMember(dataViewConstructor, PropertyIds::length, TaggedInt::ToVarUnchecked(3), PropertyConfigurable);
  1528. library->AddMember(dataViewConstructor, PropertyIds::prototype, scriptContext->GetLibrary()->dataViewPrototype, PropertyNone);
  1529. library->AddMember(dataViewConstructor, PropertyIds::name, scriptContext->GetPropertyString(PropertyIds::DataView), PropertyConfigurable);
  1530. dataViewConstructor->SetHasNoEnumerableProperties(true);
  1531. return true;
  1532. }
  1533. bool JavascriptLibrary::InitializeDataViewPrototype(DynamicObject* dataViewPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  1534. {
  1535. typeHandler->Convert(dataViewPrototype, mode, 2);
  1536. ScriptContext* scriptContext = dataViewPrototype->GetScriptContext();
  1537. JavascriptLibrary* library = dataViewPrototype->GetLibrary();
  1538. library->AddMember(dataViewPrototype, PropertyIds::constructor, library->dataViewConstructor);
  1539. library->AddFunctionToLibraryObject(dataViewPrototype, PropertyIds::setInt8, &DataView::EntryInfo::SetInt8, 2);
  1540. library->AddFunctionToLibraryObject(dataViewPrototype, PropertyIds::setUint8, &DataView::EntryInfo::SetUint8, 2);
  1541. library->AddFunctionToLibraryObject(dataViewPrototype, PropertyIds::setInt16, &DataView::EntryInfo::SetInt16, 2);
  1542. library->AddFunctionToLibraryObject(dataViewPrototype, PropertyIds::setUint16, &DataView::EntryInfo::SetUint16, 2);
  1543. library->AddFunctionToLibraryObject(dataViewPrototype, PropertyIds::setInt32, &DataView::EntryInfo::SetInt32, 2);
  1544. library->AddFunctionToLibraryObject(dataViewPrototype, PropertyIds::setUint32, &DataView::EntryInfo::SetUint32, 2);
  1545. library->AddFunctionToLibraryObject(dataViewPrototype, PropertyIds::setFloat32, &DataView::EntryInfo::SetFloat32, 2);
  1546. library->AddFunctionToLibraryObject(dataViewPrototype, PropertyIds::setFloat64, &DataView::EntryInfo::SetFloat64, 2);
  1547. library->AddFunctionToLibraryObject(dataViewPrototype, PropertyIds::getInt8, &DataView::EntryInfo::GetInt8, 1);
  1548. library->AddFunctionToLibraryObject(dataViewPrototype, PropertyIds::getUint8, &DataView::EntryInfo::GetUint8, 1);
  1549. library->AddFunctionToLibraryObject(dataViewPrototype, PropertyIds::getInt16, &DataView::EntryInfo::GetInt16, 1);
  1550. library->AddFunctionToLibraryObject(dataViewPrototype, PropertyIds::getUint16, &DataView::EntryInfo::GetUint16, 1);
  1551. library->AddFunctionToLibraryObject(dataViewPrototype, PropertyIds::getInt32, &DataView::EntryInfo::GetInt32, 1);
  1552. library->AddFunctionToLibraryObject(dataViewPrototype, PropertyIds::getUint32, &DataView::EntryInfo::GetUint32, 1);
  1553. library->AddFunctionToLibraryObject(dataViewPrototype, PropertyIds::getFloat32, &DataView::EntryInfo::GetFloat32, 1);
  1554. library->AddFunctionToLibraryObject(dataViewPrototype, PropertyIds::getFloat64, &DataView::EntryInfo::GetFloat64, 1);
  1555. library->AddAccessorsToLibraryObject(dataViewPrototype, PropertyIds::buffer, &DataView::EntryInfo::GetterBuffer, nullptr);
  1556. library->AddAccessorsToLibraryObject(dataViewPrototype, PropertyIds::byteLength, &DataView::EntryInfo::GetterByteLength, nullptr);
  1557. library->AddAccessorsToLibraryObject(dataViewPrototype, PropertyIds::byteOffset, &DataView::EntryInfo::GetterByteOffset, nullptr);
  1558. if (scriptContext->GetConfig()->IsES6ToStringTagEnabled())
  1559. {
  1560. library->AddMember(dataViewPrototype, PropertyIds::_symbolToStringTag, library->CreateStringFromCppLiteral(_u("DataView")), PropertyConfigurable);
  1561. }
  1562. dataViewPrototype->SetHasNoEnumerableProperties(true);
  1563. return true;
  1564. }
  1565. bool JavascriptLibrary::InitializeTypedArrayConstructor(DynamicObject* typedArrayConstructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  1566. {
  1567. typeHandler->Convert(typedArrayConstructor, mode, 5);
  1568. JavascriptLibrary* library = typedArrayConstructor->GetLibrary();
  1569. library->AddMember(typedArrayConstructor, PropertyIds::length, TaggedInt::ToVarUnchecked(3), PropertyConfigurable);
  1570. library->AddMember(typedArrayConstructor, PropertyIds::name, library->CreateStringFromCppLiteral(_u("TypedArray")), PropertyConfigurable);
  1571. library->AddMember(typedArrayConstructor, PropertyIds::prototype, library->typedArrayPrototype, PropertyNone);
  1572. library->AddFunctionToLibraryObject(typedArrayConstructor, PropertyIds::from, &TypedArrayBase::EntryInfo::From, 1);
  1573. library->AddFunctionToLibraryObject(typedArrayConstructor, PropertyIds::of, &TypedArrayBase::EntryInfo::Of, 0);
  1574. library->AddSpeciesAccessorsToLibraryObject(typedArrayConstructor, &TypedArrayBase::EntryInfo::GetterSymbolSpecies);
  1575. typedArrayConstructor->SetHasNoEnumerableProperties(true);
  1576. return true;
  1577. }
  1578. bool JavascriptLibrary::InitializeTypedArrayPrototype(DynamicObject* typedarrayPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  1579. {
  1580. typeHandler->Convert(typedarrayPrototype, mode, 31);
  1581. ScriptContext* scriptContext = typedarrayPrototype->GetScriptContext();
  1582. JavascriptLibrary* library = typedarrayPrototype->GetLibrary();
  1583. #ifdef ENABLE_JS_BUILTINS
  1584. if (scriptContext->IsJsBuiltInEnabled())
  1585. {
  1586. library->EnsureBuiltInEngineIsReady();
  1587. }
  1588. #endif
  1589. library->AddMember(typedarrayPrototype, PropertyIds::constructor, library->typedArrayConstructor);
  1590. library->AddFunctionToLibraryObject(typedarrayPrototype, PropertyIds::set, &TypedArrayBase::EntryInfo::Set, 2);
  1591. library->AddFunctionToLibraryObject(typedarrayPrototype, PropertyIds::subarray, &TypedArrayBase::EntryInfo::Subarray, 2);
  1592. library->AddFunctionToLibraryObject(typedarrayPrototype, PropertyIds::copyWithin, &TypedArrayBase::EntryInfo::CopyWithin, 2);
  1593. library->AddFunctionToLibraryObject(typedarrayPrototype, PropertyIds::every, &TypedArrayBase::EntryInfo::Every, 1);
  1594. library->AddFunctionToLibraryObject(typedarrayPrototype, PropertyIds::fill, &TypedArrayBase::EntryInfo::Fill, 1);
  1595. library->AddFunctionToLibraryObject(typedarrayPrototype, PropertyIds::filter, &TypedArrayBase::EntryInfo::Filter, 1);
  1596. library->AddFunctionToLibraryObject(typedarrayPrototype, PropertyIds::find, &TypedArrayBase::EntryInfo::Find, 1);
  1597. library->AddFunctionToLibraryObject(typedarrayPrototype, PropertyIds::findIndex, &TypedArrayBase::EntryInfo::FindIndex, 1);
  1598. library->AddFunctionToLibraryObject(typedarrayPrototype, PropertyIds::forEach, &TypedArrayBase::EntryInfo::ForEach, 1);
  1599. library->AddFunctionToLibraryObject(typedarrayPrototype, PropertyIds::indexOf, &TypedArrayBase::EntryInfo::IndexOf, 1);
  1600. library->AddFunctionToLibraryObject(typedarrayPrototype, PropertyIds::join, &TypedArrayBase::EntryInfo::Join, 1);
  1601. library->AddFunctionToLibraryObject(typedarrayPrototype, PropertyIds::lastIndexOf, &TypedArrayBase::EntryInfo::LastIndexOf, 1);
  1602. library->AddFunctionToLibraryObject(typedarrayPrototype, PropertyIds::map, &TypedArrayBase::EntryInfo::Map, 1);
  1603. library->AddFunctionToLibraryObject(typedarrayPrototype, PropertyIds::reduce, &TypedArrayBase::EntryInfo::Reduce, 1);
  1604. library->AddFunctionToLibraryObject(typedarrayPrototype, PropertyIds::reduceRight, &TypedArrayBase::EntryInfo::ReduceRight, 1);
  1605. library->AddFunctionToLibraryObject(typedarrayPrototype, PropertyIds::reverse, &TypedArrayBase::EntryInfo::Reverse, 0);
  1606. library->AddFunctionToLibraryObject(typedarrayPrototype, PropertyIds::slice, &TypedArrayBase::EntryInfo::Slice, 2);
  1607. library->AddFunctionToLibraryObject(typedarrayPrototype, PropertyIds::some, &TypedArrayBase::EntryInfo::Some, 1);
  1608. library->AddFunctionToLibraryObject(typedarrayPrototype, PropertyIds::sort, &TypedArrayBase::EntryInfo::Sort, 1);
  1609. library->AddFunctionToLibraryObject(typedarrayPrototype, PropertyIds::entries, &TypedArrayBase::EntryInfo::Entries, 0);
  1610. library->AddFunctionToLibraryObject(typedarrayPrototype, PropertyIds::keys, &TypedArrayBase::EntryInfo::Keys, 0);
  1611. JavascriptFunction* valuesFunc = library->AddFunctionToLibraryObject(typedarrayPrototype, PropertyIds::values, &TypedArrayBase::EntryInfo::Values, 0);
  1612. library->AddMember(typedarrayPrototype, PropertyIds::_symbolIterator, valuesFunc);
  1613. library->AddFunctionToLibraryObject(typedarrayPrototype, PropertyIds::includes, &TypedArrayBase::EntryInfo::Includes, 1);
  1614. library->AddAccessorsToLibraryObject(typedarrayPrototype, PropertyIds::buffer, &TypedArrayBase::EntryInfo::GetterBuffer, nullptr);
  1615. library->AddAccessorsToLibraryObject(typedarrayPrototype, PropertyIds::byteLength, &TypedArrayBase::EntryInfo::GetterByteLength, nullptr);
  1616. library->AddAccessorsToLibraryObject(typedarrayPrototype, PropertyIds::byteOffset, &TypedArrayBase::EntryInfo::GetterByteOffset, nullptr);
  1617. library->AddAccessorsToLibraryObject(typedarrayPrototype, PropertyIds::length, &TypedArrayBase::EntryInfo::GetterLength, nullptr);
  1618. if (scriptContext->GetConfig()->IsES6ToStringTagEnabled())
  1619. {
  1620. // ES2017 22.2.3.32 get %TypedArray%.prototype[@@toStringTag]
  1621. // %TypedArray%.prototype[@@toStringTag] is an accessor property whose set accessor function is undefined.
  1622. // This property has the attributes { [[Enumerable]]: false, [[Configurable]]: true }.
  1623. // The initial value of the name property of this function is "get [Symbol.toStringTag]".
  1624. library->AddAccessorsToLibraryObjectWithName(typedarrayPrototype, PropertyIds::_symbolToStringTag,
  1625. PropertyIds::_RuntimeFunctionNameId_toStringTag, &TypedArrayBase::EntryInfo::GetterSymbolToStringTag, nullptr);
  1626. }
  1627. // The toString and toLocaleString properties are shared between Array.prototype and %TypedArray%.prototype.
  1628. // Whichever prototype is initialized first will create the functions, the other should just load the existing function objects.
  1629. if (library->arrayPrototypeToStringFunction == nullptr)
  1630. {
  1631. Assert(library->arrayPrototypeToLocaleStringFunction == nullptr);
  1632. library->arrayPrototypeToLocaleStringFunction = library->AddFunctionToLibraryObject(typedarrayPrototype, PropertyIds::toLocaleString, &JavascriptArray::EntryInfo::ToLocaleString, 0);
  1633. library->arrayPrototypeToStringFunction = library->AddFunctionToLibraryObject(typedarrayPrototype, PropertyIds::toString, &JavascriptArray::EntryInfo::ToString, 0);
  1634. }
  1635. else
  1636. {
  1637. Assert(library->arrayPrototypeToLocaleStringFunction);
  1638. library->AddMember(typedarrayPrototype, PropertyIds::toLocaleString, library->arrayPrototypeToLocaleStringFunction);
  1639. library->AddMember(typedarrayPrototype, PropertyIds::toString, library->arrayPrototypeToStringFunction);
  1640. }
  1641. typedarrayPrototype->SetHasNoEnumerableProperties(true);
  1642. return true;
  1643. }
  1644. #define INIT_TYPEDARRAY_CONSTRUCTOR(typedArray, typedarrayPrototype, TypeName) \
  1645. bool JavascriptLibrary::Initialize##typedArray##Constructor(DynamicObject* typedArrayConstructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode) \
  1646. { \
  1647. typeHandler->Convert(typedArrayConstructor, mode, 4); \
  1648. ScriptContext* scriptContext = typedArrayConstructor->GetScriptContext(); \
  1649. JavascriptLibrary* library = typedArrayConstructor->GetLibrary(); \
  1650. library->AddMember(typedArrayConstructor, PropertyIds::length, TaggedInt::ToVarUnchecked(3), PropertyConfigurable); \
  1651. library->AddMember(typedArrayConstructor, PropertyIds::name, library->CreateStringFromCppLiteral(_u(#typedArray)), PropertyConfigurable); \
  1652. library->AddMember(typedArrayConstructor, PropertyIds::BYTES_PER_ELEMENT, TaggedInt::ToVarUnchecked(sizeof(TypeName)), PropertyNone); \
  1653. library->AddMember(typedArrayConstructor, PropertyIds::prototype, scriptContext->GetLibrary()->##typedarrayPrototype##, PropertyNone); \
  1654. typedArrayConstructor->SetHasNoEnumerableProperties(true); \
  1655. return true; \
  1656. } \
  1657. INIT_TYPEDARRAY_CONSTRUCTOR(Int8Array, Int8ArrayPrototype, int8);
  1658. INIT_TYPEDARRAY_CONSTRUCTOR(Uint8Array, Uint8ArrayPrototype, uint8);
  1659. INIT_TYPEDARRAY_CONSTRUCTOR(Uint8ClampedArray, Uint8ClampedArrayPrototype, uint8);
  1660. INIT_TYPEDARRAY_CONSTRUCTOR(Int16Array, Int16ArrayPrototype, int16);
  1661. INIT_TYPEDARRAY_CONSTRUCTOR(Uint16Array, Uint16ArrayPrototype, uint16);
  1662. INIT_TYPEDARRAY_CONSTRUCTOR(Int32Array, Int32ArrayPrototype, int32);
  1663. INIT_TYPEDARRAY_CONSTRUCTOR(Uint32Array, Uint32ArrayPrototype, uint32);
  1664. INIT_TYPEDARRAY_CONSTRUCTOR(Float32Array, Float32ArrayPrototype, float);
  1665. INIT_TYPEDARRAY_CONSTRUCTOR(Float64Array, Float64ArrayPrototype, double);
  1666. #define INIT_TYPEDARRAY_PROTOTYPE(typedArray, typedarrayPrototype, TypeName) \
  1667. bool JavascriptLibrary::Initialize##typedarrayPrototype##(DynamicObject* typedarrayPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode) \
  1668. { \
  1669. typeHandler->Convert(typedarrayPrototype, mode, 2); \
  1670. JavascriptLibrary* library = typedarrayPrototype->GetLibrary(); \
  1671. library->AddMember(typedarrayPrototype, PropertyIds::constructor, library->##typedArray##Constructor); \
  1672. library->AddMember(typedarrayPrototype, PropertyIds::BYTES_PER_ELEMENT, TaggedInt::ToVarUnchecked(sizeof(TypeName)), PropertyNone); \
  1673. typedarrayPrototype->SetHasNoEnumerableProperties(true); \
  1674. return true; \
  1675. } \
  1676. INIT_TYPEDARRAY_PROTOTYPE(Int8Array, Int8ArrayPrototype, int8);
  1677. INIT_TYPEDARRAY_PROTOTYPE(Uint8Array, Uint8ArrayPrototype, uint8);
  1678. INIT_TYPEDARRAY_PROTOTYPE(Uint8ClampedArray, Uint8ClampedArrayPrototype, uint8);
  1679. INIT_TYPEDARRAY_PROTOTYPE(Int16Array, Int16ArrayPrototype, int16);
  1680. INIT_TYPEDARRAY_PROTOTYPE(Uint16Array, Uint16ArrayPrototype, uint16);
  1681. INIT_TYPEDARRAY_PROTOTYPE(Int32Array, Int32ArrayPrototype, int32);
  1682. INIT_TYPEDARRAY_PROTOTYPE(Uint32Array, Uint32ArrayPrototype, uint32);
  1683. INIT_TYPEDARRAY_PROTOTYPE(Float32Array, Float32ArrayPrototype, float);
  1684. INIT_TYPEDARRAY_PROTOTYPE(Float64Array, Float64ArrayPrototype, double);
  1685. // For Microsoft extension typed array, like Int64Array, BoolArray, we don't have constructor as they can only be created from the projection arguments.
  1686. // there is no subarray method either as that's another way to create typed array.
  1687. #define INIT_MSINTERNAL_TYPEDARRAY_PROTOTYPE(typedArray, typedarrayPrototype) \
  1688. bool JavascriptLibrary::Initialize##typedarrayPrototype##(DynamicObject* typedarrayPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode) \
  1689. { \
  1690. typeHandler->Convert(typedarrayPrototype, mode, 1); \
  1691. JavascriptLibrary* library = typedarrayPrototype->GetLibrary(); \
  1692. library->AddFunctionToLibraryObject(typedarrayPrototype, PropertyIds::set, &##typedArray##::EntryInfo::Set, 2); \
  1693. typedarrayPrototype->SetHasNoEnumerableProperties(true); \
  1694. return true; \
  1695. } \
  1696. INIT_MSINTERNAL_TYPEDARRAY_PROTOTYPE(Int64Array, Int64ArrayPrototype);
  1697. INIT_MSINTERNAL_TYPEDARRAY_PROTOTYPE(Uint64Array, Uint64ArrayPrototype);
  1698. INIT_MSINTERNAL_TYPEDARRAY_PROTOTYPE(BoolArray, BoolArrayPrototype);
  1699. INIT_MSINTERNAL_TYPEDARRAY_PROTOTYPE(CharArray, CharArrayPrototype);
  1700. bool JavascriptLibrary::InitializeErrorConstructor(DynamicObject* constructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  1701. {
  1702. typeHandler->Convert(constructor, mode, 4);
  1703. ScriptContext* scriptContext = constructor->GetScriptContext();
  1704. JavascriptLibrary* library = constructor->GetLibrary();
  1705. library->AddMember(constructor, PropertyIds::prototype, library->errorPrototype, PropertyNone);
  1706. library->AddMember(constructor, PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable);
  1707. library->AddMember(constructor, PropertyIds::name, scriptContext->GetPropertyString(PropertyIds::Error), PropertyConfigurable);
  1708. if (scriptContext->GetConfig()->IsErrorStackTraceEnabled())
  1709. {
  1710. library->AddMember(constructor, PropertyIds::stackTraceLimit, JavascriptNumber::ToVar(JavascriptExceptionOperators::DefaultStackTraceLimit, scriptContext), PropertyConfigurable | PropertyWritable | PropertyEnumerable);
  1711. }
  1712. constructor->SetHasNoEnumerableProperties(true);
  1713. return true;
  1714. }
  1715. bool JavascriptLibrary::InitializeErrorPrototype(DynamicObject* prototype, DeferredTypeHandlerBase* typeHandler, DeferredInitializeMode mode)
  1716. {
  1717. typeHandler->Convert(prototype, mode, 4);
  1718. JavascriptLibrary* library = prototype->GetLibrary();
  1719. library->AddMember(prototype, PropertyIds::constructor, library->errorConstructor);
  1720. bool hasNoEnumerableProperties = true;
  1721. PropertyAttributes prototypeNameMessageAttributes = PropertyConfigurable | PropertyWritable;
  1722. library->AddMember(prototype, PropertyIds::name, library->CreateStringFromCppLiteral(_u("Error")), prototypeNameMessageAttributes);
  1723. library->AddMember(prototype, PropertyIds::message, library->GetEmptyString(), prototypeNameMessageAttributes);
  1724. library->AddFunctionToLibraryObject(prototype, PropertyIds::toString, &JavascriptError::EntryInfo::ToString, 0);
  1725. prototype->SetHasNoEnumerableProperties(hasNoEnumerableProperties);
  1726. return true;
  1727. }
  1728. #define INIT_ERROR_IMPL(error, errorName) \
  1729. bool JavascriptLibrary::Initialize##error##Constructor(DynamicObject* constructor, DeferredTypeHandlerBase* typeHandler, DeferredInitializeMode mode) \
  1730. { \
  1731. typeHandler->Convert(constructor, mode, 3); \
  1732. JavascriptLibrary* library = constructor->GetLibrary(); \
  1733. library->AddMember(constructor, PropertyIds::prototype, library->Get##error##Prototype(), PropertyNone); \
  1734. library->AddMember(constructor, PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable); \
  1735. PropertyAttributes prototypeNameMessageAttributes = PropertyConfigurable; \
  1736. library->AddMember(constructor, PropertyIds::name, library->CreateStringFromCppLiteral(_u(#errorName)), prototypeNameMessageAttributes); \
  1737. constructor->SetHasNoEnumerableProperties(true); \
  1738. return true; \
  1739. } \
  1740. bool JavascriptLibrary::Initialize##error##Prototype(DynamicObject* prototype, DeferredTypeHandlerBase* typeHandler, DeferredInitializeMode mode) \
  1741. { \
  1742. typeHandler->Convert(prototype, mode, 4); \
  1743. JavascriptLibrary* library = prototype->GetLibrary(); \
  1744. library->AddMember(prototype, PropertyIds::constructor, library->Get##error##Constructor()); \
  1745. bool hasNoEnumerableProperties = true; \
  1746. PropertyAttributes prototypeNameMessageAttributes = PropertyConfigurable | PropertyWritable; \
  1747. library->AddMember(prototype, PropertyIds::name, library->CreateStringFromCppLiteral(_u(#errorName)), prototypeNameMessageAttributes); \
  1748. library->AddMember(prototype, PropertyIds::message, library->GetEmptyString(), prototypeNameMessageAttributes); \
  1749. library->AddFunctionToLibraryObject(prototype, PropertyIds::toString, &JavascriptError::EntryInfo::ToString, 0); \
  1750. prototype->SetHasNoEnumerableProperties(hasNoEnumerableProperties); \
  1751. return true; \
  1752. }
  1753. #define INIT_ERROR(error) INIT_ERROR_IMPL(error, error)
  1754. INIT_ERROR(EvalError);
  1755. INIT_ERROR(RangeError);
  1756. INIT_ERROR(ReferenceError);
  1757. INIT_ERROR(SyntaxError);
  1758. INIT_ERROR(TypeError);
  1759. INIT_ERROR(URIError);
  1760. INIT_ERROR_IMPL(WebAssemblyCompileError, CompileError);
  1761. INIT_ERROR_IMPL(WebAssemblyRuntimeError, RuntimeError);
  1762. INIT_ERROR_IMPL(WebAssemblyLinkError, LinkError);
  1763. #undef INIT_ERROR
  1764. bool JavascriptLibrary::InitializeBooleanConstructor(DynamicObject* booleanConstructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  1765. {
  1766. typeHandler->Convert(booleanConstructor, mode, 3);
  1767. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterBoolean
  1768. // so that the update is in sync with profiler
  1769. ScriptContext* scriptContext = booleanConstructor->GetScriptContext();
  1770. JavascriptLibrary* library = booleanConstructor->GetLibrary();
  1771. library->AddMember(booleanConstructor, PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable);
  1772. library->AddMember(booleanConstructor, PropertyIds::prototype, library->booleanPrototype, PropertyNone);
  1773. library->AddMember(booleanConstructor, PropertyIds::name, scriptContext->GetPropertyString(PropertyIds::Boolean), PropertyConfigurable);
  1774. booleanConstructor->SetHasNoEnumerableProperties(true);
  1775. return true;
  1776. }
  1777. bool JavascriptLibrary::InitializeBooleanPrototype(DynamicObject* booleanPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  1778. {
  1779. typeHandler->Convert(booleanPrototype, mode, 3);
  1780. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterBoolean
  1781. // so that the update is in sync with profiler
  1782. JavascriptLibrary* library = booleanPrototype->GetLibrary();
  1783. ScriptContext* scriptContext = booleanPrototype->GetScriptContext();
  1784. library->AddMember(booleanPrototype, PropertyIds::constructor, library->booleanConstructor);
  1785. scriptContext->SetBuiltInLibraryFunction(JavascriptBoolean::EntryInfo::ValueOf.GetOriginalEntryPoint(),
  1786. library->AddFunctionToLibraryObject(booleanPrototype, PropertyIds::valueOf, &JavascriptBoolean::EntryInfo::ValueOf, 0));
  1787. scriptContext->SetBuiltInLibraryFunction(JavascriptBoolean::EntryInfo::ToString.GetOriginalEntryPoint(),
  1788. library->AddFunctionToLibraryObject(booleanPrototype, PropertyIds::toString, &JavascriptBoolean::EntryInfo::ToString, 0));
  1789. booleanPrototype->SetHasNoEnumerableProperties(true);
  1790. return true;
  1791. }
  1792. bool JavascriptLibrary::InitializeSymbolConstructor(DynamicObject* symbolConstructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  1793. {
  1794. typeHandler->Convert(symbolConstructor, mode, 16);
  1795. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterSymbol
  1796. // so that the update is in sync with profiler
  1797. JavascriptLibrary* library = symbolConstructor->GetLibrary();
  1798. ScriptContext* scriptContext = symbolConstructor->GetScriptContext();
  1799. library->AddMember(symbolConstructor, PropertyIds::length, TaggedInt::ToVarUnchecked(0), PropertyConfigurable);
  1800. library->AddMember(symbolConstructor, PropertyIds::prototype, library->symbolPrototype, PropertyNone);
  1801. library->AddMember(symbolConstructor, PropertyIds::name, scriptContext->GetPropertyString(PropertyIds::Symbol), PropertyConfigurable);
  1802. if (scriptContext->GetConfig()->IsES6HasInstanceEnabled())
  1803. {
  1804. library->AddMember(symbolConstructor, PropertyIds::hasInstance, library->GetSymbolHasInstance(), PropertyNone);
  1805. }
  1806. if (scriptContext->GetConfig()->IsES6IsConcatSpreadableEnabled())
  1807. {
  1808. library->AddMember(symbolConstructor, PropertyIds::isConcatSpreadable, library->GetSymbolIsConcatSpreadable(), PropertyNone);
  1809. }
  1810. library->AddMember(symbolConstructor, PropertyIds::iterator, library->GetSymbolIterator(), PropertyNone);
  1811. if (scriptContext->GetConfig()->IsES6SpeciesEnabled())
  1812. {
  1813. library->AddMember(symbolConstructor, PropertyIds::species, library->GetSymbolSpecies(), PropertyNone);
  1814. }
  1815. if (scriptContext->GetConfig()->IsES6ToPrimitiveEnabled())
  1816. {
  1817. library->AddMember(symbolConstructor, PropertyIds::toPrimitive, library->GetSymbolToPrimitive(), PropertyNone);
  1818. }
  1819. if (scriptContext->GetConfig()->IsES6ToStringTagEnabled())
  1820. {
  1821. library->AddMember(symbolConstructor, PropertyIds::toStringTag, library->GetSymbolToStringTag(), PropertyNone);
  1822. }
  1823. library->AddMember(symbolConstructor, PropertyIds::unscopables, library->GetSymbolUnscopables(), PropertyNone);
  1824. if (scriptContext->GetConfig()->IsES6RegExSymbolsEnabled())
  1825. {
  1826. library->AddMember(symbolConstructor, PropertyIds::match, library->GetSymbolMatch(), PropertyNone);
  1827. library->AddMember(symbolConstructor, PropertyIds::replace, library->GetSymbolReplace(), PropertyNone);
  1828. library->AddMember(symbolConstructor, PropertyIds::search, library->GetSymbolSearch(), PropertyNone);
  1829. library->AddMember(symbolConstructor, PropertyIds::split, library->GetSymbolSplit(), PropertyNone);
  1830. }
  1831. library->AddFunctionToLibraryObject(symbolConstructor, PropertyIds::for_, &JavascriptSymbol::EntryInfo::For, 1);
  1832. library->AddFunctionToLibraryObject(symbolConstructor, PropertyIds::keyFor, &JavascriptSymbol::EntryInfo::KeyFor, 1);
  1833. symbolConstructor->SetHasNoEnumerableProperties(true);
  1834. return true;
  1835. }
  1836. bool JavascriptLibrary::InitializeSymbolPrototype(DynamicObject* symbolPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  1837. {
  1838. typeHandler->Convert(symbolPrototype, mode, 5);
  1839. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterSymbol
  1840. // so that the update is in sync with profiler
  1841. JavascriptLibrary* library = symbolPrototype->GetLibrary();
  1842. ScriptContext* scriptContext = symbolPrototype->GetScriptContext();
  1843. library->AddMember(symbolPrototype, PropertyIds::constructor, library->symbolConstructor);
  1844. if (scriptContext->GetConfig()->IsES6ToStringTagEnabled())
  1845. {
  1846. library->AddMember(symbolPrototype, PropertyIds::_symbolToStringTag, library->CreateStringFromCppLiteral(_u("Symbol")), PropertyConfigurable);
  1847. }
  1848. scriptContext->SetBuiltInLibraryFunction(JavascriptSymbol::EntryInfo::ValueOf.GetOriginalEntryPoint(),
  1849. library->AddFunctionToLibraryObject(symbolPrototype, PropertyIds::valueOf, &JavascriptSymbol::EntryInfo::ValueOf, 0));
  1850. scriptContext->SetBuiltInLibraryFunction(JavascriptSymbol::EntryInfo::ToString.GetOriginalEntryPoint(),
  1851. library->AddFunctionToLibraryObject(symbolPrototype, PropertyIds::toString, &JavascriptSymbol::EntryInfo::ToString, 0));
  1852. if (scriptContext->GetConfig()->IsES6ToPrimitiveEnabled())
  1853. {
  1854. scriptContext->SetBuiltInLibraryFunction(JavascriptSymbol::EntryInfo::SymbolToPrimitive.GetOriginalEntryPoint(),
  1855. library->AddFunctionToLibraryObjectWithName(symbolPrototype, PropertyIds::_symbolToPrimitive, PropertyIds::_RuntimeFunctionNameId_toPrimitive,
  1856. &JavascriptSymbol::EntryInfo::SymbolToPrimitive, 1));
  1857. symbolPrototype->SetWritable(PropertyIds::_symbolToPrimitive, false);
  1858. }
  1859. symbolPrototype->SetHasNoEnumerableProperties(true);
  1860. return true;
  1861. }
  1862. bool JavascriptLibrary::InitializePromiseConstructor(DynamicObject* promiseConstructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  1863. {
  1864. typeHandler->Convert(promiseConstructor, mode, 8);
  1865. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterPromise
  1866. // so that the update is in sync with profiler
  1867. JavascriptLibrary* library = promiseConstructor->GetLibrary();
  1868. ScriptContext* scriptContext = promiseConstructor->GetScriptContext();
  1869. library->AddMember(promiseConstructor, PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable);
  1870. library->AddMember(promiseConstructor, PropertyIds::prototype, library->promisePrototype, PropertyNone);
  1871. library->AddSpeciesAccessorsToLibraryObject(promiseConstructor, &JavascriptPromise::EntryInfo::GetterSymbolSpecies);
  1872. library->AddMember(promiseConstructor, PropertyIds::name, scriptContext->GetPropertyString(PropertyIds::Promise), PropertyConfigurable);
  1873. library->AddFunctionToLibraryObject(promiseConstructor, PropertyIds::all, &JavascriptPromise::EntryInfo::All, 1);
  1874. library->AddFunctionToLibraryObject(promiseConstructor, PropertyIds::race, &JavascriptPromise::EntryInfo::Race, 1);
  1875. library->AddFunctionToLibraryObject(promiseConstructor, PropertyIds::reject, &JavascriptPromise::EntryInfo::Reject, 1);
  1876. library->AddMember(promiseConstructor, PropertyIds::resolve, library->EnsurePromiseResolveFunction(), PropertyBuiltInMethodDefaults);
  1877. promiseConstructor->SetHasNoEnumerableProperties(true);
  1878. return true;
  1879. }
  1880. JavascriptFunction* JavascriptLibrary::EnsurePromiseResolveFunction()
  1881. {
  1882. if (promiseResolveFunction == nullptr)
  1883. {
  1884. promiseResolveFunction = DefaultCreateFunction(&JavascriptPromise::EntryInfo::Resolve, 1, nullptr, nullptr, PropertyIds::resolve);
  1885. }
  1886. return promiseResolveFunction;
  1887. }
  1888. bool JavascriptLibrary::InitializePromisePrototype(DynamicObject* promisePrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  1889. {
  1890. typeHandler->Convert(promisePrototype, mode, 4);
  1891. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterPromise
  1892. // so that the update is in sync with profiler
  1893. JavascriptLibrary* library = promisePrototype->GetLibrary();
  1894. ScriptContext* scriptContext = promisePrototype->GetScriptContext();
  1895. library->AddMember(promisePrototype, PropertyIds::constructor, library->promiseConstructor);
  1896. if (scriptContext->GetConfig()->IsES6ToStringTagEnabled())
  1897. {
  1898. library->AddMember(promisePrototype, PropertyIds::_symbolToStringTag, library->CreateStringFromCppLiteral(_u("Promise")), PropertyConfigurable);
  1899. }
  1900. scriptContext->SetBuiltInLibraryFunction(JavascriptPromise::EntryInfo::Catch.GetOriginalEntryPoint(),
  1901. library->AddFunctionToLibraryObject(promisePrototype, PropertyIds::catch_, &JavascriptPromise::EntryInfo::Catch, 1));
  1902. scriptContext->SetBuiltInLibraryFunction(JavascriptPromise::EntryInfo::Finally.GetOriginalEntryPoint(),
  1903. library->AddFunctionToLibraryObject(promisePrototype, PropertyIds::finally, &JavascriptPromise::EntryInfo::Finally, 1));
  1904. library->AddMember(promisePrototype, PropertyIds::then, library->EnsurePromiseThenFunction(), PropertyBuiltInMethodDefaults);
  1905. scriptContext->SetBuiltInLibraryFunction(JavascriptPromise::EntryInfo::Then.GetOriginalEntryPoint(), library->EnsurePromiseThenFunction());
  1906. promisePrototype->SetHasNoEnumerableProperties(true);
  1907. return true;
  1908. }
  1909. JavascriptFunction* JavascriptLibrary::EnsurePromiseThenFunction()
  1910. {
  1911. if (promiseThenFunction == nullptr)
  1912. {
  1913. promiseThenFunction = DefaultCreateFunction(&JavascriptPromise::EntryInfo::Then, 2, nullptr, nullptr, PropertyIds::then);
  1914. }
  1915. return promiseThenFunction;
  1916. }
  1917. bool JavascriptLibrary::InitializeGeneratorFunctionConstructor(DynamicObject* generatorFunctionConstructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  1918. {
  1919. typeHandler->Convert(generatorFunctionConstructor, mode, 3);
  1920. JavascriptLibrary* library = generatorFunctionConstructor->GetLibrary();
  1921. library->AddMember(generatorFunctionConstructor, PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable);
  1922. library->AddMember(generatorFunctionConstructor, PropertyIds::prototype, library->generatorFunctionPrototype, PropertyNone);
  1923. library->AddMember(generatorFunctionConstructor, PropertyIds::name, library->CreateStringFromCppLiteral(_u("GeneratorFunction")), PropertyConfigurable);
  1924. generatorFunctionConstructor->SetHasNoEnumerableProperties(true);
  1925. return true;
  1926. }
  1927. bool JavascriptLibrary::InitializeGeneratorFunctionPrototype(DynamicObject* generatorFunctionPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  1928. {
  1929. typeHandler->Convert(generatorFunctionPrototype, mode, 3);
  1930. JavascriptLibrary* library = generatorFunctionPrototype->GetLibrary();
  1931. ScriptContext* scriptContext = library->GetScriptContext();
  1932. library->AddMember(generatorFunctionPrototype, PropertyIds::constructor, library->generatorFunctionConstructor, PropertyConfigurable);
  1933. library->AddMember(generatorFunctionPrototype, PropertyIds::prototype, library->generatorPrototype, PropertyConfigurable);
  1934. if (scriptContext->GetConfig()->IsES6ToStringTagEnabled())
  1935. {
  1936. library->AddMember(generatorFunctionPrototype, PropertyIds::_symbolToStringTag, library->CreateStringFromCppLiteral(_u("GeneratorFunction")), PropertyConfigurable);
  1937. }
  1938. generatorFunctionPrototype->SetHasNoEnumerableProperties(true);
  1939. return true;
  1940. }
  1941. bool JavascriptLibrary::InitializeGeneratorPrototype(DynamicObject* generatorPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  1942. {
  1943. typeHandler->Convert(generatorPrototype, mode, 5);
  1944. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterGenerator
  1945. // so that the update is in sync with profiler
  1946. JavascriptLibrary* library = generatorPrototype->GetLibrary();
  1947. ScriptContext* scriptContext = library->GetScriptContext();
  1948. library->AddMember(generatorPrototype, PropertyIds::constructor, library->generatorFunctionPrototype, PropertyConfigurable);
  1949. if (scriptContext->GetConfig()->IsES6ToStringTagEnabled()) {
  1950. library->AddMember(generatorPrototype, PropertyIds::_symbolToStringTag, library->CreateStringFromCppLiteral(_u("Generator")), PropertyConfigurable);
  1951. }
  1952. library->AddMember(generatorPrototype, PropertyIds::return_, library->EnsureGeneratorReturnFunction(), PropertyBuiltInMethodDefaults);
  1953. library->AddMember(generatorPrototype, PropertyIds::next, library->EnsureGeneratorNextFunction(), PropertyBuiltInMethodDefaults);
  1954. library->AddMember(generatorPrototype, PropertyIds::throw_, library->EnsureGeneratorThrowFunction(), PropertyBuiltInMethodDefaults);
  1955. generatorPrototype->SetHasNoEnumerableProperties(true);
  1956. return true;
  1957. }
  1958. JavascriptFunction* JavascriptLibrary::EnsureGeneratorReturnFunction()
  1959. {
  1960. if (generatorReturnFunction == nullptr)
  1961. {
  1962. generatorReturnFunction = DefaultCreateFunction(&JavascriptGenerator::EntryInfo::Return, 1, nullptr, nullptr, PropertyIds::return_);
  1963. }
  1964. return generatorReturnFunction;
  1965. }
  1966. JavascriptFunction* JavascriptLibrary::EnsureGeneratorNextFunction()
  1967. {
  1968. if (generatorNextFunction == nullptr)
  1969. {
  1970. generatorNextFunction = DefaultCreateFunction(&JavascriptGenerator::EntryInfo::Next, 1, nullptr, nullptr, PropertyIds::next);
  1971. }
  1972. return generatorNextFunction;
  1973. }
  1974. JavascriptFunction* JavascriptLibrary::EnsureGeneratorThrowFunction()
  1975. {
  1976. if (generatorThrowFunction == nullptr)
  1977. {
  1978. generatorThrowFunction = DefaultCreateFunction(&JavascriptGenerator::EntryInfo::Throw, 1, nullptr, nullptr, PropertyIds::throw_);
  1979. }
  1980. return generatorThrowFunction;
  1981. }
  1982. bool JavascriptLibrary::InitializeAsyncFunctionConstructor(DynamicObject* asyncFunctionConstructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  1983. {
  1984. typeHandler->Convert(asyncFunctionConstructor, mode, 3);
  1985. JavascriptLibrary* library = asyncFunctionConstructor->GetLibrary();
  1986. library->AddMember(asyncFunctionConstructor, PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable);
  1987. library->AddMember(asyncFunctionConstructor, PropertyIds::prototype, library->asyncFunctionPrototype, PropertyNone);
  1988. library->AddMember(asyncFunctionConstructor, PropertyIds::name, library->CreateStringFromCppLiteral(_u("AsyncFunction")), PropertyConfigurable);
  1989. asyncFunctionConstructor->SetHasNoEnumerableProperties(true);
  1990. return true;
  1991. }
  1992. bool JavascriptLibrary::InitializeAsyncFunctionPrototype(DynamicObject* asyncFunctionPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  1993. {
  1994. typeHandler->Convert(asyncFunctionPrototype, mode, 2);
  1995. JavascriptLibrary* library = asyncFunctionPrototype->GetLibrary();
  1996. ScriptContext* scriptContext = library->GetScriptContext();
  1997. library->AddMember(asyncFunctionPrototype, PropertyIds::constructor, library->asyncFunctionConstructor, PropertyConfigurable);
  1998. if (scriptContext->GetConfig()->IsES6ToStringTagEnabled())
  1999. {
  2000. library->AddMember(asyncFunctionPrototype, PropertyIds::_symbolToStringTag, library->CreateStringFromCppLiteral(_u("AsyncFunction")), PropertyConfigurable);
  2001. }
  2002. asyncFunctionPrototype->SetHasNoEnumerableProperties(true);
  2003. return true;
  2004. }
  2005. bool JavascriptLibrary::InitializeProxyConstructor(DynamicObject* proxyConstructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  2006. {
  2007. typeHandler->Convert(proxyConstructor, mode, 4);
  2008. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterSymbol
  2009. // so that the update is in sync with profiler
  2010. JavascriptLibrary* library = proxyConstructor->GetLibrary();
  2011. ScriptContext* scriptContext = proxyConstructor->GetScriptContext();
  2012. library->AddMember(proxyConstructor, PropertyIds::length, TaggedInt::ToVarUnchecked(2), PropertyConfigurable);
  2013. library->AddMember(proxyConstructor, PropertyIds::name, scriptContext->GetPropertyString(PropertyIds::Proxy), PropertyConfigurable);
  2014. library->AddFunctionToLibraryObject(proxyConstructor, PropertyIds::revocable, &JavascriptProxy::EntryInfo::Revocable, 2, PropertyConfigurable);
  2015. proxyConstructor->SetHasNoEnumerableProperties(true);
  2016. return true;
  2017. }
  2018. bool JavascriptLibrary::InitializeProxyPrototype(DynamicObject* proxyPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  2019. {
  2020. typeHandler->Convert(proxyPrototype, mode, 1);
  2021. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterSymbol
  2022. // so that the update is in sync with profiler
  2023. JavascriptLibrary* library = proxyPrototype->GetLibrary();
  2024. library->AddMember(proxyPrototype, PropertyIds::constructor, library->proxyConstructor);
  2025. proxyPrototype->SetHasNoEnumerableProperties(true);
  2026. return true;
  2027. }
  2028. bool JavascriptLibrary::InitializeDateConstructor(DynamicObject* dateConstructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  2029. {
  2030. typeHandler->Convert(dateConstructor, mode, 6);
  2031. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterDate
  2032. // so that the update is in sync with profiler
  2033. JavascriptLibrary* library = dateConstructor->GetLibrary();
  2034. ScriptContext* scriptContext = dateConstructor->GetScriptContext();
  2035. library->AddMember(dateConstructor, PropertyIds::length, TaggedInt::ToVarUnchecked(7), PropertyConfigurable);
  2036. library->AddMember(dateConstructor, PropertyIds::prototype, library->datePrototype, PropertyNone);
  2037. library->AddMember(dateConstructor, PropertyIds::name, scriptContext->GetPropertyString(PropertyIds::Date), PropertyConfigurable);
  2038. library->AddFunctionToLibraryObject(dateConstructor, PropertyIds::parse, &JavascriptDate::EntryInfo::Parse, 1); // should be static
  2039. library->AddFunctionToLibraryObject(dateConstructor, PropertyIds::now, &JavascriptDate::EntryInfo::Now, 0); // should be static
  2040. library->AddFunctionToLibraryObject(dateConstructor, PropertyIds::UTC, &JavascriptDate::EntryInfo::UTC, 7); // should be static
  2041. dateConstructor->SetHasNoEnumerableProperties(true);
  2042. return true;
  2043. }
  2044. bool JavascriptLibrary::InitializeDatePrototype(DynamicObject* datePrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  2045. {
  2046. typeHandler->Convert(datePrototype, mode, 51);
  2047. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterDate
  2048. // so that the update is in sync with profiler
  2049. ScriptContext* scriptContext = datePrototype->GetScriptContext();
  2050. JavascriptLibrary* library = datePrototype->GetLibrary();
  2051. library->AddMember(datePrototype, PropertyIds::constructor, library->dateConstructor);
  2052. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::GetDate.GetOriginalEntryPoint(),
  2053. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::getDate, &JavascriptDate::EntryInfo::GetDate, 0));
  2054. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::GetDay.GetOriginalEntryPoint(),
  2055. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::getDay, &JavascriptDate::EntryInfo::GetDay, 0));
  2056. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::GetFullYear.GetOriginalEntryPoint(),
  2057. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::getFullYear, &JavascriptDate::EntryInfo::GetFullYear, 0));
  2058. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::GetHours.GetOriginalEntryPoint(),
  2059. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::getHours, &JavascriptDate::EntryInfo::GetHours, 0));
  2060. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::GetMilliseconds.GetOriginalEntryPoint(),
  2061. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::getMilliseconds, &JavascriptDate::EntryInfo::GetMilliseconds, 0));
  2062. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::GetMinutes.GetOriginalEntryPoint(),
  2063. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::getMinutes, &JavascriptDate::EntryInfo::GetMinutes, 0));
  2064. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::GetMonth.GetOriginalEntryPoint(),
  2065. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::getMonth, &JavascriptDate::EntryInfo::GetMonth, 0));
  2066. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::GetSeconds.GetOriginalEntryPoint(),
  2067. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::getSeconds, &JavascriptDate::EntryInfo::GetSeconds, 0));
  2068. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::GetTime.GetOriginalEntryPoint(),
  2069. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::getTime, &JavascriptDate::EntryInfo::GetTime, 0));
  2070. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::GetTimezoneOffset.GetOriginalEntryPoint(),
  2071. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::getTimezoneOffset, &JavascriptDate::EntryInfo::GetTimezoneOffset, 0));
  2072. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::GetUTCDate.GetOriginalEntryPoint(),
  2073. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::getUTCDate, &JavascriptDate::EntryInfo::GetUTCDate, 0));
  2074. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::GetUTCDay.GetOriginalEntryPoint(),
  2075. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::getUTCDay, &JavascriptDate::EntryInfo::GetUTCDay, 0));
  2076. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::GetUTCFullYear.GetOriginalEntryPoint(),
  2077. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::getUTCFullYear, &JavascriptDate::EntryInfo::GetUTCFullYear, 0));
  2078. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::GetUTCHours.GetOriginalEntryPoint(),
  2079. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::getUTCHours, &JavascriptDate::EntryInfo::GetUTCHours, 0));
  2080. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::GetUTCMilliseconds.GetOriginalEntryPoint(),
  2081. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::getUTCMilliseconds, &JavascriptDate::EntryInfo::GetUTCMilliseconds, 0));
  2082. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::GetUTCMinutes.GetOriginalEntryPoint(),
  2083. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::getUTCMinutes, &JavascriptDate::EntryInfo::GetUTCMinutes, 0));
  2084. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::GetUTCMonth.GetOriginalEntryPoint(),
  2085. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::getUTCMonth, &JavascriptDate::EntryInfo::GetUTCMonth, 0));
  2086. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::GetUTCSeconds.GetOriginalEntryPoint(),
  2087. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::getUTCSeconds, &JavascriptDate::EntryInfo::GetUTCSeconds, 0));
  2088. if (scriptContext->GetConfig()->SupportsES3Extensions())
  2089. {
  2090. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::GetVarDate.GetOriginalEntryPoint(),
  2091. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::getVarDate, &JavascriptDate::EntryInfo::GetVarDate, 0));
  2092. }
  2093. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::GetYear.GetOriginalEntryPoint(),
  2094. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::getYear, &JavascriptDate::EntryInfo::GetYear, 0));
  2095. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::SetDate.GetOriginalEntryPoint(),
  2096. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::setDate, &JavascriptDate::EntryInfo::SetDate, 1));
  2097. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::SetFullYear.GetOriginalEntryPoint(),
  2098. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::setFullYear, &JavascriptDate::EntryInfo::SetFullYear, 3));
  2099. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::SetHours.GetOriginalEntryPoint(),
  2100. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::setHours, &JavascriptDate::EntryInfo::SetHours, 4));
  2101. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::SetMilliseconds.GetOriginalEntryPoint(),
  2102. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::setMilliseconds, &JavascriptDate::EntryInfo::SetMilliseconds, 1));
  2103. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::SetMinutes.GetOriginalEntryPoint(),
  2104. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::setMinutes, &JavascriptDate::EntryInfo::SetMinutes, 3));
  2105. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::SetMonth.GetOriginalEntryPoint(),
  2106. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::setMonth, &JavascriptDate::EntryInfo::SetMonth, 2));
  2107. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::SetSeconds.GetOriginalEntryPoint(),
  2108. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::setSeconds, &JavascriptDate::EntryInfo::SetSeconds, 2));
  2109. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::SetTime.GetOriginalEntryPoint(),
  2110. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::setTime, &JavascriptDate::EntryInfo::SetTime, 1));
  2111. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::SetUTCDate.GetOriginalEntryPoint(),
  2112. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::setUTCDate, &JavascriptDate::EntryInfo::SetUTCDate, 1));
  2113. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::SetUTCFullYear.GetOriginalEntryPoint(),
  2114. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::setUTCFullYear, &JavascriptDate::EntryInfo::SetUTCFullYear, 3));
  2115. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::SetUTCHours.GetOriginalEntryPoint(),
  2116. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::setUTCHours, &JavascriptDate::EntryInfo::SetUTCHours, 4));
  2117. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::SetUTCMilliseconds.GetOriginalEntryPoint(),
  2118. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::setUTCMilliseconds, &JavascriptDate::EntryInfo::SetUTCMilliseconds, 1));
  2119. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::SetUTCMinutes.GetOriginalEntryPoint(),
  2120. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::setUTCMinutes, &JavascriptDate::EntryInfo::SetUTCMinutes, 3));
  2121. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::SetUTCMonth.GetOriginalEntryPoint(),
  2122. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::setUTCMonth, &JavascriptDate::EntryInfo::SetUTCMonth, 2));
  2123. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::SetUTCSeconds.GetOriginalEntryPoint(),
  2124. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::setUTCSeconds, &JavascriptDate::EntryInfo::SetUTCSeconds, 2));
  2125. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::SetYear.GetOriginalEntryPoint(),
  2126. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::setYear, &JavascriptDate::EntryInfo::SetYear, 1));
  2127. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::ToDateString.GetOriginalEntryPoint(),
  2128. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::toDateString, &JavascriptDate::EntryInfo::ToDateString, 0));
  2129. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::ToISOString.GetOriginalEntryPoint(),
  2130. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::toISOString, &JavascriptDate::EntryInfo::ToISOString, 0));
  2131. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::ToJSON.GetOriginalEntryPoint(),
  2132. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::toJSON, &JavascriptDate::EntryInfo::ToJSON, 1));
  2133. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::ToLocaleDateString.GetOriginalEntryPoint(),
  2134. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::toLocaleDateString, &JavascriptDate::EntryInfo::ToLocaleDateString, 0));
  2135. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::ToLocaleString.GetOriginalEntryPoint(),
  2136. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::toLocaleString, &JavascriptDate::EntryInfo::ToLocaleString, 0));
  2137. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::ToLocaleTimeString.GetOriginalEntryPoint(),
  2138. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::toLocaleTimeString, &JavascriptDate::EntryInfo::ToLocaleTimeString, 0));
  2139. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::ToString.GetOriginalEntryPoint(),
  2140. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::toString, &JavascriptDate::EntryInfo::ToString, 0));
  2141. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::ToTimeString.GetOriginalEntryPoint(),
  2142. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::toTimeString, &JavascriptDate::EntryInfo::ToTimeString, 0));
  2143. // Spec stipulates toGMTString must be the same function object as toUTCString
  2144. JavascriptFunction *toUTCStringFunc = library->AddFunctionToLibraryObject(datePrototype, PropertyIds::toUTCString, &JavascriptDate::EntryInfo::ToUTCString, 0);
  2145. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::ToUTCString.GetOriginalEntryPoint(), toUTCStringFunc);
  2146. library->AddMember(datePrototype, PropertyIds::toGMTString, toUTCStringFunc, PropertyBuiltInMethodDefaults);
  2147. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::ValueOf.GetOriginalEntryPoint(),
  2148. library->AddFunctionToLibraryObject(datePrototype, PropertyIds::valueOf, &JavascriptDate::EntryInfo::ValueOf, 0));
  2149. if (scriptContext->GetConfig()->IsES6ToPrimitiveEnabled())
  2150. {
  2151. scriptContext->SetBuiltInLibraryFunction(JavascriptDate::EntryInfo::SymbolToPrimitive.GetOriginalEntryPoint(),
  2152. library->AddFunctionToLibraryObjectWithName(datePrototype, PropertyIds::_symbolToPrimitive, PropertyIds::_RuntimeFunctionNameId_toPrimitive,
  2153. &JavascriptDate::EntryInfo::SymbolToPrimitive, 1));
  2154. datePrototype->SetWritable(PropertyIds::_symbolToPrimitive, false);
  2155. }
  2156. datePrototype->SetHasNoEnumerableProperties(true);
  2157. return true;
  2158. }
  2159. bool JavascriptLibrary::InitializeFunctionConstructor(DynamicObject* functionConstructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  2160. {
  2161. typeHandler->Convert(functionConstructor, mode, 3);
  2162. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterFunction
  2163. // so that the update is in sync with profiler
  2164. ScriptContext* scriptContext = functionConstructor->GetScriptContext();
  2165. JavascriptLibrary* library = functionConstructor->GetLibrary();
  2166. library->AddMember(functionConstructor, PropertyIds::prototype, library->functionPrototype, PropertyNone);
  2167. library->AddMember(functionConstructor, PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable);
  2168. library->AddMember(functionConstructor, PropertyIds::name, scriptContext->GetPropertyString(PropertyIds::Function), PropertyConfigurable);
  2169. functionConstructor->SetHasNoEnumerableProperties(true);
  2170. #ifdef ALLOW_JIT_REPRO
  2171. if (CONFIG_FLAG(JitRepro))
  2172. {
  2173. library->AddFunctionToLibraryObject(functionConstructor, PropertyIds::invokeJit, &JavascriptFunction::EntryInfo::InvokeJit, 1);
  2174. }
  2175. #endif
  2176. return true;
  2177. }
  2178. bool JavascriptLibrary::InitializeFunctionPrototype(DynamicObject* functionPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  2179. {
  2180. typeHandler->Convert(functionPrototype, mode, 7);
  2181. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterFunction
  2182. // so that the update is in sync with profiler
  2183. ScriptContext* scriptContext = functionPrototype->GetScriptContext();
  2184. JavascriptLibrary* library = functionPrototype->GetLibrary();
  2185. Field(JavascriptFunction*)* builtinFuncs = library->GetBuiltinFunctions();
  2186. library->AddMember(functionPrototype, PropertyIds::constructor, library->functionConstructor);
  2187. library->AddMember(functionPrototype, PropertyIds::length, TaggedInt::ToVarUnchecked(0), PropertyConfigurable);
  2188. library->AddMember(functionPrototype, PropertyIds::name, LiteralString::CreateEmptyString(scriptContext->GetLibrary()->GetStringTypeStatic()), PropertyConfigurable);
  2189. JavascriptFunction *func = library->AddFunctionToLibraryObject(functionPrototype, PropertyIds::apply, &JavascriptFunction::EntryInfo::Apply, 2);
  2190. builtinFuncs[BuiltinFunction::JavascriptFunction_Apply] = func;
  2191. library->AddFunctionToLibraryObject(functionPrototype, PropertyIds::bind, &JavascriptFunction::EntryInfo::Bind, 1);
  2192. func = library->AddFunctionToLibraryObject(functionPrototype, PropertyIds::call, &JavascriptFunction::EntryInfo::Call, 1);
  2193. builtinFuncs[BuiltinFunction::JavascriptFunction_Call] = func;
  2194. library->AddFunctionToLibraryObject(functionPrototype, PropertyIds::toString, &JavascriptFunction::EntryInfo::ToString, 0);
  2195. if (scriptContext->GetConfig()->IsES6HasInstanceEnabled())
  2196. {
  2197. scriptContext->SetBuiltInLibraryFunction(JavascriptFunction::EntryInfo::SymbolHasInstance.GetOriginalEntryPoint(),
  2198. library->AddFunctionToLibraryObjectWithName(functionPrototype, PropertyIds::_symbolHasInstance, PropertyIds::_RuntimeFunctionNameId_hasInstance,
  2199. &JavascriptFunction::EntryInfo::SymbolHasInstance, 1));
  2200. functionPrototype->SetWritable(PropertyIds::_symbolHasInstance, false);
  2201. functionPrototype->SetConfigurable(PropertyIds::_symbolHasInstance, false);
  2202. }
  2203. functionPrototype->DynamicObject::SetAccessors(PropertyIds::caller, library->throwTypeErrorRestrictedPropertyAccessorFunction, library->throwTypeErrorRestrictedPropertyAccessorFunction);
  2204. functionPrototype->SetEnumerable(PropertyIds::caller, false);
  2205. functionPrototype->DynamicObject::SetAccessors(PropertyIds::arguments, library->throwTypeErrorRestrictedPropertyAccessorFunction, library->throwTypeErrorRestrictedPropertyAccessorFunction);
  2206. functionPrototype->SetEnumerable(PropertyIds::arguments, false);
  2207. DebugOnly(CheckRegisteredBuiltIns(builtinFuncs, scriptContext));
  2208. functionPrototype->SetHasNoEnumerableProperties(true);
  2209. return true;
  2210. }
  2211. void JavascriptLibrary::InitializeComplexThings()
  2212. {
  2213. emptyRegexPattern = RegexHelper::CompileDynamic(scriptContext, _u(""), 0, _u(""), 0, false);
  2214. Recycler *const recycler = GetRecycler();
  2215. const ScriptConfiguration *scriptConfig = scriptContext->GetConfig();
  2216. // Creating the regex prototype object requires compiling an empty regex, which may require error types to be
  2217. // initialized first (such as when a stack probe fails). So, the regex prototype and other things that depend on it are
  2218. // initialized here, which will be after the dependency types are initialized.
  2219. //
  2220. // In ES6, RegExp.prototype is not a RegExp object itself so we do not need to wait and create an empty RegExp.
  2221. // Instead, we just create an ordinary object prototype for RegExp.prototype in InitializePrototypes.
  2222. if (!scriptConfig->IsES6PrototypeChain() && regexPrototype == nullptr)
  2223. {
  2224. regexPrototype = RecyclerNew(recycler, JavascriptRegExp, emptyRegexPattern,
  2225. DynamicType::New(scriptContext, TypeIds_RegEx, objectPrototype, nullptr,
  2226. DeferredTypeHandler<InitializeRegexPrototype, DefaultDeferredTypeFilter, true>::GetDefaultInstance()));
  2227. }
  2228. PathTypeHandlerNoAttr *typeHandler =
  2229. PathTypeHandlerNoAttr::New(scriptContext, this->GetRootPath(), 0, 0, 0, true, true);
  2230. // See JavascriptRegExp::IsWritable for property writability
  2231. if (!scriptConfig->IsES6RegExPrototypePropertiesEnabled())
  2232. {
  2233. typeHandler->ClearHasOnlyWritableDataProperties();
  2234. }
  2235. regexType = DynamicType::New(scriptContext, TypeIds_RegEx, regexPrototype, nullptr, typeHandler, true, true);
  2236. }
  2237. bool JavascriptLibrary::InitializeMathObject(DynamicObject* mathObject, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  2238. {
  2239. typeHandler->Convert(mathObject, mode, 42);
  2240. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterMath
  2241. // so that the update is in sync with profiler
  2242. ScriptContext* scriptContext = mathObject->GetScriptContext();
  2243. JavascriptLibrary* library = mathObject->GetLibrary();
  2244. library->AddMember(mathObject, PropertyIds::E, JavascriptNumber::New(Math::E, scriptContext), PropertyNone);
  2245. library->AddMember(mathObject, PropertyIds::LN10, JavascriptNumber::New(Math::LN10, scriptContext), PropertyNone);
  2246. library->AddMember(mathObject, PropertyIds::LN2, JavascriptNumber::New(Math::LN2, scriptContext), PropertyNone);
  2247. library->AddMember(mathObject, PropertyIds::LOG2E, JavascriptNumber::New(Math::LOG2E, scriptContext), PropertyNone);
  2248. library->AddMember(mathObject, PropertyIds::LOG10E, JavascriptNumber::New(Math::LOG10E, scriptContext), PropertyNone);
  2249. library->AddMember(mathObject, PropertyIds::PI, library->pi, PropertyNone);
  2250. library->AddMember(mathObject, PropertyIds::SQRT1_2, JavascriptNumber::New(Math::SQRT1_2, scriptContext), PropertyNone);
  2251. library->AddMember(mathObject, PropertyIds::SQRT2, JavascriptNumber::New(Math::SQRT2, scriptContext), PropertyNone);
  2252. Field(JavascriptFunction*)* builtinFuncs = library->GetBuiltinFunctions();
  2253. builtinFuncs[BuiltinFunction::Math_Abs] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::abs, &Math::EntryInfo::Abs, 1);
  2254. builtinFuncs[BuiltinFunction::Math_Acos] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::acos, &Math::EntryInfo::Acos, 1);
  2255. builtinFuncs[BuiltinFunction::Math_Asin] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::asin, &Math::EntryInfo::Asin, 1);
  2256. builtinFuncs[BuiltinFunction::Math_Atan] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::atan, &Math::EntryInfo::Atan, 1);
  2257. builtinFuncs[BuiltinFunction::Math_Atan2] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::atan2, &Math::EntryInfo::Atan2, 2);
  2258. builtinFuncs[BuiltinFunction::Math_Cos] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::cos, &Math::EntryInfo::Cos, 1);
  2259. builtinFuncs[BuiltinFunction::Math_Ceil] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::ceil, &Math::EntryInfo::Ceil, 1);
  2260. builtinFuncs[BuiltinFunction::Math_Exp] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::exp, &Math::EntryInfo::Exp, 1);
  2261. builtinFuncs[BuiltinFunction::Math_Floor] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::floor, &Math::EntryInfo::Floor, 1);
  2262. builtinFuncs[BuiltinFunction::Math_Log] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::log, &Math::EntryInfo::Log, 1);
  2263. builtinFuncs[BuiltinFunction::Math_Max] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::max, &Math::EntryInfo::Max, 2);
  2264. builtinFuncs[BuiltinFunction::Math_Min] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::min, &Math::EntryInfo::Min, 2);
  2265. builtinFuncs[BuiltinFunction::Math_Pow] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::pow, &Math::EntryInfo::Pow, 2);
  2266. builtinFuncs[BuiltinFunction::Math_Random] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::random, &Math::EntryInfo::Random, 0);
  2267. builtinFuncs[BuiltinFunction::Math_Round] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::round, &Math::EntryInfo::Round, 1);
  2268. builtinFuncs[BuiltinFunction::Math_Sin] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::sin, &Math::EntryInfo::Sin, 1);
  2269. builtinFuncs[BuiltinFunction::Math_Sqrt] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::sqrt, &Math::EntryInfo::Sqrt, 1);
  2270. builtinFuncs[BuiltinFunction::Math_Tan] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::tan, &Math::EntryInfo::Tan, 1);
  2271. if (scriptContext->GetConfig()->IsES6MathExtensionsEnabled())
  2272. {
  2273. builtinFuncs[BuiltinFunction::Math_Imul] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::imul, &Math::EntryInfo::Imul, 2);
  2274. builtinFuncs[BuiltinFunction::Math_Fround] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::fround, &Math::EntryInfo::Fround, 1);
  2275. /*builtinFuncs[BuiltinFunction::Math_Log10] =*/ library->AddFunctionToLibraryObject(mathObject, PropertyIds::log10, &Math::EntryInfo::Log10, 1);
  2276. /*builtinFuncs[BuiltinFunction::Math_Log2] =*/ library->AddFunctionToLibraryObject(mathObject, PropertyIds::log2, &Math::EntryInfo::Log2, 1);
  2277. /*builtinFuncs[BuiltinFunction::Math_Log1p] =*/ library->AddFunctionToLibraryObject(mathObject, PropertyIds::log1p, &Math::EntryInfo::Log1p, 1);
  2278. /*builtinFuncs[BuiltinFunction::Math_Expm1] =*/ library->AddFunctionToLibraryObject(mathObject, PropertyIds::expm1, &Math::EntryInfo::Expm1, 1);
  2279. /*builtinFuncs[BuiltinFunction::Math_Cosh] =*/ library->AddFunctionToLibraryObject(mathObject, PropertyIds::cosh, &Math::EntryInfo::Cosh, 1);
  2280. /*builtinFuncs[BuiltinFunction::Math_Sinh] =*/ library->AddFunctionToLibraryObject(mathObject, PropertyIds::sinh, &Math::EntryInfo::Sinh, 1);
  2281. /*builtinFuncs[BuiltinFunction::Math_Tanh] =*/ library->AddFunctionToLibraryObject(mathObject, PropertyIds::tanh, &Math::EntryInfo::Tanh, 1);
  2282. /*builtinFuncs[BuiltinFunction::Math_Acosh] =*/ library->AddFunctionToLibraryObject(mathObject, PropertyIds::acosh, &Math::EntryInfo::Acosh, 1);
  2283. /*builtinFuncs[BuiltinFunction::Math_Asinh] =*/ library->AddFunctionToLibraryObject(mathObject, PropertyIds::asinh, &Math::EntryInfo::Asinh, 1);
  2284. /*builtinFuncs[BuiltinFunction::Math_Atanh] =*/ library->AddFunctionToLibraryObject(mathObject, PropertyIds::atanh, &Math::EntryInfo::Atanh, 1);
  2285. /*builtinFuncs[BuiltinFunction::Math_Hypot] =*/ library->AddFunctionToLibraryObject(mathObject, PropertyIds::hypot, &Math::EntryInfo::Hypot, 2);
  2286. /*builtinFuncs[BuiltinFunction::Math_Trunc] =*/ library->AddFunctionToLibraryObject(mathObject, PropertyIds::trunc, &Math::EntryInfo::Trunc, 1);
  2287. /*builtinFuncs[BuiltinFunction::Math_Sign] =*/ library->AddFunctionToLibraryObject(mathObject, PropertyIds::sign, &Math::EntryInfo::Sign, 1);
  2288. /*builtinFuncs[BuiltinFunction::Math_Cbrt] =*/ library->AddFunctionToLibraryObject(mathObject, PropertyIds::cbrt, &Math::EntryInfo::Cbrt, 1);
  2289. /*builtinFuncs[BuiltinFunction::Math_Clz32] =*/ library->AddFunctionToLibraryObject(mathObject, PropertyIds::clz32, &Math::EntryInfo::Clz32, 1);
  2290. }
  2291. if (scriptContext->GetConfig()->IsES6ToStringTagEnabled())
  2292. {
  2293. library->AddMember(mathObject, PropertyIds::_symbolToStringTag, library->CreateStringFromCppLiteral(_u("Math")), PropertyConfigurable);
  2294. }
  2295. DebugOnly(CheckRegisteredBuiltIns(builtinFuncs, scriptContext));
  2296. mathObject->SetHasNoEnumerableProperties(true);
  2297. return true;
  2298. }
  2299. #ifdef ENABLE_WASM
  2300. bool JavascriptLibrary::InitializeWebAssemblyTablePrototype(DynamicObject* prototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  2301. {
  2302. typeHandler->Convert(prototype, mode, 6);
  2303. JavascriptLibrary* library = prototype->GetLibrary();
  2304. ScriptContext* scriptContext = prototype->GetScriptContext();
  2305. library->AddMember(prototype, PropertyIds::constructor, library->webAssemblyTableConstructor);
  2306. if (scriptContext->GetConfig()->IsES6ToStringTagEnabled())
  2307. {
  2308. library->AddMember(prototype, PropertyIds::_symbolToStringTag, library->CreateStringFromCppLiteral(_u("WebAssembly.Table")), PropertyConfigurable);
  2309. }
  2310. scriptContext->SetBuiltInLibraryFunction(WebAssemblyTable::EntryInfo::Grow.GetOriginalEntryPoint(),
  2311. library->AddFunctionToLibraryObject(prototype, PropertyIds::grow, &WebAssemblyTable::EntryInfo::Grow, 1));
  2312. scriptContext->SetBuiltInLibraryFunction(WebAssemblyTable::EntryInfo::Get.GetOriginalEntryPoint(),
  2313. library->AddFunctionToLibraryObject(prototype, PropertyIds::get, &WebAssemblyTable::EntryInfo::Get, 1));
  2314. scriptContext->SetBuiltInLibraryFunction(WebAssemblyTable::EntryInfo::Set.GetOriginalEntryPoint(),
  2315. library->AddFunctionToLibraryObject(prototype, PropertyIds::set, &WebAssemblyTable::EntryInfo::Set, 2));
  2316. library->AddAccessorsToLibraryObject(prototype, PropertyIds::length, &WebAssemblyTable::EntryInfo::GetterLength, nullptr);
  2317. prototype->SetHasNoEnumerableProperties(true);
  2318. return true;
  2319. }
  2320. bool JavascriptLibrary::InitializeWebAssemblyTableConstructor(DynamicObject* constructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  2321. {
  2322. typeHandler->Convert(constructor, mode, 3);
  2323. JavascriptLibrary* library = constructor->GetLibrary();
  2324. library->AddMember(constructor, PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable);
  2325. library->AddMember(constructor, PropertyIds::prototype, library->webAssemblyTablePrototype, PropertyNone);
  2326. library->AddMember(constructor, PropertyIds::name, library->CreateStringFromCppLiteral(_u("Table")), PropertyConfigurable);
  2327. constructor->SetHasNoEnumerableProperties(true);
  2328. return true;
  2329. }
  2330. bool JavascriptLibrary::InitializeWebAssemblyMemoryPrototype(DynamicObject* prototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  2331. {
  2332. typeHandler->Convert(prototype, mode, 4);
  2333. JavascriptLibrary* library = prototype->GetLibrary();
  2334. ScriptContext* scriptContext = prototype->GetScriptContext();
  2335. library->AddMember(prototype, PropertyIds::constructor, library->webAssemblyMemoryConstructor);
  2336. if (scriptContext->GetConfig()->IsES6ToStringTagEnabled())
  2337. {
  2338. library->AddMember(prototype, PropertyIds::_symbolToStringTag, library->CreateStringFromCppLiteral(_u("WebAssembly.Memory")), PropertyConfigurable);
  2339. }
  2340. scriptContext->SetBuiltInLibraryFunction(WebAssemblyMemory::EntryInfo::Grow.GetOriginalEntryPoint(),
  2341. library->AddFunctionToLibraryObject(prototype, PropertyIds::grow, &WebAssemblyMemory::EntryInfo::Grow, 1));
  2342. library->AddAccessorsToLibraryObject(prototype, PropertyIds::buffer, &WebAssemblyMemory::EntryInfo::GetterBuffer, nullptr);
  2343. prototype->SetHasNoEnumerableProperties(true);
  2344. return true;
  2345. }
  2346. bool JavascriptLibrary::InitializeWebAssemblyMemoryConstructor(DynamicObject* constructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  2347. {
  2348. typeHandler->Convert(constructor, mode, 3);
  2349. JavascriptLibrary* library = constructor->GetLibrary();
  2350. library->AddMember(constructor, PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable);
  2351. library->AddMember(constructor, PropertyIds::prototype, library->webAssemblyMemoryPrototype, PropertyNone);
  2352. library->AddMember(constructor, PropertyIds::name, library->CreateStringFromCppLiteral(_u("Memory")), PropertyConfigurable);
  2353. constructor->SetHasNoEnumerableProperties(true);
  2354. return true;
  2355. }
  2356. bool JavascriptLibrary::InitializeWebAssemblyInstancePrototype(DynamicObject* prototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  2357. {
  2358. typeHandler->Convert(prototype, mode, 3);
  2359. JavascriptLibrary* library = prototype->GetLibrary();
  2360. ScriptContext* scriptContext = prototype->GetScriptContext();
  2361. library->AddMember(prototype, PropertyIds::constructor, library->webAssemblyInstanceConstructor);
  2362. library->AddAccessorsToLibraryObject(prototype, PropertyIds::exports, &WebAssemblyInstance::EntryInfo::GetterExports, nullptr);
  2363. if (scriptContext->GetConfig()->IsES6ToStringTagEnabled())
  2364. {
  2365. library->AddMember(prototype, PropertyIds::_symbolToStringTag, library->CreateStringFromCppLiteral(_u("WebAssembly.Instance")), PropertyConfigurable);
  2366. }
  2367. prototype->SetHasNoEnumerableProperties(true);
  2368. return true;
  2369. }
  2370. bool JavascriptLibrary::InitializeWebAssemblyInstanceConstructor(DynamicObject* constructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  2371. {
  2372. typeHandler->Convert(constructor, mode, 3);
  2373. JavascriptLibrary* library = constructor->GetLibrary();
  2374. library->AddMember(constructor, PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable);
  2375. library->AddMember(constructor, PropertyIds::prototype, library->webAssemblyInstancePrototype, PropertyNone);
  2376. library->AddMember(constructor, PropertyIds::name, library->CreateStringFromCppLiteral(_u("Instance")), PropertyConfigurable);
  2377. constructor->SetHasNoEnumerableProperties(true);
  2378. return true;
  2379. }
  2380. bool JavascriptLibrary::InitializeWebAssemblyModulePrototype(DynamicObject* prototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  2381. {
  2382. typeHandler->Convert(prototype, mode, 2);
  2383. JavascriptLibrary* library = prototype->GetLibrary();
  2384. ScriptContext* scriptContext = prototype->GetScriptContext();
  2385. library->AddMember(prototype, PropertyIds::constructor, library->webAssemblyModuleConstructor);
  2386. if (scriptContext->GetConfig()->IsES6ToStringTagEnabled())
  2387. {
  2388. library->AddMember(prototype, PropertyIds::_symbolToStringTag, library->CreateStringFromCppLiteral(_u("WebAssembly.Module")), PropertyConfigurable);
  2389. }
  2390. prototype->SetHasNoEnumerableProperties(true);
  2391. return true;
  2392. }
  2393. bool JavascriptLibrary::InitializeWebAssemblyModuleConstructor(DynamicObject* constructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  2394. {
  2395. typeHandler->Convert(constructor, mode, 5);
  2396. JavascriptLibrary* library = constructor->GetLibrary();
  2397. library->AddMember(constructor, PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable);
  2398. library->AddMember(constructor, PropertyIds::prototype, library->webAssemblyModulePrototype, PropertyNone);
  2399. library->AddMember(constructor, PropertyIds::name, library->CreateStringFromCppLiteral(_u("Module")), PropertyConfigurable);
  2400. library->AddFunctionToLibraryObject(constructor, PropertyIds::exports, &WebAssemblyModule::EntryInfo::Exports, 1);
  2401. library->AddFunctionToLibraryObject(constructor, PropertyIds::imports, &WebAssemblyModule::EntryInfo::Imports, 1);
  2402. library->AddFunctionToLibraryObject(constructor, PropertyIds::customSections, &WebAssemblyModule::EntryInfo::CustomSections, 2);
  2403. constructor->SetHasNoEnumerableProperties(true);
  2404. return true;
  2405. }
  2406. bool JavascriptLibrary::InitializeWebAssemblyObject(DynamicObject* webAssemblyObject, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  2407. {
  2408. JavascriptLibrary* library = webAssemblyObject->GetLibrary();
  2409. int slots = 9;
  2410. #ifdef ENABLE_WABT
  2411. // Attaching wabt for testing
  2412. ++slots;
  2413. #endif
  2414. typeHandler->Convert(webAssemblyObject, mode, slots);
  2415. #ifdef ENABLE_WABT
  2416. // Build wabt object
  2417. Js::DynamicObject* wabtObject = library->CreateObject(true);
  2418. library->AddFunctionToLibraryObject(wabtObject, PropertyIds::convertWast2Wasm, &WabtInterface::EntryInfo::ConvertWast2Wasm, 1);
  2419. library->AddMember(webAssemblyObject, PropertyIds::wabt, wabtObject, PropertyNone);
  2420. #endif
  2421. library->webAssemblyCompileFunction =
  2422. library->AddFunctionToLibraryObject(webAssemblyObject, PropertyIds::compile, &WebAssembly::EntryInfo::Compile, 1);
  2423. library->AddFunctionToLibraryObject(webAssemblyObject, PropertyIds::compileStreaming, &WebAssembly::EntryInfo::CompileStreaming, 1);
  2424. library->AddFunctionToLibraryObject(webAssemblyObject, PropertyIds::validate, &WebAssembly::EntryInfo::Validate, 1);
  2425. library->AddFunctionToLibraryObject(webAssemblyObject, PropertyIds::instantiate, &WebAssembly::EntryInfo::Instantiate, 1);
  2426. library->AddFunctionToLibraryObject(webAssemblyObject, PropertyIds::instantiateStreaming, &WebAssembly::EntryInfo::InstantiateStreaming, 1);
  2427. library->webAssemblyQueryResponseFunction = library->DefaultCreateFunction(&WebAssembly::EntryInfo::QueryResponse, 1, nullptr, nullptr, PropertyIds::undefined);
  2428. library->webAssemblyInstantiateBoundFunction = library->DefaultCreateFunction(&WebAssembly::EntryInfo::InstantiateBound, 1, nullptr, nullptr, PropertyIds::undefined);
  2429. library->AddFunction(webAssemblyObject, PropertyIds::Module, library->webAssemblyModuleConstructor);
  2430. library->AddFunction(webAssemblyObject, PropertyIds::Instance, library->webAssemblyInstanceConstructor);
  2431. library->AddFunction(webAssemblyObject, PropertyIds::CompileError, library->webAssemblyCompileErrorConstructor);
  2432. library->AddFunction(webAssemblyObject, PropertyIds::RuntimeError, library->webAssemblyRuntimeErrorConstructor);
  2433. library->AddFunction(webAssemblyObject, PropertyIds::LinkError, library->webAssemblyLinkErrorConstructor);
  2434. library->AddFunction(webAssemblyObject, PropertyIds::Memory, library->webAssemblyMemoryConstructor);
  2435. library->AddFunction(webAssemblyObject, PropertyIds::Table, library->webAssemblyTableConstructor);
  2436. library->AddMember(webAssemblyObject, PropertyIds::_symbolToStringTag, library->CreateStringFromCppLiteral(_u("WebAssembly")), PropertyConfigurable);
  2437. return true;
  2438. }
  2439. #endif
  2440. bool JavascriptLibrary::InitializeReflectObject(DynamicObject* reflectObject, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  2441. {
  2442. typeHandler->Convert(reflectObject, mode, 12);
  2443. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterReflect
  2444. // so that the update is in sync with profiler
  2445. ScriptContext* scriptContext = reflectObject->GetScriptContext();
  2446. JavascriptLibrary* library = reflectObject->GetLibrary();
  2447. scriptContext->SetBuiltInLibraryFunction(JavascriptReflect::EntryInfo::DefineProperty.GetOriginalEntryPoint(),
  2448. library->AddFunctionToLibraryObject(reflectObject, PropertyIds::defineProperty, &JavascriptReflect::EntryInfo::DefineProperty, 3));
  2449. scriptContext->SetBuiltInLibraryFunction(JavascriptReflect::EntryInfo::DeleteProperty.GetOriginalEntryPoint(),
  2450. library->AddFunctionToLibraryObject(reflectObject, PropertyIds::deleteProperty, &JavascriptReflect::EntryInfo::DeleteProperty, 2));
  2451. scriptContext->SetBuiltInLibraryFunction(JavascriptReflect::EntryInfo::Get.GetOriginalEntryPoint(),
  2452. library->AddFunctionToLibraryObject(reflectObject, PropertyIds::get, &JavascriptReflect::EntryInfo::Get, 2));
  2453. scriptContext->SetBuiltInLibraryFunction(JavascriptReflect::EntryInfo::GetOwnPropertyDescriptor.GetOriginalEntryPoint(),
  2454. library->AddFunctionToLibraryObject(reflectObject, PropertyIds::getOwnPropertyDescriptor, &JavascriptReflect::EntryInfo::GetOwnPropertyDescriptor, 2));
  2455. scriptContext->SetBuiltInLibraryFunction(JavascriptReflect::EntryInfo::GetPrototypeOf.GetOriginalEntryPoint(),
  2456. library->AddFunctionToLibraryObject(reflectObject, PropertyIds::getPrototypeOf, &JavascriptReflect::EntryInfo::GetPrototypeOf, 1));
  2457. scriptContext->SetBuiltInLibraryFunction(JavascriptReflect::EntryInfo::Has.GetOriginalEntryPoint(),
  2458. library->AddFunctionToLibraryObject(reflectObject, PropertyIds::has, &JavascriptReflect::EntryInfo::Has, 2));
  2459. scriptContext->SetBuiltInLibraryFunction(JavascriptReflect::EntryInfo::IsExtensible.GetOriginalEntryPoint(),
  2460. library->AddFunctionToLibraryObject(reflectObject, PropertyIds::isExtensible, &JavascriptReflect::EntryInfo::IsExtensible, 1));
  2461. scriptContext->SetBuiltInLibraryFunction(JavascriptReflect::EntryInfo::OwnKeys.GetOriginalEntryPoint(),
  2462. library->AddFunctionToLibraryObject(reflectObject, PropertyIds::ownKeys, &JavascriptReflect::EntryInfo::OwnKeys, 1));
  2463. scriptContext->SetBuiltInLibraryFunction(JavascriptReflect::EntryInfo::PreventExtensions.GetOriginalEntryPoint(),
  2464. library->AddFunctionToLibraryObject(reflectObject, PropertyIds::preventExtensions, &JavascriptReflect::EntryInfo::PreventExtensions, 1));
  2465. scriptContext->SetBuiltInLibraryFunction(JavascriptReflect::EntryInfo::Set.GetOriginalEntryPoint(),
  2466. library->AddFunctionToLibraryObject(reflectObject, PropertyIds::set, &JavascriptReflect::EntryInfo::Set, 3));
  2467. scriptContext->SetBuiltInLibraryFunction(JavascriptReflect::EntryInfo::SetPrototypeOf.GetOriginalEntryPoint(),
  2468. library->AddFunctionToLibraryObject(reflectObject, PropertyIds::setPrototypeOf, &JavascriptReflect::EntryInfo::SetPrototypeOf, 2));
  2469. scriptContext->SetBuiltInLibraryFunction(JavascriptReflect::EntryInfo::Apply.GetOriginalEntryPoint(),
  2470. library->AddFunctionToLibraryObject(reflectObject, PropertyIds::apply, &JavascriptReflect::EntryInfo::Apply, 3));
  2471. scriptContext->SetBuiltInLibraryFunction(JavascriptReflect::EntryInfo::Construct.GetOriginalEntryPoint(),
  2472. library->AddFunctionToLibraryObject(reflectObject, PropertyIds::construct, &JavascriptReflect::EntryInfo::Construct, 2));
  2473. return true;
  2474. }
  2475. void JavascriptLibrary::InitializeStaticValues()
  2476. {
  2477. constructorCacheDefaultInstance = &Js::ConstructorCache::DefaultInstance;
  2478. absDoubleCst = Js::JavascriptNumber::AbsDoubleCst;
  2479. uintConvertConst = Js::JavascriptNumber::UIntConvertConst;
  2480. defaultPropertyDescriptor.SetValue(undefinedValue);
  2481. defaultPropertyDescriptor.SetWritable(false);
  2482. defaultPropertyDescriptor.SetGetter(defaultAccessorFunction);
  2483. defaultPropertyDescriptor.SetSetter(defaultAccessorFunction);
  2484. defaultPropertyDescriptor.SetEnumerable(false);
  2485. defaultPropertyDescriptor.SetConfigurable(false);
  2486. #if !defined(TARGET_64)
  2487. VirtualTableRecorder<Js::JavascriptNumber>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableJavascriptNumber);
  2488. #else
  2489. vtableAddresses[VTableValue::VtableJavascriptNumber] = 0;
  2490. #endif
  2491. VirtualTableRecorder<Js::DynamicObject>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableDynamicObject);
  2492. vtableAddresses[VTableValue::VtableInvalid] = Js::ScriptContextOptimizationOverrideInfo::InvalidVtable;
  2493. VirtualTableRecorder<Js::PropertyString>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtablePropertyString);
  2494. VirtualTableRecorder<Js::LazyJSONString>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableLazyJSONString);
  2495. VirtualTableRecorder<Js::JavascriptBoolean>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableJavascriptBoolean);
  2496. VirtualTableRecorder<Js::JavascriptArray>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableJavascriptArray);
  2497. VirtualTableRecorder<Js::Int8Array>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableInt8Array);
  2498. VirtualTableRecorder<Js::Uint8Array>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableUint8Array);
  2499. VirtualTableRecorder<Js::Uint8ClampedArray>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableUint8ClampedArray);
  2500. VirtualTableRecorder<Js::Int16Array>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableInt16Array);
  2501. VirtualTableRecorder<Js::Uint16Array>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableUint16Array);
  2502. VirtualTableRecorder<Js::Int32Array>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableInt32Array);
  2503. VirtualTableRecorder<Js::Uint32Array>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableUint32Array);
  2504. VirtualTableRecorder<Js::Float32Array>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableFloat32Array);
  2505. VirtualTableRecorder<Js::Float64Array>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableFloat64Array);
  2506. VirtualTableRecorder<Js::Int64Array>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableInt64Array);
  2507. VirtualTableRecorder<Js::Uint64Array>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableUint64Array);
  2508. VirtualTableRecorder<Js::Int8VirtualArray>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableInt8VirtualArray);
  2509. VirtualTableRecorder<Js::Uint8VirtualArray>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableUint8VirtualArray);
  2510. VirtualTableRecorder<Js::Uint8ClampedVirtualArray>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableUint8ClampedVirtualArray);
  2511. VirtualTableRecorder<Js::Int16VirtualArray>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableInt16VirtualArray);
  2512. VirtualTableRecorder<Js::Uint16VirtualArray>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableUint16VirtualArray);
  2513. VirtualTableRecorder<Js::Int32VirtualArray>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableInt32VirtualArray);
  2514. VirtualTableRecorder<Js::Uint32VirtualArray>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableUint32VirtualArray);
  2515. VirtualTableRecorder<Js::Float32VirtualArray>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableFloat32VirtualArray);
  2516. VirtualTableRecorder<Js::Float64VirtualArray>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableFloat64VirtualArray);
  2517. VirtualTableRecorder<Js::BoolArray>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableBoolArray);
  2518. VirtualTableRecorder<Js::CharArray>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableCharArray);
  2519. VirtualTableRecorder<Js::JavascriptNativeIntArray>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableNativeIntArray);
  2520. #if ENABLE_COPYONACCESS_ARRAY
  2521. VirtualTableRecorder<Js::JavascriptCopyOnAccessNativeIntArray>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableCopyOnAccessNativeIntArray);
  2522. #endif
  2523. VirtualTableRecorder<Js::JavascriptNativeFloatArray>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableNativeFloatArray);
  2524. // don't validate vtable for VtableJavascriptNativeIntArray because its vtable is used for VtableNativeIntArray
  2525. vtableAddresses[VTableValue::VtableJavascriptNativeIntArray] = VirtualTableInfo<Js::JavascriptNativeIntArray>::Address;
  2526. VirtualTableRecorder<Js::JavascriptRegExp>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableJavascriptRegExp);
  2527. VirtualTableRecorder<Js::StackScriptFunction>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableStackScriptFunction);
  2528. VirtualTableRecorder<Js::ScriptFunction>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableScriptFunction);
  2529. VirtualTableRecorder<Js::JavascriptGeneratorFunction>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableJavascriptGeneratorFunction);
  2530. VirtualTableRecorder<Js::JavascriptAsyncFunction>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableJavascriptAsyncFunction);
  2531. VirtualTableRecorder<Js::ConcatStringMulti>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableConcatStringMulti);
  2532. VirtualTableRecorder<Js::CompoundString>::RecordVirtualTableAddress(vtableAddresses, VTableValue::VtableCompoundString);
  2533. for (TypeId typeId = static_cast<TypeId>(0); typeId < TypeIds_Limit; typeId = static_cast<TypeId>(typeId + 1))
  2534. {
  2535. switch (typeId)
  2536. {
  2537. case TypeIds_Undefined:
  2538. typeDisplayStrings[typeId] = GetUndefinedDisplayString();
  2539. break;
  2540. case TypeIds_Function:
  2541. typeDisplayStrings[typeId] = stringCache.GetFunctionTypeDisplayString();
  2542. break;
  2543. case TypeIds_Boolean:
  2544. typeDisplayStrings[typeId] = stringCache.GetBooleanTypeDisplayString();
  2545. break;
  2546. case TypeIds_String:
  2547. typeDisplayStrings[typeId] = stringCache.GetStringTypeDisplayString();
  2548. break;
  2549. case TypeIds_Symbol:
  2550. typeDisplayStrings[typeId] = stringCache.GetSymbolTypeDisplayString();
  2551. break;
  2552. case TypeIds_VariantDate:
  2553. typeDisplayStrings[typeId] = stringCache.GetVariantDateTypeDisplayString();
  2554. break;
  2555. case TypeIds_Integer:
  2556. case TypeIds_Number:
  2557. case TypeIds_Int64Number:
  2558. case TypeIds_UInt64Number:
  2559. typeDisplayStrings[typeId] = stringCache.GetNumberTypeDisplayString();
  2560. break;
  2561. case TypeIds_Enumerator:
  2562. case TypeIds_HostDispatch:
  2563. case TypeIds_WithScopeObject:
  2564. case TypeIds_UndeclBlockVar:
  2565. case TypeIds_Proxy:
  2566. case TypeIds_SpreadArgument:
  2567. typeDisplayStrings[typeId] = nullptr;
  2568. break;
  2569. default:
  2570. typeDisplayStrings[typeId] = stringCache.GetObjectTypeDisplayString();
  2571. break;
  2572. }
  2573. }
  2574. }
  2575. // Note: This function is only used in float preferencing scenarios. Should remove it once we do away with float preferencing.
  2576. // Cases like,
  2577. // case PropertyIds::concat:
  2578. // case PropertyIds::indexOf:
  2579. // case PropertyIds::lastIndexOf:
  2580. // case PropertyIds::slice:
  2581. // which have same names for Array and String cannot be resolved just by the property id
  2582. BuiltinFunction JavascriptLibrary::GetBuiltinFunctionForPropId(PropertyId id)
  2583. {
  2584. switch (id)
  2585. {
  2586. case PropertyIds::abs:
  2587. return BuiltinFunction::Math_Abs;
  2588. // For now, avoid mapping Math.atan2 to a direct CRT call, as the
  2589. // fast CRT helper doesn't handle denormals correctly.
  2590. // case PropertyIds::atan2:
  2591. // return BuiltinFunction::Atan2;
  2592. case PropertyIds::acos:
  2593. return BuiltinFunction::Math_Acos;
  2594. case PropertyIds::asin:
  2595. return BuiltinFunction::Math_Asin;
  2596. case PropertyIds::atan:
  2597. return BuiltinFunction::Math_Atan;
  2598. case PropertyIds::cos:
  2599. return BuiltinFunction::Math_Cos;
  2600. case PropertyIds::exp:
  2601. return BuiltinFunction::Math_Exp;
  2602. case PropertyIds::log:
  2603. return BuiltinFunction::Math_Log;
  2604. case PropertyIds::pow:
  2605. return BuiltinFunction::Math_Pow;
  2606. case PropertyIds::random:
  2607. return BuiltinFunction::Math_Random;
  2608. case PropertyIds::sin:
  2609. return BuiltinFunction::Math_Sin;
  2610. case PropertyIds::sqrt:
  2611. return BuiltinFunction::Math_Sqrt;
  2612. case PropertyIds::tan:
  2613. return BuiltinFunction::Math_Tan;
  2614. case PropertyIds::floor:
  2615. return BuiltinFunction::Math_Floor;
  2616. case PropertyIds::ceil:
  2617. return BuiltinFunction::Math_Ceil;
  2618. case PropertyIds::round:
  2619. return BuiltinFunction::Math_Round;
  2620. case PropertyIds::max:
  2621. return BuiltinFunction::Math_Max;
  2622. case PropertyIds::min:
  2623. return BuiltinFunction::Math_Min;
  2624. case PropertyIds::imul:
  2625. return BuiltinFunction::Math_Imul;
  2626. case PropertyIds::fround:
  2627. return BuiltinFunction::Math_Fround;
  2628. case PropertyIds::codePointAt:
  2629. return BuiltinFunction::JavascriptString_CodePointAt;
  2630. case PropertyIds::push:
  2631. return BuiltinFunction::JavascriptArray_Push;
  2632. case PropertyIds::concat:
  2633. return BuiltinFunction::JavascriptArray_Concat;
  2634. case PropertyIds::indexOf:
  2635. return BuiltinFunction::JavascriptArray_IndexOf;
  2636. case PropertyIds::includes:
  2637. return BuiltinFunction::JavascriptArray_Includes;
  2638. case PropertyIds::isArray:
  2639. return BuiltinFunction::JavascriptArray_IsArray;
  2640. case PropertyIds::join:
  2641. return BuiltinFunction::JavascriptArray_Join;
  2642. case PropertyIds::lastIndexOf:
  2643. return BuiltinFunction::JavascriptArray_LastIndexOf;
  2644. case PropertyIds::reverse:
  2645. return BuiltinFunction::JavascriptArray_Reverse;
  2646. case PropertyIds::shift:
  2647. return BuiltinFunction::JavascriptArray_Shift;
  2648. case PropertyIds::slice:
  2649. return BuiltinFunction::JavascriptArray_Slice;
  2650. case PropertyIds::splice:
  2651. return BuiltinFunction::JavascriptArray_Splice;
  2652. case PropertyIds::unshift:
  2653. return BuiltinFunction::JavascriptArray_Unshift;
  2654. case PropertyIds::apply:
  2655. return BuiltinFunction::JavascriptFunction_Apply;
  2656. case PropertyIds::charAt:
  2657. return BuiltinFunction::JavascriptString_CharAt;
  2658. case PropertyIds::charCodeAt:
  2659. return BuiltinFunction::JavascriptString_CharCodeAt;
  2660. case PropertyIds::fromCharCode:
  2661. return BuiltinFunction::JavascriptString_FromCharCode;
  2662. case PropertyIds::fromCodePoint:
  2663. return BuiltinFunction::JavascriptString_FromCodePoint;
  2664. case PropertyIds::link:
  2665. return BuiltinFunction::JavascriptString_Link;
  2666. case PropertyIds::localeCompare:
  2667. return BuiltinFunction::JavascriptString_LocaleCompare;
  2668. case PropertyIds::match:
  2669. return BuiltinFunction::JavascriptString_Match;
  2670. case PropertyIds::replace:
  2671. return BuiltinFunction::JavascriptString_Replace;
  2672. case PropertyIds::search:
  2673. return BuiltinFunction::JavascriptString_Search;
  2674. case PropertyIds::_symbolSearch:
  2675. return BuiltinFunction::JavascriptRegExp_SymbolSearch;
  2676. case PropertyIds::split:
  2677. return BuiltinFunction::JavascriptString_Split;
  2678. case PropertyIds::substr:
  2679. return BuiltinFunction::JavascriptString_Substr;
  2680. case PropertyIds::substring:
  2681. return BuiltinFunction::JavascriptString_Substring;
  2682. case PropertyIds::toLocaleLowerCase:
  2683. return BuiltinFunction::JavascriptString_ToLocaleLowerCase;
  2684. case PropertyIds::toLocaleUpperCase:
  2685. return BuiltinFunction::JavascriptString_ToLocaleUpperCase;
  2686. case PropertyIds::toLowerCase:
  2687. return BuiltinFunction::JavascriptString_ToLowerCase;
  2688. case PropertyIds::toUpperCase:
  2689. return BuiltinFunction::JavascriptString_ToUpperCase;
  2690. case PropertyIds::trim:
  2691. return BuiltinFunction::JavascriptString_Trim;
  2692. case PropertyIds::trimLeft:
  2693. return BuiltinFunction::JavascriptString_TrimLeft;
  2694. case PropertyIds::trimRight:
  2695. return BuiltinFunction::JavascriptString_TrimRight;
  2696. case PropertyIds::padStart:
  2697. return BuiltinFunction::JavascriptString_PadStart;
  2698. case PropertyIds::padEnd:
  2699. return BuiltinFunction::JavascriptString_PadEnd;
  2700. case PropertyIds::exec:
  2701. return BuiltinFunction::JavascriptRegExp_Exec;
  2702. case PropertyIds::hasOwnProperty:
  2703. return BuiltinFunction::JavascriptObject_HasOwnProperty;
  2704. default:
  2705. return BuiltinFunction::None;
  2706. }
  2707. }
  2708. #if DBG
  2709. /*static*/
  2710. void JavascriptLibrary::CheckRegisteredBuiltIns(
  2711. Field(JavascriptFunction*)* builtInFuncs, ScriptContext *scriptContext)
  2712. {
  2713. byte count = BuiltinFunction::Count;
  2714. for (byte index = 0; index < count; index++)
  2715. {
  2716. Assert(!builtInFuncs[index] || (index == GetBuiltInForFuncInfo((intptr_t)builtInFuncs[index]->GetFunctionInfo(), scriptContext->GetThreadContext())));
  2717. }
  2718. }
  2719. #endif
  2720. // Returns built-in enum value for given funcInfo. Ultimately this will work for all built-ins (not only Math.*).
  2721. // Used by inliner.
  2722. BuiltinFunction JavascriptLibrary::GetBuiltInForFuncInfo(intptr_t funcInfoAddr, ThreadContextInfo * context)
  2723. {
  2724. #define LIBRARY_FUNCTION(target, name, argc, flags, EntryInfo) \
  2725. if(funcInfoAddr == (intptr_t)ShiftAddr(context, &EntryInfo)) \
  2726. { \
  2727. return BuiltinFunction::##target##_##name; \
  2728. }
  2729. #include "LibraryFunction.h"
  2730. #undef LIBRARY_FUNCTION
  2731. return BuiltinFunction::None;
  2732. }
  2733. // Returns true if the function's return type is always float.
  2734. BOOL JavascriptLibrary::IsFltFunc(BuiltinFunction index)
  2735. {
  2736. // Note: MathFunction is one of built-ins.
  2737. if (!JavascriptLibrary::CanFloatPreferenceFunc(index))
  2738. {
  2739. return FALSE;
  2740. }
  2741. Js::BuiltInFlags builtInFlags = JavascriptLibrary::GetFlagsForBuiltIn(index);
  2742. Js::BuiltInArgSpecializationType dstType = Js::JavascriptLibrary::GetBuiltInArgType(builtInFlags, Js::BuiltInArgShift::BIAS_Dst);
  2743. bool isFloatFunc = dstType == Js::BuiltInArgSpecializationType::BIAST_Float;
  2744. return isFloatFunc;
  2745. }
  2746. size_t const JavascriptLibrary::LibraryFunctionArgC[] = {
  2747. #define LIBRARY_FUNCTION(obj, name, argc, flags, entry) argc,
  2748. #include "LibraryFunction.h"
  2749. #undef LIBRARY_FUNCTION
  2750. 0
  2751. };
  2752. #if ENABLE_DEBUG_CONFIG_OPTIONS
  2753. char16 const * const JavascriptLibrary::LibraryFunctionName[] = {
  2754. #define LIBRARY_FUNCTION(obj, name, argc, flags, entry) _u(#obj) _u(".") _u(#name),
  2755. #include "LibraryFunction.h"
  2756. #undef LIBRARY_FUNCTION
  2757. 0
  2758. };
  2759. #endif
  2760. int const JavascriptLibrary::LibraryFunctionFlags[] = {
  2761. #define LIBRARY_FUNCTION(obj, name, argc, flags, entry) flags,
  2762. #include "LibraryFunction.h"
  2763. #undef LIBRARY_FUNCTION
  2764. BIF_None
  2765. };
  2766. bool JavascriptLibrary::IsFloatFunctionCallsite(BuiltinFunction index, size_t argc)
  2767. {
  2768. if (IsFltFunc(index))
  2769. {
  2770. Assert(index < BuiltinFunction::Count);
  2771. if (argc)
  2772. {
  2773. return JavascriptLibrary::LibraryFunctionArgC[index] <= (argc - 1 /* this */);
  2774. }
  2775. }
  2776. return false;
  2777. }
  2778. // For abs, min, max -- return can be int or float, but still return true from here.
  2779. BOOL JavascriptLibrary::CanFloatPreferenceFunc(BuiltinFunction index)
  2780. {
  2781. // Shortcut the common case:
  2782. if (index == BuiltinFunction::None)
  2783. {
  2784. return FALSE;
  2785. }
  2786. switch (index)
  2787. {
  2788. case BuiltinFunction::Math_Abs:
  2789. case BuiltinFunction::Math_Acos:
  2790. case BuiltinFunction::Math_Asin:
  2791. case BuiltinFunction::Math_Atan:
  2792. case BuiltinFunction::Math_Cos:
  2793. case BuiltinFunction::Math_Exp:
  2794. case BuiltinFunction::Math_Log:
  2795. case BuiltinFunction::Math_Min:
  2796. case BuiltinFunction::Math_Max:
  2797. case BuiltinFunction::Math_Pow:
  2798. case BuiltinFunction::Math_Random:
  2799. case BuiltinFunction::Math_Sin:
  2800. case BuiltinFunction::Math_Sqrt:
  2801. case BuiltinFunction::Math_Tan:
  2802. case BuiltinFunction::Math_Fround:
  2803. return TRUE;
  2804. }
  2805. return FALSE;
  2806. }
  2807. bool JavascriptLibrary::IsFltBuiltInConst(PropertyId propertyId)
  2808. {
  2809. switch (propertyId)
  2810. {
  2811. case Js::PropertyIds::E:
  2812. case Js::PropertyIds::LN10:
  2813. case Js::PropertyIds::LN2:
  2814. case Js::PropertyIds::LOG2E:
  2815. case Js::PropertyIds::LOG10E:
  2816. case Js::PropertyIds::PI:
  2817. case Js::PropertyIds::SQRT1_2:
  2818. case Js::PropertyIds::SQRT2:
  2819. return true;
  2820. }
  2821. return false;
  2822. }
  2823. void JavascriptLibrary::TypeAndPrototypesAreEnsuredToHaveOnlyWritableDataProperties(Type *const type)
  2824. {
  2825. Assert(type);
  2826. Assert(type->GetScriptContext() == scriptContext);
  2827. Assert(type->AreThisAndPrototypesEnsuredToHaveOnlyWritableDataProperties());
  2828. Assert(!scriptContext->IsClosed());
  2829. if(typesEnsuredToHaveOnlyWritableDataPropertiesInItAndPrototypeChain->Count() == 0)
  2830. {
  2831. scriptContext->RegisterPrototypeChainEnsuredToHaveOnlyWritableDataPropertiesScriptContext();
  2832. }
  2833. typesEnsuredToHaveOnlyWritableDataPropertiesInItAndPrototypeChain->Add(type);
  2834. }
  2835. void JavascriptLibrary::NoPrototypeChainsAreEnsuredToHaveOnlyWritableDataProperties()
  2836. {
  2837. for(int i = 0; i < typesEnsuredToHaveOnlyWritableDataPropertiesInItAndPrototypeChain->Count(); ++i)
  2838. {
  2839. typesEnsuredToHaveOnlyWritableDataPropertiesInItAndPrototypeChain
  2840. ->Item(i)
  2841. ->SetAreThisAndPrototypesEnsuredToHaveOnlyWritableDataProperties(false);
  2842. }
  2843. typesEnsuredToHaveOnlyWritableDataPropertiesInItAndPrototypeChain->ClearAndZero();
  2844. }
  2845. bool JavascriptLibrary::ArrayIteratorPrototypeHasUserDefinedNext(ScriptContext *scriptContext)
  2846. {
  2847. Var arrayIteratorPrototypeNext = nullptr;
  2848. ImplicitCallFlags flags = scriptContext->GetThreadContext()->TryWithDisabledImplicitCall(
  2849. [&]() { arrayIteratorPrototypeNext = JavascriptOperators::GetPropertyNoCache(scriptContext->GetLibrary()->GetArrayIteratorPrototype(), PropertyIds::next, scriptContext); });
  2850. return (flags != ImplicitCall_None) || arrayIteratorPrototypeNext != scriptContext->GetLibrary()->GetArrayIteratorPrototypeBuiltinNextFunction();
  2851. }
  2852. bool JavascriptLibrary::InitializeNumberConstructor(DynamicObject* numberConstructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  2853. {
  2854. typeHandler->Convert(numberConstructor, mode, 17);
  2855. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterNumber
  2856. // so that the update is in sync with profiler
  2857. ScriptContext* scriptContext = numberConstructor->GetScriptContext();
  2858. JavascriptLibrary* library = numberConstructor->GetLibrary();
  2859. library->AddMember(numberConstructor, PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable);
  2860. library->AddMember(numberConstructor, PropertyIds::prototype, library->numberPrototype, PropertyNone);
  2861. library->AddMember(numberConstructor, PropertyIds::name, scriptContext->GetPropertyString(PropertyIds::Number), PropertyConfigurable);
  2862. library->AddMember(numberConstructor, PropertyIds::MAX_VALUE, library->maxValue, PropertyNone);
  2863. library->AddMember(numberConstructor, PropertyIds::MIN_VALUE, library->minValue, PropertyNone);
  2864. library->AddMember(numberConstructor, PropertyIds::NaN, library->nan, PropertyNone);
  2865. library->AddMember(numberConstructor, PropertyIds::NEGATIVE_INFINITY, library->negativeInfinite, PropertyNone);
  2866. library->AddMember(numberConstructor, PropertyIds::POSITIVE_INFINITY, library->positiveInfinite, PropertyNone);
  2867. if (scriptContext->GetConfig()->IsES6NumberExtensionsEnabled())
  2868. {
  2869. #ifdef DBG
  2870. double epsilon = 0.0;
  2871. for (double next = 1.0; next + 1.0 != 1.0; next = next / 2.0)
  2872. {
  2873. epsilon = next;
  2874. }
  2875. Assert(epsilon == Math::EPSILON);
  2876. #endif
  2877. library->AddMember(numberConstructor, PropertyIds::EPSILON, JavascriptNumber::New(Math::EPSILON, scriptContext), PropertyNone);
  2878. library->AddMember(numberConstructor, PropertyIds::MAX_SAFE_INTEGER, JavascriptNumber::New(Math::MAX_SAFE_INTEGER, scriptContext), PropertyNone);
  2879. library->AddMember(numberConstructor, PropertyIds::MIN_SAFE_INTEGER, JavascriptNumber::New(Math::MIN_SAFE_INTEGER, scriptContext), PropertyNone);
  2880. AssertMsg(library->parseIntFunctionObject != nullptr, "Where is parseIntFunctionObject? Should have been initialized with Global object initialization");
  2881. AssertMsg(library->parseFloatFunctionObject != nullptr, "Where is parseIntFunctionObject? Should have been initialized with Global object initialization");
  2882. library->AddMember(numberConstructor, PropertyIds::parseInt, library->parseIntFunctionObject);
  2883. library->AddMember(numberConstructor, PropertyIds::parseFloat, library->parseFloatFunctionObject);
  2884. library->AddFunctionToLibraryObject(numberConstructor, PropertyIds::isNaN, &JavascriptNumber::EntryInfo::IsNaN, 1);
  2885. library->AddFunctionToLibraryObject(numberConstructor, PropertyIds::isFinite, &JavascriptNumber::EntryInfo::IsFinite, 1);
  2886. library->AddFunctionToLibraryObject(numberConstructor, PropertyIds::isInteger, &JavascriptNumber::EntryInfo::IsInteger, 1);
  2887. library->AddFunctionToLibraryObject(numberConstructor, PropertyIds::isSafeInteger, &JavascriptNumber::EntryInfo::IsSafeInteger, 1);
  2888. }
  2889. numberConstructor->SetHasNoEnumerableProperties(true);
  2890. return true;
  2891. }
  2892. bool JavascriptLibrary::InitializeNumberPrototype(DynamicObject* numberPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  2893. {
  2894. typeHandler->Convert(numberPrototype, mode, 8);
  2895. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterNumber
  2896. // so that the update is in sync with profiler
  2897. ScriptContext* scriptContext = numberPrototype->GetScriptContext();
  2898. JavascriptLibrary* library = numberPrototype->GetLibrary();
  2899. library->AddMember(numberPrototype, PropertyIds::constructor, library->numberConstructor);
  2900. scriptContext->SetBuiltInLibraryFunction(JavascriptNumber::EntryInfo::ToExponential.GetOriginalEntryPoint(),
  2901. library->AddFunctionToLibraryObject(numberPrototype, PropertyIds::toExponential, &JavascriptNumber::EntryInfo::ToExponential, 1));
  2902. scriptContext->SetBuiltInLibraryFunction(JavascriptNumber::EntryInfo::ToFixed.GetOriginalEntryPoint(),
  2903. library->AddFunctionToLibraryObject(numberPrototype, PropertyIds::toFixed, &JavascriptNumber::EntryInfo::ToFixed, 1));
  2904. scriptContext->SetBuiltInLibraryFunction(JavascriptNumber::EntryInfo::ToPrecision.GetOriginalEntryPoint(),
  2905. library->AddFunctionToLibraryObject(numberPrototype, PropertyIds::toPrecision, &JavascriptNumber::EntryInfo::ToPrecision, 1));
  2906. scriptContext->SetBuiltInLibraryFunction(JavascriptNumber::EntryInfo::ToLocaleString.GetOriginalEntryPoint(),
  2907. library->AddFunctionToLibraryObject(numberPrototype, PropertyIds::toLocaleString, &JavascriptNumber::EntryInfo::ToLocaleString, 0));
  2908. scriptContext->SetBuiltInLibraryFunction(JavascriptNumber::EntryInfo::ToString.GetOriginalEntryPoint(),
  2909. library->AddFunctionToLibraryObject(numberPrototype, PropertyIds::toString, &JavascriptNumber::EntryInfo::ToString, 1));
  2910. scriptContext->SetBuiltInLibraryFunction(JavascriptNumber::EntryInfo::ValueOf.GetOriginalEntryPoint(),
  2911. library->AddFunctionToLibraryObject(numberPrototype, PropertyIds::valueOf, &JavascriptNumber::EntryInfo::ValueOf, 0));
  2912. numberPrototype->SetHasNoEnumerableProperties(true);
  2913. return true;
  2914. }
  2915. bool JavascriptLibrary::InitializeObjectConstructor(DynamicObject* objectConstructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  2916. {
  2917. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterObject
  2918. // so that the update is in sync with profiler
  2919. JavascriptLibrary* library = objectConstructor->GetLibrary();
  2920. ScriptContext* scriptContext = objectConstructor->GetScriptContext();
  2921. int propertyCount = 18;
  2922. if (scriptContext->GetConfig()->IsES6ObjectExtensionsEnabled())
  2923. {
  2924. propertyCount += 2;
  2925. }
  2926. if (scriptContext->GetConfig()->IsESObjectGetOwnPropertyDescriptorsEnabled())
  2927. {
  2928. propertyCount++;
  2929. }
  2930. typeHandler->Convert(objectConstructor, mode, propertyCount);
  2931. library->AddMember(objectConstructor, PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable);
  2932. library->AddMember(objectConstructor, PropertyIds::prototype, library->objectPrototype, PropertyNone);
  2933. library->AddMember(objectConstructor, PropertyIds::name, scriptContext->GetPropertyString(PropertyIds::Object), PropertyConfigurable);
  2934. scriptContext->SetBuiltInLibraryFunction(JavascriptObject::EntryInfo::DefineProperty.GetOriginalEntryPoint(),
  2935. library->AddFunctionToLibraryObject(objectConstructor, PropertyIds::defineProperty, &JavascriptObject::EntryInfo::DefineProperty, 3));
  2936. scriptContext->SetBuiltInLibraryFunction(JavascriptObject::EntryInfo::GetOwnPropertyDescriptor.GetOriginalEntryPoint(),
  2937. library->AddFunctionToLibraryObject(objectConstructor, PropertyIds::getOwnPropertyDescriptor, &JavascriptObject::EntryInfo::GetOwnPropertyDescriptor, 2));
  2938. if (scriptContext->GetConfig()->IsESObjectGetOwnPropertyDescriptorsEnabled())
  2939. {
  2940. scriptContext->SetBuiltInLibraryFunction(JavascriptObject::EntryInfo::GetOwnPropertyDescriptors.GetOriginalEntryPoint(),
  2941. library->AddFunctionToLibraryObject(objectConstructor, PropertyIds::getOwnPropertyDescriptors, &JavascriptObject::EntryInfo::GetOwnPropertyDescriptors, 1));
  2942. }
  2943. scriptContext->SetBuiltInLibraryFunction(JavascriptObject::EntryInfo::DefineProperties.GetOriginalEntryPoint(),
  2944. library->AddFunctionToLibraryObject(objectConstructor, PropertyIds::defineProperties, &JavascriptObject::EntryInfo::DefineProperties, 2));
  2945. library->AddFunctionToLibraryObject(objectConstructor, PropertyIds::create, &JavascriptObject::EntryInfo::Create, 2);
  2946. scriptContext->SetBuiltInLibraryFunction(JavascriptObject::EntryInfo::Seal.GetOriginalEntryPoint(),
  2947. library->AddFunctionToLibraryObject(objectConstructor, PropertyIds::seal, &JavascriptObject::EntryInfo::Seal, 1));
  2948. library->AddMember(objectConstructor, PropertyIds::freeze, library->EnsureObjectFreezeFunction(), PropertyBuiltInMethodDefaults);
  2949. scriptContext->SetBuiltInLibraryFunction(JavascriptObject::EntryInfo::Freeze.GetOriginalEntryPoint(), library->EnsureObjectFreezeFunction());
  2950. scriptContext->SetBuiltInLibraryFunction(JavascriptObject::EntryInfo::PreventExtensions.GetOriginalEntryPoint(),
  2951. library->AddFunctionToLibraryObject(objectConstructor, PropertyIds::preventExtensions, &JavascriptObject::EntryInfo::PreventExtensions, 1));
  2952. scriptContext->SetBuiltInLibraryFunction(JavascriptObject::EntryInfo::IsSealed.GetOriginalEntryPoint(),
  2953. library->AddFunctionToLibraryObject(objectConstructor, PropertyIds::isSealed, &JavascriptObject::EntryInfo::IsSealed, 1));
  2954. scriptContext->SetBuiltInLibraryFunction(JavascriptObject::EntryInfo::IsFrozen.GetOriginalEntryPoint(),
  2955. library->AddFunctionToLibraryObject(objectConstructor, PropertyIds::isFrozen, &JavascriptObject::EntryInfo::IsFrozen, 1));
  2956. scriptContext->SetBuiltInLibraryFunction(JavascriptObject::EntryInfo::IsExtensible.GetOriginalEntryPoint(),
  2957. library->AddFunctionToLibraryObject(objectConstructor, PropertyIds::isExtensible, &JavascriptObject::EntryInfo::IsExtensible, 1));
  2958. scriptContext->SetBuiltInLibraryFunction(JavascriptObject::EntryInfo::GetPrototypeOf.GetOriginalEntryPoint(),
  2959. library->AddFunctionToLibraryObject(objectConstructor, PropertyIds::getPrototypeOf, &JavascriptObject::EntryInfo::GetPrototypeOf, 1));
  2960. scriptContext->SetBuiltInLibraryFunction(JavascriptObject::EntryInfo::Keys.GetOriginalEntryPoint(),
  2961. library->AddFunctionToLibraryObject(objectConstructor, PropertyIds::keys, &JavascriptObject::EntryInfo::Keys, 1));
  2962. scriptContext->SetBuiltInLibraryFunction(JavascriptObject::EntryInfo::GetOwnPropertyNames.GetOriginalEntryPoint(),
  2963. library->AddFunctionToLibraryObject(objectConstructor, PropertyIds::getOwnPropertyNames, &JavascriptObject::EntryInfo::GetOwnPropertyNames, 1));
  2964. scriptContext->SetBuiltInLibraryFunction(JavascriptObject::EntryInfo::SetPrototypeOf.GetOriginalEntryPoint(),
  2965. library->AddFunctionToLibraryObject(objectConstructor, PropertyIds::setPrototypeOf, &JavascriptObject::EntryInfo::SetPrototypeOf, 2));
  2966. scriptContext->SetBuiltInLibraryFunction(JavascriptObject::EntryInfo::GetOwnPropertySymbols.GetOriginalEntryPoint(),
  2967. library->AddFunctionToLibraryObject(objectConstructor, PropertyIds::getOwnPropertySymbols, &JavascriptObject::EntryInfo::GetOwnPropertySymbols, 1));
  2968. if (scriptContext->GetConfig()->IsES6ObjectExtensionsEnabled())
  2969. {
  2970. scriptContext->SetBuiltInLibraryFunction(JavascriptObject::EntryInfo::Is.GetOriginalEntryPoint(),
  2971. library->AddFunctionToLibraryObject(objectConstructor, PropertyIds::is, &JavascriptObject::EntryInfo::Is, 2));
  2972. scriptContext->SetBuiltInLibraryFunction(JavascriptObject::EntryInfo::Assign.GetOriginalEntryPoint(),
  2973. library->AddFunctionToLibraryObject(objectConstructor, PropertyIds::assign, &JavascriptObject::EntryInfo::Assign, 2));
  2974. }
  2975. if (scriptContext->GetConfig()->IsES7ValuesEntriesEnabled())
  2976. {
  2977. scriptContext->SetBuiltInLibraryFunction(JavascriptObject::EntryInfo::Values.GetOriginalEntryPoint(),
  2978. library->AddFunctionToLibraryObject(objectConstructor, PropertyIds::values, &JavascriptObject::EntryInfo::Values, 1));
  2979. scriptContext->SetBuiltInLibraryFunction(JavascriptObject::EntryInfo::Entries.GetOriginalEntryPoint(),
  2980. library->AddFunctionToLibraryObject(objectConstructor, PropertyIds::entries, &JavascriptObject::EntryInfo::Entries, 1));
  2981. }
  2982. objectConstructor->SetHasNoEnumerableProperties(true);
  2983. return true;
  2984. }
  2985. JavascriptFunction* JavascriptLibrary::EnsureObjectFreezeFunction()
  2986. {
  2987. if (objectFreezeFunction == nullptr)
  2988. {
  2989. objectFreezeFunction = DefaultCreateFunction(&JavascriptObject::EntryInfo::Freeze, 1, nullptr, nullptr, PropertyIds::freeze);
  2990. }
  2991. return objectFreezeFunction;
  2992. }
  2993. bool JavascriptLibrary::InitializeObjectPrototype(DynamicObject* objectPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  2994. {
  2995. JavascriptLibrary* library = objectPrototype->GetLibrary();
  2996. ScriptContext* scriptContext = objectPrototype->GetScriptContext();
  2997. Field(JavascriptFunction*)* builtinFuncs = library->GetBuiltinFunctions();
  2998. typeHandler->Convert(objectPrototype, mode, 11, true);
  2999. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterObject
  3000. // so that the update is in sync with profiler
  3001. library->AddMember(objectPrototype, PropertyIds::constructor, library->objectConstructor);
  3002. builtinFuncs[BuiltinFunction::JavascriptObject_HasOwnProperty] = library->AddFunctionToLibraryObject(objectPrototype, PropertyIds::hasOwnProperty, &JavascriptObject::EntryInfo::HasOwnProperty, 1);
  3003. library->AddFunctionToLibraryObject(objectPrototype, PropertyIds::propertyIsEnumerable, &JavascriptObject::EntryInfo::PropertyIsEnumerable, 1);
  3004. library->AddFunctionToLibraryObject(objectPrototype, PropertyIds::isPrototypeOf, &JavascriptObject::EntryInfo::IsPrototypeOf, 1);
  3005. library->AddFunctionToLibraryObject(objectPrototype, PropertyIds::toLocaleString, &JavascriptObject::EntryInfo::ToLocaleString, 0);
  3006. library->objectToStringFunction = library->AddFunctionToLibraryObject(objectPrototype, PropertyIds::toString, &JavascriptObject::EntryInfo::ToString, 0);
  3007. library->objectValueOfFunction = library->AddFunctionToLibraryObject(objectPrototype, PropertyIds::valueOf, &JavascriptObject::EntryInfo::ValueOf, 0);
  3008. scriptContext->SetBuiltInLibraryFunction(JavascriptObject::EntryInfo::ToString.GetOriginalEntryPoint(), library->objectToStringFunction);
  3009. bool hadOnlyWritableDataProperties = objectPrototype->GetDynamicType()->GetTypeHandler()->GetHasOnlyWritableDataProperties();
  3010. objectPrototype->SetAccessors(PropertyIds::__proto__, library->Get__proto__getterFunction(), library->Get__proto__setterFunction(), PropertyOperation_NonFixedValue);
  3011. objectPrototype->SetEnumerable(PropertyIds::__proto__, FALSE);
  3012. // Let's pretend __proto__ is actually writable. We'll make sure we always go through a special code path when writing to it.
  3013. if (hadOnlyWritableDataProperties)
  3014. {
  3015. objectPrototype->GetDynamicType()->GetTypeHandler()->SetHasOnlyWritableDataProperties();
  3016. }
  3017. library->AddFunctionToLibraryObject(objectPrototype, PropertyIds::__defineGetter__, &JavascriptObject::EntryInfo::DefineGetter, 2);
  3018. library->AddFunctionToLibraryObject(objectPrototype, PropertyIds::__defineSetter__, &JavascriptObject::EntryInfo::DefineSetter, 2);
  3019. library->AddFunctionToLibraryObject(objectPrototype, PropertyIds::__lookupGetter__, &JavascriptObject::EntryInfo::LookupGetter, 1);
  3020. library->AddFunctionToLibraryObject(objectPrototype, PropertyIds::__lookupSetter__, &JavascriptObject::EntryInfo::LookupSetter, 1);
  3021. objectPrototype->SetHasNoEnumerableProperties(true);
  3022. return true;
  3023. }
  3024. bool JavascriptLibrary::InitializeRegexConstructor(DynamicObject* regexConstructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  3025. {
  3026. JavascriptLibrary* library = regexConstructor->GetLibrary();
  3027. ScriptContext* scriptContext = regexConstructor->GetScriptContext();
  3028. typeHandler->Convert(regexConstructor, mode, 3);
  3029. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterRegExp
  3030. // so that the update is in sync with profiler
  3031. library->AddMember(regexConstructor, PropertyIds::length, TaggedInt::ToVarUnchecked(2), PropertyConfigurable);
  3032. library->AddMember(regexConstructor, PropertyIds::prototype, library->regexPrototype, PropertyNone);
  3033. library->AddSpeciesAccessorsToLibraryObject(regexConstructor, &JavascriptRegExp::EntryInfo::GetterSymbolSpecies);
  3034. library->AddMember(regexConstructor, PropertyIds::name, scriptContext->GetPropertyString(PropertyIds::RegExp), PropertyConfigurable);
  3035. regexConstructor->SetHasNoEnumerableProperties(true);
  3036. return true;
  3037. }
  3038. bool JavascriptLibrary::InitializeRegexPrototype(DynamicObject* regexPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  3039. {
  3040. typeHandler->Convert(regexPrototype, mode, 24);
  3041. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterRegExp
  3042. // so that the update is in sync with profiler
  3043. JavascriptFunction * func;
  3044. JavascriptLibrary* library = regexPrototype->GetLibrary();
  3045. Field(JavascriptFunction*)* builtinFuncs = library->GetBuiltinFunctions();
  3046. library->AddMember(regexPrototype, PropertyIds::constructor, library->regexConstructor);
  3047. library->regexConstructorSlotIndex = 0;
  3048. Assert(regexPrototype->GetSlot(library->regexConstructorSlotIndex) == library->regexConstructor);
  3049. func = library->AddFunctionToLibraryObject(regexPrototype, PropertyIds::exec, &JavascriptRegExp::EntryInfo::Exec, 1);
  3050. builtinFuncs[BuiltinFunction::JavascriptRegExp_Exec] = func;
  3051. library->regexExecFunction = func;
  3052. library->regexExecSlotIndex = 1;
  3053. Assert(regexPrototype->GetSlot(library->regexExecSlotIndex) == library->regexExecFunction);
  3054. library->AddFunctionToLibraryObject(regexPrototype, PropertyIds::test, &JavascriptRegExp::EntryInfo::Test, 1);
  3055. library->AddFunctionToLibraryObject(regexPrototype, PropertyIds::toString, &JavascriptRegExp::EntryInfo::ToString, 0);
  3056. // This is deprecated. Should be guarded with appropriate version flag.
  3057. library->AddFunctionToLibraryObject(regexPrototype, PropertyIds::compile, &JavascriptRegExp::EntryInfo::Compile, 2);
  3058. const ScriptConfiguration* scriptConfig = regexPrototype->GetScriptContext()->GetConfig();
  3059. if (scriptConfig->IsES6RegExPrototypePropertiesEnabled())
  3060. {
  3061. library->regexGlobalGetterFunction =
  3062. library->AddGetterToLibraryObject(regexPrototype, PropertyIds::global, &JavascriptRegExp::EntryInfo::GetterGlobal);
  3063. library->regexGlobalGetterSlotIndex = 5;
  3064. Assert(regexPrototype->GetSlot(library->regexGlobalGetterSlotIndex) == library->regexGlobalGetterFunction);
  3065. library->AddAccessorsToLibraryObject(regexPrototype, PropertyIds::ignoreCase, &JavascriptRegExp::EntryInfo::GetterIgnoreCase, nullptr);
  3066. library->AddAccessorsToLibraryObject(regexPrototype, PropertyIds::multiline, &JavascriptRegExp::EntryInfo::GetterMultiline, nullptr);
  3067. library->AddAccessorsToLibraryObject(regexPrototype, PropertyIds::options, &JavascriptRegExp::EntryInfo::GetterOptions, nullptr);
  3068. library->AddAccessorsToLibraryObject(regexPrototype, PropertyIds::source, &JavascriptRegExp::EntryInfo::GetterSource, nullptr);
  3069. library->regexFlagsGetterFunction =
  3070. library->AddGetterToLibraryObject(regexPrototype, PropertyIds::flags, &JavascriptRegExp::EntryInfo::GetterFlags);
  3071. library->regexFlagsGetterSlotIndex = 15;
  3072. Assert(regexPrototype->GetSlot(library->regexFlagsGetterSlotIndex) == library->regexFlagsGetterFunction);
  3073. if (scriptConfig->IsES6RegExStickyEnabled())
  3074. {
  3075. library->regexStickyGetterFunction =
  3076. library->AddGetterToLibraryObject(regexPrototype, PropertyIds::sticky, &JavascriptRegExp::EntryInfo::GetterSticky);
  3077. library->regexStickyGetterSlotIndex = 17;
  3078. Assert(regexPrototype->GetSlot(library->regexStickyGetterSlotIndex) == library->regexStickyGetterFunction);
  3079. }
  3080. if (scriptConfig->IsES6UnicodeExtensionsEnabled())
  3081. {
  3082. library->regexUnicodeGetterFunction =
  3083. library->AddGetterToLibraryObject(regexPrototype, PropertyIds::unicode, &JavascriptRegExp::EntryInfo::GetterUnicode);
  3084. library->regexUnicodeGetterSlotIndex = 19;
  3085. Assert(regexPrototype->GetSlot(library->regexUnicodeGetterSlotIndex) == library->regexUnicodeGetterFunction);
  3086. }
  3087. }
  3088. if (scriptConfig->IsES6RegExSymbolsEnabled())
  3089. {
  3090. library->AddFunctionToLibraryObjectWithName(
  3091. regexPrototype,
  3092. PropertyIds::_symbolMatch,
  3093. PropertyIds::_RuntimeFunctionNameId_match,
  3094. &JavascriptRegExp::EntryInfo::SymbolMatch,
  3095. 1);
  3096. library->AddFunctionToLibraryObjectWithName(
  3097. regexPrototype,
  3098. PropertyIds::_symbolReplace,
  3099. PropertyIds::_RuntimeFunctionNameId_replace,
  3100. &JavascriptRegExp::EntryInfo::SymbolReplace,
  3101. 2);
  3102. builtinFuncs[BuiltinFunction::JavascriptRegExp_SymbolSearch] = library->AddFunctionToLibraryObjectWithName(
  3103. regexPrototype,
  3104. PropertyIds::_symbolSearch,
  3105. PropertyIds::_RuntimeFunctionNameId_search,
  3106. &JavascriptRegExp::EntryInfo::SymbolSearch,
  3107. 1);
  3108. library->AddFunctionToLibraryObjectWithName(
  3109. regexPrototype,
  3110. PropertyIds::_symbolSplit,
  3111. PropertyIds::_RuntimeFunctionNameId_split,
  3112. &JavascriptRegExp::EntryInfo::SymbolSplit,
  3113. 2);
  3114. }
  3115. DebugOnly(CheckRegisteredBuiltIns(builtinFuncs, library->GetScriptContext()));
  3116. regexPrototype->SetHasNoEnumerableProperties(true);
  3117. library->regexPrototypeType = regexPrototype->GetDynamicType();
  3118. return true;
  3119. }
  3120. bool JavascriptLibrary::InitializeStringConstructor(DynamicObject* stringConstructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  3121. {
  3122. typeHandler->Convert(stringConstructor, mode, 6);
  3123. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterString
  3124. // so that the update is in sync with profiler
  3125. JavascriptLibrary* library = stringConstructor->GetLibrary();
  3126. ScriptContext* scriptContext = stringConstructor->GetScriptContext();
  3127. Field(JavascriptFunction*)* builtinFuncs = library->GetBuiltinFunctions();
  3128. library->AddMember(stringConstructor, PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable);
  3129. library->AddMember(stringConstructor, PropertyIds::prototype, library->stringPrototype, PropertyNone);
  3130. library->AddMember(stringConstructor, PropertyIds::name, scriptContext->GetPropertyString(PropertyIds::String), PropertyConfigurable);
  3131. builtinFuncs[BuiltinFunction::JavascriptString_FromCharCode] = library->AddFunctionToLibraryObject(stringConstructor, PropertyIds::fromCharCode, &JavascriptString::EntryInfo::FromCharCode, 1);
  3132. if(scriptContext->GetConfig()->IsES6UnicodeExtensionsEnabled())
  3133. {
  3134. builtinFuncs[BuiltinFunction::JavascriptString_FromCodePoint] = library->AddFunctionToLibraryObject(stringConstructor, PropertyIds::fromCodePoint, &JavascriptString::EntryInfo::FromCodePoint, 1);
  3135. }
  3136. /* No inlining String_Raw */ library->AddFunctionToLibraryObject(stringConstructor, PropertyIds::raw, &JavascriptString::EntryInfo::Raw, 1);
  3137. DebugOnly(CheckRegisteredBuiltIns(builtinFuncs, scriptContext));
  3138. stringConstructor->SetHasNoEnumerableProperties(true);
  3139. return true;
  3140. }
  3141. bool JavascriptLibrary::InitializeStringPrototype(DynamicObject* stringPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  3142. {
  3143. typeHandler->Convert(stringPrototype, mode, 38);
  3144. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterString
  3145. // so that the update is in sync with profiler
  3146. ScriptContext* scriptContext = stringPrototype->GetScriptContext();
  3147. JavascriptLibrary* library = stringPrototype->GetLibrary();
  3148. Field(JavascriptFunction*)* builtinFuncs = library->GetBuiltinFunctions();
  3149. library->AddMember(stringPrototype, PropertyIds::constructor, library->stringConstructor);
  3150. builtinFuncs[BuiltinFunction::JavascriptString_IndexOf] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::indexOf, &JavascriptString::EntryInfo::IndexOf, 1);
  3151. builtinFuncs[BuiltinFunction::JavascriptString_LastIndexOf] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::lastIndexOf, &JavascriptString::EntryInfo::LastIndexOf, 1);
  3152. builtinFuncs[BuiltinFunction::JavascriptString_Replace] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::replace, &JavascriptString::EntryInfo::Replace, 2);
  3153. builtinFuncs[BuiltinFunction::JavascriptString_Search] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::search, &JavascriptString::EntryInfo::Search, 1);
  3154. builtinFuncs[BuiltinFunction::JavascriptString_Slice] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::slice, &JavascriptString::EntryInfo::Slice, 2);
  3155. if (CONFIG_FLAG(ES6Unicode))
  3156. {
  3157. builtinFuncs[BuiltinFunction::JavascriptString_CodePointAt] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::codePointAt, &JavascriptString::EntryInfo::CodePointAt, 1);
  3158. /* builtinFuncs[BuiltinFunction::String_Normalize] =*/library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::normalize, &JavascriptString::EntryInfo::Normalize, 0);
  3159. }
  3160. builtinFuncs[BuiltinFunction::JavascriptString_CharAt] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::charAt, &JavascriptString::EntryInfo::CharAt, 1);
  3161. builtinFuncs[BuiltinFunction::JavascriptString_CharCodeAt] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::charCodeAt, &JavascriptString::EntryInfo::CharCodeAt, 1);
  3162. builtinFuncs[BuiltinFunction::JavascriptString_Concat] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::concat, &JavascriptString::EntryInfo::Concat, 1);
  3163. builtinFuncs[BuiltinFunction::JavascriptString_LocaleCompare] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::localeCompare, &JavascriptString::EntryInfo::LocaleCompare, 1);
  3164. builtinFuncs[BuiltinFunction::JavascriptString_Match] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::match, &JavascriptString::EntryInfo::Match, 1);
  3165. builtinFuncs[BuiltinFunction::JavascriptString_Split] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::split, &JavascriptString::EntryInfo::Split, 2);
  3166. builtinFuncs[BuiltinFunction::JavascriptString_Substring] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::substring, &JavascriptString::EntryInfo::Substring, 2);
  3167. builtinFuncs[BuiltinFunction::JavascriptString_Substr] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::substr, &JavascriptString::EntryInfo::Substr, 2);
  3168. builtinFuncs[BuiltinFunction::JavascriptString_ToLocaleLowerCase] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::toLocaleLowerCase, &JavascriptString::EntryInfo::ToLocaleLowerCase, 0);
  3169. builtinFuncs[BuiltinFunction::JavascriptString_ToLocaleUpperCase] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::toLocaleUpperCase, &JavascriptString::EntryInfo::ToLocaleUpperCase, 0);
  3170. builtinFuncs[BuiltinFunction::JavascriptString_ToLowerCase] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::toLowerCase, &JavascriptString::EntryInfo::ToLowerCase, 0);
  3171. scriptContext->SetBuiltInLibraryFunction(JavascriptString::EntryInfo::ToString.GetOriginalEntryPoint(),
  3172. library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::toString, &JavascriptString::EntryInfo::ToString, 0));
  3173. builtinFuncs[BuiltinFunction::JavascriptString_ToUpperCase] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::toUpperCase, &JavascriptString::EntryInfo::ToUpperCase, 0);
  3174. builtinFuncs[BuiltinFunction::JavascriptString_Trim] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::trim, &JavascriptString::EntryInfo::Trim, 0);
  3175. scriptContext->SetBuiltInLibraryFunction(JavascriptString::EntryInfo::ValueOf.GetOriginalEntryPoint(),
  3176. library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::valueOf, &JavascriptString::EntryInfo::ValueOf, 0));
  3177. /* No inlining String_Anchor */ library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::anchor, &JavascriptString::EntryInfo::Anchor, 1);
  3178. /* No inlining String_Big */ library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::big, &JavascriptString::EntryInfo::Big, 0);
  3179. /* No inlining String_Blink */ library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::blink, &JavascriptString::EntryInfo::Blink, 0);
  3180. /* No inlining String_Bold */ library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::bold, &JavascriptString::EntryInfo::Bold, 0);
  3181. /* No inlining String_Fixed */ library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::fixed, &JavascriptString::EntryInfo::Fixed, 0);
  3182. /* No inlining String_FontColor */ library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::fontcolor, &JavascriptString::EntryInfo::FontColor, 1);
  3183. /* No inlining String_FontSize */ library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::fontsize, &JavascriptString::EntryInfo::FontSize, 1);
  3184. /* No inlining String_Italics */ library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::italics, &JavascriptString::EntryInfo::Italics, 0);
  3185. builtinFuncs[BuiltinFunction::JavascriptString_Link] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::link, &JavascriptString::EntryInfo::Link, 1);
  3186. /* No inlining String_Small */ library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::Small, &JavascriptString::EntryInfo::Small, 0);
  3187. /* No inlining String_Strike */ library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::strike, &JavascriptString::EntryInfo::Strike, 0);
  3188. /* No inlining String_Sub */ library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::sub, &JavascriptString::EntryInfo::Sub, 0);
  3189. /* No inlining String_Sup */ library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::sup, &JavascriptString::EntryInfo::Sup, 0);
  3190. if (scriptContext->GetConfig()->IsES6StringExtensionsEnabled())
  3191. {
  3192. /* No inlining String_Repeat */ library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::repeat, &JavascriptString::EntryInfo::Repeat, 1);
  3193. /* No inlining String_StartsWith */ library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::startsWith, &JavascriptString::EntryInfo::StartsWith, 1);
  3194. /* No inlining String_EndsWith */ library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::endsWith, &JavascriptString::EntryInfo::EndsWith, 1);
  3195. /* No inlining String_Includes */ library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::includes, &JavascriptString::EntryInfo::Includes, 1);
  3196. builtinFuncs[BuiltinFunction::JavascriptString_TrimLeft] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::trimLeft, &JavascriptString::EntryInfo::TrimLeft, 0);
  3197. builtinFuncs[BuiltinFunction::JavascriptString_TrimRight] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::trimRight, &JavascriptString::EntryInfo::TrimRight, 0);
  3198. }
  3199. library->AddFunctionToLibraryObjectWithName(stringPrototype, PropertyIds::_symbolIterator, PropertyIds::_RuntimeFunctionNameId_iterator, &JavascriptString::EntryInfo::SymbolIterator, 0);
  3200. builtinFuncs[BuiltinFunction::JavascriptString_PadStart] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::padStart, &JavascriptString::EntryInfo::PadStart, 1);
  3201. builtinFuncs[BuiltinFunction::JavascriptString_PadEnd] = library->AddFunctionToLibraryObject(stringPrototype, PropertyIds::padEnd, &JavascriptString::EntryInfo::PadEnd, 1);
  3202. DebugOnly(CheckRegisteredBuiltIns(builtinFuncs, scriptContext));
  3203. stringPrototype->SetHasNoEnumerableProperties(true);
  3204. return true;
  3205. }
  3206. bool JavascriptLibrary::InitializeMapConstructor(DynamicObject* mapConstructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  3207. {
  3208. typeHandler->Convert(mapConstructor, mode, 3);
  3209. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterMap
  3210. // so that the update is in sync with profiler
  3211. JavascriptLibrary* library = mapConstructor->GetLibrary();
  3212. ScriptContext* scriptContext = mapConstructor->GetScriptContext();
  3213. library->AddMember(mapConstructor, PropertyIds::length, TaggedInt::ToVarUnchecked(0), PropertyConfigurable);
  3214. library->AddMember(mapConstructor, PropertyIds::prototype, library->mapPrototype, PropertyNone);
  3215. library->AddSpeciesAccessorsToLibraryObject(mapConstructor, &JavascriptMap::EntryInfo::GetterSymbolSpecies);
  3216. library->AddMember(mapConstructor, PropertyIds::name, scriptContext->GetPropertyString(PropertyIds::Map), PropertyConfigurable);
  3217. mapConstructor->SetHasNoEnumerableProperties(true);
  3218. return true;
  3219. }
  3220. bool JavascriptLibrary::InitializeMapPrototype(DynamicObject* mapPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  3221. {
  3222. typeHandler->Convert(mapPrototype, mode, 13, true);
  3223. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterMap
  3224. // so that the update is in sync with profiler
  3225. ScriptContext* scriptContext = mapPrototype->GetScriptContext();
  3226. JavascriptLibrary* library = mapPrototype->GetLibrary();
  3227. library->AddMember(mapPrototype, PropertyIds::constructor, library->mapConstructor);
  3228. library->AddFunctionToLibraryObject(mapPrototype, PropertyIds::clear, &JavascriptMap::EntryInfo::Clear, 0);
  3229. library->AddFunctionToLibraryObject(mapPrototype, PropertyIds::delete_, &JavascriptMap::EntryInfo::Delete, 1);
  3230. library->AddFunctionToLibraryObject(mapPrototype, PropertyIds::forEach, &JavascriptMap::EntryInfo::ForEach, 1);
  3231. library->AddFunctionToLibraryObject(mapPrototype, PropertyIds::get, &JavascriptMap::EntryInfo::Get, 1);
  3232. library->AddFunctionToLibraryObject(mapPrototype, PropertyIds::has, &JavascriptMap::EntryInfo::Has, 1);
  3233. library->AddFunctionToLibraryObject(mapPrototype, PropertyIds::set, &JavascriptMap::EntryInfo::Set, 2);
  3234. library->AddAccessorsToLibraryObject(mapPrototype, PropertyIds::size, &JavascriptMap::EntryInfo::SizeGetter, nullptr);
  3235. JavascriptFunction* entriesFunc;
  3236. entriesFunc = library->AddFunctionToLibraryObject(mapPrototype, PropertyIds::entries, &JavascriptMap::EntryInfo::Entries, 0);
  3237. library->AddFunctionToLibraryObject(mapPrototype, PropertyIds::keys, &JavascriptMap::EntryInfo::Keys, 0);
  3238. library->AddFunctionToLibraryObject(mapPrototype, PropertyIds::values, &JavascriptMap::EntryInfo::Values, 0);
  3239. library->AddMember(mapPrototype, PropertyIds::_symbolIterator, entriesFunc);
  3240. if (scriptContext->GetConfig()->IsES6ToStringTagEnabled())
  3241. {
  3242. library->AddMember(mapPrototype, PropertyIds::_symbolToStringTag, library->CreateStringFromCppLiteral(_u("Map")), PropertyConfigurable);
  3243. }
  3244. mapPrototype->SetHasNoEnumerableProperties(true);
  3245. return true;
  3246. }
  3247. bool JavascriptLibrary::InitializeSetConstructor(DynamicObject* setConstructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  3248. {
  3249. typeHandler->Convert(setConstructor, mode, 3);
  3250. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterSet
  3251. // so that the update is in sync with profiler
  3252. JavascriptLibrary* library = setConstructor->GetLibrary();
  3253. ScriptContext* scriptContext = setConstructor->GetScriptContext();
  3254. library->AddMember(setConstructor, PropertyIds::length, TaggedInt::ToVarUnchecked(0), PropertyConfigurable);
  3255. library->AddMember(setConstructor, PropertyIds::prototype, library->setPrototype, PropertyNone);
  3256. library->AddSpeciesAccessorsToLibraryObject(setConstructor, &JavascriptSet::EntryInfo::GetterSymbolSpecies);
  3257. library->AddMember(setConstructor, PropertyIds::name, scriptContext->GetPropertyString(PropertyIds::Set), PropertyConfigurable);
  3258. setConstructor->SetHasNoEnumerableProperties(true);
  3259. return true;
  3260. }
  3261. bool JavascriptLibrary::InitializeSetPrototype(DynamicObject* setPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  3262. {
  3263. typeHandler->Convert(setPrototype, mode, 12, true);
  3264. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterSet
  3265. // so that the update is in sync with profiler
  3266. ScriptContext* scriptContext = setPrototype->GetScriptContext();
  3267. JavascriptLibrary* library = setPrototype->GetLibrary();
  3268. library->AddMember(setPrototype, PropertyIds::constructor, library->setConstructor);
  3269. library->AddFunctionToLibraryObject(setPrototype, PropertyIds::add, &JavascriptSet::EntryInfo::Add, 1);
  3270. library->AddFunctionToLibraryObject(setPrototype, PropertyIds::clear, &JavascriptSet::EntryInfo::Clear, 0);
  3271. library->AddFunctionToLibraryObject(setPrototype, PropertyIds::delete_, &JavascriptSet::EntryInfo::Delete, 1);
  3272. library->AddFunctionToLibraryObject(setPrototype, PropertyIds::forEach, &JavascriptSet::EntryInfo::ForEach, 1);
  3273. library->AddFunctionToLibraryObject(setPrototype, PropertyIds::has, &JavascriptSet::EntryInfo::Has, 1);
  3274. library->AddAccessorsToLibraryObject(setPrototype, PropertyIds::size, &JavascriptSet::EntryInfo::SizeGetter, nullptr);
  3275. JavascriptFunction* valuesFunc;
  3276. library->AddFunctionToLibraryObject(setPrototype, PropertyIds::entries, &JavascriptSet::EntryInfo::Entries, 0);
  3277. valuesFunc = library->AddFunctionToLibraryObject(setPrototype, PropertyIds::values, &JavascriptSet::EntryInfo::Values, 0);
  3278. library->AddMember(setPrototype, PropertyIds::keys, valuesFunc);
  3279. library->AddMember(setPrototype, PropertyIds::_symbolIterator, valuesFunc);
  3280. if (scriptContext->GetConfig()->IsES6ToStringTagEnabled())
  3281. {
  3282. library->AddMember(setPrototype, PropertyIds::_symbolToStringTag, library->CreateStringFromCppLiteral(_u("Set")), PropertyConfigurable);
  3283. }
  3284. setPrototype->SetHasNoEnumerableProperties(true);
  3285. return true;
  3286. }
  3287. bool JavascriptLibrary::InitializeWeakMapConstructor(DynamicObject* weakMapConstructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  3288. {
  3289. typeHandler->Convert(weakMapConstructor, mode, 3);
  3290. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterWeakMap
  3291. // so that the update is in sync with profiler
  3292. JavascriptLibrary* library = weakMapConstructor->GetLibrary();
  3293. ScriptContext* scriptContext = weakMapConstructor->GetScriptContext();
  3294. library->AddMember(weakMapConstructor, PropertyIds::length, TaggedInt::ToVarUnchecked(0), PropertyConfigurable);
  3295. library->AddMember(weakMapConstructor, PropertyIds::prototype, library->weakMapPrototype, PropertyNone);
  3296. library->AddMember(weakMapConstructor, PropertyIds::name, scriptContext->GetPropertyString(PropertyIds::WeakMap), PropertyConfigurable);
  3297. weakMapConstructor->SetHasNoEnumerableProperties(true);
  3298. return true;
  3299. }
  3300. bool JavascriptLibrary::InitializeWeakMapPrototype(DynamicObject* weakMapPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  3301. {
  3302. typeHandler->Convert(weakMapPrototype, mode, 6);
  3303. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterWeakMap
  3304. // so that the update is in sync with profiler
  3305. ScriptContext* scriptContext = weakMapPrototype->GetScriptContext();
  3306. JavascriptLibrary* library = weakMapPrototype->GetLibrary();
  3307. library->AddMember(weakMapPrototype, PropertyIds::constructor, library->weakMapConstructor);
  3308. library->AddFunctionToLibraryObject(weakMapPrototype, PropertyIds::delete_, &JavascriptWeakMap::EntryInfo::Delete, 1);
  3309. library->AddFunctionToLibraryObject(weakMapPrototype, PropertyIds::get, &JavascriptWeakMap::EntryInfo::Get, 1);
  3310. library->AddFunctionToLibraryObject(weakMapPrototype, PropertyIds::has, &JavascriptWeakMap::EntryInfo::Has, 1);
  3311. library->AddFunctionToLibraryObject(weakMapPrototype, PropertyIds::set, &JavascriptWeakMap::EntryInfo::Set, 2);
  3312. if (scriptContext->GetConfig()->IsES6ToStringTagEnabled())
  3313. {
  3314. library->AddMember(weakMapPrototype, PropertyIds::_symbolToStringTag, library->CreateStringFromCppLiteral(_u("WeakMap")), PropertyConfigurable);
  3315. }
  3316. weakMapPrototype->SetHasNoEnumerableProperties(true);
  3317. return true;
  3318. }
  3319. bool JavascriptLibrary::InitializeWeakSetConstructor(DynamicObject* weakSetConstructor, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  3320. {
  3321. typeHandler->Convert(weakSetConstructor, mode, 3);
  3322. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterWeakSet
  3323. // so that the update is in sync with profiler
  3324. JavascriptLibrary* library = weakSetConstructor->GetLibrary();
  3325. ScriptContext* scriptContext = weakSetConstructor->GetScriptContext();
  3326. library->AddMember(weakSetConstructor, PropertyIds::length, TaggedInt::ToVarUnchecked(0), PropertyConfigurable);
  3327. library->AddMember(weakSetConstructor, PropertyIds::prototype, library->weakSetPrototype, PropertyNone);
  3328. library->AddMember(weakSetConstructor, PropertyIds::name, scriptContext->GetPropertyString(PropertyIds::WeakSet), PropertyConfigurable);
  3329. weakSetConstructor->SetHasNoEnumerableProperties(true);
  3330. return true;
  3331. }
  3332. bool JavascriptLibrary::InitializeWeakSetPrototype(DynamicObject* weakSetPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  3333. {
  3334. typeHandler->Convert(weakSetPrototype, mode, 5);
  3335. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterWeakSet
  3336. // so that the update is in sync with profiler
  3337. ScriptContext* scriptContext = weakSetPrototype->GetScriptContext();
  3338. JavascriptLibrary* library = weakSetPrototype->GetLibrary();
  3339. library->AddMember(weakSetPrototype, PropertyIds::constructor, library->weakSetConstructor);
  3340. library->AddFunctionToLibraryObject(weakSetPrototype, PropertyIds::add, &JavascriptWeakSet::EntryInfo::Add, 1);
  3341. library->AddFunctionToLibraryObject(weakSetPrototype, PropertyIds::delete_, &JavascriptWeakSet::EntryInfo::Delete, 1);
  3342. library->AddFunctionToLibraryObject(weakSetPrototype, PropertyIds::has, &JavascriptWeakSet::EntryInfo::Has, 1);
  3343. if (scriptContext->GetConfig()->IsES6ToStringTagEnabled())
  3344. {
  3345. library->AddMember(weakSetPrototype, PropertyIds::_symbolToStringTag, library->CreateStringFromCppLiteral(_u("WeakSet")), PropertyConfigurable);
  3346. }
  3347. weakSetPrototype->SetHasNoEnumerableProperties(true);
  3348. return true;
  3349. }
  3350. bool JavascriptLibrary::InitializeIteratorPrototype(DynamicObject* iteratorPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  3351. {
  3352. typeHandler->Convert(iteratorPrototype, mode, 1);
  3353. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterIterator
  3354. // so that the update is in sync with profiler
  3355. JavascriptLibrary* library = iteratorPrototype->GetLibrary();
  3356. library->AddFunctionToLibraryObjectWithName(iteratorPrototype, PropertyIds::_symbolIterator, PropertyIds::_RuntimeFunctionNameId_iterator,
  3357. &JavascriptIterator::EntryInfo::SymbolIterator, 0);
  3358. return true;
  3359. }
  3360. bool JavascriptLibrary::InitializeArrayIteratorPrototype(DynamicObject* arrayIteratorPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  3361. {
  3362. typeHandler->Convert(arrayIteratorPrototype, mode, 2);
  3363. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterArrayIterator
  3364. // so that the update is in sync with profiler
  3365. JavascriptLibrary* library = arrayIteratorPrototype->GetLibrary();
  3366. ScriptContext* scriptContext = library->GetScriptContext();
  3367. #ifdef ENABLE_JS_BUILTINS
  3368. Assert(!scriptContext->IsJsBuiltInEnabled());
  3369. #endif
  3370. library->arrayIteratorPrototypeBuiltinNextFunction = library->AddFunctionToLibraryObject(arrayIteratorPrototype, PropertyIds::next, &JavascriptArrayIterator::EntryInfo::Next, 0);
  3371. if (scriptContext->GetConfig()->IsES6ToStringTagEnabled())
  3372. {
  3373. library->AddMember(arrayIteratorPrototype, PropertyIds::_symbolToStringTag, library->CreateStringFromCppLiteral(_u("Array Iterator")), PropertyConfigurable);
  3374. }
  3375. return true;
  3376. }
  3377. bool JavascriptLibrary::InitializeMapIteratorPrototype(DynamicObject* mapIteratorPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  3378. {
  3379. typeHandler->Convert(mapIteratorPrototype, mode, 2);
  3380. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterMapIterator
  3381. // so that the update is in sync with profiler
  3382. JavascriptLibrary* library = mapIteratorPrototype->GetLibrary();
  3383. ScriptContext* scriptContext = library->GetScriptContext();
  3384. library->AddFunctionToLibraryObject(mapIteratorPrototype, PropertyIds::next, &JavascriptMapIterator::EntryInfo::Next, 0);
  3385. if (scriptContext->GetConfig()->IsES6ToStringTagEnabled())
  3386. {
  3387. library->AddMember(mapIteratorPrototype, PropertyIds::_symbolToStringTag, library->CreateStringFromCppLiteral(_u("Map Iterator")), PropertyConfigurable);
  3388. }
  3389. return true;
  3390. }
  3391. bool JavascriptLibrary::InitializeSetIteratorPrototype(DynamicObject* setIteratorPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  3392. {
  3393. typeHandler->Convert(setIteratorPrototype, mode, 2);
  3394. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterSetIterator
  3395. // so that the update is in sync with profiler
  3396. JavascriptLibrary* library = setIteratorPrototype->GetLibrary();
  3397. ScriptContext* scriptContext = library->GetScriptContext();
  3398. library->AddFunctionToLibraryObject(setIteratorPrototype, PropertyIds::next, &JavascriptSetIterator::EntryInfo::Next, 0);
  3399. if (scriptContext->GetConfig()->IsES6ToStringTagEnabled())
  3400. {
  3401. library->AddMember(setIteratorPrototype, PropertyIds::_symbolToStringTag, library->CreateStringFromCppLiteral(_u("Set Iterator")), PropertyConfigurable);
  3402. }
  3403. return true;
  3404. }
  3405. bool JavascriptLibrary::InitializeStringIteratorPrototype(DynamicObject* stringIteratorPrototype, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  3406. {
  3407. typeHandler->Convert(stringIteratorPrototype, mode, 2);
  3408. // Note: Any new function addition/deletion/modification should also be updated in JavascriptLibrary::ProfilerRegisterStringIterator
  3409. // so that the update is in sync with profiler
  3410. JavascriptLibrary* library = stringIteratorPrototype->GetLibrary();
  3411. ScriptContext* scriptContext = library->GetScriptContext();
  3412. library->AddFunctionToLibraryObject(stringIteratorPrototype, PropertyIds::next, &JavascriptStringIterator::EntryInfo::Next, 0);
  3413. if (scriptContext->GetConfig()->IsES6ToStringTagEnabled())
  3414. {
  3415. library->AddMember(stringIteratorPrototype, PropertyIds::_symbolToStringTag, library->CreateStringFromCppLiteral(_u("String Iterator")), PropertyConfigurable);
  3416. }
  3417. return true;
  3418. }
  3419. RuntimeFunction* JavascriptLibrary::CreateBuiltinConstructor(FunctionInfo * functionInfo, DynamicTypeHandler * typeHandler, DynamicObject* prototype)
  3420. {
  3421. Assert((functionInfo->GetAttributes() & FunctionInfo::Attributes::ErrorOnNew) == 0);
  3422. if (prototype == nullptr)
  3423. {
  3424. prototype = functionPrototype;
  3425. }
  3426. ConstructorCache* ctorCache = ((functionInfo->GetAttributes() & FunctionInfo::Attributes::SkipDefaultNewObject) != 0) ?
  3427. static_cast<ConstructorCache*>(this->builtInConstructorCache) : &ConstructorCache::DefaultInstance;
  3428. DynamicType* type = DynamicType::New(scriptContext, TypeIds_Function, prototype, functionInfo->GetOriginalEntryPoint(), typeHandler);
  3429. return RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, RuntimeFunction, type, functionInfo, ctorCache);
  3430. }
  3431. JavascriptExternalFunction* JavascriptLibrary::CreateExternalConstructor(Js::ExternalMethod entryPoint, PropertyId nameId, RecyclableObject * prototype)
  3432. {
  3433. Assert(nameId >= Js::InternalPropertyIds::Count && scriptContext->IsTrackedPropertyId(nameId));
  3434. JavascriptExternalFunction* function = this->CreateIdMappedExternalFunction(entryPoint, idMappedFunctionWithPrototypeType);
  3435. function->SetFunctionNameId(TaggedInt::ToVarUnchecked(nameId));
  3436. Js::RecyclableObject* objPrototype;
  3437. if (prototype == nullptr)
  3438. {
  3439. objPrototype = CreateConstructorPrototypeObject(function);
  3440. Assert(!objPrototype->IsEnumerable(Js::PropertyIds::constructor));
  3441. }
  3442. else
  3443. {
  3444. objPrototype = Js::RecyclableObject::FromVar(prototype);
  3445. Js::JavascriptOperators::InitProperty(objPrototype, Js::PropertyIds::constructor, function);
  3446. objPrototype->SetEnumerable(Js::PropertyIds::constructor, false);
  3447. }
  3448. Assert(!function->IsEnumerable(Js::PropertyIds::prototype));
  3449. Assert(!function->IsConfigurable(Js::PropertyIds::prototype));
  3450. Assert(!function->IsWritable(Js::PropertyIds::prototype));
  3451. function->SetPropertyWithAttributes(Js::PropertyIds::prototype, objPrototype, PropertyNone, nullptr);
  3452. JavascriptString * functionName = nullptr;
  3453. DebugOnly(bool status =) function->GetFunctionName(&functionName);
  3454. AssertMsg(status,"CreateExternalConstructor sets the functionNameId, status should always be true");
  3455. function->SetPropertyWithAttributes(PropertyIds::name, functionName, PropertyConfigurable, nullptr);
  3456. return function;
  3457. }
  3458. JavascriptExternalFunction* JavascriptLibrary::CreateExternalConstructor(Js::ExternalMethod entryPoint, PropertyId nameId, InitializeMethod method, unsigned short deferredTypeSlots, bool hasAccessors)
  3459. {
  3460. Assert(nameId >= Js::InternalPropertyIds::Count && scriptContext->IsTrackedPropertyId(nameId));
  3461. JavascriptExternalFunction* function = nullptr;
  3462. if (entryPoint != nullptr)
  3463. {
  3464. function = RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, JavascriptExternalFunction, entryPoint,
  3465. externalConstructorFunctionWithDeferredPrototypeType, method, deferredTypeSlots, hasAccessors);
  3466. }
  3467. else
  3468. {
  3469. function = RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, JavascriptExternalFunction,
  3470. defaultExternalConstructorFunctionWithDeferredPrototypeType, method, deferredTypeSlots, hasAccessors);
  3471. }
  3472. function->SetFunctionNameId(TaggedInt::ToVarUnchecked(nameId));
  3473. return function;
  3474. }
  3475. DynamicType* JavascriptLibrary::GetCachedJsrtExternalType(uintptr_t finalizeCallback)
  3476. {
  3477. RecyclerWeakReference<DynamicType>* dynamicTypeWeakRef = nullptr;
  3478. DynamicType* dynamicType = nullptr;
  3479. if (jsrtExternalTypesCache == nullptr)
  3480. {
  3481. jsrtExternalTypesCache = RecyclerNew(recycler, JsrtExternalTypesCache, recycler, 3);
  3482. // Register for periodic cleanup
  3483. scriptContext->RegisterWeakReferenceDictionary(jsrtExternalTypesCache);
  3484. }
  3485. if (jsrtExternalTypesCache->TryGetValue(finalizeCallback, &dynamicTypeWeakRef))
  3486. {
  3487. dynamicType = dynamicTypeWeakRef->Get();
  3488. }
  3489. return dynamicType;
  3490. }
  3491. void JavascriptLibrary::CacheJsrtExternalType(uintptr_t finalizeCallback, DynamicType* dynamicTypeToCache)
  3492. {
  3493. jsrtExternalTypesCache->Item(finalizeCallback, recycler->CreateWeakReferenceHandle<DynamicType>(dynamicTypeToCache));
  3494. }
  3495. void JavascriptLibrary::DefaultCreateFunction(ParseableFunctionInfo * functionInfo, int length, DynamicObject * prototype, PropertyId nameId)
  3496. {
  3497. Assert(nameId >= Js::InternalPropertyIds::Count && scriptContext->IsTrackedPropertyId(nameId));
  3498. ScriptFunction* function = scriptContext->GetLibrary()->CreateScriptFunction(functionInfo);
  3499. function->SetPropertyWithAttributes(PropertyIds::length, TaggedInt::ToVarUnchecked(length), PropertyConfigurable, nullptr);
  3500. AddMember(prototype, nameId, function);
  3501. }
  3502. RuntimeFunction* JavascriptLibrary::DefaultCreateFunction(FunctionInfo * functionInfo, int length, DynamicObject * prototype, DynamicType * functionType, PropertyId nameId)
  3503. {
  3504. Assert(nameId >= Js::InternalPropertyIds::Count && scriptContext->IsTrackedPropertyId(nameId));
  3505. return DefaultCreateFunction(functionInfo, length, prototype, functionType, TaggedInt::ToVarUnchecked((int)nameId));
  3506. }
  3507. RuntimeFunction* JavascriptLibrary::DefaultCreateFunction(FunctionInfo * functionInfo, int length, DynamicObject * prototype, DynamicType * functionType, Var nameId)
  3508. {
  3509. Assert(nameId != nullptr);
  3510. RuntimeFunction * function;
  3511. if (nullptr == functionType)
  3512. {
  3513. functionType = (nullptr == prototype) ?
  3514. CreateFunctionWithLengthAndNameType(functionInfo) :
  3515. CreateFunctionWithLengthAndPrototypeType(functionInfo);
  3516. }
  3517. function = RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, RuntimeFunction, functionType, functionInfo);
  3518. if (prototype != nullptr)
  3519. {
  3520. // NOTE: Assume all built in function prototype doesn't contain valueOf or toString that has side effects
  3521. function->SetPropertyWithAttributes(PropertyIds::prototype, prototype, PropertyNone, nullptr, PropertyOperation_None, SideEffects_None);
  3522. }
  3523. function->SetPropertyWithAttributes(PropertyIds::length, TaggedInt::ToVarUnchecked(length), PropertyConfigurable, nullptr);
  3524. function->SetFunctionNameId(nameId);
  3525. JavascriptString * functionName = nullptr;
  3526. DebugOnly(bool status = ) function->GetFunctionName(&functionName);
  3527. AssertMsg(status, "DefaultCreateFunction sets the functionNameId, status should always be true");
  3528. function->SetPropertyWithAttributes(PropertyIds::name, functionName, PropertyConfigurable, nullptr);
  3529. #ifdef HEAP_ENUMERATION_VALIDATION
  3530. if (prototype) prototype->SetHeapEnumValidationCookie(HEAP_ENUMERATION_LIBRARY_OBJECT_COOKIE);
  3531. function->SetHeapEnumValidationCookie(HEAP_ENUMERATION_LIBRARY_OBJECT_COOKIE);
  3532. #endif
  3533. return function;
  3534. }
  3535. JavascriptFunction* JavascriptLibrary::AddFunction(DynamicObject* object, PropertyId propertyId, RuntimeFunction* function)
  3536. {
  3537. AddMember(object, propertyId, function);
  3538. function->SetFunctionNameId(TaggedInt::ToVarUnchecked((int)propertyId));
  3539. return function;
  3540. }
  3541. JavascriptFunction * JavascriptLibrary::AddFunctionToLibraryObject(DynamicObject* object, PropertyId propertyId, FunctionInfo * functionInfo, int length, PropertyAttributes attributes)
  3542. {
  3543. RuntimeFunction* function = DefaultCreateFunction(functionInfo, length, nullptr, nullptr, propertyId);
  3544. AddMember(object, propertyId, function, attributes);
  3545. return function;
  3546. }
  3547. JavascriptFunction * JavascriptLibrary::AddFunctionToLibraryObjectWithPrototype(DynamicObject * object, PropertyId propertyId, FunctionInfo * functionInfo, int length, DynamicObject * prototype, DynamicType * functionType)
  3548. {
  3549. RuntimeFunction* function = DefaultCreateFunction(functionInfo, length, prototype, functionType, propertyId);
  3550. AddMember(object, propertyId, function);
  3551. return function;
  3552. }
  3553. JavascriptFunction * JavascriptLibrary::AddFunctionToLibraryObjectWithName(DynamicObject* object, PropertyId propertyId, PropertyId name, FunctionInfo * functionInfo, int length)
  3554. {
  3555. RuntimeFunction* function = DefaultCreateFunction(functionInfo, length, nullptr, nullptr, name);
  3556. AddMember(object, propertyId, function);
  3557. return function;
  3558. }
  3559. RuntimeFunction* JavascriptLibrary::AddGetterToLibraryObject(DynamicObject* object, PropertyId propertyId, FunctionInfo* functionInfo)
  3560. {
  3561. RuntimeFunction* getter = CreateGetterFunction(propertyId, functionInfo);
  3562. AddAccessorsToLibraryObject(object, propertyId, getter, nullptr);
  3563. return getter;
  3564. }
  3565. void JavascriptLibrary::AddAccessorsToLibraryObject(DynamicObject* object, PropertyId propertyId, FunctionInfo * getterFunctionInfo, FunctionInfo * setterFunctionInfo)
  3566. {
  3567. AddAccessorsToLibraryObjectWithName(object, propertyId, propertyId, getterFunctionInfo, setterFunctionInfo);
  3568. }
  3569. void JavascriptLibrary::AddAccessorsToLibraryObject(DynamicObject* object, PropertyId propertyId, RecyclableObject* getterFunction, RecyclableObject* setterFunction)
  3570. {
  3571. if (getterFunction == nullptr)
  3572. {
  3573. getterFunction = GetUndefined();
  3574. }
  3575. if (setterFunction == nullptr)
  3576. {
  3577. setterFunction = GetUndefined();
  3578. }
  3579. object->SetAccessors(propertyId, getterFunction, setterFunction);
  3580. object->SetAttributes(propertyId, PropertyConfigurable | PropertyWritable);
  3581. }
  3582. void JavascriptLibrary::AddAccessorsToLibraryObjectWithName(DynamicObject* object, PropertyId propertyId, PropertyId nameId, FunctionInfo * getterFunctionInfo, FunctionInfo * setterFunctionInfo)
  3583. {
  3584. Js::RuntimeFunction* getterFunction = (getterFunctionInfo != nullptr)
  3585. ? CreateGetterFunction(nameId, getterFunctionInfo)
  3586. : nullptr;
  3587. Js::RuntimeFunction* setterFunction = (setterFunctionInfo != nullptr)
  3588. ? CreateSetterFunction(nameId, setterFunctionInfo)
  3589. : nullptr;
  3590. AddAccessorsToLibraryObject(object, propertyId, getterFunction, setterFunction);
  3591. }
  3592. void JavascriptLibrary::AddSpeciesAccessorsToLibraryObject(DynamicObject* object, FunctionInfo * getterFunctionInfo)
  3593. {
  3594. if (scriptContext->GetConfig()->IsES6SpeciesEnabled())
  3595. {
  3596. AddAccessorsToLibraryObjectWithName(object, PropertyIds::_symbolSpecies, PropertyIds::_RuntimeFunctionNameId_species, getterFunctionInfo, nullptr);
  3597. }
  3598. }
  3599. RuntimeFunction* JavascriptLibrary::CreateGetterFunction(PropertyId nameId, FunctionInfo* functionInfo)
  3600. {
  3601. Var name_withGetPrefix = LiteralString::Concat(LiteralString::NewCopySz(_u("get "), scriptContext), scriptContext->GetPropertyString(nameId));
  3602. RuntimeFunction* getterFunction = DefaultCreateFunction(functionInfo, 0, nullptr, nullptr, name_withGetPrefix);
  3603. getterFunction->SetPropertyWithAttributes(PropertyIds::length, TaggedInt::ToVarUnchecked(0), PropertyConfigurable, nullptr);
  3604. return getterFunction;
  3605. }
  3606. RuntimeFunction* JavascriptLibrary::CreateSetterFunction(PropertyId nameId, FunctionInfo* functionInfo)
  3607. {
  3608. Var name_withSetPrefix = LiteralString::Concat(LiteralString::NewCopySz(_u("set "), scriptContext), scriptContext->GetPropertyString(nameId));
  3609. RuntimeFunction* setterFunction = DefaultCreateFunction(functionInfo, 0, nullptr, nullptr, name_withSetPrefix);
  3610. setterFunction->SetPropertyWithAttributes(PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable, nullptr);
  3611. return setterFunction;
  3612. }
  3613. void JavascriptLibrary::AddMember(DynamicObject* object, PropertyId propertyId, Var value)
  3614. {
  3615. AddMember(object, propertyId, value, PropertyBuiltInMethodDefaults);
  3616. }
  3617. void JavascriptLibrary::AddMember(DynamicObject* object, PropertyId propertyId, Var value, PropertyAttributes attributes)
  3618. {
  3619. // NOTE: Assume all built in member doesn't have side effect
  3620. object->SetPropertyWithAttributes(propertyId, value, attributes, nullptr, PropertyOperation_None, SideEffects_None);
  3621. }
  3622. bool JavascriptLibrary::InitializeJSONObject(DynamicObject* JSONObject, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  3623. {
  3624. typeHandler->Convert(JSONObject, mode, 3);
  3625. JavascriptLibrary* library = JSONObject->GetLibrary();
  3626. library->AddMember(JSONObject, PropertyIds::stringify, library->EnsureJSONStringifyFunction());
  3627. JSONObject->GetScriptContext()->SetBuiltInLibraryFunction(JSON::EntryInfo::Stringify.GetOriginalEntryPoint(), library->EnsureJSONStringifyFunction());
  3628. library->AddFunctionToLibraryObject(JSONObject, PropertyIds::parse, &JSON::EntryInfo::Parse, 2);
  3629. if (JSONObject->GetScriptContext()->GetConfig()->IsES6ToStringTagEnabled())
  3630. {
  3631. library->AddMember(JSONObject, PropertyIds::_symbolToStringTag, JSONObject->GetLibrary()->CreateStringFromCppLiteral(_u("JSON")), PropertyConfigurable);
  3632. }
  3633. JSONObject->SetHasNoEnumerableProperties(true);
  3634. return true;
  3635. }
  3636. JavascriptFunction* JavascriptLibrary::EnsureJSONStringifyFunction()
  3637. {
  3638. if (jsonStringifyFunction == nullptr)
  3639. {
  3640. jsonStringifyFunction = DefaultCreateFunction(&JSON::EntryInfo::Stringify, 3, nullptr, nullptr, PropertyIds::stringify);
  3641. }
  3642. return jsonStringifyFunction;
  3643. }
  3644. #if defined(ENABLE_INTL_OBJECT) || defined(ENABLE_JS_BUILTINS) || defined(ENABLE_PROJECTION)
  3645. bool JavascriptLibrary::InitializeEngineInterfaceObject(DynamicObject* engineInterface, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  3646. {
  3647. typeHandler->Convert(engineInterface, mode, 3);
  3648. EngineInterfaceObject::FromVar(engineInterface)->Initialize();
  3649. engineInterface->SetHasNoEnumerableProperties(true);
  3650. return true;
  3651. }
  3652. #endif
  3653. void JavascriptLibrary::SetNativeHostPromiseContinuationFunction(PromiseContinuationCallback function, void *state)
  3654. {
  3655. this->nativeHostPromiseContinuationFunction = function;
  3656. this->nativeHostPromiseContinuationFunctionState = state;
  3657. }
  3658. void JavascriptLibrary::SetNativeHostPromiseRejectionTrackerCallback(HostPromiseRejectionTrackerCallback function, void *state)
  3659. {
  3660. this->nativeHostPromiseRejectionTracker = function;
  3661. this->nativeHostPromiseContinuationFunctionState = state;
  3662. }
  3663. void JavascriptLibrary::CallNativeHostPromiseRejectionTracker(Var promise, Var reason, bool handled)
  3664. {
  3665. if (this->nativeHostPromiseRejectionTracker != nullptr)
  3666. {
  3667. BEGIN_LEAVE_SCRIPT(scriptContext);
  3668. try
  3669. {
  3670. this->nativeHostPromiseRejectionTracker(promise, reason, handled, this->nativeHostPromiseContinuationFunctionState);
  3671. }
  3672. catch (...)
  3673. {
  3674. // Hosts are required not to pass exceptions back across the callback boundary. If
  3675. // this happens, it is a bug in the host, not something that we are expected to
  3676. // handle gracefully.
  3677. Js::Throw::FatalInternalError();
  3678. }
  3679. END_LEAVE_SCRIPT(scriptContext);
  3680. }
  3681. }
  3682. void JavascriptLibrary::SetJsrtContext(FinalizableObject* jsrtContext)
  3683. {
  3684. // With JsrtContext supporting cross context, ensure that it doesn't get GCed
  3685. // prematurely. So pin the instance to javascriptLibrary so it will stay alive
  3686. // until any object of it are alive.
  3687. Assert(this->jsrtContextObject == nullptr);
  3688. this->jsrtContextObject = jsrtContext;
  3689. }
  3690. FinalizableObject* JavascriptLibrary::GetJsrtContext()
  3691. {
  3692. return this->jsrtContextObject;
  3693. }
  3694. void JavascriptLibrary::EnqueueTask(Var taskVar)
  3695. {
  3696. Assert(JavascriptFunction::Is(taskVar));
  3697. if(this->nativeHostPromiseContinuationFunction)
  3698. {
  3699. #if ENABLE_TTD
  3700. TTDAssert(this->scriptContext != nullptr, "We shouldn't be adding tasks if this is the case???");
  3701. if(this->scriptContext->ShouldPerformReplayAction())
  3702. {
  3703. this->scriptContext->GetThreadContext()->TTDRootNestingCount++;
  3704. this->scriptContext->GetThreadContext()->TTDLog->ReplayEnqueueTaskEvent(scriptContext, taskVar);
  3705. this->scriptContext->GetThreadContext()->TTDRootNestingCount--;
  3706. }
  3707. else if(this->scriptContext->ShouldPerformRecordAction())
  3708. {
  3709. this->scriptContext->GetThreadContext()->TTDRootNestingCount++;
  3710. TTD::NSLogEvents::EventLogEntry* evt = this->scriptContext->GetThreadContext()->TTDLog->RecordEnqueueTaskEvent(taskVar);
  3711. BEGIN_LEAVE_SCRIPT(this->scriptContext);
  3712. try
  3713. {
  3714. this->nativeHostPromiseContinuationFunction(taskVar, this->nativeHostPromiseContinuationFunctionState);
  3715. }
  3716. catch(...)
  3717. {
  3718. // Hosts are required not to pass exceptions back across the callback boundary. If
  3719. // this happens, it is a bug in the host, not something that we are expected to
  3720. // handle gracefully.
  3721. Js::Throw::FatalInternalError();
  3722. }
  3723. END_LEAVE_SCRIPT(this->scriptContext);
  3724. this->scriptContext->GetThreadContext()->TTDLog->RecordEnqueueTaskEvent_Complete(evt);
  3725. this->scriptContext->GetThreadContext()->TTDRootNestingCount--;
  3726. }
  3727. else
  3728. {
  3729. BEGIN_LEAVE_SCRIPT(scriptContext);
  3730. try
  3731. {
  3732. this->nativeHostPromiseContinuationFunction(taskVar, this->nativeHostPromiseContinuationFunctionState);
  3733. }
  3734. catch(...)
  3735. {
  3736. // Hosts are required not to pass exceptions back across the callback boundary. If
  3737. // this happens, it is a bug in the host, not something that we are expected to
  3738. // handle gracefully.
  3739. Js::Throw::FatalInternalError();
  3740. }
  3741. END_LEAVE_SCRIPT(scriptContext);
  3742. }
  3743. #else
  3744. BEGIN_LEAVE_SCRIPT(scriptContext);
  3745. try
  3746. {
  3747. this->nativeHostPromiseContinuationFunction(taskVar, this->nativeHostPromiseContinuationFunctionState);
  3748. }
  3749. catch (...)
  3750. {
  3751. // Hosts are required not to pass exceptions back across the callback boundary. If
  3752. // this happens, it is a bug in the host, not something that we are expected to
  3753. // handle gracefully.
  3754. Js::Throw::FatalInternalError();
  3755. }
  3756. END_LEAVE_SCRIPT(scriptContext);
  3757. #endif
  3758. }
  3759. else
  3760. {
  3761. #if ENABLE_TTD
  3762. if(this->scriptContext->ShouldPerformRecordOrReplayAction())
  3763. {
  3764. //
  3765. //TODO: need to implement support for this path
  3766. //
  3767. TTDAssert(false, "Path not implemented in TTD!!!");
  3768. }
  3769. #endif
  3770. HRESULT hr = scriptContext->GetHostScriptContext()->EnqueuePromiseTask(taskVar);
  3771. if (hr != S_OK)
  3772. {
  3773. Js::JavascriptError::MapAndThrowError(scriptContext, JSERR_HostMaybeMissingPromiseContinuationCallback);
  3774. }
  3775. }
  3776. }
  3777. #ifdef ENABLE_JS_BUILTINS
  3778. bool JavascriptLibrary::InitializeBuiltInObject(DynamicObject* builtInObject, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  3779. {
  3780. typeHandler->Convert(builtInObject, mode, /*initSlotCapacity*/ 2);
  3781. if (builtInObject->GetScriptContext()->IsJsBuiltInEnabled())
  3782. {
  3783. auto builtInInitializer = [&](JsBuiltInEngineInterfaceExtensionObject* builtInExtension, ScriptContext * scriptContext) -> void
  3784. {
  3785. builtInExtension->InjectJsBuiltInLibraryCode(scriptContext);
  3786. };
  3787. builtInObject->GetLibrary()->InitializeBuiltInForPrototypes(builtInInitializer);
  3788. }
  3789. return true;
  3790. }
  3791. void JavascriptLibrary::EnsureBuiltInEngineIsReady()
  3792. {
  3793. if (scriptContext->IsJsBuiltInEnabled())
  3794. {
  3795. auto builtInInitializer = [&](JsBuiltInEngineInterfaceExtensionObject* builtInExtension, ScriptContext * scriptContext) -> void
  3796. {
  3797. builtInExtension->InjectJsBuiltInLibraryCode(scriptContext);
  3798. };
  3799. scriptContext->GetLibrary()->InitializeBuiltInForPrototypes(builtInInitializer);
  3800. }
  3801. }
  3802. template <class Fn>
  3803. void JavascriptLibrary::InitializeBuiltInForPrototypes(Fn fn)
  3804. {
  3805. if (scriptContext->VerifyAlive()) // Can't initialize if scriptContext closed, will need to run script
  3806. {
  3807. Assert(engineInterfaceObject != nullptr);
  3808. JsBuiltInEngineInterfaceExtensionObject* builtInExtension = static_cast<JsBuiltInEngineInterfaceExtensionObject*>(GetEngineInterfaceObject()->GetEngineExtension(EngineInterfaceExtensionKind_JsBuiltIn));
  3809. fn(builtInExtension, scriptContext);
  3810. }
  3811. }
  3812. bool JavascriptLibrary::InitializeChakraLibraryObject(DynamicObject * chakraLibraryObject, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  3813. {
  3814. JavascriptLibrary* library = chakraLibraryObject->GetLibrary();
  3815. typeHandler->Convert(chakraLibraryObject, mode, 4);
  3816. library->AddFunctionToLibraryObject(chakraLibraryObject, PropertyIds::GetLength, &JsBuiltInEngineInterfaceExtensionObject::EntryInfo::JsBuiltIn_Internal_GetLength, 1);
  3817. library->AddFunctionToLibraryObject(chakraLibraryObject, PropertyIds::InitInternalProperties, &JsBuiltInEngineInterfaceExtensionObject::EntryInfo::JsBuiltIn_Internal_InitInternalProperties, 1);
  3818. library->AddMember(chakraLibraryObject, PropertyIds::isArray, library->isArrayFunction);
  3819. library->AddMember(chakraLibraryObject, PropertyIds::Object, library->objectConstructor);
  3820. return true;
  3821. }
  3822. #endif // ENABLE_JS_BUILTINS
  3823. #ifdef ENABLE_INTL_OBJECT
  3824. void JavascriptLibrary::ResetIntlObject()
  3825. {
  3826. IntlObject = DynamicObject::New(
  3827. recycler,
  3828. DynamicType::New(scriptContext,
  3829. TypeIds_Object, objectPrototype, nullptr,
  3830. DeferredTypeHandler<InitializeIntlObject>::GetDefaultInstance()));
  3831. }
  3832. void JavascriptLibrary::EnsureIntlObjectReady()
  3833. {
  3834. Assert(this->IntlObject != nullptr);
  3835. // For Intl builtins, we need to make sure the Intl object has been initialized before fetching the
  3836. // builtins from the EngineInterfaceObject. This is because the builtins are actually created via
  3837. // Intl.js and are registered into the EngineInterfaceObject as part of Intl object initialization.
  3838. JavascriptExceptionObject * caughtExceptionObject = nullptr;
  3839. try
  3840. {
  3841. this->IntlObject->GetTypeHandler()->EnsureObjectReady(this->IntlObject);
  3842. }
  3843. catch (const JavascriptException& err)
  3844. {
  3845. caughtExceptionObject = err.GetAndClear();
  3846. }
  3847. // Propagate the OOM and SOE exceptions only
  3848. if (caughtExceptionObject != nullptr &&
  3849. (caughtExceptionObject == ThreadContext::GetContextForCurrentThread()->GetPendingOOMErrorObject() ||
  3850. caughtExceptionObject == ThreadContext::GetContextForCurrentThread()->GetPendingSOErrorObject()))
  3851. {
  3852. caughtExceptionObject = caughtExceptionObject->CloneIfStaticExceptionObject(scriptContext);
  3853. JavascriptExceptionOperators::DoThrow(caughtExceptionObject, scriptContext);
  3854. }
  3855. }
  3856. bool JavascriptLibrary::InitializeIntlObject(DynamicObject* IntlObject, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  3857. {
  3858. typeHandler->Convert(IntlObject, mode, /*initSlotCapacity*/ 2);
  3859. auto intlInitializer = [&](IntlEngineInterfaceExtensionObject* intlExtension, ScriptContext * scriptContext, DynamicObject* intlObject) ->void
  3860. {
  3861. intlExtension->InjectIntlLibraryCode(scriptContext, intlObject, IntlInitializationType::Intl);
  3862. };
  3863. IntlObject->GetLibrary()->InitializeIntlForPrototypes(intlInitializer);
  3864. return true;
  3865. }
  3866. void JavascriptLibrary::InitializeIntlForStringPrototype()
  3867. {
  3868. auto stringPrototypeInitializer = [&](IntlEngineInterfaceExtensionObject* intlExtension, ScriptContext * scriptContext, DynamicObject* intlObject) ->void
  3869. {
  3870. intlExtension->InjectIntlLibraryCode(scriptContext, intlObject, IntlInitializationType::StringPrototype);
  3871. };
  3872. InitializeIntlForPrototypes(stringPrototypeInitializer);
  3873. }
  3874. void JavascriptLibrary::InitializeIntlForDatePrototype()
  3875. {
  3876. auto datePrototypeInitializer = [&](IntlEngineInterfaceExtensionObject* intlExtension, ScriptContext * scriptContext, DynamicObject* intlObject) ->void
  3877. {
  3878. intlExtension->InjectIntlLibraryCode(scriptContext, intlObject, IntlInitializationType::DatePrototype);
  3879. };
  3880. InitializeIntlForPrototypes(datePrototypeInitializer);
  3881. }
  3882. void JavascriptLibrary::InitializeIntlForNumberPrototype()
  3883. {
  3884. auto numberPrototypeInitializer = [&](IntlEngineInterfaceExtensionObject* intlExtension, ScriptContext * scriptContext, DynamicObject* intlObject) ->void
  3885. {
  3886. intlExtension->InjectIntlLibraryCode(scriptContext, intlObject, IntlInitializationType::NumberPrototype);
  3887. };
  3888. InitializeIntlForPrototypes(numberPrototypeInitializer);
  3889. }
  3890. template <class Fn>
  3891. void JavascriptLibrary::InitializeIntlForPrototypes(Fn fn)
  3892. {
  3893. ScriptContext* scriptContext = this->IntlObject->GetScriptContext();
  3894. if (scriptContext->VerifyAlive()) // Can't initialize if scriptContext closed, will need to run script
  3895. {
  3896. JavascriptLibrary* library = this->IntlObject->GetLibrary();
  3897. Assert(library->engineInterfaceObject != nullptr);
  3898. IntlEngineInterfaceExtensionObject* intlExtension = static_cast<IntlEngineInterfaceExtensionObject*>(library->GetEngineInterfaceObject()->GetEngineExtension(EngineInterfaceExtensionKind_Intl));
  3899. fn(intlExtension, scriptContext, IntlObject);
  3900. }
  3901. }
  3902. #endif
  3903. void JavascriptLibrary::SetProfileMode(bool fSet)
  3904. {
  3905. inProfileMode = fSet;
  3906. }
  3907. void JavascriptLibrary::SetDispatchProfile(bool fSet, JavascriptMethod dispatchInvoke)
  3908. {
  3909. if (!fSet)
  3910. {
  3911. this->inDispatchProfileMode = false;
  3912. if (dispatchInvoke != nullptr)
  3913. {
  3914. this->GetScriptContext()->GetHostScriptContext()->SetDispatchInvoke(dispatchInvoke);
  3915. }
  3916. idMappedFunctionWithPrototypeType->SetEntryPoint(JavascriptExternalFunction::ExternalFunctionThunk);
  3917. externalFunctionWithDeferredPrototypeType->SetEntryPoint(JavascriptExternalFunction::ExternalFunctionThunk);
  3918. stdCallFunctionWithDeferredPrototypeType->SetEntryPoint(JavascriptExternalFunction::StdCallExternalFunctionThunk);
  3919. }
  3920. else
  3921. {
  3922. this->inDispatchProfileMode = true;
  3923. if (dispatchInvoke != nullptr)
  3924. {
  3925. this->GetScriptContext()->GetHostScriptContext()->SetDispatchInvoke(dispatchInvoke);
  3926. }
  3927. idMappedFunctionWithPrototypeType->SetEntryPoint(ProfileEntryThunk);
  3928. externalFunctionWithDeferredPrototypeType->SetEntryPoint(ProfileEntryThunk);
  3929. stdCallFunctionWithDeferredPrototypeType->SetEntryPoint(ProfileEntryThunk);
  3930. }
  3931. }
  3932. JavascriptString* JavascriptLibrary::CreateEmptyString()
  3933. {
  3934. return LiteralStringWithPropertyStringPtr::CreateEmptyString(this);
  3935. }
  3936. JavascriptRegExp* JavascriptLibrary::CreateEmptyRegExp()
  3937. {
  3938. return RecyclerNew(scriptContext->GetRecycler(), JavascriptRegExp, emptyRegexPattern,
  3939. this->GetRegexType());
  3940. }
  3941. #if ENABLE_TTD
  3942. Js::PropertyId JavascriptLibrary::ExtractPrimitveSymbolId_TTD(Var value)
  3943. {
  3944. return Js::JavascriptSymbol::FromVar(value)->GetValue()->GetPropertyId();
  3945. }
  3946. Js::RecyclableObject* JavascriptLibrary::CreatePrimitveSymbol_TTD(Js::PropertyId pid)
  3947. {
  3948. return this->scriptContext->GetSymbol(pid);
  3949. }
  3950. Js::RecyclableObject* JavascriptLibrary::CreatePrimitveSymbol_TTD(Js::JavascriptString* str)
  3951. {
  3952. return this->CreateSymbol(str);
  3953. }
  3954. Js::RecyclableObject* JavascriptLibrary::CreateDefaultBoxedObject_TTD(Js::TypeId kind)
  3955. {
  3956. switch(kind)
  3957. {
  3958. case Js::TypeIds_BooleanObject:
  3959. return this->CreateBooleanObject();
  3960. case Js::TypeIds_NumberObject:
  3961. return this->CreateNumberObject(Js::TaggedInt::ToVarUnchecked(0));
  3962. case Js::TypeIds_StringObject:
  3963. return this->CreateStringObject(nullptr);
  3964. case Js::TypeIds_SymbolObject:
  3965. return this->CreateSymbolObject(nullptr);
  3966. default:
  3967. TTDAssert(false, "Unsupported nullptr value boxed object.");
  3968. return nullptr;
  3969. }
  3970. }
  3971. void JavascriptLibrary::SetBoxedObjectValue_TTD(Js::RecyclableObject* obj, Js::Var value)
  3972. {
  3973. switch(obj->GetTypeId())
  3974. {
  3975. case Js::TypeIds_BooleanObject:
  3976. Js::JavascriptBooleanObject::FromVar(obj)->SetValue_TTD(value);
  3977. break;
  3978. case Js::TypeIds_NumberObject:
  3979. Js::JavascriptNumberObject::FromVar(obj)->SetValue_TTD(value);
  3980. break;
  3981. case Js::TypeIds_StringObject:
  3982. Js::JavascriptStringObject::FromVar(obj)->SetValue_TTD(value);
  3983. break;
  3984. case Js::TypeIds_SymbolObject:
  3985. Js::JavascriptSymbolObject::FromVar(obj)->SetValue_TTD(value);
  3986. break;
  3987. default:
  3988. TTDAssert(false, "Unsupported nullptr value boxed object.");
  3989. break;
  3990. }
  3991. }
  3992. Js::RecyclableObject* JavascriptLibrary::CreateDate_TTD(double value)
  3993. {
  3994. return this->CreateDate(value);
  3995. }
  3996. Js::RecyclableObject* JavascriptLibrary::CreateRegex_TTD(const char16* patternSource, uint32 patternLength, UnifiedRegex::RegexFlags flags, CharCount lastIndex, Js::Var lastVar)
  3997. {
  3998. Js::JavascriptRegExp* re = Js::JavascriptRegExp::CreateRegEx(patternSource, patternLength, flags, this->scriptContext);
  3999. re->SetLastIndexInfo_TTD(lastIndex, lastVar);
  4000. return re;
  4001. }
  4002. Js::RecyclableObject* JavascriptLibrary::CreateError_TTD()
  4003. {
  4004. return this->CreateError();
  4005. }
  4006. Js::RecyclableObject* JavascriptLibrary::CreateES5Array_TTD()
  4007. {
  4008. Js::JavascriptArray* arrayObj = this->CreateArray();
  4009. arrayObj->GetTypeHandler()->ConvertToTypeWithItemAttributes(arrayObj);
  4010. return arrayObj;
  4011. }
  4012. void JavascriptLibrary::SetLengthWritableES5Array_TTD(Js::RecyclableObject* es5Array, bool isLengthWritable)
  4013. {
  4014. Js::ES5Array* es5a = Js::ES5Array::FromVar(es5Array);
  4015. if(es5a->IsLengthWritable() != isLengthWritable)
  4016. {
  4017. es5a->SetWritable(Js::PropertyIds::length, isLengthWritable ? TRUE : FALSE);
  4018. }
  4019. }
  4020. Js::RecyclableObject* JavascriptLibrary::CreateSet_TTD()
  4021. {
  4022. return JavascriptSet::CreateForSnapshotRestore(this->scriptContext);
  4023. }
  4024. Js::RecyclableObject* JavascriptLibrary::CreateWeakSet_TTD()
  4025. {
  4026. return this->CreateWeakSet();
  4027. }
  4028. void JavascriptLibrary::AddSetElementInflate_TTD(Js::JavascriptSet* set, Var value)
  4029. {
  4030. set->Add(value);
  4031. }
  4032. void JavascriptLibrary::AddWeakSetElementInflate_TTD(Js::JavascriptWeakSet* set, Var value)
  4033. {
  4034. set->GetScriptContext()->TTDContextInfo->TTDWeakReferencePinSet->Add(Js::DynamicObject::FromVar(value));
  4035. set->Add(Js::DynamicObject::FromVar(value));
  4036. }
  4037. Js::RecyclableObject* JavascriptLibrary::CreateMap_TTD()
  4038. {
  4039. return JavascriptMap::CreateForSnapshotRestore(this->scriptContext);
  4040. }
  4041. Js::RecyclableObject* JavascriptLibrary::CreateWeakMap_TTD()
  4042. {
  4043. return this->CreateWeakMap();
  4044. }
  4045. void JavascriptLibrary::AddMapElementInflate_TTD(Js::JavascriptMap* map, Var key, Var value)
  4046. {
  4047. map->Set(key, value);
  4048. }
  4049. void JavascriptLibrary::AddWeakMapElementInflate_TTD(Js::JavascriptWeakMap* map, Var key, Var value)
  4050. {
  4051. map->GetScriptContext()->TTDContextInfo->TTDWeakReferencePinSet->Add(Js::DynamicObject::FromVar(key));
  4052. map->Set(Js::DynamicObject::FromVar(key), value);
  4053. }
  4054. Js::RecyclableObject* JavascriptLibrary::CreateExternalFunction_TTD(Js::Var fname)
  4055. {
  4056. if(TaggedInt::Is(fname))
  4057. {
  4058. PropertyId pid = TaggedInt::ToInt32(fname);
  4059. if(!scriptContext->IsTrackedPropertyId(pid))
  4060. {
  4061. scriptContext->TrackPid(pid);
  4062. }
  4063. }
  4064. JavascriptExternalFunction* function = this->CreateIdMappedExternalFunction(&JavascriptExternalFunction::TTDReplayDummyExternalMethod, stdCallFunctionWithDeferredPrototypeType);
  4065. function->SetFunctionNameId(fname);
  4066. function->SetCallbackState(nullptr);
  4067. return function;
  4068. }
  4069. Js::RecyclableObject* JavascriptLibrary::CreateBoundFunction_TTD(
  4070. RecyclableObject* function, Var bThis, uint32 ct, Field(Var)* args)
  4071. {
  4072. return BoundFunction::InflateBoundFunction(this->scriptContext, function, bThis, ct, args);
  4073. }
  4074. Js::RecyclableObject* JavascriptLibrary::CreateProxy_TTD(RecyclableObject* handler, RecyclableObject* target)
  4075. {
  4076. JavascriptProxy* newProxy = RecyclerNew(this->scriptContext->GetRecycler(), JavascriptProxy, this->GetProxyType(), this->scriptContext, target, handler);
  4077. if(target != nullptr && JavascriptConversion::IsCallable(target))
  4078. {
  4079. newProxy->ChangeType();
  4080. newProxy->GetDynamicType()->SetEntryPoint(JavascriptProxy::FunctionCallTrap);
  4081. }
  4082. return newProxy;
  4083. }
  4084. Js::RecyclableObject* JavascriptLibrary::CreateRevokeFunction_TTD(RecyclableObject* proxy)
  4085. {
  4086. RuntimeFunction* revoker = RecyclerNewEnumClass(this->scriptContext->GetRecycler(), JavascriptLibrary::EnumFunctionClass, RuntimeFunction, this->CreateFunctionWithLengthType(&JavascriptProxy::EntryInfo::Revoke), &JavascriptProxy::EntryInfo::Revoke);
  4087. revoker->SetPropertyWithAttributes(Js::PropertyIds::length, Js::TaggedInt::ToVarUnchecked(0), PropertyNone, NULL);
  4088. revoker->SetInternalProperty(Js::InternalPropertyIds::RevocableProxy, proxy, PropertyOperationFlags::PropertyOperation_Force, nullptr);
  4089. return revoker;
  4090. }
  4091. Js::RecyclableObject* JavascriptLibrary::CreateHeapArguments_TTD(uint32 numOfArguments, uint32 formalCount, ActivationObject* frameObject, byte* deletedArray)
  4092. {
  4093. Js::HeapArgumentsObject* argsObj = this->CreateHeapArguments(frameObject, formalCount);
  4094. argsObj->SetNumberOfArguments(numOfArguments);
  4095. for(uint32 i = 0; i < formalCount; ++i)
  4096. {
  4097. if(deletedArray[i])
  4098. {
  4099. argsObj->DeleteItemAt(i);
  4100. }
  4101. }
  4102. return argsObj;
  4103. }
  4104. Js::RecyclableObject* JavascriptLibrary::CreateES5HeapArguments_TTD(uint32 numOfArguments, uint32 formalCount, ActivationObject* frameObject, byte* deletedArray)
  4105. {
  4106. Js::HeapArgumentsObject* argsObj = this->CreateHeapArguments(frameObject, formalCount);
  4107. argsObj->SetNumberOfArguments(numOfArguments);
  4108. for(uint32 i = 0; i < formalCount; ++i)
  4109. {
  4110. if(deletedArray[i])
  4111. {
  4112. argsObj->DeleteItemAt(i);
  4113. }
  4114. }
  4115. return argsObj->ConvertToES5HeapArgumentsObject_TTD();
  4116. }
  4117. Js::JavascriptPromiseCapability* JavascriptLibrary::CreatePromiseCapability_TTD(Var promise, Var resolve, Var reject)
  4118. {
  4119. return JavascriptPromiseCapability::New(promise, resolve, reject, this->scriptContext);
  4120. }
  4121. Js::JavascriptPromiseReaction* JavascriptLibrary::CreatePromiseReaction_TTD(RecyclableObject* handler, JavascriptPromiseCapability* capabilities)
  4122. {
  4123. return JavascriptPromiseReaction::New(capabilities, handler, this->scriptContext);
  4124. }
  4125. Js::RecyclableObject* JavascriptLibrary::CreatePromise_TTD(uint32 status, Var result, JsUtil::List<Js::JavascriptPromiseReaction*, HeapAllocator>& resolveReactions, JsUtil::List<Js::JavascriptPromiseReaction*, HeapAllocator>& rejectReactions)
  4126. {
  4127. return JavascriptPromise::InitializePromise_TTD(this->scriptContext, status, result, resolveReactions, rejectReactions);
  4128. }
  4129. JavascriptPromiseResolveOrRejectFunctionAlreadyResolvedWrapper* JavascriptLibrary::CreateAlreadyDefinedWrapper_TTD(bool alreadyDefined)
  4130. {
  4131. JavascriptPromiseResolveOrRejectFunctionAlreadyResolvedWrapper* alreadyResolvedRecord = RecyclerNewStructZ(scriptContext->GetRecycler(), JavascriptPromiseResolveOrRejectFunctionAlreadyResolvedWrapper);
  4132. alreadyResolvedRecord->alreadyResolved = alreadyDefined;
  4133. return alreadyResolvedRecord;
  4134. }
  4135. Js::RecyclableObject* JavascriptLibrary::CreatePromiseResolveOrRejectFunction_TTD(RecyclableObject* promise, bool isReject, JavascriptPromiseResolveOrRejectFunctionAlreadyResolvedWrapper* alreadyResolved)
  4136. {
  4137. TTDAssert(JavascriptPromise::Is(promise), "Not a promise!");
  4138. return this->CreatePromiseResolveOrRejectFunction(JavascriptPromise::EntryResolveOrRejectFunction, static_cast<JavascriptPromise*>(promise), isReject, alreadyResolved);
  4139. }
  4140. Js::RecyclableObject* JavascriptLibrary::CreatePromiseReactionTaskFunction_TTD(JavascriptPromiseReaction* reaction, Var argument)
  4141. {
  4142. return this->CreatePromiseReactionTaskFunction(JavascriptPromise::EntryReactionTaskFunction, reaction, argument);
  4143. }
  4144. JavascriptPromiseAllResolveElementFunctionRemainingElementsWrapper* JavascriptLibrary::CreateRemainingElementsWrapper_TTD(Js::ScriptContext* ctx, uint32 value)
  4145. {
  4146. JavascriptPromiseAllResolveElementFunctionRemainingElementsWrapper* remainingElementsWrapper = RecyclerNewStructZ(ctx->GetRecycler(), JavascriptPromiseAllResolveElementFunctionRemainingElementsWrapper);
  4147. remainingElementsWrapper->remainingElements = value;
  4148. return remainingElementsWrapper;
  4149. }
  4150. Js::RecyclableObject* JavascriptLibrary::CreatePromiseAllResolveElementFunction_TTD(Js::JavascriptPromiseCapability* capabilities, uint32 index, Js::JavascriptPromiseAllResolveElementFunctionRemainingElementsWrapper* wrapper, Js::RecyclableObject* values, bool alreadyCalled)
  4151. {
  4152. Js::JavascriptPromiseAllResolveElementFunction* res = this->CreatePromiseAllResolveElementFunction(JavascriptPromise::EntryAllResolveElementFunction, index, Js::JavascriptArray::FromVar(values), capabilities, wrapper);
  4153. res->SetAlreadyCalled(alreadyCalled);
  4154. return res;
  4155. }
  4156. Js::RecyclableObject* JavascriptLibrary::CreateJavascriptGenerator_TTD(Js::ScriptContext *ctx,
  4157. Js::RecyclableObject *prototype, Js::Arguments &arguments,
  4158. Js::JavascriptGenerator::GeneratorState generatorState)
  4159. {
  4160. Js::DynamicType* generatorType = CreateGeneratorType(prototype);
  4161. Js::JavascriptGenerator* generator = Js::JavascriptGenerator::New(ctx->GetRecycler(), generatorType, arguments, generatorState);
  4162. return generator;
  4163. }
  4164. #endif // ENABLE_TTD
  4165. void JavascriptLibrary::SetCrossSiteForLockedFunctionType(JavascriptFunction * function)
  4166. {
  4167. Assert(function->GetDynamicType()->GetIsLocked());
  4168. if (ScriptFunction::Is(function))
  4169. {
  4170. this->SetCrossSiteForLockedNonBuiltInFunctionType(function);
  4171. }
  4172. else if (BoundFunction::Is(function))
  4173. {
  4174. this->SetCrossSiteForLockedNonBuiltInFunctionType(function);
  4175. }
  4176. else
  4177. {
  4178. DynamicTypeHandler * typeHandler = function->GetDynamicType()->GetTypeHandler();
  4179. if (typeHandler == JavascriptLibrary::GetDeferredPrototypeFunctionTypeHandler(this->GetScriptContext())
  4180. || typeHandler == JavascriptLibrary::GetDeferredPrototypeFunctionWithLengthTypeHandler(this->GetScriptContext()))
  4181. {
  4182. function->ReplaceType(crossSiteDeferredPrototypeFunctionType);
  4183. }
  4184. else if (typeHandler == Js::DeferredTypeHandler<Js::JavascriptExternalFunction::DeferredConstructorInitializer>::GetDefaultInstance())
  4185. {
  4186. function->ReplaceType(crossSiteExternalConstructFunctionWithPrototypeType);
  4187. }
  4188. else if (typeHandler == &SharedIdMappedFunctionWithPrototypeTypeHandler)
  4189. {
  4190. function->ReplaceType(crossSiteIdMappedFunctionWithPrototypeType);
  4191. }
  4192. else
  4193. {
  4194. this->SetCrossSiteForLockedNonBuiltInFunctionType(function);
  4195. }
  4196. }
  4197. }
  4198. void JavascriptLibrary::SetCrossSiteForLockedNonBuiltInFunctionType(JavascriptFunction * function)
  4199. {
  4200. DynamicTypeHandler *typeHandler = function->GetTypeHandler();
  4201. if (typeHandler->IsPathTypeHandler())
  4202. {
  4203. PathTypeHandlerBase::FromTypeHandler(typeHandler)->ConvertToNonShareableTypeHandler(function);
  4204. }
  4205. else
  4206. {
  4207. function->ChangeType();
  4208. }
  4209. function->SetEntryPoint(scriptContext->CurrentCrossSiteThunk);
  4210. }
  4211. JavascriptExternalFunction*
  4212. JavascriptLibrary::CreateExternalFunction(ExternalMethod entryPoint, PropertyId nameId, Var signature, UINT64 flags, bool isLengthAvailable)
  4213. {
  4214. Assert(nameId == 0 || scriptContext->IsTrackedPropertyId(nameId));
  4215. return CreateExternalFunction(entryPoint, TaggedInt::ToVarUnchecked(nameId), signature, flags, isLengthAvailable);
  4216. }
  4217. JavascriptExternalFunction*
  4218. JavascriptLibrary::CreateExternalFunction(ExternalMethod entryPoint, Var nameId, Var signature, UINT64 flags, bool isLengthAvailable)
  4219. {
  4220. JavascriptExternalFunction* function = this->CreateIdMappedExternalFunction(entryPoint, isLengthAvailable ? externalFunctionWithLengthAndDeferredPrototypeType : externalFunctionWithDeferredPrototypeType);
  4221. function->SetExternalFlags(flags);
  4222. function->SetFunctionNameId(nameId);
  4223. function->SetSignature(signature);
  4224. #ifdef HEAP_ENUMERATION_VALIDATION
  4225. function->SetHeapEnumValidationCookie(HEAP_ENUMERATION_LIBRARY_OBJECT_COOKIE);
  4226. #endif
  4227. JS_ETW_INTERNAL(EventWriteJSCRIPT_BUILD_DIRECT_FUNCTION(scriptContext, function, TaggedInt::Is(nameId) ? scriptContext->GetThreadContext()->GetPropertyName(TaggedInt::ToInt32(nameId))->GetBuffer() : ((JavascriptString *)nameId)->GetString()));
  4228. #if DBG_DUMP
  4229. if (Js::Configuration::Global.flags.Trace.IsEnabled(Js::HostPhase))
  4230. {
  4231. Output::Print(_u("Create new external function: methodAddr= %p, propertyRecord= %p, propertyName= %s\n"),
  4232. this, nameId,
  4233. TaggedInt::Is(nameId) ? scriptContext->GetThreadContext()->GetPropertyName(TaggedInt::ToInt32(nameId))->GetBuffer() : ((JavascriptString *)nameId)->GetString());
  4234. }
  4235. #endif
  4236. return function;
  4237. }
  4238. void JavascriptLibrary::EnsureStringTemplateCallsiteObjectList()
  4239. {
  4240. if (this->stringTemplateCallsiteObjectList == nullptr)
  4241. {
  4242. this->stringTemplateCallsiteObjectList = RecyclerNew(GetRecycler(), StringTemplateCallsiteObjectList, GetRecycler());
  4243. }
  4244. }
  4245. void JavascriptLibrary::AddStringTemplateCallsiteObject(RecyclableObject* callsite)
  4246. {
  4247. this->EnsureStringTemplateCallsiteObjectList();
  4248. RecyclerWeakReference<RecyclableObject>* callsiteRef = this->GetRecycler()->CreateWeakReferenceHandle<RecyclableObject>(callsite);
  4249. this->stringTemplateCallsiteObjectList->Item(callsiteRef);
  4250. }
  4251. RecyclableObject* JavascriptLibrary::TryGetStringTemplateCallsiteObject(ParseNodePtr pnode)
  4252. {
  4253. this->EnsureStringTemplateCallsiteObjectList();
  4254. RecyclerWeakReference<RecyclableObject>* callsiteRef = this->stringTemplateCallsiteObjectList->LookupWithKey(pnode);
  4255. if (callsiteRef)
  4256. {
  4257. RecyclableObject* callsite = callsiteRef->Get();
  4258. if (callsite)
  4259. {
  4260. return callsite;
  4261. }
  4262. }
  4263. return nullptr;
  4264. }
  4265. RecyclableObject* JavascriptLibrary::TryGetStringTemplateCallsiteObject(RecyclableObject* callsite)
  4266. {
  4267. this->EnsureStringTemplateCallsiteObjectList();
  4268. RecyclerWeakReference<RecyclableObject>* callsiteRef = this->GetRecycler()->CreateWeakReferenceHandle<RecyclableObject>(callsite);
  4269. RecyclerWeakReference<RecyclableObject>* existingCallsiteRef = this->stringTemplateCallsiteObjectList->LookupWithKey(callsiteRef);
  4270. if (existingCallsiteRef)
  4271. {
  4272. RecyclableObject* existingCallsite = existingCallsiteRef->Get();
  4273. if (existingCallsite)
  4274. {
  4275. return existingCallsite;
  4276. }
  4277. }
  4278. return nullptr;
  4279. }
  4280. #if DBG_DUMP
  4281. const char16* JavascriptLibrary::GetStringTemplateCallsiteObjectKey(Var callsite)
  4282. {
  4283. // Calculate the key for the string template callsite object.
  4284. // Key is combination of the raw string literals delimited by '${}' since string template literals cannot include that symbol.
  4285. // `str1${expr1}str2${expr2}str3` => "str1${}str2${}str3"
  4286. ES5Array* callsiteObj = ES5Array::FromVar(callsite);
  4287. ScriptContext* scriptContext = callsiteObj->GetScriptContext();
  4288. Var var = JavascriptOperators::OP_GetProperty(callsiteObj, Js::PropertyIds::raw, scriptContext);
  4289. ES5Array* rawArray = ES5Array::FromVar(var);
  4290. uint32 arrayLength = rawArray->GetLength();
  4291. uint32 totalStringLength = 0;
  4292. JavascriptString* str;
  4293. Assert(arrayLength != 0);
  4294. // Count the size in characters of the raw strings
  4295. for (uint32 i = 0; i < arrayLength; i++)
  4296. {
  4297. rawArray->DirectGetItemAt(i, &var);
  4298. str = JavascriptString::FromVar(var);
  4299. totalStringLength += str->GetLength();
  4300. }
  4301. uint32 keyLength = totalStringLength + (arrayLength - 1) * 3 + 1;
  4302. char16* key = RecyclerNewArray(scriptContext->GetRecycler(), char16, keyLength);
  4303. char16* ptr = key;
  4304. charcount_t remainingSpace = keyLength;
  4305. // Get first item before loop - there always must be at least one item
  4306. rawArray->DirectGetItemAt(0, &var);
  4307. str = JavascriptString::FromVar(var);
  4308. charcount_t len = str->GetLength();
  4309. js_wmemcpy_s(ptr, remainingSpace, str->GetSz(), len);
  4310. ptr += len;
  4311. remainingSpace -= len;
  4312. // Append a delimiter and the rest of the items
  4313. for (uint32 i = 1; i < arrayLength; i++)
  4314. {
  4315. len = 3; // strlen(_u("${}"));
  4316. js_wmemcpy_s(ptr, remainingSpace, _u("${}"), len);
  4317. ptr += len;
  4318. remainingSpace -= len;
  4319. rawArray->DirectGetItemAt(i, &var);
  4320. str = JavascriptString::FromVar(var);
  4321. len = str->GetLength();
  4322. js_wmemcpy_s(ptr, remainingSpace, str->GetSz(), len);
  4323. ptr += len;
  4324. remainingSpace -= len;
  4325. }
  4326. // Ensure string is terminated
  4327. key[keyLength - 1] = _u('\0');
  4328. return key;
  4329. }
  4330. #endif
  4331. bool StringTemplateCallsiteObjectComparer<ParseNodePtr>::Equals(ParseNodePtr x, RecyclerWeakReference<Js::RecyclableObject>* y)
  4332. {
  4333. Assert(x != nullptr);
  4334. Assert(x->nop == knopStrTemplate);
  4335. Js::RecyclableObject* obj = y->Get();
  4336. // If the weak reference is dead, we can't be equal.
  4337. if (obj == nullptr)
  4338. {
  4339. return false;
  4340. }
  4341. Js::ES5Array* callsite = Js::ES5Array::FromVar(obj);
  4342. uint32 length = callsite->GetLength();
  4343. Js::Var element;
  4344. Js::JavascriptString* str;
  4345. IdentPtr pid;
  4346. // If the length of string literals is different, these callsite objects are not equal.
  4347. if (x->AsParseNodeStrTemplate()->countStringLiterals != length)
  4348. {
  4349. return false;
  4350. }
  4351. JS_REENTRANCY_LOCK(reentrancyLock, callsite->GetScriptContext()->GetThreadContext());
  4352. Unused(reentrancyLock);
  4353. element = Js::JavascriptOperators::OP_GetProperty(callsite, Js::PropertyIds::raw, callsite->GetScriptContext());
  4354. Js::ES5Array* rawArray = Js::ES5Array::FromVar(element);
  4355. // Length of the raw strings should be the same as the cooked string literals.
  4356. AssertOrFailFast(length != 0 && length == rawArray->GetLength());
  4357. x = x->AsParseNodeStrTemplate()->pnodeStringRawLiterals;
  4358. for (uint32 i = 0; i < length - 1; i++)
  4359. {
  4360. BOOL hasElem = rawArray->DirectGetItemAt(i, &element);
  4361. AssertOrFailFast(hasElem);
  4362. str = Js::JavascriptString::FromVar(element);
  4363. Assert(x->nop == knopList);
  4364. Assert(x->AsParseNodeBin()->pnode1->nop == knopStr);
  4365. pid = x->AsParseNodeBin()->pnode1->AsParseNodePid()->pid;
  4366. // If strings have different length, they aren't equal
  4367. if (pid->Cch() != str->GetLength())
  4368. {
  4369. return false;
  4370. }
  4371. // If the strings at this index are not equal, the callsite objects are not equal.
  4372. if (!JsUtil::CharacterBuffer<char16>::StaticEquals(pid->Psz(), str->GetSz(), str->GetLength()))
  4373. {
  4374. return false;
  4375. }
  4376. x = x->AsParseNodeBin()->pnode2;
  4377. }
  4378. // There should be one more string in the callsite array - and the final string in the ParseNode
  4379. BOOL hasLastElem = rawArray->DirectGetItemAt(length - 1, &element);
  4380. AssertOrFailFast(hasLastElem);
  4381. str = Js::JavascriptString::FromVar(element);
  4382. Assert(x->nop == knopStr);
  4383. pid = x->AsParseNodePid()->pid;
  4384. // If strings have different length, they aren't equal
  4385. if (pid->Cch() != str->GetLength())
  4386. {
  4387. return false;
  4388. }
  4389. // If the strings at this index are not equal, the callsite objects are not equal.
  4390. if (!JsUtil::CharacterBuffer<char16>::StaticEquals(pid->Psz(), str->GetSz(), str->GetLength()))
  4391. {
  4392. return false;
  4393. }
  4394. return true;
  4395. }
  4396. bool StringTemplateCallsiteObjectComparer<ParseNodePtr>::Equals(ParseNodePtr x, ParseNodePtr y)
  4397. {
  4398. Assert(x != nullptr && y != nullptr);
  4399. Assert(x->nop == knopStrTemplate && y->nop == knopStrTemplate);
  4400. // If the ParseNode is the same, they are equal.
  4401. if (x == y)
  4402. {
  4403. return true;
  4404. }
  4405. x = x->AsParseNodeStrTemplate()->pnodeStringRawLiterals;
  4406. y = y->AsParseNodeStrTemplate()->pnodeStringRawLiterals;
  4407. // If one of the templates only includes one string value, the raw literals ParseNode will
  4408. // be a knopStr instead of knopList.
  4409. if (x->nop != y->nop)
  4410. {
  4411. return false;
  4412. }
  4413. const char16* pid_x;
  4414. const char16* pid_y;
  4415. while (x->nop == knopList)
  4416. {
  4417. // If y is knopStr here, that means x has more strings in the list than y does.
  4418. if (y->nop != knopList)
  4419. {
  4420. return false;
  4421. }
  4422. Assert(x->AsParseNodeBin()->pnode1->nop == knopStr);
  4423. Assert(y->AsParseNodeBin()->pnode1->nop == knopStr);
  4424. pid_x = x->AsParseNodeBin()->pnode1->AsParseNodePid()->pid->Psz();
  4425. pid_y = y->AsParseNodeBin()->pnode1->AsParseNodePid()->pid->Psz();
  4426. // If the pid values of each raw string don't match each other, these are different.
  4427. if (!DefaultComparer<const char16*>::Equals(pid_x, pid_y))
  4428. {
  4429. return false;
  4430. }
  4431. x = x->AsParseNodeBin()->pnode2;
  4432. y = y->AsParseNodeBin()->pnode2;
  4433. }
  4434. // If y is still knopList here, that means y has more strings in the list than x does.
  4435. if (y->nop != knopStr)
  4436. {
  4437. return false;
  4438. }
  4439. Assert(x->nop == knopStr);
  4440. pid_x = x->AsParseNodePid()->pid->Psz();
  4441. pid_y = y->AsParseNodePid()->pid->Psz();
  4442. // This is the final string in the raw literals list. Return true if they are equal.
  4443. return DefaultComparer<const char16*>::Equals(pid_x, pid_y);
  4444. }
  4445. hash_t StringTemplateCallsiteObjectComparer<ParseNodePtr>::GetHashCode(ParseNodePtr i)
  4446. {
  4447. hash_t hash = 0;
  4448. Assert(i != nullptr);
  4449. Assert(i->nop == knopStrTemplate);
  4450. i = i->AsParseNodeStrTemplate()->pnodeStringRawLiterals;
  4451. const char16* pid;
  4452. while (i->nop == knopList)
  4453. {
  4454. Assert(i->AsParseNodeBin()->pnode1->nop == knopStr);
  4455. pid = i->AsParseNodeBin()->pnode1->AsParseNodePid()->pid->Psz();
  4456. hash ^= DefaultComparer<const char16*>::GetHashCode(pid);
  4457. hash ^= DefaultComparer<const char16*>::GetHashCode(_u("${}"));
  4458. i = i->AsParseNodeBin()->pnode2;
  4459. }
  4460. Assert(i->nop == knopStr);
  4461. pid = i->AsParseNodePid()->pid->Psz();
  4462. hash ^= DefaultComparer<const char16*>::GetHashCode(pid);
  4463. return hash;
  4464. }
  4465. bool StringTemplateCallsiteObjectComparer<RecyclerWeakReference<Js::RecyclableObject>*>::Equals(RecyclerWeakReference<Js::RecyclableObject>* x, ParseNodePtr y)
  4466. {
  4467. return StringTemplateCallsiteObjectComparer<ParseNodePtr>::Equals(y, x);
  4468. }
  4469. bool StringTemplateCallsiteObjectComparer<RecyclerWeakReference<Js::RecyclableObject>*>::Equals(RecyclerWeakReference<Js::RecyclableObject>* x, RecyclerWeakReference<Js::RecyclableObject>* y)
  4470. {
  4471. Js::RecyclableObject* objLeft = x->Get();
  4472. Js::RecyclableObject* objRight = y->Get();
  4473. // If either WeakReference is dead, we can't be equal to anything.
  4474. if (objLeft == nullptr || objRight == nullptr)
  4475. {
  4476. return false;
  4477. }
  4478. // If the Var pointers are the same, they are equal.
  4479. if (objLeft == objRight)
  4480. {
  4481. return true;
  4482. }
  4483. Js::ES5Array* arrayLeft = Js::ES5Array::FromVar(objLeft);
  4484. Js::ES5Array* arrayRight = Js::ES5Array::FromVar(objRight);
  4485. uint32 lengthLeft = arrayLeft->GetLength();
  4486. uint32 lengthRight = arrayRight->GetLength();
  4487. Js::Var varLeft;
  4488. Js::Var varRight;
  4489. // If the length of string literals is different, these callsite objects are not equal.
  4490. if (lengthLeft != lengthRight)
  4491. {
  4492. return false;
  4493. }
  4494. AssertOrFailFast(lengthLeft != 0 && lengthRight != 0);
  4495. JS_REENTRANCY_LOCK(reentrancyLock, arrayLeft->GetScriptContext()->GetThreadContext());
  4496. Unused(reentrancyLock);
  4497. // Change to the set of raw strings.
  4498. varLeft = Js::JavascriptOperators::OP_GetProperty(arrayLeft, Js::PropertyIds::raw, arrayLeft->GetScriptContext());
  4499. arrayLeft = Js::ES5Array::FromVar(varLeft);
  4500. varRight = Js::JavascriptOperators::OP_GetProperty(arrayRight, Js::PropertyIds::raw, arrayRight->GetScriptContext());
  4501. arrayRight = Js::ES5Array::FromVar(varRight);
  4502. // Length of the raw strings should be the same as the cooked string literals.
  4503. AssertOrFailFast(lengthLeft == arrayLeft->GetLength());
  4504. AssertOrFailFast(lengthRight == arrayRight->GetLength());
  4505. for (uint32 i = 0; i < lengthLeft; i++)
  4506. {
  4507. BOOL hasLeft = arrayLeft->DirectGetItemAt(i, &varLeft);
  4508. AssertOrFailFast(hasLeft);
  4509. BOOL hasRight = arrayRight->DirectGetItemAt(i, &varRight);
  4510. AssertOrFailFast(hasRight);
  4511. // If the strings at this index are not equal, the callsite objects are not equal.
  4512. if (!Js::JavascriptString::Equals(JavascriptString::FromVar(varLeft), JavascriptString::FromVar(varRight)))
  4513. {
  4514. return false;
  4515. }
  4516. }
  4517. return true;
  4518. }
  4519. hash_t StringTemplateCallsiteObjectComparer<RecyclerWeakReference<Js::RecyclableObject>*>::GetHashCode(RecyclerWeakReference<Js::RecyclableObject>* o)
  4520. {
  4521. hash_t hash = 0;
  4522. Js::RecyclableObject* obj = o->Get();
  4523. if (obj == nullptr)
  4524. {
  4525. return hash;
  4526. }
  4527. JS_REENTRANCY_LOCK(reentrancyLock, obj->GetScriptContext()->GetThreadContext());
  4528. Unused(reentrancyLock);
  4529. Js::ES5Array* callsite = Js::ES5Array::FromVar(obj);
  4530. Js::Var var = Js::JavascriptOperators::OP_GetProperty(callsite, Js::PropertyIds::raw, callsite->GetScriptContext());
  4531. Js::ES5Array* rawArray = Js::ES5Array::FromVar(var);
  4532. AssertOrFailFast(rawArray->GetLength() > 0);
  4533. rawArray->DirectGetItemAt(0, &var);
  4534. Js::JavascriptString* str = Js::JavascriptString::FromVar(var);
  4535. hash ^= DefaultComparer<const char16*>::GetHashCode(str->GetSz());
  4536. for (uint32 i = 1; i < rawArray->GetLength(); i++)
  4537. {
  4538. hash ^= DefaultComparer<const char16*>::GetHashCode(_u("${}"));
  4539. BOOL hasItem = rawArray->DirectGetItemAt(i, &var);
  4540. AssertOrFailFast(hasItem);
  4541. str = Js::JavascriptString::FromVar(var);
  4542. hash ^= DefaultComparer<const char16*>::GetHashCode(str->GetSz());
  4543. }
  4544. return hash;
  4545. }
  4546. DynamicType * JavascriptLibrary::GetObjectLiteralType(uint16 requestedInlineSlotCapacity)
  4547. {
  4548. if (requestedInlineSlotCapacity <= MaxPreInitializedObjectTypeInlineSlotCount)
  4549. {
  4550. return objectTypes[DynamicTypeHandler::RoundUpInlineSlotCapacity(requestedInlineSlotCapacity) / InlineSlotCountIncrement];
  4551. }
  4552. else
  4553. {
  4554. return objectTypes[PreInitializedObjectTypeCount - 1];
  4555. }
  4556. }
  4557. DynamicType * JavascriptLibrary::GetObjectHeaderInlinedLiteralType(uint16 requestedInlineSlotCapacity)
  4558. {
  4559. Assert(requestedInlineSlotCapacity <= MaxPreInitializedObjectHeaderInlinedTypeInlineSlotCount);
  4560. return
  4561. objectHeaderInlinedTypes[
  4562. (
  4563. DynamicTypeHandler::RoundUpObjectHeaderInlinedInlineSlotCapacity(requestedInlineSlotCapacity) -
  4564. DynamicTypeHandler::GetObjectHeaderInlinableSlotCapacity()
  4565. ) / InlineSlotCountIncrement];
  4566. }
  4567. HeapArgumentsObject* JavascriptLibrary::CreateHeapArguments(Var frameObj, uint32 formalCount, bool isStrictMode)
  4568. {
  4569. AssertMsg(heapArgumentsType, "Where's heapArgumentsType?");
  4570. Recycler *recycler = this->GetRecycler();
  4571. EnsureArrayPrototypeValuesFunction(); //InitializeArrayPrototype can be delay loaded, which could prevent us from access to array.prototype.values
  4572. DynamicType * argumentsType = nullptr;
  4573. if (isStrictMode)
  4574. {
  4575. //TODO: Make DictionaryTypeHandler shareable - So that Arguments' type can be cached on the javascriptLibrary.
  4576. DictionaryTypeHandler * dictTypeHandlerForArgumentsInStrictMode = DictionaryTypeHandler::CreateTypeHandlerForArgumentsInStrictMode(recycler, scriptContext);
  4577. argumentsType = DynamicType::New(scriptContext, TypeIds_Arguments, objectPrototype, nullptr,
  4578. dictTypeHandlerForArgumentsInStrictMode, false, false);
  4579. }
  4580. else
  4581. {
  4582. argumentsType = heapArgumentsType;
  4583. }
  4584. return RecyclerNew(recycler, HeapArgumentsObject, recycler,
  4585. frameObj != GetNull() ? static_cast<ActivationObject*>(frameObj) : nullptr,
  4586. formalCount, argumentsType);
  4587. }
  4588. JavascriptArray* JavascriptLibrary::CreateArray()
  4589. {
  4590. AssertMsg(arrayType, "Where's arrayType?");
  4591. return JavascriptArray::New<Var, JavascriptArray>(this->GetRecycler(), arrayType);
  4592. }
  4593. JavascriptArray* JavascriptLibrary::CreateArray(uint32 length)
  4594. {
  4595. AssertMsg(arrayType, "Where's arrayType?");
  4596. JavascriptArray* arr = JavascriptArray::New<Var, JavascriptArray, 0>(length, arrayType, this->GetRecycler());
  4597. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_ARRAY(arr));
  4598. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  4599. arr->CheckForceES5Array();
  4600. #endif
  4601. return arr;
  4602. }
  4603. JavascriptArray *JavascriptLibrary::CreateArrayOnStack(void *const stackAllocationPointer)
  4604. {
  4605. return JavascriptArray::New<JavascriptArray, 0>(stackAllocationPointer, 0, arrayType);
  4606. }
  4607. JavascriptNativeIntArray* JavascriptLibrary::CreateNativeIntArray()
  4608. {
  4609. AssertMsg(nativeIntArrayType, "Where's nativeIntArrayType?");
  4610. return JavascriptArray::New<int32, JavascriptNativeIntArray>(this->GetRecycler(), nativeIntArrayType);
  4611. }
  4612. JavascriptNativeIntArray* JavascriptLibrary::CreateNativeIntArray(uint32 length)
  4613. {
  4614. AssertMsg(nativeIntArrayType, "Where's nativeIntArrayType?");
  4615. JavascriptNativeIntArray* arr = JavascriptArray::New<int32, JavascriptNativeIntArray, 0>(length, nativeIntArrayType, this->GetRecycler());
  4616. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_ARRAY(arr));
  4617. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  4618. arr->CheckForceES5Array();
  4619. #endif
  4620. return arr;
  4621. }
  4622. JavascriptNativeFloatArray* JavascriptLibrary::CreateNativeFloatArray()
  4623. {
  4624. AssertMsg(nativeFloatArrayType, "Where's nativeFloatArrayType?");
  4625. return JavascriptArray::New<double, JavascriptNativeFloatArray>(this->GetRecycler(), nativeFloatArrayType);
  4626. }
  4627. JavascriptNativeFloatArray* JavascriptLibrary::CreateNativeFloatArray(uint32 length)
  4628. {
  4629. AssertMsg(nativeFloatArrayType, "Where's nativeIntArrayType?");
  4630. JavascriptNativeFloatArray* arr = JavascriptArray::New<double, JavascriptNativeFloatArray, 0>(length, nativeFloatArrayType, this->GetRecycler());
  4631. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_ARRAY(arr));
  4632. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  4633. arr->CheckForceES5Array();
  4634. #endif
  4635. return arr;
  4636. }
  4637. JavascriptArray* JavascriptLibrary::CreateArrayLiteral(uint32 length)
  4638. {
  4639. AssertMsg(arrayType, "Where's arrayType?");
  4640. JavascriptArray* arr = JavascriptArray::NewLiteral<Var, JavascriptArray, 0>(length, arrayType, this->GetRecycler());
  4641. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_ARRAY(arr));
  4642. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  4643. arr->CheckForceES5Array();
  4644. #endif
  4645. return arr;
  4646. }
  4647. JavascriptNativeIntArray* JavascriptLibrary::CreateNativeIntArrayLiteral(uint32 length)
  4648. {
  4649. AssertMsg(nativeIntArrayType, "Where's arrayType?");
  4650. JavascriptNativeIntArray* arr = JavascriptArray::NewLiteral<int32, JavascriptNativeIntArray, 0>(length, nativeIntArrayType, this->GetRecycler());
  4651. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_ARRAY(arr));
  4652. return arr;
  4653. }
  4654. #if ENABLE_COPYONACCESS_ARRAY
  4655. JavascriptNativeIntArray* JavascriptLibrary::CreateCopyOnAccessNativeIntArrayLiteral(ArrayCallSiteInfo *arrayInfo, FunctionBody *functionBody, const Js::AuxArray<int32> *ints)
  4656. {
  4657. AssertMsg(copyOnAccessNativeIntArrayType, "Where's arrayType?");
  4658. JavascriptNativeIntArray* arr = JavascriptArray::NewCopyOnAccessLiteral<int32, JavascriptCopyOnAccessNativeIntArray, 0>(copyOnAccessNativeIntArrayType, arrayInfo, functionBody, ints, this->GetRecycler());
  4659. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_ARRAY(arr));
  4660. return arr;
  4661. }
  4662. #endif
  4663. JavascriptNativeFloatArray* JavascriptLibrary::CreateNativeFloatArrayLiteral(uint32 length)
  4664. {
  4665. AssertMsg(nativeFloatArrayType, "Where's arrayType?");
  4666. JavascriptNativeFloatArray* arr = JavascriptArray::NewLiteral<double, JavascriptNativeFloatArray, 0>(length, nativeFloatArrayType, this->GetRecycler());
  4667. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_ARRAY(arr));
  4668. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  4669. arr->CheckForceES5Array();
  4670. #endif
  4671. return arr;
  4672. }
  4673. JavascriptArray* JavascriptLibrary::CreateArray(uint32 length, uint32 size)
  4674. {
  4675. AssertMsg(arrayType, "Where's arrayType?");
  4676. JavascriptArray* arr = RecyclerNew(this->GetRecycler(), JavascriptArray, length, size, arrayType);
  4677. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_ARRAY(arr));
  4678. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  4679. arr->CheckForceES5Array();
  4680. #endif
  4681. return arr;
  4682. }
  4683. ArrayBuffer* JavascriptLibrary::CreateArrayBuffer(uint32 length)
  4684. {
  4685. ArrayBuffer* arr = JavascriptArrayBuffer::Create(length, arrayBufferType);
  4686. return arr;
  4687. }
  4688. ArrayBuffer* JavascriptLibrary::CreateArrayBuffer(byte* buffer, uint32 length)
  4689. {
  4690. ArrayBuffer* arr = JavascriptArrayBuffer::Create(buffer, length, arrayBufferType);
  4691. return arr;
  4692. }
  4693. #ifdef ENABLE_WASM
  4694. Js::WebAssemblyArrayBuffer* JavascriptLibrary::CreateWebAssemblyArrayBuffer(uint32 length)
  4695. {
  4696. return WebAssemblyArrayBuffer::Create(nullptr, length, arrayBufferType);
  4697. }
  4698. Js::WebAssemblyArrayBuffer* JavascriptLibrary::CreateWebAssemblyArrayBuffer(byte* buffer, uint32 length)
  4699. {
  4700. return WebAssemblyArrayBuffer::Create(buffer, length, arrayBufferType);
  4701. }
  4702. #ifdef ENABLE_WASM_THREADS
  4703. WebAssemblySharedArrayBuffer* JavascriptLibrary::CreateWebAssemblySharedArrayBuffer(uint32 length, uint32 maxLength)
  4704. {
  4705. return WebAssemblySharedArrayBuffer::Create(length, maxLength, sharedArrayBufferType);
  4706. }
  4707. WebAssemblySharedArrayBuffer* JavascriptLibrary::CreateWebAssemblySharedArrayBuffer(SharedContents *contents)
  4708. {
  4709. return WebAssemblySharedArrayBuffer::Create(contents, sharedArrayBufferType);
  4710. }
  4711. #endif
  4712. #endif
  4713. SharedArrayBuffer* JavascriptLibrary::CreateSharedArrayBuffer(uint32 length)
  4714. {
  4715. return JavascriptSharedArrayBuffer::Create(length, sharedArrayBufferType);
  4716. }
  4717. SharedArrayBuffer* JavascriptLibrary::CreateSharedArrayBuffer(SharedContents *contents)
  4718. {
  4719. #ifdef ENABLE_WASM_THREADS
  4720. if (contents && contents->IsWebAssembly())
  4721. {
  4722. return CreateWebAssemblySharedArrayBuffer(contents);
  4723. }
  4724. #endif
  4725. return JavascriptSharedArrayBuffer::Create(contents, sharedArrayBufferType);
  4726. }
  4727. ArrayBuffer* JavascriptLibrary::CreateProjectionArraybuffer(uint32 length)
  4728. {
  4729. ArrayBuffer* arr = ProjectionArrayBuffer::Create(length, arrayBufferType);
  4730. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(arr));
  4731. return arr;
  4732. }
  4733. ArrayBuffer* JavascriptLibrary::CreateProjectionArraybuffer(byte* buffer, uint32 length)
  4734. {
  4735. ArrayBuffer* arr = ProjectionArrayBuffer::Create(buffer, length, arrayBufferType);
  4736. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(arr));
  4737. return arr;
  4738. }
  4739. DataView* JavascriptLibrary::CreateDataView(ArrayBufferBase* arrayBuffer, uint32 offset, uint32 length)
  4740. {
  4741. DataView* dataView = RecyclerNew(this->GetRecycler(), DataView, arrayBuffer, offset, length, dataViewType);
  4742. return dataView;
  4743. }
  4744. JavascriptBoolean* JavascriptLibrary::CreateBoolean(BOOL value)
  4745. {
  4746. AssertMsg(booleanTrue, "Where's booleanTrue?");
  4747. AssertMsg(booleanFalse, "Where's booleanFalse?");
  4748. return value ? booleanTrue : booleanFalse;
  4749. }
  4750. JavascriptDate* JavascriptLibrary::CreateDate()
  4751. {
  4752. AssertMsg(dateType, "Where's dateType?");
  4753. return RecyclerNew(this->GetRecycler(), JavascriptDate, 0, dateType);
  4754. }
  4755. JavascriptDate* JavascriptLibrary::CreateDate(double value)
  4756. {
  4757. AssertMsg(dateType, "Where's dateType?");
  4758. return RecyclerNew(this->GetRecycler(), JavascriptDate, value, dateType);
  4759. }
  4760. JavascriptDate* JavascriptLibrary::CreateDate(SYSTEMTIME* pst)
  4761. {
  4762. AssertMsg(dateType, "Where's dateType?");
  4763. double value = DateImplementation::TimeFromSt(pst);
  4764. return CreateDate(value);
  4765. }
  4766. JavascriptMap* JavascriptLibrary::CreateMap()
  4767. {
  4768. AssertMsg(mapType, "Where's mapType?");
  4769. return RecyclerNew(this->GetRecycler(), JavascriptMap, mapType);
  4770. }
  4771. JavascriptSet* JavascriptLibrary::CreateSet()
  4772. {
  4773. AssertMsg(setType, "Where's setType?");
  4774. return RecyclerNew(this->GetRecycler(), JavascriptSet, setType);
  4775. }
  4776. JavascriptWeakMap* JavascriptLibrary::CreateWeakMap()
  4777. {
  4778. AssertMsg(weakMapType, "Where's weakMapType?");
  4779. return RecyclerNewFinalized(this->GetRecycler(), JavascriptWeakMap, weakMapType);
  4780. }
  4781. JavascriptWeakSet* JavascriptLibrary::CreateWeakSet()
  4782. {
  4783. AssertMsg(weakSetType, "Where's weakSetType?");
  4784. return RecyclerNewFinalized(this->GetRecycler(), JavascriptWeakSet, weakSetType);
  4785. }
  4786. JavascriptPromise* JavascriptLibrary::CreatePromise()
  4787. {
  4788. AssertMsg(promiseType, "Where's promiseType?");
  4789. return RecyclerNew(this->GetRecycler(), JavascriptPromise, promiseType);
  4790. }
  4791. JavascriptPromiseAsyncSpawnExecutorFunction* JavascriptLibrary::CreatePromiseAsyncSpawnExecutorFunction(JavascriptGenerator* generator, Var target)
  4792. {
  4793. JavascriptMethod entryPoint = JavascriptPromise::EntryJavascriptPromiseAsyncSpawnExecutorFunction;
  4794. FunctionInfo* functionInfo = RecyclerNew(this->GetRecycler(), FunctionInfo, entryPoint);
  4795. DynamicType* type = CreateDeferredPrototypeFunctionType(this->inDispatchProfileMode ? ProfileEntryThunk : entryPoint);
  4796. JavascriptPromiseAsyncSpawnExecutorFunction* function = RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, JavascriptPromiseAsyncSpawnExecutorFunction, type, functionInfo, generator, target);
  4797. return function;
  4798. }
  4799. JavascriptPromiseAsyncSpawnStepArgumentExecutorFunction* JavascriptLibrary::CreatePromiseAsyncSpawnStepArgumentExecutorFunction(JavascriptMethod entryPoint, JavascriptGenerator* generator, Var argument, Var resolve, Var reject, bool isReject)
  4800. {
  4801. FunctionInfo* functionInfo = RecyclerNew(this->GetRecycler(), FunctionInfo, entryPoint);
  4802. DynamicType* type = CreateDeferredPrototypeFunctionType(this->inDispatchProfileMode ? ProfileEntryThunk : entryPoint);
  4803. JavascriptPromiseAsyncSpawnStepArgumentExecutorFunction* function = RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, JavascriptPromiseAsyncSpawnStepArgumentExecutorFunction, type, functionInfo, generator, argument, resolve, reject, isReject);
  4804. return function;
  4805. }
  4806. JavascriptGenerator* JavascriptLibrary::CreateGenerator(Arguments& args, ScriptFunction* scriptFunction, RecyclableObject* prototype)
  4807. {
  4808. Assert(scriptContext->GetConfig()->IsES6GeneratorsEnabled());
  4809. DynamicType* generatorType = CreateGeneratorType(prototype);
  4810. return JavascriptGenerator::New(this->GetRecycler(), generatorType, args, scriptFunction);
  4811. }
  4812. JavascriptError* JavascriptLibrary::CreateError()
  4813. {
  4814. AssertMsg(errorType, "Where's errorType?");
  4815. JavascriptError *pError = RecyclerNew(this->GetRecycler(), JavascriptError, errorType);
  4816. JavascriptError::SetErrorType(pError, kjstError);
  4817. return pError;
  4818. }
  4819. JavascriptSymbol* JavascriptLibrary::CreateSymbol(JavascriptString* description)
  4820. {
  4821. return this->CreateSymbol(description->GetString(), (int)description->GetLength());
  4822. }
  4823. JavascriptSymbol* JavascriptLibrary::CreateSymbol(const char16* description, int descriptionLength)
  4824. {
  4825. ENTER_PINNED_SCOPE(const Js::PropertyRecord, propertyRecord);
  4826. propertyRecord = this->scriptContext->GetThreadContext()->UncheckedAddPropertyId(description, descriptionLength, /*bind*/false, /*isSymbol*/true);
  4827. LEAVE_PINNED_SCOPE();
  4828. return this->CreateSymbol(propertyRecord);
  4829. }
  4830. JavascriptSymbol* JavascriptLibrary::CreateSymbol(const PropertyRecord* propertyRecord)
  4831. {
  4832. AssertMsg(symbolTypeStatic, "Where's symbolTypeStatic?");
  4833. SymbolCacheMap* symbolMap = this->EnsureSymbolMap();
  4834. JavascriptSymbol* symbol = RecyclerNew(this->GetRecycler(), JavascriptSymbol, propertyRecord, symbolTypeStatic);
  4835. symbolMap->Item(propertyRecord->GetPropertyId(), recycler->CreateWeakReferenceHandle(symbol));
  4836. return symbol;
  4837. }
  4838. JavascriptError* JavascriptLibrary::CreateExternalError(ErrorTypeEnum errorTypeEnum)
  4839. {
  4840. DynamicType* baseErrorType = NULL;
  4841. switch (errorTypeEnum)
  4842. {
  4843. case kjstError:
  4844. default:
  4845. baseErrorType = errorType;
  4846. break;
  4847. case kjstEvalError:
  4848. baseErrorType = evalErrorType;
  4849. break;
  4850. case kjstRangeError:
  4851. baseErrorType = rangeErrorType;
  4852. break;
  4853. case kjstReferenceError:
  4854. baseErrorType = referenceErrorType;
  4855. break;
  4856. case kjstSyntaxError:
  4857. baseErrorType = syntaxErrorType;
  4858. break;
  4859. case kjstTypeError:
  4860. baseErrorType = typeErrorType;
  4861. break;
  4862. case kjstURIError:
  4863. baseErrorType = uriErrorType;
  4864. break;
  4865. case kjstWebAssemblyCompileError:
  4866. baseErrorType = webAssemblyCompileErrorType;
  4867. break;
  4868. case kjstWebAssemblyRuntimeError:
  4869. baseErrorType = webAssemblyRuntimeErrorType;
  4870. break;
  4871. case kjstWebAssemblyLinkError:
  4872. baseErrorType = webAssemblyLinkErrorType;
  4873. break;
  4874. }
  4875. JavascriptError *pError = RecyclerNew(recycler, JavascriptError, baseErrorType, TRUE);
  4876. JavascriptError::SetErrorType(pError, errorTypeEnum);
  4877. return pError;
  4878. }
  4879. #define CREATE_ERROR(name, field, id) \
  4880. JavascriptError* JavascriptLibrary::Create##name() \
  4881. { \
  4882. AssertMsg(field, "Where's field?"); \
  4883. JavascriptError *pError = RecyclerNew(this->GetRecycler(), JavascriptError, field); \
  4884. JavascriptError::SetErrorType(pError, id); \
  4885. return pError; \
  4886. }
  4887. CREATE_ERROR(EvalError, evalErrorType, kjstEvalError);
  4888. CREATE_ERROR(RangeError, rangeErrorType, kjstRangeError);
  4889. CREATE_ERROR(ReferenceError, referenceErrorType, kjstReferenceError);
  4890. CREATE_ERROR(SyntaxError, syntaxErrorType, kjstSyntaxError);
  4891. CREATE_ERROR(TypeError, typeErrorType, kjstTypeError);
  4892. CREATE_ERROR(URIError, uriErrorType, kjstURIError);
  4893. CREATE_ERROR(WebAssemblyCompileError, webAssemblyCompileErrorType, kjstWebAssemblyCompileError);
  4894. CREATE_ERROR(WebAssemblyRuntimeError, webAssemblyRuntimeErrorType, kjstWebAssemblyRuntimeError);
  4895. CREATE_ERROR(WebAssemblyLinkError, webAssemblyLinkErrorType, kjstWebAssemblyLinkError);
  4896. #undef CREATE_ERROR
  4897. JavascriptError* JavascriptLibrary::CreateStackOverflowError()
  4898. {
  4899. #if DBG
  4900. // If we are doing a heap enum, we need to be able to allocate the error object.
  4901. Recycler::AutoAllowAllocationDuringHeapEnum autoAllowAllocationDuringHeapEnum(this->GetRecycler());
  4902. #endif
  4903. JavascriptError* stackOverflowError = scriptContext->GetLibrary()->CreateError();
  4904. JavascriptError::SetErrorMessage(stackOverflowError, VBSERR_OutOfStack, NULL, scriptContext);
  4905. return stackOverflowError;
  4906. }
  4907. JavascriptError* JavascriptLibrary::CreateOutOfMemoryError()
  4908. {
  4909. JavascriptError* outOfMemoryError = scriptContext->GetLibrary()->CreateError();
  4910. JavascriptError::SetErrorMessage(outOfMemoryError, VBSERR_OutOfMemory, NULL, scriptContext);
  4911. return outOfMemoryError;
  4912. }
  4913. JavascriptFunction* JavascriptLibrary::CreateNonProfiledFunction(FunctionInfo * functionInfo)
  4914. {
  4915. Assert(functionInfo->GetAttributes() & FunctionInfo::DoNotProfile);
  4916. return RecyclerNew(this->GetRecycler(), RuntimeFunction,
  4917. CreateDeferredPrototypeFunctionTypeNoProfileThunk(functionInfo->GetOriginalEntryPoint()),
  4918. functionInfo);
  4919. }
  4920. ScriptFunction* JavascriptLibrary::CreateScriptFunction(FunctionProxy * proxy)
  4921. {
  4922. ScriptFunctionType* deferredPrototypeType = proxy->EnsureDeferredPrototypeType();
  4923. return RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, ScriptFunction, proxy, deferredPrototypeType);
  4924. }
  4925. AsmJsScriptFunction* JavascriptLibrary::CreateAsmJsScriptFunction(FunctionProxy * proxy)
  4926. {
  4927. ScriptFunctionType* deferredPrototypeType = proxy->EnsureDeferredPrototypeType();
  4928. return RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, AsmJsScriptFunction, proxy, deferredPrototypeType);
  4929. }
  4930. #ifdef ENABLE_WASM
  4931. WasmScriptFunction * JavascriptLibrary::CreateWasmScriptFunction(FunctionProxy* proxy)
  4932. {
  4933. ScriptFunctionType* deferredPrototypeType = proxy->EnsureDeferredPrototypeType();
  4934. return RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, WasmScriptFunction, proxy, deferredPrototypeType);
  4935. }
  4936. #endif
  4937. ScriptFunctionWithInlineCache* JavascriptLibrary::CreateScriptFunctionWithInlineCache(FunctionProxy * proxy)
  4938. {
  4939. ScriptFunctionType* deferredPrototypeType = proxy->EnsureDeferredPrototypeType();
  4940. return RecyclerNewWithInfoBits(this->GetRecycler(), (Memory::ObjectInfoBits)(EnumFunctionClass | Memory::FinalizableObjectBits), ScriptFunctionWithInlineCache, proxy, deferredPrototypeType);
  4941. }
  4942. GeneratorVirtualScriptFunction* JavascriptLibrary::CreateGeneratorVirtualScriptFunction(FunctionProxy * proxy)
  4943. {
  4944. ScriptFunctionType* deferredPrototypeType = proxy->EnsureDeferredPrototypeType();
  4945. return RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, GeneratorVirtualScriptFunction, proxy, deferredPrototypeType);
  4946. }
  4947. DynamicType * JavascriptLibrary::CreateGeneratorType(RecyclableObject* prototype)
  4948. {
  4949. return DynamicType::New(scriptContext, TypeIds_Generator, prototype, nullptr, NullTypeHandler<false>::GetDefaultInstance());
  4950. }
  4951. template <class MethodType>
  4952. JavascriptExternalFunction* JavascriptLibrary::CreateIdMappedExternalFunction(MethodType entryPoint, DynamicType *pPrototypeType)
  4953. {
  4954. return RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, JavascriptExternalFunction, entryPoint, pPrototypeType);
  4955. }
  4956. JavascriptGeneratorFunction* JavascriptLibrary::CreateGeneratorFunction(JavascriptMethod entryPoint, GeneratorVirtualScriptFunction* scriptFunction)
  4957. {
  4958. Assert(scriptContext->GetConfig()->IsES6GeneratorsEnabled());
  4959. DynamicType* type = CreateDeferredPrototypeGeneratorFunctionType(entryPoint, scriptFunction->IsAnonymousFunction());
  4960. return RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, JavascriptGeneratorFunction, type, scriptFunction);
  4961. }
  4962. JavascriptGeneratorFunction* JavascriptLibrary::CreateGeneratorFunction(JavascriptMethod entryPoint, bool isAnonymousFunction)
  4963. {
  4964. Assert(scriptContext->GetConfig()->IsES6GeneratorsEnabled());
  4965. DynamicType* type = CreateDeferredPrototypeGeneratorFunctionType(entryPoint, isAnonymousFunction);
  4966. return RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, JavascriptGeneratorFunction, type, nullptr);
  4967. }
  4968. JavascriptAsyncFunction* JavascriptLibrary::CreateAsyncFunction(JavascriptMethod entryPoint, GeneratorVirtualScriptFunction* scriptFunction)
  4969. {
  4970. DynamicType* type = CreateDeferredPrototypeAsyncFunctionType(entryPoint, scriptFunction->IsAnonymousFunction());
  4971. return RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, JavascriptAsyncFunction, type, scriptFunction);
  4972. }
  4973. JavascriptAsyncFunction* JavascriptLibrary::CreateAsyncFunction(JavascriptMethod entryPoint, bool isAnonymousFunction)
  4974. {
  4975. DynamicType* type = CreateDeferredPrototypeAsyncFunctionType(entryPoint, isAnonymousFunction);
  4976. return RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, JavascriptAsyncFunction, type, nullptr);
  4977. }
  4978. JavascriptExternalFunction* JavascriptLibrary::CreateStdCallExternalFunction(StdCallJavascriptMethod entryPoint, Var name, void *callbackState)
  4979. {
  4980. Var functionNameOrId = name;
  4981. if (JavascriptString::Is(name))
  4982. {
  4983. JavascriptString * functionName = JavascriptString::FromVar(name);
  4984. const char16 * functionNameBuffer = functionName->GetString();
  4985. int functionNameBufferLength = functionName->GetLengthAsSignedInt();
  4986. PropertyId functionNamePropertyId = scriptContext->GetOrAddPropertyIdTracked(functionNameBuffer, functionNameBufferLength);
  4987. functionNameOrId = TaggedInt::ToVarUnchecked(functionNamePropertyId);
  4988. }
  4989. AssertOrFailFast(TaggedInt::Is(functionNameOrId));
  4990. JavascriptExternalFunction* function = this->CreateIdMappedExternalFunction(entryPoint, stdCallFunctionWithDeferredPrototypeType);
  4991. function->SetFunctionNameId(functionNameOrId);
  4992. function->SetCallbackState(callbackState);
  4993. return function;
  4994. }
  4995. JavascriptPromiseCapabilitiesExecutorFunction* JavascriptLibrary::CreatePromiseCapabilitiesExecutorFunction(JavascriptMethod entryPoint, JavascriptPromiseCapability* capability)
  4996. {
  4997. Assert(scriptContext->GetConfig()->IsES6PromiseEnabled());
  4998. FunctionInfo* functionInfo = &Js::JavascriptPromise::EntryInfo::CapabilitiesExecutorFunction;
  4999. DynamicType* type = DynamicType::New(scriptContext, TypeIds_Function, functionPrototype, entryPoint, GetDeferredAnonymousFunctionTypeHandler());
  5000. JavascriptPromiseCapabilitiesExecutorFunction* function = RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, JavascriptPromiseCapabilitiesExecutorFunction, type, functionInfo, capability);
  5001. function->SetPropertyWithAttributes(PropertyIds::length, TaggedInt::ToVarUnchecked(2), PropertyConfigurable, nullptr);
  5002. return function;
  5003. }
  5004. JavascriptPromiseResolveOrRejectFunction* JavascriptLibrary::CreatePromiseResolveOrRejectFunction(JavascriptMethod entryPoint, JavascriptPromise* promise, bool isReject, JavascriptPromiseResolveOrRejectFunctionAlreadyResolvedWrapper* alreadyResolvedRecord)
  5005. {
  5006. Assert(scriptContext->GetConfig()->IsES6PromiseEnabled());
  5007. FunctionInfo* functionInfo = &Js::JavascriptPromise::EntryInfo::ResolveOrRejectFunction;
  5008. DynamicType* type = DynamicType::New(scriptContext, TypeIds_Function, functionPrototype, entryPoint, GetDeferredAnonymousFunctionTypeHandler());
  5009. JavascriptPromiseResolveOrRejectFunction* function = RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, JavascriptPromiseResolveOrRejectFunction, type, functionInfo, promise, isReject, alreadyResolvedRecord);
  5010. function->SetPropertyWithAttributes(PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable, nullptr);
  5011. return function;
  5012. }
  5013. JavascriptPromiseReactionTaskFunction* JavascriptLibrary::CreatePromiseReactionTaskFunction(JavascriptMethod entryPoint, JavascriptPromiseReaction* reaction, Var argument)
  5014. {
  5015. Assert(scriptContext->GetConfig()->IsES6PromiseEnabled());
  5016. FunctionInfo* functionInfo = RecyclerNew(this->GetRecycler(), FunctionInfo, entryPoint);
  5017. DynamicType* type = CreateDeferredPrototypeFunctionType(entryPoint);
  5018. return RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, JavascriptPromiseReactionTaskFunction, type, functionInfo, reaction, argument);
  5019. }
  5020. JavascriptPromiseResolveThenableTaskFunction* JavascriptLibrary::CreatePromiseResolveThenableTaskFunction(JavascriptMethod entryPoint, JavascriptPromise* promise, RecyclableObject* thenable, RecyclableObject* thenFunction)
  5021. {
  5022. Assert(scriptContext->GetConfig()->IsES6PromiseEnabled());
  5023. FunctionInfo* functionInfo = RecyclerNew(this->GetRecycler(), FunctionInfo, entryPoint);
  5024. DynamicType* type = CreateDeferredPrototypeFunctionType(entryPoint);
  5025. return RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, JavascriptPromiseResolveThenableTaskFunction, type, functionInfo, promise, thenable, thenFunction);
  5026. }
  5027. JavascriptPromiseAllResolveElementFunction* JavascriptLibrary::CreatePromiseAllResolveElementFunction(JavascriptMethod entryPoint, uint32 index, JavascriptArray* values, JavascriptPromiseCapability* capabilities, JavascriptPromiseAllResolveElementFunctionRemainingElementsWrapper* remainingElements)
  5028. {
  5029. Assert(scriptContext->GetConfig()->IsES6PromiseEnabled());
  5030. FunctionInfo* functionInfo = &Js::JavascriptPromise::EntryInfo::AllResolveElementFunction;
  5031. DynamicType* type = DynamicType::New(scriptContext, TypeIds_Function, functionPrototype, entryPoint, GetDeferredAnonymousFunctionTypeHandler());
  5032. JavascriptPromiseAllResolveElementFunction* function = RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, JavascriptPromiseAllResolveElementFunction, type, functionInfo, index, values, capabilities, remainingElements);
  5033. function->SetPropertyWithAttributes(PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable, nullptr);
  5034. return function;
  5035. }
  5036. JavascriptPromiseThenFinallyFunction* JavascriptLibrary::CreatePromiseThenFinallyFunction(JavascriptMethod entryPoint, RecyclableObject* OnFinally, RecyclableObject* Constructor, bool shouldThrow)
  5037. {
  5038. Assert(scriptContext->GetConfig()->IsES6PromiseEnabled());
  5039. FunctionInfo* functionInfo = RecyclerNew(this->GetRecycler(), FunctionInfo, entryPoint);
  5040. DynamicType* type = DynamicType::New(scriptContext, TypeIds_Function, functionPrototype, entryPoint, GetDeferredAnonymousFunctionTypeHandler());
  5041. JavascriptPromiseThenFinallyFunction* function = RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, JavascriptPromiseThenFinallyFunction, type, functionInfo, OnFinally, Constructor, shouldThrow);
  5042. function->SetPropertyWithAttributes(PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable, nullptr);
  5043. return function;
  5044. }
  5045. JavascriptPromiseThunkFinallyFunction* JavascriptLibrary::CreatePromiseThunkFinallyFunction(JavascriptMethod entryPoint, Var value, bool shouldThrow)
  5046. {
  5047. Assert(scriptContext->GetConfig()->IsES6PromiseEnabled());
  5048. FunctionInfo* functionInfo = RecyclerNew(this->GetRecycler(), FunctionInfo, entryPoint);
  5049. DynamicType* type = CreateDeferredPrototypeFunctionType(entryPoint);
  5050. return RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, JavascriptPromiseThunkFinallyFunction, type, functionInfo, value, shouldThrow);
  5051. }
  5052. JavascriptExternalFunction* JavascriptLibrary::CreateWrappedExternalFunction(JavascriptExternalFunction* wrappedFunction)
  5053. {
  5054. // The wrapped function will have profiling, so the wrapper function does not need it.
  5055. JavascriptExternalFunction* function = RecyclerNew(this->GetRecycler(), JavascriptExternalFunction, wrappedFunction, wrappedFunctionWithDeferredPrototypeType);
  5056. function->SetFunctionNameId(wrappedFunction->GetSourceString());
  5057. return function;
  5058. }
  5059. #if !FLOATVAR
  5060. JavascriptNumber * JavascriptLibrary::CreateNumber(double value, RecyclerJavascriptNumberAllocator * numberAllocator)
  5061. {
  5062. AssertMsg(numberTypeStatic, "Where's numberTypeStatic?");
  5063. return AllocatorNew(RecyclerJavascriptNumberAllocator, numberAllocator, JavascriptNumber, value, numberTypeStatic);
  5064. }
  5065. #if ENABLE_NATIVE_CODEGEN
  5066. JavascriptNumber* JavascriptLibrary::CreateCodeGenNumber(CodeGenNumberAllocator * alloc, double value)
  5067. {
  5068. AssertMsg(numberTypeStatic, "Where's numberTypeStatic?");
  5069. return new (alloc->Alloc()) JavascriptNumber(value, numberTypeStatic);
  5070. }
  5071. #endif
  5072. #endif
  5073. DynamicObject* JavascriptLibrary::CreateGeneratorConstructorPrototypeObject()
  5074. {
  5075. AssertMsg(generatorConstructorPrototypeObjectType, "Where's generatorConstructorPrototypeObjectType?");
  5076. DynamicObject * prototype = DynamicObject::New(this->GetRecycler(), generatorConstructorPrototypeObjectType);
  5077. // Generator functions' prototype objects are not created with a .constructor property
  5078. return prototype;
  5079. }
  5080. DynamicObject* JavascriptLibrary::CreateConstructorPrototypeObject(JavascriptFunction * constructor)
  5081. {
  5082. AssertMsg(constructorPrototypeObjectType, "Where's constructorPrototypeObjectType?");
  5083. DynamicObject * prototype = DynamicObject::New(this->GetRecycler(), constructorPrototypeObjectType);
  5084. AddMember(prototype, PropertyIds::constructor, constructor);
  5085. return prototype;
  5086. }
  5087. DynamicObject* JavascriptLibrary::CreateObject(
  5088. const bool allowObjectHeaderInlining,
  5089. const PropertyIndex requestedInlineSlotCapacity)
  5090. {
  5091. Assert(GetObjectType());
  5092. Assert(GetObjectHeaderInlinedType());
  5093. const bool useObjectHeaderInlining =
  5094. allowObjectHeaderInlining && FunctionBody::DoObjectHeaderInliningForObjectLiteral(requestedInlineSlotCapacity);
  5095. DynamicType *const type =
  5096. useObjectHeaderInlining
  5097. ? GetObjectHeaderInlinedLiteralType(requestedInlineSlotCapacity)
  5098. : GetObjectLiteralType(requestedInlineSlotCapacity);
  5099. return DynamicObject::New(GetRecycler(), type);
  5100. }
  5101. DynamicObject* JavascriptLibrary::CreateObject(DynamicTypeHandler * typeHandler)
  5102. {
  5103. return DynamicObject::New(this->GetRecycler(),
  5104. Js::DynamicType::New(scriptContext, Js::TypeIds_Object, this->GetObjectPrototype(),
  5105. RecyclableObject::DefaultEntryPoint, typeHandler, false, false));
  5106. }
  5107. DynamicType* JavascriptLibrary::CreateObjectType(RecyclableObject* prototype, Js::TypeId typeId, uint16 requestedInlineSlotCapacity)
  5108. {
  5109. const bool useObjectHeaderInlining = FunctionBody::DoObjectHeaderInliningForConstructor(requestedInlineSlotCapacity);
  5110. const uint16 offsetOfInlineSlots =
  5111. useObjectHeaderInlining
  5112. ? DynamicTypeHandler::GetOffsetOfObjectHeaderInlineSlots()
  5113. : sizeof(DynamicObject);
  5114. DynamicType* dynamicType = nullptr;
  5115. const bool useCache = prototype->GetScriptContext() == this->scriptContext;
  5116. #if DBG
  5117. DynamicType* oldCachedType = nullptr;
  5118. char16 reason[1024];
  5119. swprintf_s(reason, 1024, _u("Cache not populated."));
  5120. #endif
  5121. // Always use `TypeOfPrototypeObjectInlined` because we are creating DynamicType of TypeIds_Object
  5122. AssertMsg(typeId == TypeIds_Object, "CreateObjectType() is used to create other objects. Please update cacheSlot for protoObjectCache first.");
  5123. if (useCache &&
  5124. prototype->GetInternalProperty(prototype, Js::InternalPropertyIds::TypeOfPrototypeObjectInlined, (Js::Var*) &dynamicType, nullptr, this->scriptContext))
  5125. {
  5126. //If the prototype is externalObject, then ExternalObject::Reinitialize can set all the properties to undefined in navigation scenario.
  5127. //Check to make sure dynamicType which is stored as a Js::Var is not undefined.
  5128. //See Blue 419324
  5129. if (dynamicType != nullptr && (Js::Var)dynamicType != this->GetUndefined())
  5130. {
  5131. DynamicTypeHandler *const dynamicTypeHandler = dynamicType->GetTypeHandler();
  5132. if (dynamicTypeHandler->IsObjectHeaderInlinedTypeHandler() == useObjectHeaderInlining &&
  5133. (
  5134. dynamicTypeHandler->GetInlineSlotCapacity() ==
  5135. (
  5136. useObjectHeaderInlining
  5137. ? DynamicTypeHandler::RoundUpObjectHeaderInlinedInlineSlotCapacity(requestedInlineSlotCapacity)
  5138. : DynamicTypeHandler::RoundUpInlineSlotCapacity(requestedInlineSlotCapacity)
  5139. )
  5140. ))
  5141. {
  5142. Assert(dynamicType->GetIsShared());
  5143. if (PHASE_TRACE1(TypeShareForChangePrototypePhase))
  5144. {
  5145. #if DBG
  5146. if (PHASE_VERBOSE_TRACE1(TypeShareForChangePrototypePhase))
  5147. {
  5148. Output::Print(_u("TypeSharing: Reusing prototype [0x%p] object's InlineSlot cache 0x%p in CreateObject.\n"), prototype, dynamicType);
  5149. }
  5150. else
  5151. {
  5152. #endif
  5153. Output::Print(_u("TypeSharing: Reusing prototype object's InlineSlot cache in __proto__.\n"));
  5154. #if DBG
  5155. }
  5156. #endif
  5157. Output::Flush();
  5158. }
  5159. return dynamicType;
  5160. }
  5161. #if DBG
  5162. if (PHASE_VERBOSE_TRACE1(TypeShareForChangePrototypePhase))
  5163. {
  5164. if (dynamicTypeHandler->IsObjectHeaderInlinedTypeHandler() != useObjectHeaderInlining)
  5165. {
  5166. swprintf_s(reason, 1024, _u("useObjectHeaderInlining mismatch."));
  5167. }
  5168. else
  5169. {
  5170. uint16 cachedCapacity = dynamicTypeHandler->GetInlineSlotCapacity();
  5171. uint16 requiredCapacity = useObjectHeaderInlining
  5172. ? DynamicTypeHandler::RoundUpObjectHeaderInlinedInlineSlotCapacity(requestedInlineSlotCapacity)
  5173. : DynamicTypeHandler::RoundUpInlineSlotCapacity(requestedInlineSlotCapacity);
  5174. swprintf_s(reason, 1024, _u("inlineSlotCapacity mismatch. Required = %d, Cached = %d"), requiredCapacity, cachedCapacity);
  5175. }
  5176. }
  5177. #endif
  5178. }
  5179. }
  5180. #if DBG
  5181. if (PHASE_VERBOSE_TRACE1(TypeShareForChangePrototypePhase))
  5182. {
  5183. if (dynamicType == nullptr)
  5184. {
  5185. swprintf_s(reason, 1024, _u("cached type was null"));
  5186. }
  5187. else if ((Js::Var)dynamicType == this->GetUndefined())
  5188. {
  5189. swprintf_s(reason, 1024, _u("cached type was undefined"));
  5190. }
  5191. }
  5192. oldCachedType = dynamicType;
  5193. #endif
  5194. PathTypeHandlerNoAttr* typeHandler = PathTypeHandlerNoAttr::New(scriptContext, this->GetRootPath(), 0, requestedInlineSlotCapacity, offsetOfInlineSlots, true, true);
  5195. dynamicType = DynamicType::New(scriptContext, typeId, prototype, RecyclableObject::DefaultEntryPoint, typeHandler, true, true);
  5196. if (useCache)
  5197. {
  5198. prototype->SetInternalProperty(Js::InternalPropertyIds::TypeOfPrototypeObjectInlined, (Var)dynamicType, PropertyOperationFlags::PropertyOperation_Force, nullptr);
  5199. if (PHASE_TRACE1(TypeShareForChangePrototypePhase))
  5200. {
  5201. #if DBG
  5202. if (PHASE_VERBOSE_TRACE1(TypeShareForChangePrototypePhase))
  5203. {
  5204. Output::Print(_u("TypeSharing: Updating prototype [0x%p] object's InlineSlot cache from 0x%p to 0x%p in CreateObject. Reason = %s\n"), prototype, oldCachedType, dynamicType, reason);
  5205. }
  5206. else
  5207. {
  5208. #endif
  5209. Output::Print(_u("TypeSharing: Updating prototype object's InlineSlot cache in CreateObject.\n"));
  5210. #if DBG
  5211. }
  5212. #endif
  5213. Output::Flush();
  5214. }
  5215. }
  5216. return dynamicType;
  5217. }
  5218. DynamicType* JavascriptLibrary::CreateObjectTypeNoCache(RecyclableObject* prototype, Js::TypeId typeId)
  5219. {
  5220. return DynamicType::New(scriptContext, typeId, prototype, RecyclableObject::DefaultEntryPoint,
  5221. PathTypeHandlerNoAttr::New(scriptContext, this->GetRootPath(), 0, 0, 0, true, true), true, true);
  5222. }
  5223. DynamicType* JavascriptLibrary::CreateObjectType(RecyclableObject* prototype, uint16 requestedInlineSlotCapacity)
  5224. {
  5225. // We can't reuse the type in objectType even if the prototype is the object prototype, because those has inline slot capacity fixed
  5226. return CreateObjectType(prototype, TypeIds_Object, requestedInlineSlotCapacity);
  5227. }
  5228. DynamicObject* JavascriptLibrary::CreateObject(RecyclableObject* prototype, uint16 requestedInlineSlotCapacity)
  5229. {
  5230. Assert(JavascriptOperators::IsObjectOrNull(prototype));
  5231. DynamicType* type = nullptr;
  5232. // If requested capacity is 0, we can't shrink, so it is already fixed and we can reuse the cached types
  5233. // For other inline slot capacities, we might want to shrink so we can't use the cached types (whose slot capacities are fixed)
  5234. //
  5235. // REVIEW: Do we really need non-fixed inline slot capacity? The obvious downside is it prevents type sharing with the cached types
  5236. if (requestedInlineSlotCapacity == 0 && JavascriptOperators::IsNull(prototype))
  5237. {
  5238. type = GetNullPrototypeObjectType();
  5239. }
  5240. else if(requestedInlineSlotCapacity == 0 && prototype == GetObjectPrototype())
  5241. {
  5242. type = GetObjectType();
  5243. }
  5244. else
  5245. {
  5246. type = CreateObjectType(prototype, requestedInlineSlotCapacity);
  5247. }
  5248. return DynamicObject::New(this->GetRecycler(), type);
  5249. }
  5250. PropertyStringCacheMap* JavascriptLibrary::EnsurePropertyStringMap()
  5251. {
  5252. if (this->propertyStringMap == nullptr)
  5253. {
  5254. this->propertyStringMap = RecyclerNew(this->recycler, PropertyStringCacheMap, this->GetRecycler(), 71);
  5255. this->scriptContext->RegisterWeakReferenceDictionary((JsUtil::IWeakReferenceDictionary*) this->propertyStringMap);
  5256. }
  5257. return this->propertyStringMap;
  5258. }
  5259. EnumeratorCache* JavascriptLibrary::GetObjectAssignCache(Type* type)
  5260. {
  5261. // Size must be power of 2 for cache indexing to work
  5262. CompileAssert((Cache::AssignCacheSize & (Cache::AssignCacheSize - 1)) == 0);
  5263. if (this->cache.assignCache == nullptr)
  5264. {
  5265. this->cache.assignCache = AllocatorNewArrayZ(CacheAllocator, scriptContext->GetEnumeratorAllocator(), EnumeratorCache, Cache::AssignCacheSize);
  5266. }
  5267. return &this->cache.assignCache[(((uintptr_t)type) >> PolymorphicInlineCacheShift) & (Cache::AssignCacheSize - 1)];
  5268. }
  5269. SymbolCacheMap* JavascriptLibrary::EnsureSymbolMap()
  5270. {
  5271. if (this->symbolMap == nullptr)
  5272. {
  5273. this->symbolMap = RecyclerNew(this->recycler, SymbolCacheMap, this->GetRecycler(), 71);
  5274. this->scriptContext->RegisterWeakReferenceDictionary((JsUtil::IWeakReferenceDictionary*) this->symbolMap);
  5275. }
  5276. return this->symbolMap;
  5277. }
  5278. DynamicObject* JavascriptLibrary::CreateActivationObject()
  5279. {
  5280. AssertMsg(activationObjectType, "Where's activationObjectType?");
  5281. return RecyclerNew(this->GetRecycler(), ActivationObject, activationObjectType);
  5282. }
  5283. DynamicObject* JavascriptLibrary::CreatePseudoActivationObject()
  5284. {
  5285. AssertMsg(activationObjectType, "Where's activationObjectType?");
  5286. return RecyclerNew(this->GetRecycler(), PseudoActivationObject, activationObjectType);
  5287. }
  5288. DynamicObject* JavascriptLibrary::CreateBlockActivationObject()
  5289. {
  5290. AssertMsg(activationObjectType, "Where's activationObjectType?");
  5291. return RecyclerNew(this->GetRecycler(), BlockActivationObject, activationObjectType);
  5292. }
  5293. DynamicObject* JavascriptLibrary::CreateConsoleScopeActivationObject()
  5294. {
  5295. AssertMsg(activationObjectType, "Where's activationObjectType?");
  5296. return RecyclerNew(this->GetRecycler(), ConsoleScopeActivationObject, activationObjectType);
  5297. }
  5298. JavascriptString* JavascriptLibrary::GetEmptyString() const
  5299. {
  5300. AssertMsg(emptyString, "Where's emptyString?");
  5301. #ifdef PROFILE_STRINGS
  5302. StringProfiler::RecordEmptyStringRequest(scriptContext);
  5303. #endif
  5304. return emptyString;
  5305. }
  5306. PropertyString* JavascriptLibrary::CreatePropertyString(const Js::PropertyRecord* propertyRecord)
  5307. {
  5308. return PropertyString::New(GetStringTypeStatic(), propertyRecord, this->GetRecycler());
  5309. }
  5310. JavascriptVariantDate* JavascriptLibrary::CreateVariantDate(const double value)
  5311. {
  5312. AssertMsg(variantDateType, "Where's variantDateType?");
  5313. return RecyclerNewLeafZ(this->GetRecycler(), JavascriptVariantDate, value, variantDateType);
  5314. }
  5315. JavascriptBooleanObject* JavascriptLibrary::CreateBooleanObject()
  5316. {
  5317. AssertMsg(booleanTypeDynamic, "Where's booleanTypeDynamic?");
  5318. return RecyclerNew(this->GetRecycler(), JavascriptBooleanObject, nullptr, booleanTypeDynamic);
  5319. }
  5320. JavascriptBooleanObject* JavascriptLibrary::CreateBooleanObject(BOOL value)
  5321. {
  5322. AssertMsg(booleanTypeDynamic, "Where's booleanTypeDynamic?");
  5323. return RecyclerNew(this->GetRecycler(), JavascriptBooleanObject, CreateBoolean(value), booleanTypeDynamic);
  5324. }
  5325. JavascriptSymbolObject* JavascriptLibrary::CreateSymbolObject(JavascriptSymbol* value)
  5326. {
  5327. AssertMsg(symbolTypeDynamic, "Where's symbolTypeDynamic?");
  5328. return RecyclerNew(this->GetRecycler(), JavascriptSymbolObject, value, symbolTypeDynamic);
  5329. }
  5330. JavascriptNumberObject* JavascriptLibrary::CreateNumberObject(Var number)
  5331. {
  5332. AssertMsg(numberTypeDynamic, "Where's numberTypeDynamic?");
  5333. return RecyclerNew(this->GetRecycler(), JavascriptNumberObject, number, numberTypeDynamic);
  5334. }
  5335. JavascriptNumberObject* JavascriptLibrary::CreateNumberObjectWithCheck(double value)
  5336. {
  5337. return CreateNumberObject(JavascriptNumber::ToVarWithCheck(value, scriptContext));
  5338. }
  5339. JavascriptStringObject* JavascriptLibrary::CreateStringObject(JavascriptString* value)
  5340. {
  5341. AssertMsg(stringTypeDynamic, "Where's stringTypeDynamic?");
  5342. return RecyclerNew(this->GetRecycler(), JavascriptStringObject, value, stringTypeDynamic);
  5343. }
  5344. JavascriptStringObject* JavascriptLibrary::CreateStringObject(const char16* value, charcount_t length)
  5345. {
  5346. AssertMsg(stringTypeDynamic, "Where's stringTypeDynamic?");
  5347. return RecyclerNew(this->GetRecycler(), JavascriptStringObject,
  5348. Js::JavascriptString::NewWithBuffer(value, length, scriptContext), stringTypeDynamic);
  5349. }
  5350. JavascriptRegExp* JavascriptLibrary::CreateRegExp(UnifiedRegex::RegexPattern* pattern)
  5351. {
  5352. AssertMsg(regexType, "Where's regexType?");
  5353. return RecyclerNew(this->GetRecycler(), JavascriptRegExp, pattern, regexType);
  5354. }
  5355. JavascriptArrayIterator* JavascriptLibrary::CreateArrayIterator(Var iterable, JavascriptArrayIteratorKind kind)
  5356. {
  5357. AssertMsg(arrayIteratorType, "Where's arrayIteratorType");
  5358. return RecyclerNew(this->GetRecycler(), JavascriptArrayIterator, arrayIteratorType, iterable, kind);
  5359. }
  5360. JavascriptMapIterator* JavascriptLibrary::CreateMapIterator(JavascriptMap* map, JavascriptMapIteratorKind kind)
  5361. {
  5362. AssertMsg(mapIteratorType, "Where's mapIteratorType");
  5363. return RecyclerNew(this->GetRecycler(), JavascriptMapIterator, mapIteratorType, map, kind);
  5364. }
  5365. JavascriptSetIterator* JavascriptLibrary::CreateSetIterator(JavascriptSet* set, JavascriptSetIteratorKind kind)
  5366. {
  5367. AssertMsg(setIteratorType, "Where's setIteratorType");
  5368. return RecyclerNew(this->GetRecycler(), JavascriptSetIterator, setIteratorType, set, kind);
  5369. }
  5370. JavascriptStringIterator* JavascriptLibrary::CreateStringIterator(JavascriptString* string)
  5371. {
  5372. AssertMsg(stringIteratorType, "Where's stringIteratorType");
  5373. return RecyclerNew(this->GetRecycler(), JavascriptStringIterator, stringIteratorType, string);
  5374. }
  5375. DynamicObject* JavascriptLibrary::CreateIteratorResultObject(Var value, Var done)
  5376. {
  5377. DynamicObject* iteratorResult = DynamicObject::New(this->GetRecycler(), iteratorResultType);
  5378. iteratorResult->SetSlot(SetSlotArguments(Js::PropertyIds::value, 0, value));
  5379. iteratorResult->SetSlot(SetSlotArguments(Js::PropertyIds::done, 1, done));
  5380. return iteratorResult;
  5381. }
  5382. JavascriptListIterator* JavascriptLibrary::CreateListIterator(ListForListIterator* list)
  5383. {
  5384. JavascriptListIterator* iterator = RecyclerNew(this->GetRecycler(), JavascriptListIterator, listIteratorType, list);
  5385. RuntimeFunction* nextFunction = DefaultCreateFunction(&JavascriptListIterator::EntryInfo::Next, 0, nullptr, nullptr, PropertyIds::next);
  5386. JavascriptOperators::SetProperty(iterator, iterator, PropertyIds::next, RuntimeFunction::FromVar(nextFunction), GetScriptContext(), PropertyOperation_None);
  5387. return iterator;
  5388. }
  5389. DynamicObject* JavascriptLibrary::CreateIteratorResultObjectValueFalse(Var value)
  5390. {
  5391. return CreateIteratorResultObject(value, GetFalse());
  5392. }
  5393. DynamicObject* JavascriptLibrary::CreateIteratorResultObjectUndefinedTrue()
  5394. {
  5395. return CreateIteratorResultObject(GetUndefined(), GetTrue());
  5396. }
  5397. RecyclableObject* JavascriptLibrary::CreateThrowErrorObject(JavascriptError* error)
  5398. {
  5399. return ThrowErrorObject::New(this->throwErrorObjectType, error, this->GetRecycler());
  5400. }
  5401. #if ENABLE_COPYONACCESS_ARRAY
  5402. bool JavascriptLibrary::IsCopyOnAccessArrayCallSite(JavascriptLibrary *lib, ArrayCallSiteInfo *arrayInfo, uint32 length)
  5403. {
  5404. return
  5405. lib->cacheForCopyOnAccessArraySegments
  5406. && lib->cacheForCopyOnAccessArraySegments->IsNotOverHardLimit()
  5407. && (
  5408. PHASE_FORCE1(CopyOnAccessArrayPhase) // -force:copyonaccessarray is only restricted by hard limit of the segment cache
  5409. || (
  5410. !arrayInfo->isNotCopyOnAccessArray // from profile
  5411. && !PHASE_OFF1(CopyOnAccessArrayPhase)
  5412. && lib->cacheForCopyOnAccessArraySegments->IsNotFull() // cache size soft limit through -copyonaccessarraysegmentcachesize:<number>
  5413. && length <= (uint32)CONFIG_FLAG(MaxCopyOnAccessArrayLength) // -maxcopyonaccessarraylength:<number>
  5414. && length >= (uint32)CONFIG_FLAG(MinCopyOnAccessArrayLength) // -mincopyonaccessarraylength:<number>
  5415. )
  5416. )
  5417. #if ENABLE_TTD
  5418. && !lib->GetScriptContext()->GetThreadContext()->IsRuntimeInTTDMode()
  5419. #endif
  5420. ;
  5421. }
  5422. bool JavascriptLibrary::IsCachedCopyOnAccessArrayCallSite(const JavascriptLibrary *lib, ArrayCallSiteInfo *arrayInfo)
  5423. {
  5424. return lib->cacheForCopyOnAccessArraySegments
  5425. && lib->cacheForCopyOnAccessArraySegments->IsValidIndex(arrayInfo->copyOnAccessArrayCacheIndex);
  5426. }
  5427. #endif
  5428. // static
  5429. bool JavascriptLibrary::IsTypedArrayConstructor(Var constructor, ScriptContext* scriptContext)
  5430. {
  5431. JavascriptLibrary* library = scriptContext->GetLibrary();
  5432. return constructor == library->GetInt8ArrayConstructor()
  5433. || constructor == library->GetUint8ArrayConstructor()
  5434. || constructor == library->GetUint8ClampedArrayConstructor()
  5435. || constructor == library->GetInt16ArrayConstructor()
  5436. || constructor == library->GetUint16ArrayConstructor()
  5437. || constructor == library->GetInt32ArrayConstructor()
  5438. || constructor == library->GetUint32ArrayConstructor()
  5439. || constructor == library->GetFloat32ArrayConstructor()
  5440. || constructor == library->GetFloat64ArrayConstructor();
  5441. }
  5442. Field(JavascriptFunction*)* JavascriptLibrary::GetBuiltinFunctions()
  5443. {
  5444. AssertMsg(this->builtinFunctions, "builtinFunctions table must've been initialized as part of library initialization!");
  5445. return this->builtinFunctions;
  5446. }
  5447. void JavascriptLibrary::SetIsPRNGSeeded(bool val)
  5448. {
  5449. this->isPRNGSeeded = val;
  5450. #if ENABLE_NATIVE_CODEGEN
  5451. if (JITManager::GetJITManager()->IsOOPJITEnabled() && JITManager::GetJITManager()->IsConnected())
  5452. {
  5453. PSCRIPTCONTEXT_HANDLE remoteScriptContext = this->scriptContext->GetRemoteScriptAddr();
  5454. if (remoteScriptContext)
  5455. {
  5456. HRESULT hr = JITManager::GetJITManager()->SetIsPRNGSeeded(remoteScriptContext, val);
  5457. JITManager::HandleServerCallResult(hr, RemoteCallType::StateUpdate);
  5458. }
  5459. }
  5460. #endif
  5461. }
  5462. INT_PTR* JavascriptLibrary::GetVTableAddresses()
  5463. {
  5464. AssertMsg(this->vtableAddresses, "vtableAddresses table must've been initialized as part of library initialization!");
  5465. return this->vtableAddresses;
  5466. }
  5467. #if ENABLE_NATIVE_CODEGEN
  5468. //static
  5469. BuiltinFunction JavascriptLibrary::GetBuiltInInlineCandidateId(OpCode opCode)
  5470. {
  5471. switch (opCode)
  5472. {
  5473. case OpCode::InlineMathAcos:
  5474. return BuiltinFunction::Math_Acos;
  5475. case OpCode::InlineMathAsin:
  5476. return BuiltinFunction::Math_Asin;
  5477. case OpCode::InlineMathAtan:
  5478. return BuiltinFunction::Math_Atan;
  5479. case OpCode::InlineMathAtan2:
  5480. return BuiltinFunction::Math_Atan2;
  5481. case OpCode::InlineMathCos:
  5482. return BuiltinFunction::Math_Cos;
  5483. case OpCode::InlineMathExp:
  5484. return BuiltinFunction::Math_Exp;
  5485. case OpCode::InlineMathLog:
  5486. return BuiltinFunction::Math_Log;
  5487. case OpCode::InlineMathPow:
  5488. return BuiltinFunction::Math_Pow;
  5489. case OpCode::InlineMathSin:
  5490. return BuiltinFunction::Math_Sin;
  5491. case OpCode::InlineMathSqrt:
  5492. return BuiltinFunction::Math_Sqrt;
  5493. case OpCode::InlineMathTan:
  5494. return BuiltinFunction::Math_Tan;
  5495. case OpCode::InlineMathAbs:
  5496. return BuiltinFunction::Math_Abs;
  5497. case OpCode::InlineMathClz:
  5498. return BuiltinFunction::Math_Clz32;
  5499. case OpCode::InlineMathCeil:
  5500. return BuiltinFunction::Math_Ceil;
  5501. case OpCode::InlineMathFloor:
  5502. return BuiltinFunction::Math_Floor;
  5503. case OpCode::InlineMathMax:
  5504. return BuiltinFunction::Math_Max;
  5505. case OpCode::InlineMathMin:
  5506. return BuiltinFunction::Math_Min;
  5507. case OpCode::InlineMathImul:
  5508. return BuiltinFunction::Math_Imul;
  5509. case OpCode::InlineMathRandom:
  5510. return BuiltinFunction::Math_Random;
  5511. case OpCode::InlineMathRound:
  5512. return BuiltinFunction::Math_Round;
  5513. case OpCode::InlineMathFround:
  5514. return BuiltinFunction::Math_Fround;
  5515. case OpCode::InlineStringCharAt:
  5516. return BuiltinFunction::JavascriptString_CharAt;
  5517. case OpCode::InlineStringCharCodeAt:
  5518. return BuiltinFunction::JavascriptString_CharCodeAt;
  5519. case OpCode::InlineStringCodePointAt:
  5520. return BuiltinFunction::JavascriptString_CodePointAt;
  5521. case OpCode::InlineArrayPop:
  5522. return BuiltinFunction::JavascriptArray_Pop;
  5523. case OpCode::InlineArrayPush:
  5524. return BuiltinFunction::JavascriptArray_Push;
  5525. case OpCode::InlineFunctionApply:
  5526. return BuiltinFunction::JavascriptFunction_Apply;
  5527. case OpCode::InlineFunctionCall:
  5528. return BuiltinFunction::JavascriptFunction_Call;
  5529. case OpCode::InlineRegExpExec:
  5530. return BuiltinFunction::JavascriptRegExp_Exec;
  5531. }
  5532. return BuiltinFunction::None;
  5533. }
  5534. #endif
  5535. // Parses given flags and arg kind (dst or src1, or src2) returns the type the arg must be type-specialized to.
  5536. // static
  5537. BuiltInArgSpecializationType JavascriptLibrary::GetBuiltInArgType(BuiltInFlags flags, BuiltInArgShift argKind)
  5538. {
  5539. Assert(argKind == BuiltInArgShift::BIAS_Dst || BuiltInArgShift::BIAS_Src1 || BuiltInArgShift::BIAS_Src2);
  5540. BuiltInArgSpecializationType type = static_cast<BuiltInArgSpecializationType>(
  5541. (flags >> argKind) & // Shift-out everything to the right of start of interesting area.
  5542. ((1 << Js::BIAS_ArgSize) - 1)); // Mask-out everything to the left of interesting area.
  5543. return type;
  5544. }
  5545. ModuleRecordList* JavascriptLibrary::EnsureModuleRecordList()
  5546. {
  5547. if (moduleRecordList == nullptr)
  5548. {
  5549. moduleRecordList = RecyclerNew(recycler, ModuleRecordList, recycler);
  5550. }
  5551. return moduleRecordList;
  5552. }
  5553. SourceTextModuleRecord* JavascriptLibrary::GetModuleRecord(uint moduleId)
  5554. {
  5555. Assert((moduleRecordList->Count() >= 0) && (moduleId < (uint)moduleRecordList->Count()));
  5556. if (moduleId >= (uint)moduleRecordList->Count())
  5557. {
  5558. Js::Throw::FatalInternalError();
  5559. }
  5560. return moduleRecordList->Item(moduleId);
  5561. }
  5562. void JavascriptLibrary::BindReference(void * addr)
  5563. {
  5564. // The last void* is the linklist connecting to next block.
  5565. if (bindRefChunkCurrent == bindRefChunkEnd)
  5566. {
  5567. Field(void*)* tmpBindRefChunk = RecyclerNewArrayZ(recycler,
  5568. Field(void*), HeapConstants::ObjectGranularity / sizeof(void *));
  5569. // reserve the last void* as the linklist node.
  5570. bindRefChunkEnd = tmpBindRefChunk + (HeapConstants::ObjectGranularity / sizeof(void *) -1 );
  5571. if (bindRefChunkBegin == nullptr)
  5572. {
  5573. bindRefChunkCurrent = tmpBindRefChunk;
  5574. bindRefChunkBegin = bindRefChunkCurrent;
  5575. }
  5576. else
  5577. {
  5578. *bindRefChunkCurrent = tmpBindRefChunk;
  5579. bindRefChunkCurrent = tmpBindRefChunk;
  5580. }
  5581. }
  5582. Assert((bindRefChunkCurrent+1) <= bindRefChunkEnd);
  5583. *bindRefChunkCurrent = addr;
  5584. bindRefChunkCurrent++;
  5585. }
  5586. void JavascriptLibrary::CleanupForClose()
  5587. {
  5588. bindRefChunkCurrent = nullptr;
  5589. bindRefChunkEnd = nullptr;
  5590. }
  5591. void JavascriptLibrary::BeginDynamicFunctionReferences()
  5592. {
  5593. if (this->dynamicFunctionReference == nullptr)
  5594. {
  5595. this->dynamicFunctionReference = RecyclerNew(this->recycler, FunctionReferenceList, this->recycler);
  5596. this->dynamicFunctionReferenceDepth = 0;
  5597. }
  5598. this->dynamicFunctionReferenceDepth++;
  5599. }
  5600. void JavascriptLibrary::EndDynamicFunctionReferences()
  5601. {
  5602. Assert(this->dynamicFunctionReference != nullptr);
  5603. this->dynamicFunctionReferenceDepth--;
  5604. if (this->dynamicFunctionReferenceDepth == 0)
  5605. {
  5606. this->dynamicFunctionReference->Clear();
  5607. }
  5608. }
  5609. void JavascriptLibrary::RegisterDynamicFunctionReference(FunctionProxy* func)
  5610. {
  5611. Assert(this->dynamicFunctionReferenceDepth > 0);
  5612. this->dynamicFunctionReference->Push(func);
  5613. }
  5614. #ifdef ENABLE_SCRIPT_PROFILING
  5615. // Register for profiler
  5616. #define DEFINE_OBJECT_NAME(object) const char16 *pwszObjectName = _u(#object);
  5617. #define REGISTER_OBJECT(object)\
  5618. if (FAILED(hr = this->ProfilerRegister##object()))\
  5619. {\
  5620. return hr; \
  5621. }\
  5622. #define REG_LIB_FUNC_CORE(pwszObjectName, pwszFunctionName, functionPropertyId, entryPoint)\
  5623. if (FAILED(hr = this->GetScriptContext()->RegisterLibraryFunction(pwszObjectName, pwszFunctionName, functionPropertyId, entryPoint)))\
  5624. {\
  5625. return hr; \
  5626. }\
  5627. #define REG_OBJECTS_DYNAMIC_LIB_FUNC(pwszFunctionName, nFuncNameLen, entryPoint) {\
  5628. Js::PropertyRecord const * propRecord; \
  5629. this->GetScriptContext()->GetOrAddPropertyRecord(pwszFunctionName, nFuncNameLen, &propRecord); \
  5630. REG_LIB_FUNC_CORE(pwszObjectName, pwszFunctionName, propRecord->GetPropertyId(), entryPoint)\
  5631. }
  5632. #define REG_LIB_FUNC(pwszObjectName, functionPropertyId, entryPoint)\
  5633. REG_LIB_FUNC_CORE(pwszObjectName, _u(#functionPropertyId), PropertyIds::##functionPropertyId, entryPoint)\
  5634. #define REG_OBJECTS_LIB_FUNC(functionPropertyId, entryPoint)\
  5635. REG_LIB_FUNC(pwszObjectName, functionPropertyId, entryPoint)\
  5636. #define REG_OBJECTS_LIB_FUNC2(functionPropertyId, pwszFunctionPropertyName, entryPoint)\
  5637. REG_LIB_FUNC_CORE(pwszObjectName, pwszFunctionPropertyName, PropertyIds::##functionPropertyId, entryPoint)\
  5638. #define REG_GLOBAL_LIB_FUNC(functionPropertyId, entryPoint)\
  5639. REG_LIB_FUNC(NULL, functionPropertyId, entryPoint)\
  5640. #define REG_GLOBAL_CONSTRUCTOR(functionPropertyId)\
  5641. REG_GLOBAL_LIB_FUNC(functionPropertyId, Javascript##functionPropertyId##::NewInstance)\
  5642. #define REGISTER_ERROR_OBJECT(functionPropertyId)\
  5643. REG_GLOBAL_LIB_FUNC(functionPropertyId, JavascriptError::New##functionPropertyId##Instance)\
  5644. REG_LIB_FUNC(_u(#functionPropertyId), toString, JavascriptError::EntryToString)\
  5645. HRESULT JavascriptLibrary::ProfilerRegisterBuiltIns()
  5646. {
  5647. HRESULT hr = S_OK;
  5648. // Register functions directly in global scope
  5649. REG_GLOBAL_LIB_FUNC(eval, GlobalObject::EntryEval);
  5650. REG_GLOBAL_LIB_FUNC(parseInt, GlobalObject::EntryParseInt);
  5651. REG_GLOBAL_LIB_FUNC(parseFloat, GlobalObject::EntryParseFloat);
  5652. REG_GLOBAL_LIB_FUNC(isNaN, GlobalObject::EntryIsNaN);
  5653. REG_GLOBAL_LIB_FUNC(isFinite, GlobalObject::EntryIsFinite);
  5654. REG_GLOBAL_LIB_FUNC(decodeURI, GlobalObject::EntryDecodeURI);
  5655. REG_GLOBAL_LIB_FUNC(decodeURIComponent, GlobalObject::EntryDecodeURIComponent);
  5656. REG_GLOBAL_LIB_FUNC(encodeURI, GlobalObject::EntryEncodeURI);
  5657. REG_GLOBAL_LIB_FUNC(encodeURIComponent, GlobalObject::EntryEncodeURIComponent);
  5658. REG_GLOBAL_LIB_FUNC(escape, GlobalObject::EntryEscape);
  5659. REG_GLOBAL_LIB_FUNC(unescape, GlobalObject::EntryUnEscape);
  5660. REG_GLOBAL_LIB_FUNC(CollectGarbage, GlobalObject::EntryCollectGarbage);
  5661. // Register constructors, prototypes and objects in global
  5662. REGISTER_OBJECT(Object);
  5663. REGISTER_OBJECT(Array);
  5664. REGISTER_OBJECT(Boolean);
  5665. REGISTER_OBJECT(Date);
  5666. REGISTER_OBJECT(Function);
  5667. REGISTER_OBJECT(Math);
  5668. REGISTER_OBJECT(Number);
  5669. REGISTER_OBJECT(String);
  5670. REGISTER_OBJECT(RegExp);
  5671. REGISTER_OBJECT(JSON);
  5672. REGISTER_OBJECT(Map);
  5673. REGISTER_OBJECT(Set);
  5674. REGISTER_OBJECT(WeakMap);
  5675. REGISTER_OBJECT(WeakSet);
  5676. REGISTER_OBJECT(Symbol);
  5677. REGISTER_OBJECT(Iterator);
  5678. REGISTER_OBJECT(ArrayIterator);
  5679. REGISTER_OBJECT(MapIterator);
  5680. REGISTER_OBJECT(SetIterator);
  5681. REGISTER_OBJECT(StringIterator);
  5682. REGISTER_OBJECT(TypedArray);
  5683. ScriptConfiguration const& config = *(scriptContext->GetConfig());
  5684. if (config.IsES6PromiseEnabled())
  5685. {
  5686. REGISTER_OBJECT(Promise);
  5687. }
  5688. if (config.IsES6ProxyEnabled())
  5689. {
  5690. REGISTER_OBJECT(Proxy);
  5691. REGISTER_OBJECT(Reflect);
  5692. }
  5693. #ifdef IR_VIEWER
  5694. if (Js::Configuration::Global.flags.IsEnabled(Js::IRViewerFlag))
  5695. {
  5696. REGISTER_OBJECT(IRViewer);
  5697. }
  5698. #endif /* IR_VIEWER */
  5699. // Error Constructors and prototypes
  5700. REGISTER_ERROR_OBJECT(Error);
  5701. REGISTER_ERROR_OBJECT(EvalError);
  5702. REGISTER_ERROR_OBJECT(RangeError);
  5703. REGISTER_ERROR_OBJECT(ReferenceError);
  5704. REGISTER_ERROR_OBJECT(SyntaxError);
  5705. REGISTER_ERROR_OBJECT(TypeError);
  5706. REGISTER_ERROR_OBJECT(URIError);
  5707. #ifdef ENABLE_PROJECTION
  5708. if (config.IsWinRTEnabled())
  5709. {
  5710. REGISTER_ERROR_OBJECT(WinRTError);
  5711. }
  5712. #endif
  5713. return hr;
  5714. }
  5715. HRESULT JavascriptLibrary::ProfilerRegisterObject()
  5716. {
  5717. HRESULT hr = S_OK;
  5718. REG_GLOBAL_CONSTRUCTOR(Object);
  5719. DEFINE_OBJECT_NAME(Object);
  5720. REG_OBJECTS_LIB_FUNC(defineProperty, JavascriptObject::EntryDefineProperty);
  5721. REG_OBJECTS_LIB_FUNC(getOwnPropertyDescriptor, JavascriptObject::EntryGetOwnPropertyDescriptor);
  5722. if (scriptContext->GetConfig()->IsESObjectGetOwnPropertyDescriptorsEnabled())
  5723. {
  5724. REG_OBJECTS_LIB_FUNC(getOwnPropertyDescriptors, JavascriptObject::EntryGetOwnPropertyDescriptors);
  5725. }
  5726. REG_OBJECTS_LIB_FUNC(defineProperties, JavascriptObject::EntryDefineProperties);
  5727. REG_OBJECTS_LIB_FUNC(create, JavascriptObject::EntryCreate);
  5728. REG_OBJECTS_LIB_FUNC(seal, JavascriptObject::EntrySeal);
  5729. REG_OBJECTS_LIB_FUNC(freeze, JavascriptObject::EntryFreeze);
  5730. REG_OBJECTS_LIB_FUNC(preventExtensions, JavascriptObject::EntryPreventExtensions);
  5731. REG_OBJECTS_LIB_FUNC(isSealed, JavascriptObject::EntryIsSealed);
  5732. REG_OBJECTS_LIB_FUNC(isFrozen, JavascriptObject::EntryIsFrozen);
  5733. REG_OBJECTS_LIB_FUNC(isExtensible, JavascriptObject::EntryIsExtensible);
  5734. REG_OBJECTS_LIB_FUNC(getPrototypeOf, JavascriptObject::EntryGetPrototypeOf);
  5735. REG_OBJECTS_LIB_FUNC(keys, JavascriptObject::EntryKeys);
  5736. REG_OBJECTS_LIB_FUNC(getOwnPropertyNames, JavascriptObject::EntryGetOwnPropertyNames);
  5737. REG_OBJECTS_LIB_FUNC(setPrototypeOf, JavascriptObject::EntrySetPrototypeOf);
  5738. REG_OBJECTS_LIB_FUNC(getOwnPropertySymbols, JavascriptObject::EntryGetOwnPropertySymbols);
  5739. REG_OBJECTS_LIB_FUNC(hasOwnProperty, JavascriptObject::EntryHasOwnProperty);
  5740. REG_OBJECTS_LIB_FUNC(propertyIsEnumerable, JavascriptObject::EntryPropertyIsEnumerable);
  5741. REG_OBJECTS_LIB_FUNC(isPrototypeOf, JavascriptObject::EntryIsPrototypeOf);
  5742. REG_OBJECTS_LIB_FUNC(toLocaleString, JavascriptObject::EntryToLocaleString);
  5743. REG_OBJECTS_LIB_FUNC(toString, JavascriptObject::EntryToString);
  5744. REG_OBJECTS_LIB_FUNC(valueOf, JavascriptObject::EntryValueOf);
  5745. REG_OBJECTS_LIB_FUNC(__defineGetter__, JavascriptObject::EntryDefineGetter);
  5746. REG_OBJECTS_LIB_FUNC(__defineSetter__, JavascriptObject::EntryDefineSetter);
  5747. ScriptConfiguration const& config = *(scriptContext->GetConfig());
  5748. if (config.IsES6ObjectExtensionsEnabled())
  5749. {
  5750. REG_OBJECTS_LIB_FUNC(is, JavascriptObject::EntryIs);
  5751. REG_OBJECTS_LIB_FUNC(assign, JavascriptObject::EntryAssign);
  5752. }
  5753. if (config.IsES7ValuesEntriesEnabled())
  5754. {
  5755. REG_OBJECTS_LIB_FUNC(values, JavascriptObject::EntryValues);
  5756. REG_OBJECTS_LIB_FUNC(entries, JavascriptObject::EntryEntries);
  5757. }
  5758. return hr;
  5759. }
  5760. HRESULT JavascriptLibrary::ProfilerRegisterArray()
  5761. {
  5762. HRESULT hr = S_OK;
  5763. REG_GLOBAL_CONSTRUCTOR(Array);
  5764. DEFINE_OBJECT_NAME(Array);
  5765. REG_OBJECTS_LIB_FUNC(isArray, JavascriptArray::EntryIsArray);
  5766. REG_OBJECTS_LIB_FUNC(concat, JavascriptArray::EntryConcat);
  5767. REG_OBJECTS_LIB_FUNC(join, JavascriptArray::EntryJoin);
  5768. REG_OBJECTS_LIB_FUNC(pop, JavascriptArray::EntryPop);
  5769. REG_OBJECTS_LIB_FUNC(push, JavascriptArray::EntryPush);
  5770. REG_OBJECTS_LIB_FUNC(reverse, JavascriptArray::EntryReverse);
  5771. REG_OBJECTS_LIB_FUNC(shift, JavascriptArray::EntryShift);
  5772. REG_OBJECTS_LIB_FUNC(slice, JavascriptArray::EntrySlice);
  5773. REG_OBJECTS_LIB_FUNC(sort, JavascriptArray::EntrySort);
  5774. REG_OBJECTS_LIB_FUNC(splice, JavascriptArray::EntrySplice);
  5775. REG_OBJECTS_LIB_FUNC(toLocaleString, JavascriptArray::EntryToLocaleString);
  5776. REG_OBJECTS_LIB_FUNC(toString, JavascriptArray::EntryToString);
  5777. REG_OBJECTS_LIB_FUNC(unshift, JavascriptArray::EntryUnshift);
  5778. REG_OBJECTS_LIB_FUNC(indexOf, JavascriptArray::EntryIndexOf);
  5779. REG_OBJECTS_LIB_FUNC(every, JavascriptArray::EntryEvery);
  5780. REG_OBJECTS_LIB_FUNC(filter, JavascriptArray::EntryFilter);
  5781. REG_OBJECTS_LIB_FUNC(forEach, JavascriptArray::EntryForEach);
  5782. REG_OBJECTS_LIB_FUNC(lastIndexOf, JavascriptArray::EntryLastIndexOf);
  5783. REG_OBJECTS_LIB_FUNC(map, JavascriptArray::EntryMap);
  5784. REG_OBJECTS_LIB_FUNC(reduce, JavascriptArray::EntryReduce);
  5785. REG_OBJECTS_LIB_FUNC(reduceRight, JavascriptArray::EntryReduceRight);
  5786. REG_OBJECTS_LIB_FUNC(some, JavascriptArray::EntrySome);
  5787. ScriptConfiguration const& config = *(scriptContext->GetConfig());
  5788. if (config.IsES6StringExtensionsEnabled())
  5789. {
  5790. REG_OBJECTS_LIB_FUNC(find, JavascriptArray::EntryFind);
  5791. REG_OBJECTS_LIB_FUNC(findIndex, JavascriptArray::EntryFindIndex);
  5792. }
  5793. // All iterator constructor functions are implemeted in Js when JsBuiltIns are enabled and their entrypoint will be the same
  5794. // The profiler cannot distinguish between them
  5795. if (!scriptContext->IsJsBuiltInEnabled())
  5796. {
  5797. REG_OBJECTS_LIB_FUNC(entries, JavascriptArray::EntryEntries)
  5798. REG_OBJECTS_LIB_FUNC(keys, JavascriptArray::EntryKeys)
  5799. REG_OBJECTS_LIB_FUNC(values, JavascriptArray::EntryValues)
  5800. // _symbolIterator is just an alias for values on Array.prototype so do not register it as its own function
  5801. }
  5802. REG_OBJECTS_LIB_FUNC(fill, JavascriptArray::EntryFill)
  5803. REG_OBJECTS_LIB_FUNC(copyWithin, JavascriptArray::EntryCopyWithin)
  5804. REG_OBJECTS_LIB_FUNC(from, JavascriptArray::EntryFrom);
  5805. REG_OBJECTS_LIB_FUNC(of, JavascriptArray::EntryOf);
  5806. REG_OBJECTS_LIB_FUNC(includes, JavascriptArray::EntryIncludes);
  5807. return hr;
  5808. }
  5809. HRESULT JavascriptLibrary::ProfilerRegisterBoolean()
  5810. {
  5811. HRESULT hr = S_OK;
  5812. REG_GLOBAL_CONSTRUCTOR(Boolean);
  5813. DEFINE_OBJECT_NAME(Boolean);
  5814. REG_OBJECTS_LIB_FUNC(valueOf, JavascriptBoolean::EntryValueOf);
  5815. REG_OBJECTS_LIB_FUNC(toString, JavascriptBoolean::EntryToString);
  5816. return hr;
  5817. }
  5818. HRESULT JavascriptLibrary::ProfilerRegisterDate()
  5819. {
  5820. HRESULT hr = S_OK;
  5821. REG_GLOBAL_CONSTRUCTOR(Date);
  5822. DEFINE_OBJECT_NAME(Date);
  5823. REG_OBJECTS_LIB_FUNC(parse, JavascriptDate::EntryParse);
  5824. REG_OBJECTS_LIB_FUNC(now, JavascriptDate::EntryNow);
  5825. REG_OBJECTS_LIB_FUNC(UTC, JavascriptDate::EntryUTC);
  5826. REG_OBJECTS_LIB_FUNC(getDate, JavascriptDate::EntryGetDate);
  5827. REG_OBJECTS_LIB_FUNC(getDay, JavascriptDate::EntryGetDay);
  5828. REG_OBJECTS_LIB_FUNC(getFullYear, JavascriptDate::EntryGetFullYear);
  5829. REG_OBJECTS_LIB_FUNC(getHours, JavascriptDate::EntryGetHours);
  5830. REG_OBJECTS_LIB_FUNC(getMilliseconds, JavascriptDate::EntryGetMilliseconds);
  5831. REG_OBJECTS_LIB_FUNC(getMinutes, JavascriptDate::EntryGetMinutes);
  5832. REG_OBJECTS_LIB_FUNC(getMonth, JavascriptDate::EntryGetMonth);
  5833. REG_OBJECTS_LIB_FUNC(getSeconds, JavascriptDate::EntryGetSeconds);
  5834. REG_OBJECTS_LIB_FUNC(getTime, JavascriptDate::EntryGetTime);
  5835. REG_OBJECTS_LIB_FUNC(getTimezoneOffset, JavascriptDate::EntryGetTimezoneOffset);
  5836. REG_OBJECTS_LIB_FUNC(getUTCDate, JavascriptDate::EntryGetUTCDate);
  5837. REG_OBJECTS_LIB_FUNC(getUTCDay, JavascriptDate::EntryGetUTCDay);
  5838. REG_OBJECTS_LIB_FUNC(getUTCFullYear, JavascriptDate::EntryGetUTCFullYear);
  5839. REG_OBJECTS_LIB_FUNC(getUTCHours, JavascriptDate::EntryGetUTCHours);
  5840. REG_OBJECTS_LIB_FUNC(getUTCMilliseconds, JavascriptDate::EntryGetUTCMilliseconds);
  5841. REG_OBJECTS_LIB_FUNC(getUTCMinutes, JavascriptDate::EntryGetUTCMinutes);
  5842. REG_OBJECTS_LIB_FUNC(getUTCMonth, JavascriptDate::EntryGetUTCMonth);
  5843. REG_OBJECTS_LIB_FUNC(getUTCSeconds, JavascriptDate::EntryGetUTCSeconds);
  5844. ScriptConfiguration const& config = *(scriptContext->GetConfig());
  5845. if (config.SupportsES3Extensions())
  5846. {
  5847. REG_OBJECTS_LIB_FUNC(getVarDate, JavascriptDate::EntryGetVarDate);
  5848. }
  5849. REG_OBJECTS_LIB_FUNC(getYear, JavascriptDate::EntryGetYear);
  5850. REG_OBJECTS_LIB_FUNC(setDate, JavascriptDate::EntrySetDate);
  5851. REG_OBJECTS_LIB_FUNC(setFullYear, JavascriptDate::EntrySetFullYear);
  5852. REG_OBJECTS_LIB_FUNC(setHours, JavascriptDate::EntrySetHours);
  5853. REG_OBJECTS_LIB_FUNC(setMilliseconds, JavascriptDate::EntrySetMilliseconds);
  5854. REG_OBJECTS_LIB_FUNC(setMinutes, JavascriptDate::EntrySetMinutes);
  5855. REG_OBJECTS_LIB_FUNC(setMonth, JavascriptDate::EntrySetMonth);
  5856. REG_OBJECTS_LIB_FUNC(setSeconds, JavascriptDate::EntrySetSeconds);
  5857. REG_OBJECTS_LIB_FUNC(setTime, JavascriptDate::EntrySetTime);
  5858. REG_OBJECTS_LIB_FUNC(setUTCDate, JavascriptDate::EntrySetUTCDate);
  5859. REG_OBJECTS_LIB_FUNC(setUTCFullYear, JavascriptDate::EntrySetUTCFullYear);
  5860. REG_OBJECTS_LIB_FUNC(setUTCHours, JavascriptDate::EntrySetUTCHours);
  5861. REG_OBJECTS_LIB_FUNC(setUTCMilliseconds, JavascriptDate::EntrySetUTCMilliseconds);
  5862. REG_OBJECTS_LIB_FUNC(setUTCMinutes, JavascriptDate::EntrySetUTCMinutes);
  5863. REG_OBJECTS_LIB_FUNC(setUTCMonth, JavascriptDate::EntrySetUTCMonth);
  5864. REG_OBJECTS_LIB_FUNC(setUTCSeconds, JavascriptDate::EntrySetUTCSeconds);
  5865. REG_OBJECTS_LIB_FUNC(setYear, JavascriptDate::EntrySetYear);
  5866. REG_OBJECTS_LIB_FUNC(toDateString, JavascriptDate::EntryToDateString);
  5867. REG_OBJECTS_LIB_FUNC(toISOString, JavascriptDate::EntryToISOString);
  5868. REG_OBJECTS_LIB_FUNC(toJSON, JavascriptDate::EntryToJSON);
  5869. REG_OBJECTS_LIB_FUNC(toLocaleDateString, JavascriptDate::EntryToLocaleDateString);
  5870. REG_OBJECTS_LIB_FUNC(toLocaleString, JavascriptDate::EntryToLocaleString);
  5871. REG_OBJECTS_LIB_FUNC(toLocaleTimeString, JavascriptDate::EntryToLocaleTimeString);
  5872. REG_OBJECTS_LIB_FUNC(toString, JavascriptDate::EntryToString);
  5873. REG_OBJECTS_LIB_FUNC(toTimeString, JavascriptDate::EntryToTimeString);
  5874. REG_OBJECTS_LIB_FUNC(toUTCString, JavascriptDate::EntryToUTCString);
  5875. REG_OBJECTS_LIB_FUNC(valueOf, JavascriptDate::EntryValueOf);
  5876. return hr;
  5877. }
  5878. HRESULT JavascriptLibrary::ProfilerRegisterFunction()
  5879. {
  5880. HRESULT hr = S_OK;
  5881. REG_GLOBAL_CONSTRUCTOR(Function);
  5882. DEFINE_OBJECT_NAME(Function);
  5883. REG_OBJECTS_LIB_FUNC(apply, JavascriptFunction::EntryApply);
  5884. REG_OBJECTS_LIB_FUNC(bind, JavascriptFunction::EntryBind);
  5885. REG_OBJECTS_LIB_FUNC(call, JavascriptFunction::EntryCall);
  5886. REG_OBJECTS_LIB_FUNC(toString, JavascriptFunction::EntryToString);
  5887. return hr;
  5888. }
  5889. HRESULT JavascriptLibrary::ProfilerRegisterMath()
  5890. {
  5891. HRESULT hr = S_OK;
  5892. DEFINE_OBJECT_NAME(Math);
  5893. REG_OBJECTS_LIB_FUNC(abs, Math::Abs);
  5894. REG_OBJECTS_LIB_FUNC(acos, Math::Acos);
  5895. REG_OBJECTS_LIB_FUNC(asin, Math::Asin);
  5896. REG_OBJECTS_LIB_FUNC(atan, Math::Atan);
  5897. REG_OBJECTS_LIB_FUNC(atan2, Math::Atan2);
  5898. REG_OBJECTS_LIB_FUNC(ceil, Math::Ceil);
  5899. REG_OBJECTS_LIB_FUNC(cos, Math::Cos);
  5900. REG_OBJECTS_LIB_FUNC(exp, Math::Exp);
  5901. REG_OBJECTS_LIB_FUNC(floor, Math::Floor);
  5902. REG_OBJECTS_LIB_FUNC(log, Math::Log);
  5903. REG_OBJECTS_LIB_FUNC(max, Math::Max);
  5904. REG_OBJECTS_LIB_FUNC(min, Math::Min);
  5905. REG_OBJECTS_LIB_FUNC(pow, Math::Pow);
  5906. REG_OBJECTS_LIB_FUNC(random, Math::Random);
  5907. REG_OBJECTS_LIB_FUNC(round, Math::Round);
  5908. REG_OBJECTS_LIB_FUNC(sin, Math::Sin);
  5909. REG_OBJECTS_LIB_FUNC(sqrt, Math::Sqrt);
  5910. REG_OBJECTS_LIB_FUNC(tan, Math::Tan);
  5911. if (scriptContext->GetConfig()->IsES6MathExtensionsEnabled())
  5912. {
  5913. REG_OBJECTS_LIB_FUNC(log10, Math::Log10);
  5914. REG_OBJECTS_LIB_FUNC(log2, Math::Log2);
  5915. REG_OBJECTS_LIB_FUNC(log1p, Math::Log1p);
  5916. REG_OBJECTS_LIB_FUNC(expm1, Math::Expm1);
  5917. REG_OBJECTS_LIB_FUNC(cosh, Math::Cosh);
  5918. REG_OBJECTS_LIB_FUNC(sinh, Math::Sinh);
  5919. REG_OBJECTS_LIB_FUNC(tanh, Math::Tanh);
  5920. REG_OBJECTS_LIB_FUNC(acosh, Math::Acosh);
  5921. REG_OBJECTS_LIB_FUNC(asinh, Math::Asinh);
  5922. REG_OBJECTS_LIB_FUNC(atanh, Math::Atanh);
  5923. REG_OBJECTS_LIB_FUNC(hypot, Math::Hypot);
  5924. REG_OBJECTS_LIB_FUNC(trunc, Math::Trunc);
  5925. REG_OBJECTS_LIB_FUNC(sign, Math::Sign);
  5926. REG_OBJECTS_LIB_FUNC(cbrt, Math::Cbrt);
  5927. REG_OBJECTS_LIB_FUNC(imul, Math::Imul);
  5928. REG_OBJECTS_LIB_FUNC(clz32, Math::Clz32);
  5929. REG_OBJECTS_LIB_FUNC(fround, Math::Fround);
  5930. }
  5931. return hr;
  5932. }
  5933. HRESULT JavascriptLibrary::ProfilerRegisterNumber()
  5934. {
  5935. HRESULT hr = S_OK;
  5936. REG_GLOBAL_CONSTRUCTOR(Number);
  5937. DEFINE_OBJECT_NAME(Number);
  5938. if (scriptContext->GetConfig()->IsES6NumberExtensionsEnabled())
  5939. {
  5940. REG_OBJECTS_LIB_FUNC(isNaN, JavascriptNumber::EntryIsNaN);
  5941. REG_OBJECTS_LIB_FUNC(isFinite, JavascriptNumber::EntryIsFinite);
  5942. REG_OBJECTS_LIB_FUNC(isInteger, JavascriptNumber::EntryIsInteger);
  5943. REG_OBJECTS_LIB_FUNC(isSafeInteger, JavascriptNumber::EntryIsSafeInteger);
  5944. }
  5945. REG_OBJECTS_LIB_FUNC(toExponential, JavascriptNumber::EntryToExponential);
  5946. REG_OBJECTS_LIB_FUNC(toFixed, JavascriptNumber::EntryToFixed);
  5947. REG_OBJECTS_LIB_FUNC(toPrecision, JavascriptNumber::EntryToPrecision);
  5948. REG_OBJECTS_LIB_FUNC(toLocaleString, JavascriptNumber::EntryToLocaleString);
  5949. REG_OBJECTS_LIB_FUNC(toString, JavascriptNumber::EntryToString);
  5950. REG_OBJECTS_LIB_FUNC(valueOf, JavascriptNumber::EntryValueOf);
  5951. return hr;
  5952. }
  5953. HRESULT JavascriptLibrary::ProfilerRegisterString()
  5954. {
  5955. HRESULT hr = S_OK;
  5956. REG_GLOBAL_CONSTRUCTOR(String);
  5957. DEFINE_OBJECT_NAME(String);
  5958. REG_OBJECTS_LIB_FUNC(fromCharCode, JavascriptString::EntryFromCharCode);
  5959. ScriptConfiguration const& config = *(scriptContext->GetConfig());
  5960. if (config.IsES6UnicodeExtensionsEnabled())
  5961. {
  5962. REG_OBJECTS_LIB_FUNC(fromCodePoint, JavascriptString::EntryFromCodePoint);
  5963. REG_OBJECTS_LIB_FUNC(codePointAt, JavascriptString::EntryCodePointAt);
  5964. REG_OBJECTS_LIB_FUNC(normalize, JavascriptString::EntryNormalize);
  5965. }
  5966. REG_OBJECTS_LIB_FUNC(indexOf, JavascriptString::EntryIndexOf);
  5967. REG_OBJECTS_LIB_FUNC(lastIndexOf, JavascriptString::EntryLastIndexOf);
  5968. REG_OBJECTS_LIB_FUNC(replace, JavascriptString::EntryReplace);
  5969. REG_OBJECTS_LIB_FUNC(search, JavascriptString::EntrySearch);
  5970. REG_OBJECTS_LIB_FUNC(slice, JavascriptString::EntrySlice);
  5971. REG_OBJECTS_LIB_FUNC(charAt, JavascriptString::EntryCharAt);
  5972. REG_OBJECTS_LIB_FUNC(charCodeAt, JavascriptString::EntryCharCodeAt);
  5973. REG_OBJECTS_LIB_FUNC(concat, JavascriptString::EntryConcat);
  5974. REG_OBJECTS_LIB_FUNC(localeCompare, JavascriptString::EntryLocaleCompare);
  5975. REG_OBJECTS_LIB_FUNC(match, JavascriptString::EntryMatch);
  5976. REG_OBJECTS_LIB_FUNC(split, JavascriptString::EntrySplit);
  5977. REG_OBJECTS_LIB_FUNC(substring, JavascriptString::EntrySubstring);
  5978. REG_OBJECTS_LIB_FUNC(substr, JavascriptString::EntrySubstr);
  5979. REG_OBJECTS_LIB_FUNC(toLocaleLowerCase, JavascriptString::EntryToLocaleLowerCase);
  5980. REG_OBJECTS_LIB_FUNC(toLocaleUpperCase, JavascriptString::EntryToLocaleUpperCase);
  5981. REG_OBJECTS_LIB_FUNC(toLowerCase, JavascriptString::EntryToLowerCase);
  5982. REG_OBJECTS_LIB_FUNC(toString, JavascriptString::EntryToString);
  5983. REG_OBJECTS_LIB_FUNC(toUpperCase, JavascriptString::EntryToUpperCase);
  5984. REG_OBJECTS_LIB_FUNC(trim, JavascriptString::EntryTrim);
  5985. REG_OBJECTS_LIB_FUNC(valueOf, JavascriptString::EntryValueOf);
  5986. REG_OBJECTS_LIB_FUNC(anchor, JavascriptString::EntryAnchor);
  5987. REG_OBJECTS_LIB_FUNC(big, JavascriptString::EntryBig);
  5988. REG_OBJECTS_LIB_FUNC(blink, JavascriptString::EntryBlink);
  5989. REG_OBJECTS_LIB_FUNC(bold, JavascriptString::EntryBold);
  5990. REG_OBJECTS_LIB_FUNC(fixed, JavascriptString::EntryFixed);
  5991. REG_OBJECTS_LIB_FUNC(fontcolor, JavascriptString::EntryFontColor);
  5992. REG_OBJECTS_LIB_FUNC(fontsize, JavascriptString::EntryFontSize);
  5993. REG_OBJECTS_LIB_FUNC(italics, JavascriptString::EntryItalics);
  5994. REG_OBJECTS_LIB_FUNC(link, JavascriptString::EntryLink);
  5995. REG_OBJECTS_DYNAMIC_LIB_FUNC(_u("small"), 5, JavascriptString::EntrySmall);
  5996. REG_OBJECTS_LIB_FUNC(strike, JavascriptString::EntryStrike);
  5997. REG_OBJECTS_LIB_FUNC(sub, JavascriptString::EntrySub);
  5998. REG_OBJECTS_LIB_FUNC(sup, JavascriptString::EntrySup);
  5999. if (config.IsES6StringExtensionsEnabled())
  6000. {
  6001. REG_OBJECTS_LIB_FUNC(repeat, JavascriptString::EntryRepeat);
  6002. REG_OBJECTS_LIB_FUNC(startsWith, JavascriptString::EntryStartsWith);
  6003. REG_OBJECTS_LIB_FUNC(endsWith, JavascriptString::EntryEndsWith);
  6004. REG_OBJECTS_LIB_FUNC(includes, JavascriptString::EntryIncludes);
  6005. REG_OBJECTS_LIB_FUNC(trimLeft, JavascriptString::EntryTrimLeft);
  6006. REG_OBJECTS_LIB_FUNC(trimRight, JavascriptString::EntryTrimRight);
  6007. }
  6008. REG_OBJECTS_LIB_FUNC(raw, JavascriptString::EntryRaw);
  6009. REG_OBJECTS_LIB_FUNC2(_symbolIterator, _u("[Symbol.iterator]"), JavascriptString::EntrySymbolIterator);
  6010. REG_OBJECTS_LIB_FUNC(padStart, JavascriptString::EntryPadStart);
  6011. REG_OBJECTS_LIB_FUNC(padEnd, JavascriptString::EntryPadEnd);
  6012. return hr;
  6013. }
  6014. HRESULT JavascriptLibrary::ProfilerRegisterRegExp()
  6015. {
  6016. HRESULT hr = S_OK;
  6017. REG_GLOBAL_CONSTRUCTOR(RegExp);
  6018. DEFINE_OBJECT_NAME(RegExp);
  6019. REG_OBJECTS_LIB_FUNC(exec, JavascriptRegExp::EntryExec);
  6020. REG_OBJECTS_LIB_FUNC(test, JavascriptRegExp::EntryTest);
  6021. REG_OBJECTS_LIB_FUNC(toString, JavascriptRegExp::EntryToString);
  6022. // Note: This is deprecated
  6023. REG_OBJECTS_LIB_FUNC(compile, JavascriptRegExp::EntryCompile);
  6024. return hr;
  6025. }
  6026. HRESULT JavascriptLibrary::ProfilerRegisterJSON()
  6027. {
  6028. HRESULT hr = S_OK;
  6029. DEFINE_OBJECT_NAME(JSON);
  6030. REG_OBJECTS_LIB_FUNC(stringify, JSON::Stringify);
  6031. REG_OBJECTS_LIB_FUNC(parse, JSON::Parse);
  6032. return hr;
  6033. }
  6034. HRESULT JavascriptLibrary::ProfilerRegisterAtomics()
  6035. {
  6036. HRESULT hr = S_OK;
  6037. DEFINE_OBJECT_NAME(Atomics);
  6038. REG_OBJECTS_LIB_FUNC(add, AtomicsObject::EntryAdd);
  6039. REG_OBJECTS_LIB_FUNC(and_, AtomicsObject::EntryAnd);
  6040. REG_OBJECTS_LIB_FUNC(compareExchange, AtomicsObject::EntryCompareExchange);
  6041. REG_OBJECTS_LIB_FUNC(exchange, AtomicsObject::EntryExchange);
  6042. REG_OBJECTS_LIB_FUNC(isLockFree, AtomicsObject::EntryIsLockFree);
  6043. REG_OBJECTS_LIB_FUNC(load, AtomicsObject::EntryLoad);
  6044. REG_OBJECTS_LIB_FUNC(or_, AtomicsObject::EntryOr);
  6045. REG_OBJECTS_LIB_FUNC(store, AtomicsObject::EntryStore);
  6046. REG_OBJECTS_LIB_FUNC(sub, AtomicsObject::EntrySub);
  6047. REG_OBJECTS_LIB_FUNC(wait, AtomicsObject::EntryWait);
  6048. REG_OBJECTS_LIB_FUNC(wake, AtomicsObject::EntryWake);
  6049. REG_OBJECTS_LIB_FUNC(xor_, AtomicsObject::EntryXor);
  6050. return hr;
  6051. }
  6052. HRESULT JavascriptLibrary::ProfilerRegisterWeakMap()
  6053. {
  6054. HRESULT hr = S_OK;
  6055. REG_GLOBAL_CONSTRUCTOR(WeakMap);
  6056. DEFINE_OBJECT_NAME(WeakMap);
  6057. REG_OBJECTS_LIB_FUNC2(delete_, _u("delete"), JavascriptWeakMap::EntryDelete);
  6058. REG_OBJECTS_LIB_FUNC(get, JavascriptWeakMap::EntryGet);
  6059. REG_OBJECTS_LIB_FUNC(has, JavascriptWeakMap::EntryHas);
  6060. REG_OBJECTS_LIB_FUNC(set, JavascriptWeakMap::EntrySet);
  6061. return hr;
  6062. }
  6063. HRESULT JavascriptLibrary::ProfilerRegisterWeakSet()
  6064. {
  6065. HRESULT hr = S_OK;
  6066. REG_GLOBAL_CONSTRUCTOR(WeakSet);
  6067. DEFINE_OBJECT_NAME(WeakSet);
  6068. REG_OBJECTS_LIB_FUNC(add, JavascriptWeakSet::EntryAdd);
  6069. REG_OBJECTS_LIB_FUNC2(delete_, _u("delete"), JavascriptWeakSet::EntryDelete);
  6070. REG_OBJECTS_LIB_FUNC(has, JavascriptWeakSet::EntryHas);
  6071. return hr;
  6072. }
  6073. HRESULT JavascriptLibrary::ProfilerRegisterMap()
  6074. {
  6075. HRESULT hr = S_OK;
  6076. REG_GLOBAL_CONSTRUCTOR(Map);
  6077. DEFINE_OBJECT_NAME(Map);
  6078. REG_OBJECTS_LIB_FUNC(clear, JavascriptMap::EntryClear);
  6079. REG_OBJECTS_LIB_FUNC2(delete_, _u("delete"), JavascriptMap::EntryDelete);
  6080. REG_OBJECTS_LIB_FUNC(forEach, JavascriptMap::EntryForEach);
  6081. REG_OBJECTS_LIB_FUNC(get, JavascriptMap::EntryGet);
  6082. REG_OBJECTS_LIB_FUNC(has, JavascriptMap::EntryHas);
  6083. REG_OBJECTS_LIB_FUNC(set, JavascriptMap::EntrySet);
  6084. REG_OBJECTS_LIB_FUNC(entries, JavascriptMap::EntryEntries);
  6085. REG_OBJECTS_LIB_FUNC(keys, JavascriptMap::EntryKeys);
  6086. REG_OBJECTS_LIB_FUNC(values, JavascriptMap::EntryValues);
  6087. return hr;
  6088. }
  6089. HRESULT JavascriptLibrary::ProfilerRegisterSet()
  6090. {
  6091. HRESULT hr = S_OK;
  6092. REG_GLOBAL_CONSTRUCTOR(Set);
  6093. DEFINE_OBJECT_NAME(Set);
  6094. REG_OBJECTS_LIB_FUNC(add, JavascriptSet::EntryAdd);
  6095. REG_OBJECTS_LIB_FUNC(clear, JavascriptSet::EntryClear);
  6096. REG_OBJECTS_LIB_FUNC2(delete_, _u("delete"), JavascriptSet::EntryDelete);
  6097. REG_OBJECTS_LIB_FUNC(forEach, JavascriptSet::EntryForEach);
  6098. REG_OBJECTS_LIB_FUNC(has, JavascriptSet::EntryHas);
  6099. REG_OBJECTS_LIB_FUNC(entries, JavascriptSet::EntryEntries);
  6100. REG_OBJECTS_LIB_FUNC(values, JavascriptSet::EntryValues);
  6101. return hr;
  6102. }
  6103. HRESULT JavascriptLibrary::ProfilerRegisterSymbol()
  6104. {
  6105. HRESULT hr = S_OK;
  6106. REG_GLOBAL_CONSTRUCTOR(Symbol);
  6107. DEFINE_OBJECT_NAME(Symbol);
  6108. REG_OBJECTS_LIB_FUNC(valueOf, JavascriptSymbol::EntryValueOf);
  6109. REG_OBJECTS_LIB_FUNC(toString, JavascriptSymbol::EntryToString);
  6110. REG_OBJECTS_LIB_FUNC2(for_, _u("for"), JavascriptSymbol::EntryFor);
  6111. REG_OBJECTS_LIB_FUNC(keyFor, JavascriptSymbol::EntryKeyFor);
  6112. return hr;
  6113. }
  6114. HRESULT JavascriptLibrary::ProfilerRegisterIterator()
  6115. {
  6116. HRESULT hr = S_OK;
  6117. // Array Iterator has no global constructor
  6118. DEFINE_OBJECT_NAME(Iterator);
  6119. REG_OBJECTS_LIB_FUNC2(_symbolIterator, _u("[Symbol.iterator]"), JavascriptIterator::EntrySymbolIterator);
  6120. return hr;
  6121. }
  6122. HRESULT JavascriptLibrary::ProfilerRegisterArrayIterator()
  6123. {
  6124. HRESULT hr = S_OK;
  6125. // Array Iterator has no global constructor
  6126. #if defined(ENABLE_JS_BUILTINS)
  6127. if (!scriptContext->IsJsBuiltInEnabled())
  6128. {
  6129. DEFINE_OBJECT_NAME(Array Iterator);
  6130. REG_OBJECTS_LIB_FUNC(next, JavascriptArrayIterator::EntryNext);
  6131. }
  6132. #endif
  6133. return hr;
  6134. }
  6135. HRESULT JavascriptLibrary::ProfilerRegisterMapIterator()
  6136. {
  6137. HRESULT hr = S_OK;
  6138. // Map Iterator has no global constructor
  6139. DEFINE_OBJECT_NAME(Map Iterator);
  6140. REG_OBJECTS_LIB_FUNC(next, JavascriptMapIterator::EntryNext);
  6141. return hr;
  6142. }
  6143. HRESULT JavascriptLibrary::ProfilerRegisterSetIterator()
  6144. {
  6145. HRESULT hr = S_OK;
  6146. // Set Iterator has no global constructor
  6147. DEFINE_OBJECT_NAME(Set Iterator);
  6148. REG_OBJECTS_LIB_FUNC(next, JavascriptSetIterator::EntryNext);
  6149. return hr;
  6150. }
  6151. HRESULT JavascriptLibrary::ProfilerRegisterStringIterator()
  6152. {
  6153. HRESULT hr = S_OK;
  6154. // String Iterator has no global constructor
  6155. DEFINE_OBJECT_NAME(String Iterator);
  6156. REG_OBJECTS_LIB_FUNC(next, JavascriptStringIterator::EntryNext);
  6157. return hr;
  6158. }
  6159. HRESULT JavascriptLibrary::ProfilerRegisterTypedArray()
  6160. {
  6161. HRESULT hr = S_OK;
  6162. // TypedArray has no named global constructor
  6163. DEFINE_OBJECT_NAME(TypedArray);
  6164. REG_OBJECTS_LIB_FUNC(from, TypedArrayBase::EntryFrom);
  6165. REG_OBJECTS_LIB_FUNC(of, TypedArrayBase::EntryOf);
  6166. REG_OBJECTS_LIB_FUNC(set, TypedArrayBase::EntrySet);
  6167. REG_OBJECTS_LIB_FUNC(subarray, TypedArrayBase::EntrySubarray);
  6168. REG_OBJECTS_LIB_FUNC(copyWithin, TypedArrayBase::EntryCopyWithin);
  6169. REG_OBJECTS_LIB_FUNC(every, TypedArrayBase::EntryEvery);
  6170. REG_OBJECTS_LIB_FUNC(fill, TypedArrayBase::EntryFill);
  6171. REG_OBJECTS_LIB_FUNC(filter, TypedArrayBase::EntryFilter);
  6172. REG_OBJECTS_LIB_FUNC(find, TypedArrayBase::EntryFind);
  6173. REG_OBJECTS_LIB_FUNC(findIndex, TypedArrayBase::EntryFindIndex);
  6174. REG_OBJECTS_LIB_FUNC(forEach, TypedArrayBase::EntryForEach);
  6175. REG_OBJECTS_LIB_FUNC(indexOf, TypedArrayBase::EntryIndexOf);
  6176. REG_OBJECTS_LIB_FUNC(join, TypedArrayBase::EntryJoin);
  6177. REG_OBJECTS_LIB_FUNC(lastIndexOf, TypedArrayBase::EntryLastIndexOf);
  6178. REG_OBJECTS_LIB_FUNC(map, TypedArrayBase::EntryMap);
  6179. REG_OBJECTS_LIB_FUNC(reduce, TypedArrayBase::EntryReduce);
  6180. REG_OBJECTS_LIB_FUNC(reduceRight, TypedArrayBase::EntryReduceRight);
  6181. REG_OBJECTS_LIB_FUNC(reverse, TypedArrayBase::EntryReverse);
  6182. REG_OBJECTS_LIB_FUNC(slice, TypedArrayBase::EntrySlice);
  6183. REG_OBJECTS_LIB_FUNC(some, TypedArrayBase::EntrySome);
  6184. REG_OBJECTS_LIB_FUNC(sort, TypedArrayBase::EntrySort);
  6185. REG_OBJECTS_LIB_FUNC(includes, TypedArrayBase::EntryIncludes);
  6186. return hr;
  6187. }
  6188. HRESULT JavascriptLibrary::ProfilerRegisterPromise()
  6189. {
  6190. HRESULT hr = S_OK;
  6191. REG_GLOBAL_CONSTRUCTOR(Promise);
  6192. DEFINE_OBJECT_NAME(Promise);
  6193. REG_OBJECTS_LIB_FUNC(all, JavascriptPromise::EntryAll);
  6194. REG_OBJECTS_LIB_FUNC2(catch_, _u("catch"), JavascriptPromise::EntryCatch);
  6195. REG_OBJECTS_LIB_FUNC(race, JavascriptPromise::EntryRace);
  6196. REG_OBJECTS_LIB_FUNC(resolve, JavascriptPromise::EntryResolve);
  6197. REG_OBJECTS_LIB_FUNC(then, JavascriptPromise::EntryThen);
  6198. return hr;
  6199. }
  6200. HRESULT JavascriptLibrary::ProfilerRegisterProxy()
  6201. {
  6202. HRESULT hr = S_OK;
  6203. REG_GLOBAL_CONSTRUCTOR(Proxy);
  6204. DEFINE_OBJECT_NAME(Proxy);
  6205. REG_OBJECTS_LIB_FUNC(revocable, JavascriptProxy::EntryRevocable);
  6206. return hr;
  6207. }
  6208. HRESULT JavascriptLibrary::ProfilerRegisterReflect()
  6209. {
  6210. HRESULT hr = S_OK;
  6211. DEFINE_OBJECT_NAME(Reflect);
  6212. REG_OBJECTS_LIB_FUNC(defineProperty, JavascriptReflect::EntryDefineProperty);
  6213. REG_OBJECTS_LIB_FUNC(deleteProperty, JavascriptReflect::EntryDeleteProperty);
  6214. REG_OBJECTS_LIB_FUNC(get, JavascriptReflect::EntryGet);
  6215. REG_OBJECTS_LIB_FUNC(getOwnPropertyDescriptor, JavascriptReflect::EntryGetOwnPropertyDescriptor);
  6216. REG_OBJECTS_LIB_FUNC(getPrototypeOf, JavascriptReflect::EntryGetPrototypeOf);
  6217. REG_OBJECTS_LIB_FUNC(has, JavascriptReflect::EntryHas);
  6218. REG_OBJECTS_LIB_FUNC(isExtensible, JavascriptReflect::EntryIsExtensible);
  6219. REG_OBJECTS_LIB_FUNC(ownKeys, JavascriptReflect::EntryOwnKeys);
  6220. REG_OBJECTS_LIB_FUNC(preventExtensions, JavascriptReflect::EntryPreventExtensions);
  6221. REG_OBJECTS_LIB_FUNC(set, JavascriptReflect::EntrySet);
  6222. REG_OBJECTS_LIB_FUNC(setPrototypeOf, JavascriptReflect::EntrySetPrototypeOf);
  6223. REG_OBJECTS_LIB_FUNC(apply, JavascriptReflect::EntryApply);
  6224. REG_OBJECTS_LIB_FUNC(construct, JavascriptReflect::EntryConstruct);
  6225. return hr;
  6226. }
  6227. HRESULT JavascriptLibrary::ProfilerRegisterGenerator()
  6228. {
  6229. HRESULT hr = S_OK;
  6230. DEFINE_OBJECT_NAME(Generator);
  6231. REG_OBJECTS_LIB_FUNC(next, JavascriptGenerator::EntryNext);
  6232. REG_OBJECTS_LIB_FUNC(return_, JavascriptGenerator::EntryReturn);
  6233. REG_OBJECTS_LIB_FUNC(throw_, JavascriptGenerator::EntryThrow);
  6234. return hr;
  6235. }
  6236. #endif // ENABLE_SCRIPT_PROFILING
  6237. #if DBG
  6238. void JavascriptLibrary::DumpLibraryByteCode()
  6239. {
  6240. #if defined(ENABLE_JS_BUILTINS) || defined(ENABLE_INTL_OBJECT)
  6241. // We aren't going to be passing in a number to check range of -dump:LibInit, that will be done by Intl/Promise
  6242. // This is just to force init Intl code if dump:LibInit has been passed
  6243. if (CONFIG_ISENABLED(DumpFlag) && Js::Configuration::Global.flags.Dump.IsEnabled(Js::JsLibInitPhase))
  6244. {
  6245. for (uint i = 0; i <= MaxEngineInterfaceExtensionKind; i++)
  6246. {
  6247. EngineExtensionObjectBase* engineExtension = this->GetEngineInterfaceObject()->GetEngineExtension((Js::EngineInterfaceExtensionKind)i);
  6248. if (engineExtension != nullptr && engineExtension->GetHasByteCode())
  6249. {
  6250. engineExtension->DumpByteCode();
  6251. }
  6252. }
  6253. }
  6254. #endif
  6255. }
  6256. #endif
  6257. #ifdef IR_VIEWER
  6258. HRESULT JavascriptLibrary::ProfilerRegisterIRViewer()
  6259. {
  6260. HRESULT hr = S_OK;
  6261. DEFINE_OBJECT_NAME(IRViewer);
  6262. // TODO (t-doilij) move GlobalObject::EntryParseIR to JavascriptIRViewer::EntryParseIR
  6263. REG_LIB_FUNC_CORE(pwszObjectName, _u("parseIR"), scriptContext->GetOrAddPropertyIdTracked(_u("parseIR")), GlobalObject::EntryParseIR);
  6264. REG_LIB_FUNC_CORE(pwszObjectName, _u("functionList"), scriptContext->GetOrAddPropertyIdTracked(_u("functionList")), GlobalObject::EntryFunctionList);
  6265. REG_LIB_FUNC_CORE(pwszObjectName, _u("rejitFunction"), scriptContext->GetOrAddPropertyIdTracked(_u("rejitFunction")), GlobalObject::EntryRejitFunction);
  6266. return hr;
  6267. }
  6268. #endif /* IR_VIEWER */
  6269. }