JavascriptLibrary.cpp 438 KB

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