JavascriptLibrary.cpp 401 KB

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