JavascriptLibrary.cpp 435 KB

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