| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415354163541735418354193542035421354223542335424354253542635427354283542935430354313543235433354343543535436354373543835439354403544135442354433544435445354463544735448354493545035451354523545335454354553545635457354583545935460354613546235463354643546535466354673546835469354703547135472354733547435475354763547735478354793548035481354823548335484354853548635487354883548935490354913549235493354943549535496354973549835499355003550135502355033550435505355063550735508355093551035511355123551335514355153551635517355183551935520355213552235523355243552535526355273552835529355303553135532355333553435535355363553735538355393554035541355423554335544355453554635547355483554935550355513555235553355543555535556355573555835559355603556135562355633556435565355663556735568355693557035571355723557335574355753557635577355783557935580355813558235583355843558535586355873558835589355903559135592355933559435595355963559735598355993560035601356023560335604356053560635607356083560935610356113561235613356143561535616356173561835619356203562135622356233562435625356263562735628356293563035631356323563335634356353563635637356383563935640356413564235643356443564535646356473564835649356503565135652356533565435655356563565735658356593566035661356623566335664356653566635667356683566935670356713567235673356743567535676356773567835679356803568135682356833568435685356863568735688356893569035691356923569335694356953569635697356983569935700357013570235703357043570535706357073570835709357103571135712357133571435715357163571735718357193572035721357223572335724357253572635727357283572935730357313573235733357343573535736357373573835739357403574135742357433574435745357463574735748357493575035751357523575335754357553575635757357583575935760357613576235763357643576535766357673576835769357703577135772357733577435775357763577735778357793578035781357823578335784357853578635787357883578935790357913579235793357943579535796357973579835799358003580135802358033580435805358063580735808358093581035811358123581335814358153581635817358183581935820358213582235823358243582535826358273582835829358303583135832358333583435835358363583735838358393584035841358423584335844358453584635847358483584935850358513585235853358543585535856358573585835859358603586135862358633586435865358663586735868358693587035871358723587335874358753587635877358783587935880358813588235883358843588535886358873588835889358903589135892358933589435895358963589735898358993590035901359023590335904359053590635907359083590935910359113591235913359143591535916359173591835919359203592135922359233592435925359263592735928359293593035931359323593335934359353593635937359383593935940359413594235943359443594535946359473594835949359503595135952359533595435955359563595735958359593596035961359623596335964359653596635967359683596935970359713597235973359743597535976359773597835979359803598135982359833598435985359863598735988359893599035991359923599335994359953599635997359983599936000360013600236003360043600536006360073600836009360103601136012360133601436015360163601736018360193602036021360223602336024360253602636027360283602936030360313603236033360343603536036360373603836039360403604136042360433604436045360463604736048360493605036051360523605336054360553605636057360583605936060360613606236063360643606536066360673606836069360703607136072360733607436075360763607736078360793608036081360823608336084360853608636087360883608936090360913609236093360943609536096360973609836099361003610136102361033610436105361063610736108361093611036111361123611336114361153611636117361183611936120361213612236123361243612536126361273612836129361303613136132361333613436135361363613736138361393614036141361423614336144361453614636147361483614936150361513615236153361543615536156361573615836159361603616136162361633616436165361663616736168361693617036171361723617336174361753617636177361783617936180361813618236183361843618536186361873618836189361903619136192361933619436195361963619736198361993620036201362023620336204362053620636207362083620936210362113621236213362143621536216362173621836219362203622136222362233622436225362263622736228362293623036231362323623336234362353623636237362383623936240362413624236243362443624536246362473624836249362503625136252362533625436255362563625736258362593626036261362623626336264362653626636267362683626936270362713627236273362743627536276362773627836279362803628136282362833628436285362863628736288362893629036291362923629336294362953629636297362983629936300363013630236303363043630536306363073630836309363103631136312363133631436315363163631736318363193632036321363223632336324363253632636327363283632936330363313633236333363343633536336363373633836339363403634136342363433634436345363463634736348363493635036351363523635336354363553635636357363583635936360363613636236363363643636536366363673636836369363703637136372363733637436375363763637736378363793638036381363823638336384363853638636387363883638936390363913639236393363943639536396363973639836399364003640136402364033640436405364063640736408364093641036411364123641336414364153641636417364183641936420364213642236423364243642536426364273642836429364303643136432364333643436435364363643736438364393644036441364423644336444364453644636447364483644936450364513645236453364543645536456364573645836459364603646136462364633646436465364663646736468364693647036471364723647336474364753647636477364783647936480364813648236483364843648536486364873648836489364903649136492364933649436495364963649736498364993650036501365023650336504365053650636507365083650936510365113651236513365143651536516365173651836519365203652136522365233652436525365263652736528365293653036531365323653336534365353653636537365383653936540365413654236543365443654536546365473654836549365503655136552365533655436555365563655736558365593656036561365623656336564365653656636567365683656936570365713657236573365743657536576365773657836579365803658136582365833658436585365863658736588365893659036591365923659336594365953659636597365983659936600366013660236603366043660536606366073660836609366103661136612366133661436615366163661736618366193662036621366223662336624366253662636627366283662936630366313663236633366343663536636366373663836639366403664136642366433664436645366463664736648366493665036651366523665336654366553665636657366583665936660366613666236663366643666536666366673666836669366703667136672366733667436675366763667736678366793668036681366823668336684366853668636687366883668936690366913669236693366943669536696366973669836699367003670136702367033670436705367063670736708367093671036711367123671336714367153671636717367183671936720367213672236723367243672536726367273672836729367303673136732367333673436735367363673736738367393674036741367423674336744367453674636747367483674936750367513675236753367543675536756367573675836759367603676136762367633676436765367663676736768367693677036771367723677336774367753677636777367783677936780367813678236783367843678536786367873678836789367903679136792367933679436795367963679736798367993680036801368023680336804368053680636807368083680936810368113681236813368143681536816368173681836819368203682136822368233682436825368263682736828368293683036831368323683336834368353683636837368383683936840368413684236843368443684536846368473684836849368503685136852368533685436855368563685736858368593686036861368623686336864368653686636867368683686936870368713687236873368743687536876368773687836879368803688136882368833688436885368863688736888368893689036891368923689336894368953689636897368983689936900369013690236903369043690536906369073690836909369103691136912369133691436915369163691736918369193692036921369223692336924369253692636927369283692936930369313693236933369343693536936369373693836939369403694136942369433694436945369463694736948369493695036951369523695336954369553695636957369583695936960369613696236963369643696536966369673696836969369703697136972369733697436975369763697736978369793698036981369823698336984369853698636987369883698936990369913699236993369943699536996369973699836999370003700137002370033700437005370063700737008370093701037011370123701337014370153701637017370183701937020370213702237023370243702537026370273702837029370303703137032370333703437035370363703737038370393704037041370423704337044370453704637047370483704937050370513705237053370543705537056370573705837059370603706137062370633706437065370663706737068370693707037071370723707337074370753707637077370783707937080370813708237083370843708537086370873708837089370903709137092370933709437095370963709737098370993710037101371023710337104371053710637107371083710937110371113711237113371143711537116371173711837119371203712137122371233712437125371263712737128371293713037131371323713337134371353713637137371383713937140371413714237143371443714537146371473714837149371503715137152371533715437155371563715737158371593716037161371623716337164371653716637167371683716937170371713717237173371743717537176371773717837179371803718137182371833718437185371863718737188371893719037191371923719337194371953719637197371983719937200372013720237203372043720537206372073720837209372103721137212372133721437215372163721737218372193722037221372223722337224372253722637227372283722937230372313723237233372343723537236372373723837239372403724137242372433724437245372463724737248372493725037251372523725337254372553725637257372583725937260372613726237263372643726537266372673726837269372703727137272372733727437275372763727737278372793728037281372823728337284372853728637287372883728937290372913729237293372943729537296372973729837299373003730137302373033730437305373063730737308373093731037311373123731337314373153731637317373183731937320373213732237323373243732537326373273732837329373303733137332373333733437335373363733737338373393734037341373423734337344373453734637347373483734937350373513735237353373543735537356373573735837359373603736137362373633736437365373663736737368373693737037371373723737337374373753737637377373783737937380373813738237383373843738537386373873738837389373903739137392373933739437395373963739737398373993740037401374023740337404374053740637407374083740937410374113741237413374143741537416374173741837419374203742137422374233742437425374263742737428374293743037431374323743337434374353743637437374383743937440374413744237443374443744537446374473744837449374503745137452374533745437455374563745737458374593746037461374623746337464374653746637467374683746937470374713747237473374743747537476374773747837479374803748137482374833748437485374863748737488374893749037491374923749337494374953749637497374983749937500375013750237503375043750537506375073750837509375103751137512375133751437515375163751737518375193752037521375223752337524375253752637527375283752937530375313753237533375343753537536375373753837539375403754137542375433754437545375463754737548375493755037551375523755337554375553755637557375583755937560375613756237563375643756537566375673756837569375703757137572375733757437575375763757737578375793758037581375823758337584375853758637587375883758937590375913759237593375943759537596375973759837599376003760137602376033760437605376063760737608376093761037611376123761337614376153761637617376183761937620376213762237623376243762537626376273762837629376303763137632376333763437635376363763737638376393764037641376423764337644376453764637647376483764937650376513765237653376543765537656376573765837659376603766137662376633766437665376663766737668376693767037671376723767337674376753767637677376783767937680376813768237683376843768537686376873768837689376903769137692376933769437695376963769737698376993770037701377023770337704377053770637707377083770937710377113771237713377143771537716377173771837719377203772137722377233772437725377263772737728377293773037731377323773337734377353773637737377383773937740377413774237743377443774537746377473774837749377503775137752377533775437755377563775737758377593776037761377623776337764377653776637767377683776937770377713777237773377743777537776377773777837779377803778137782377833778437785377863778737788377893779037791377923779337794377953779637797377983779937800378013780237803378043780537806378073780837809378103781137812378133781437815378163781737818378193782037821378223782337824378253782637827378283782937830378313783237833378343783537836378373783837839378403784137842378433784437845378463784737848378493785037851378523785337854378553785637857378583785937860378613786237863378643786537866378673786837869378703787137872378733787437875378763787737878378793788037881378823788337884378853788637887378883788937890378913789237893378943789537896378973789837899379003790137902379033790437905379063790737908379093791037911379123791337914379153791637917379183791937920379213792237923379243792537926379273792837929379303793137932379333793437935379363793737938379393794037941379423794337944379453794637947379483794937950379513795237953379543795537956379573795837959379603796137962379633796437965379663796737968379693797037971379723797337974379753797637977379783797937980379813798237983379843798537986379873798837989379903799137992379933799437995379963799737998379993800038001380023800338004380053800638007380083800938010380113801238013380143801538016380173801838019380203802138022380233802438025380263802738028380293803038031380323803338034380353803638037380383803938040380413804238043380443804538046380473804838049380503805138052380533805438055380563805738058380593806038061380623806338064380653806638067380683806938070380713807238073380743807538076380773807838079380803808138082380833808438085380863808738088380893809038091380923809338094380953809638097380983809938100381013810238103381043810538106381073810838109381103811138112381133811438115381163811738118381193812038121381223812338124381253812638127381283812938130381313813238133381343813538136381373813838139381403814138142381433814438145381463814738148381493815038151381523815338154381553815638157381583815938160381613816238163381643816538166381673816838169381703817138172381733817438175381763817738178381793818038181381823818338184381853818638187381883818938190381913819238193381943819538196381973819838199382003820138202382033820438205382063820738208382093821038211382123821338214382153821638217382183821938220382213822238223382243822538226382273822838229382303823138232382333823438235382363823738238382393824038241382423824338244382453824638247382483824938250382513825238253382543825538256382573825838259382603826138262382633826438265382663826738268382693827038271382723827338274382753827638277382783827938280382813828238283382843828538286382873828838289382903829138292382933829438295382963829738298382993830038301383023830338304383053830638307383083830938310383113831238313383143831538316383173831838319383203832138322383233832438325383263832738328383293833038331383323833338334383353833638337383383833938340383413834238343383443834538346383473834838349383503835138352383533835438355383563835738358383593836038361383623836338364383653836638367383683836938370383713837238373383743837538376383773837838379383803838138382383833838438385383863838738388383893839038391383923839338394383953839638397383983839938400384013840238403384043840538406384073840838409384103841138412384133841438415384163841738418384193842038421384223842338424384253842638427384283842938430384313843238433384343843538436384373843838439384403844138442384433844438445384463844738448384493845038451384523845338454384553845638457384583845938460384613846238463384643846538466384673846838469384703847138472384733847438475384763847738478384793848038481384823848338484384853848638487384883848938490384913849238493384943849538496384973849838499385003850138502385033850438505385063850738508385093851038511385123851338514385153851638517385183851938520385213852238523385243852538526385273852838529385303853138532385333853438535385363853738538385393854038541385423854338544385453854638547385483854938550385513855238553385543855538556385573855838559385603856138562385633856438565385663856738568385693857038571385723857338574385753857638577385783857938580385813858238583385843858538586385873858838589385903859138592385933859438595385963859738598385993860038601386023860338604386053860638607386083860938610386113861238613386143861538616386173861838619386203862138622386233862438625386263862738628386293863038631386323863338634386353863638637386383863938640386413864238643386443864538646386473864838649386503865138652386533865438655386563865738658386593866038661386623866338664386653866638667386683866938670386713867238673386743867538676386773867838679386803868138682386833868438685386863868738688386893869038691386923869338694386953869638697386983869938700387013870238703387043870538706387073870838709387103871138712387133871438715387163871738718387193872038721387223872338724387253872638727387283872938730387313873238733387343873538736387373873838739387403874138742387433874438745387463874738748387493875038751387523875338754387553875638757387583875938760387613876238763387643876538766387673876838769387703877138772387733877438775387763877738778387793878038781387823878338784387853878638787387883878938790387913879238793387943879538796387973879838799388003880138802388033880438805388063880738808388093881038811388123881338814388153881638817388183881938820388213882238823388243882538826388273882838829388303883138832388333883438835388363883738838388393884038841388423884338844388453884638847388483884938850388513885238853388543885538856388573885838859388603886138862388633886438865388663886738868388693887038871388723887338874388753887638877388783887938880388813888238883388843888538886388873888838889388903889138892388933889438895388963889738898388993890038901389023890338904389053890638907389083890938910389113891238913389143891538916389173891838919389203892138922389233892438925389263892738928389293893038931389323893338934389353893638937389383893938940389413894238943389443894538946389473894838949389503895138952389533895438955389563895738958389593896038961389623896338964389653896638967389683896938970389713897238973389743897538976389773897838979389803898138982389833898438985389863898738988389893899038991389923899338994389953899638997389983899939000390013900239003390043900539006390073900839009390103901139012390133901439015390163901739018390193902039021390223902339024390253902639027390283902939030390313903239033390343903539036390373903839039390403904139042390433904439045390463904739048390493905039051390523905339054390553905639057390583905939060390613906239063390643906539066390673906839069390703907139072390733907439075390763907739078390793908039081390823908339084390853908639087390883908939090390913909239093390943909539096390973909839099391003910139102391033910439105391063910739108391093911039111391123911339114391153911639117391183911939120391213912239123391243912539126391273912839129391303913139132391333913439135391363913739138391393914039141391423914339144391453914639147391483914939150391513915239153391543915539156391573915839159391603916139162391633916439165391663916739168391693917039171391723917339174391753917639177391783917939180391813918239183391843918539186391873918839189391903919139192391933919439195391963919739198391993920039201392023920339204392053920639207392083920939210392113921239213392143921539216392173921839219392203922139222392233922439225392263922739228392293923039231392323923339234392353923639237392383923939240392413924239243392443924539246392473924839249392503925139252392533925439255392563925739258392593926039261392623926339264392653926639267392683926939270392713927239273392743927539276392773927839279392803928139282392833928439285392863928739288392893929039291392923929339294392953929639297392983929939300393013930239303393043930539306393073930839309393103931139312393133931439315393163931739318393193932039321393223932339324393253932639327393283932939330393313933239333393343933539336393373933839339393403934139342393433934439345393463934739348393493935039351393523935339354393553935639357393583935939360393613936239363393643936539366393673936839369393703937139372393733937439375393763937739378393793938039381393823938339384393853938639387393883938939390393913939239393393943939539396393973939839399394003940139402394033940439405394063940739408394093941039411394123941339414394153941639417394183941939420394213942239423394243942539426394273942839429394303943139432394333943439435394363943739438394393944039441394423944339444394453944639447394483944939450394513945239453394543945539456394573945839459394603946139462394633946439465394663946739468394693947039471394723947339474394753947639477394783947939480394813948239483394843948539486394873948839489394903949139492394933949439495394963949739498394993950039501395023950339504395053950639507395083950939510395113951239513395143951539516395173951839519395203952139522395233952439525395263952739528395293953039531395323953339534395353953639537395383953939540395413954239543395443954539546395473954839549395503955139552395533955439555395563955739558395593956039561395623956339564395653956639567395683956939570395713957239573395743957539576395773957839579395803958139582395833958439585395863958739588395893959039591395923959339594395953959639597395983959939600396013960239603396043960539606396073960839609396103961139612396133961439615396163961739618396193962039621396223962339624396253962639627396283962939630396313963239633396343963539636396373963839639396403964139642396433964439645396463964739648396493965039651396523965339654396553965639657396583965939660396613966239663396643966539666396673966839669396703967139672396733967439675396763967739678396793968039681396823968339684396853968639687396883968939690396913969239693396943969539696396973969839699397003970139702397033970439705397063970739708397093971039711397123971339714397153971639717397183971939720397213972239723397243972539726397273972839729397303973139732397333973439735397363973739738397393974039741397423974339744397453974639747397483974939750397513975239753397543975539756397573975839759397603976139762397633976439765397663976739768397693977039771397723977339774397753977639777397783977939780397813978239783397843978539786397873978839789397903979139792397933979439795397963979739798397993980039801398023980339804398053980639807398083980939810398113981239813398143981539816398173981839819398203982139822398233982439825398263982739828398293983039831398323983339834398353983639837398383983939840398413984239843398443984539846398473984839849398503985139852398533985439855398563985739858398593986039861398623986339864398653986639867398683986939870398713987239873398743987539876398773987839879398803988139882398833988439885398863988739888398893989039891398923989339894398953989639897398983989939900399013990239903399043990539906399073990839909399103991139912399133991439915399163991739918399193992039921399223992339924399253992639927399283992939930399313993239933399343993539936399373993839939399403994139942399433994439945399463994739948399493995039951399523995339954399553995639957399583995939960399613996239963399643996539966399673996839969399703997139972399733997439975399763997739978399793998039981399823998339984399853998639987399883998939990399913999239993399943999539996399973999839999400004000140002400034000440005400064000740008400094001040011400124001340014400154001640017400184001940020400214002240023400244002540026400274002840029400304003140032400334003440035400364003740038400394004040041400424004340044400454004640047400484004940050400514005240053400544005540056400574005840059400604006140062400634006440065400664006740068400694007040071400724007340074400754007640077400784007940080400814008240083400844008540086400874008840089400904009140092400934009440095400964009740098400994010040101401024010340104401054010640107401084010940110401114011240113401144011540116401174011840119401204012140122401234012440125401264012740128401294013040131401324013340134401354013640137401384013940140401414014240143401444014540146401474014840149401504015140152401534015440155401564015740158401594016040161401624016340164401654016640167401684016940170401714017240173401744017540176401774017840179401804018140182401834018440185401864018740188401894019040191401924019340194401954019640197401984019940200402014020240203402044020540206402074020840209402104021140212402134021440215402164021740218402194022040221402224022340224402254022640227402284022940230402314023240233402344023540236402374023840239402404024140242402434024440245402464024740248402494025040251402524025340254402554025640257402584025940260402614026240263402644026540266402674026840269402704027140272402734027440275402764027740278402794028040281402824028340284402854028640287402884028940290402914029240293402944029540296402974029840299403004030140302403034030440305403064030740308403094031040311403124031340314403154031640317403184031940320403214032240323403244032540326403274032840329403304033140332403334033440335403364033740338403394034040341403424034340344403454034640347403484034940350403514035240353403544035540356403574035840359403604036140362403634036440365403664036740368403694037040371403724037340374403754037640377403784037940380403814038240383403844038540386403874038840389403904039140392403934039440395403964039740398403994040040401404024040340404404054040640407404084040940410404114041240413404144041540416404174041840419404204042140422404234042440425404264042740428404294043040431404324043340434404354043640437404384043940440404414044240443404444044540446404474044840449404504045140452404534045440455404564045740458404594046040461404624046340464404654046640467404684046940470404714047240473404744047540476404774047840479404804048140482404834048440485404864048740488404894049040491404924049340494404954049640497404984049940500405014050240503405044050540506405074050840509405104051140512405134051440515405164051740518405194052040521405224052340524405254052640527405284052940530405314053240533405344053540536405374053840539405404054140542405434054440545405464054740548405494055040551405524055340554405554055640557405584055940560405614056240563405644056540566405674056840569405704057140572405734057440575405764057740578405794058040581405824058340584405854058640587405884058940590405914059240593405944059540596405974059840599406004060140602406034060440605406064060740608406094061040611406124061340614406154061640617406184061940620406214062240623406244062540626406274062840629406304063140632406334063440635406364063740638406394064040641406424064340644406454064640647406484064940650406514065240653406544065540656406574065840659406604066140662406634066440665406664066740668406694067040671406724067340674406754067640677406784067940680406814068240683406844068540686406874068840689406904069140692406934069440695406964069740698406994070040701407024070340704407054070640707407084070940710407114071240713407144071540716407174071840719407204072140722407234072440725407264072740728407294073040731407324073340734407354073640737407384073940740407414074240743407444074540746407474074840749407504075140752407534075440755407564075740758407594076040761407624076340764407654076640767407684076940770407714077240773407744077540776407774077840779407804078140782407834078440785407864078740788407894079040791407924079340794407954079640797407984079940800408014080240803408044080540806408074080840809408104081140812408134081440815408164081740818408194082040821408224082340824408254082640827408284082940830408314083240833408344083540836408374083840839408404084140842408434084440845408464084740848408494085040851408524085340854408554085640857408584085940860408614086240863408644086540866408674086840869408704087140872408734087440875408764087740878408794088040881408824088340884408854088640887408884088940890408914089240893408944089540896408974089840899409004090140902409034090440905409064090740908409094091040911409124091340914409154091640917409184091940920409214092240923409244092540926409274092840929409304093140932409334093440935409364093740938409394094040941409424094340944409454094640947409484094940950409514095240953409544095540956409574095840959409604096140962409634096440965409664096740968409694097040971409724097340974409754097640977409784097940980409814098240983409844098540986409874098840989409904099140992409934099440995409964099740998409994100041001410024100341004410054100641007410084100941010410114101241013410144101541016410174101841019410204102141022410234102441025410264102741028410294103041031410324103341034410354103641037410384103941040410414104241043410444104541046410474104841049410504105141052410534105441055410564105741058410594106041061410624106341064410654106641067410684106941070410714107241073410744107541076410774107841079410804108141082410834108441085410864108741088410894109041091410924109341094410954109641097410984109941100411014110241103411044110541106411074110841109411104111141112411134111441115411164111741118411194112041121411224112341124411254112641127411284112941130411314113241133411344113541136411374113841139411404114141142411434114441145411464114741148411494115041151411524115341154411554115641157411584115941160411614116241163411644116541166411674116841169411704117141172411734117441175411764117741178411794118041181411824118341184411854118641187411884118941190411914119241193411944119541196411974119841199412004120141202412034120441205412064120741208412094121041211412124121341214412154121641217412184121941220412214122241223412244122541226412274122841229412304123141232412334123441235412364123741238412394124041241412424124341244412454124641247412484124941250412514125241253412544125541256412574125841259412604126141262412634126441265412664126741268412694127041271412724127341274412754127641277412784127941280412814128241283412844128541286412874128841289412904129141292412934129441295412964129741298412994130041301413024130341304413054130641307413084130941310413114131241313413144131541316413174131841319413204132141322413234132441325413264132741328413294133041331413324133341334413354133641337413384133941340413414134241343413444134541346413474134841349413504135141352413534135441355413564135741358413594136041361413624136341364413654136641367413684136941370413714137241373413744137541376413774137841379413804138141382413834138441385413864138741388413894139041391413924139341394413954139641397413984139941400414014140241403414044140541406414074140841409414104141141412414134141441415414164141741418414194142041421414224142341424414254142641427414284142941430414314143241433414344143541436414374143841439414404144141442414434144441445414464144741448414494145041451414524145341454414554145641457414584145941460414614146241463414644146541466414674146841469414704147141472414734147441475414764147741478414794148041481414824148341484414854148641487414884148941490414914149241493414944149541496414974149841499415004150141502415034150441505415064150741508415094151041511415124151341514415154151641517415184151941520415214152241523415244152541526415274152841529415304153141532415334153441535415364153741538415394154041541415424154341544415454154641547415484154941550415514155241553415544155541556415574155841559415604156141562415634156441565415664156741568415694157041571415724157341574415754157641577415784157941580415814158241583415844158541586415874158841589415904159141592415934159441595415964159741598415994160041601416024160341604416054160641607416084160941610416114161241613416144161541616416174161841619416204162141622416234162441625416264162741628416294163041631416324163341634416354163641637416384163941640416414164241643416444164541646416474164841649416504165141652416534165441655416564165741658416594166041661416624166341664416654166641667416684166941670416714167241673416744167541676416774167841679416804168141682416834168441685416864168741688416894169041691416924169341694416954169641697416984169941700417014170241703417044170541706417074170841709417104171141712417134171441715417164171741718417194172041721417224172341724417254172641727417284172941730417314173241733417344173541736417374173841739417404174141742417434174441745417464174741748417494175041751417524175341754417554175641757417584175941760417614176241763417644176541766417674176841769417704177141772417734177441775417764177741778417794178041781417824178341784417854178641787417884178941790417914179241793417944179541796417974179841799418004180141802418034180441805418064180741808418094181041811418124181341814418154181641817418184181941820418214182241823418244182541826418274182841829418304183141832418334183441835418364183741838418394184041841418424184341844418454184641847418484184941850418514185241853418544185541856418574185841859418604186141862418634186441865418664186741868418694187041871418724187341874418754187641877418784187941880418814188241883418844188541886418874188841889418904189141892418934189441895418964189741898418994190041901419024190341904419054190641907419084190941910419114191241913419144191541916419174191841919419204192141922419234192441925419264192741928419294193041931419324193341934419354193641937419384193941940419414194241943419444194541946419474194841949419504195141952419534195441955419564195741958419594196041961419624196341964419654196641967419684196941970419714197241973419744197541976419774197841979419804198141982419834198441985419864198741988419894199041991419924199341994419954199641997419984199942000420014200242003420044200542006420074200842009420104201142012420134201442015420164201742018420194202042021420224202342024420254202642027420284202942030420314203242033420344203542036420374203842039420404204142042420434204442045420464204742048420494205042051420524205342054420554205642057420584205942060420614206242063420644206542066420674206842069420704207142072420734207442075420764207742078420794208042081420824208342084420854208642087420884208942090420914209242093420944209542096420974209842099421004210142102421034210442105421064210742108421094211042111421124211342114421154211642117421184211942120421214212242123421244212542126421274212842129421304213142132421334213442135421364213742138421394214042141421424214342144421454214642147421484214942150421514215242153421544215542156421574215842159421604216142162421634216442165421664216742168421694217042171421724217342174421754217642177421784217942180421814218242183421844218542186421874218842189421904219142192421934219442195421964219742198421994220042201422024220342204422054220642207422084220942210422114221242213422144221542216422174221842219422204222142222422234222442225422264222742228422294223042231422324223342234422354223642237422384223942240422414224242243422444224542246422474224842249422504225142252422534225442255422564225742258422594226042261422624226342264422654226642267422684226942270422714227242273422744227542276422774227842279422804228142282422834228442285422864228742288422894229042291422924229342294422954229642297422984229942300423014230242303423044230542306423074230842309423104231142312423134231442315423164231742318423194232042321423224232342324423254232642327423284232942330423314233242333423344233542336423374233842339423404234142342423434234442345423464234742348423494235042351423524235342354423554235642357423584235942360423614236242363423644236542366423674236842369423704237142372423734237442375423764237742378423794238042381423824238342384423854238642387423884238942390423914239242393423944239542396423974239842399424004240142402424034240442405424064240742408424094241042411424124241342414424154241642417424184241942420424214242242423424244242542426424274242842429424304243142432424334243442435424364243742438424394244042441424424244342444424454244642447424484244942450424514245242453424544245542456424574245842459424604246142462424634246442465424664246742468424694247042471424724247342474424754247642477424784247942480424814248242483424844248542486424874248842489424904249142492424934249442495424964249742498424994250042501425024250342504425054250642507425084250942510425114251242513425144251542516425174251842519425204252142522425234252442525425264252742528425294253042531425324253342534425354253642537425384253942540425414254242543425444254542546425474254842549425504255142552425534255442555425564255742558425594256042561425624256342564425654256642567425684256942570425714257242573425744257542576425774257842579425804258142582425834258442585425864258742588425894259042591425924259342594425954259642597425984259942600426014260242603426044260542606426074260842609426104261142612426134261442615426164261742618426194262042621426224262342624426254262642627426284262942630426314263242633426344263542636426374263842639426404264142642426434264442645426464264742648426494265042651426524265342654426554265642657426584265942660426614266242663426644266542666426674266842669426704267142672426734267442675426764267742678426794268042681426824268342684426854268642687426884268942690426914269242693426944269542696426974269842699427004270142702427034270442705427064270742708427094271042711427124271342714427154271642717427184271942720427214272242723427244272542726427274272842729427304273142732427334273442735427364273742738427394274042741427424274342744427454274642747427484274942750427514275242753427544275542756427574275842759427604276142762427634276442765427664276742768427694277042771427724277342774427754277642777427784277942780427814278242783427844278542786427874278842789427904279142792427934279442795427964279742798427994280042801428024280342804428054280642807428084280942810428114281242813428144281542816428174281842819428204282142822428234282442825428264282742828428294283042831428324283342834428354283642837428384283942840428414284242843428444284542846428474284842849428504285142852428534285442855428564285742858428594286042861428624286342864428654286642867428684286942870428714287242873428744287542876428774287842879428804288142882428834288442885428864288742888428894289042891428924289342894428954289642897428984289942900429014290242903429044290542906429074290842909429104291142912429134291442915429164291742918429194292042921429224292342924429254292642927429284292942930429314293242933429344293542936429374293842939429404294142942429434294442945429464294742948429494295042951429524295342954429554295642957429584295942960429614296242963429644296542966429674296842969429704297142972429734297442975429764297742978429794298042981429824298342984429854298642987429884298942990429914299242993429944299542996429974299842999430004300143002430034300443005430064300743008430094301043011430124301343014430154301643017430184301943020430214302243023430244302543026430274302843029430304303143032430334303443035430364303743038430394304043041430424304343044430454304643047430484304943050430514305243053430544305543056430574305843059430604306143062430634306443065430664306743068430694307043071430724307343074430754307643077430784307943080430814308243083430844308543086430874308843089430904309143092430934309443095430964309743098430994310043101431024310343104431054310643107431084310943110431114311243113431144311543116431174311843119431204312143122431234312443125431264312743128431294313043131431324313343134431354313643137431384313943140431414314243143431444314543146431474314843149431504315143152431534315443155431564315743158431594316043161431624316343164431654316643167431684316943170431714317243173431744317543176431774317843179431804318143182431834318443185431864318743188431894319043191431924319343194431954319643197431984319943200432014320243203432044320543206432074320843209432104321143212432134321443215432164321743218432194322043221432224322343224432254322643227432284322943230432314323243233432344323543236432374323843239432404324143242432434324443245432464324743248432494325043251432524325343254432554325643257432584325943260432614326243263432644326543266432674326843269432704327143272432734327443275432764327743278432794328043281432824328343284432854328643287432884328943290432914329243293432944329543296432974329843299433004330143302433034330443305433064330743308433094331043311433124331343314433154331643317433184331943320433214332243323433244332543326433274332843329433304333143332433334333443335433364333743338433394334043341433424334343344433454334643347433484334943350433514335243353433544335543356433574335843359433604336143362433634336443365433664336743368433694337043371433724337343374433754337643377433784337943380433814338243383433844338543386433874338843389433904339143392433934339443395433964339743398433994340043401434024340343404434054340643407434084340943410434114341243413434144341543416434174341843419434204342143422434234342443425434264342743428434294343043431434324343343434434354343643437434384343943440434414344243443434444344543446434474344843449434504345143452434534345443455434564345743458434594346043461434624346343464434654346643467434684346943470434714347243473434744347543476434774347843479434804348143482434834348443485434864348743488434894349043491434924349343494434954349643497434984349943500435014350243503435044350543506435074350843509435104351143512435134351443515435164351743518435194352043521435224352343524435254352643527435284352943530435314353243533435344353543536435374353843539435404354143542435434354443545435464354743548435494355043551435524355343554435554355643557435584355943560435614356243563435644356543566435674356843569435704357143572435734357443575435764357743578435794358043581435824358343584435854358643587435884358943590435914359243593435944359543596435974359843599436004360143602436034360443605436064360743608436094361043611436124361343614436154361643617436184361943620436214362243623436244362543626436274362843629436304363143632436334363443635436364363743638436394364043641436424364343644436454364643647436484364943650436514365243653436544365543656436574365843659436604366143662436634366443665436664366743668436694367043671436724367343674436754367643677436784367943680436814368243683436844368543686436874368843689436904369143692436934369443695436964369743698436994370043701437024370343704437054370643707437084370943710437114371243713437144371543716437174371843719437204372143722437234372443725437264372743728437294373043731437324373343734437354373643737437384373943740437414374243743437444374543746437474374843749437504375143752437534375443755437564375743758437594376043761437624376343764437654376643767437684376943770437714377243773437744377543776437774377843779437804378143782437834378443785437864378743788437894379043791437924379343794437954379643797437984379943800438014380243803438044380543806438074380843809438104381143812438134381443815438164381743818438194382043821438224382343824438254382643827438284382943830438314383243833438344383543836438374383843839438404384143842438434384443845438464384743848438494385043851438524385343854438554385643857438584385943860438614386243863438644386543866438674386843869438704387143872438734387443875438764387743878438794388043881438824388343884438854388643887438884388943890438914389243893438944389543896438974389843899439004390143902439034390443905439064390743908439094391043911439124391343914439154391643917439184391943920439214392243923439244392543926439274392843929439304393143932439334393443935439364393743938439394394043941439424394343944439454394643947439484394943950439514395243953439544395543956439574395843959439604396143962439634396443965439664396743968439694397043971439724397343974439754397643977439784397943980439814398243983439844398543986439874398843989439904399143992439934399443995439964399743998439994400044001440024400344004440054400644007440084400944010440114401244013440144401544016440174401844019440204402144022440234402444025440264402744028440294403044031440324403344034440354403644037440384403944040440414404244043440444404544046440474404844049440504405144052440534405444055440564405744058440594406044061440624406344064440654406644067440684406944070440714407244073440744407544076440774407844079440804408144082440834408444085440864408744088440894409044091440924409344094440954409644097440984409944100441014410244103441044410544106441074410844109441104411144112441134411444115441164411744118441194412044121441224412344124441254412644127441284412944130441314413244133441344413544136441374413844139441404414144142441434414444145441464414744148441494415044151441524415344154441554415644157441584415944160441614416244163441644416544166441674416844169441704417144172441734417444175441764417744178441794418044181441824418344184441854418644187441884418944190441914419244193441944419544196441974419844199442004420144202442034420444205442064420744208442094421044211442124421344214442154421644217442184421944220442214422244223442244422544226442274422844229442304423144232442334423444235442364423744238442394424044241442424424344244442454424644247442484424944250442514425244253442544425544256442574425844259442604426144262442634426444265442664426744268442694427044271442724427344274442754427644277442784427944280442814428244283442844428544286442874428844289442904429144292442934429444295442964429744298442994430044301443024430344304443054430644307443084430944310443114431244313443144431544316443174431844319443204432144322443234432444325443264432744328443294433044331443324433344334443354433644337443384433944340443414434244343443444434544346443474434844349443504435144352443534435444355443564435744358443594436044361443624436344364443654436644367443684436944370443714437244373443744437544376443774437844379443804438144382443834438444385443864438744388443894439044391443924439344394443954439644397443984439944400444014440244403444044440544406444074440844409444104441144412444134441444415444164441744418444194442044421444224442344424444254442644427444284442944430444314443244433444344443544436444374443844439444404444144442444434444444445444464444744448444494445044451444524445344454444554445644457444584445944460444614446244463444644446544466444674446844469444704447144472444734447444475444764447744478444794448044481444824448344484444854448644487444884448944490444914449244493444944449544496444974449844499445004450144502445034450444505445064450744508445094451044511445124451344514445154451644517445184451944520445214452244523445244452544526445274452844529445304453144532445334453444535445364453744538445394454044541445424454344544445454454644547445484454944550445514455244553445544455544556445574455844559445604456144562445634456444565445664456744568445694457044571445724457344574445754457644577445784457944580445814458244583445844458544586445874458844589445904459144592445934459444595445964459744598445994460044601446024460344604446054460644607446084460944610446114461244613446144461544616446174461844619446204462144622446234462444625446264462744628446294463044631446324463344634446354463644637446384463944640446414464244643446444464544646446474464844649446504465144652446534465444655446564465744658446594466044661446624466344664446654466644667446684466944670446714467244673446744467544676446774467844679446804468144682446834468444685446864468744688446894469044691446924469344694446954469644697446984469944700447014470244703447044470544706447074470844709447104471144712447134471444715447164471744718447194472044721447224472344724447254472644727447284472944730447314473244733447344473544736447374473844739447404474144742447434474444745447464474744748447494475044751447524475344754447554475644757447584475944760447614476244763447644476544766447674476844769447704477144772447734477444775447764477744778447794478044781447824478344784447854478644787447884478944790447914479244793447944479544796447974479844799448004480144802448034480444805448064480744808448094481044811448124481344814448154481644817448184481944820448214482244823448244482544826448274482844829448304483144832448334483444835448364483744838448394484044841448424484344844448454484644847448484484944850448514485244853448544485544856448574485844859448604486144862448634486444865448664486744868448694487044871448724487344874448754487644877448784487944880448814488244883448844488544886448874488844889448904489144892448934489444895448964489744898448994490044901449024490344904449054490644907449084490944910449114491244913449144491544916449174491844919449204492144922449234492444925449264492744928449294493044931449324493344934449354493644937449384493944940449414494244943449444494544946449474494844949449504495144952449534495444955449564495744958449594496044961449624496344964449654496644967449684496944970449714497244973449744497544976449774497844979449804498144982449834498444985449864498744988449894499044991449924499344994449954499644997449984499945000450014500245003450044500545006450074500845009450104501145012450134501445015450164501745018450194502045021450224502345024450254502645027450284502945030450314503245033450344503545036450374503845039450404504145042450434504445045450464504745048450494505045051450524505345054450554505645057450584505945060450614506245063450644506545066450674506845069450704507145072450734507445075450764507745078450794508045081450824508345084450854508645087450884508945090450914509245093450944509545096450974509845099451004510145102451034510445105451064510745108451094511045111451124511345114451154511645117451184511945120451214512245123451244512545126451274512845129451304513145132451334513445135451364513745138451394514045141451424514345144451454514645147451484514945150451514515245153451544515545156451574515845159451604516145162451634516445165451664516745168451694517045171451724517345174451754517645177451784517945180451814518245183451844518545186451874518845189451904519145192451934519445195451964519745198451994520045201452024520345204452054520645207452084520945210452114521245213452144521545216452174521845219452204522145222452234522445225452264522745228452294523045231452324523345234452354523645237452384523945240452414524245243452444524545246452474524845249452504525145252452534525445255452564525745258452594526045261452624526345264452654526645267452684526945270452714527245273452744527545276452774527845279452804528145282452834528445285452864528745288452894529045291452924529345294452954529645297452984529945300453014530245303453044530545306453074530845309453104531145312453134531445315453164531745318453194532045321453224532345324453254532645327453284532945330453314533245333453344533545336453374533845339453404534145342453434534445345453464534745348453494535045351453524535345354453554535645357453584535945360453614536245363453644536545366453674536845369453704537145372453734537445375453764537745378453794538045381453824538345384453854538645387453884538945390453914539245393453944539545396453974539845399454004540145402454034540445405454064540745408454094541045411454124541345414454154541645417454184541945420454214542245423454244542545426454274542845429454304543145432454334543445435454364543745438454394544045441454424544345444454454544645447454484544945450454514545245453454544545545456454574545845459454604546145462454634546445465454664546745468454694547045471454724547345474454754547645477454784547945480454814548245483454844548545486454874548845489454904549145492454934549445495454964549745498454994550045501455024550345504455054550645507455084550945510455114551245513455144551545516455174551845519455204552145522455234552445525455264552745528455294553045531455324553345534455354553645537455384553945540455414554245543455444554545546455474554845549455504555145552455534555445555455564555745558455594556045561455624556345564455654556645567455684556945570455714557245573455744557545576455774557845579455804558145582455834558445585455864558745588455894559045591455924559345594455954559645597455984559945600456014560245603456044560545606456074560845609456104561145612456134561445615456164561745618456194562045621456224562345624456254562645627456284562945630456314563245633456344563545636456374563845639456404564145642456434564445645456464564745648456494565045651456524565345654456554565645657456584565945660456614566245663456644566545666456674566845669456704567145672456734567445675456764567745678456794568045681456824568345684456854568645687456884568945690456914569245693456944569545696456974569845699457004570145702457034570445705457064570745708457094571045711457124571345714457154571645717457184571945720457214572245723457244572545726457274572845729457304573145732457334573445735457364573745738457394574045741457424574345744457454574645747457484574945750457514575245753457544575545756457574575845759457604576145762457634576445765457664576745768457694577045771457724577345774457754577645777457784577945780457814578245783457844578545786457874578845789457904579145792457934579445795457964579745798457994580045801458024580345804458054580645807458084580945810458114581245813458144581545816458174581845819458204582145822458234582445825458264582745828458294583045831458324583345834458354583645837458384583945840458414584245843458444584545846458474584845849458504585145852458534585445855458564585745858458594586045861458624586345864458654586645867458684586945870458714587245873458744587545876458774587845879458804588145882458834588445885458864588745888458894589045891458924589345894458954589645897458984589945900459014590245903459044590545906459074590845909459104591145912459134591445915459164591745918459194592045921459224592345924459254592645927459284592945930459314593245933459344593545936459374593845939459404594145942459434594445945459464594745948459494595045951459524595345954459554595645957459584595945960459614596245963459644596545966459674596845969459704597145972459734597445975459764597745978459794598045981459824598345984459854598645987459884598945990459914599245993459944599545996459974599845999460004600146002460034600446005460064600746008460094601046011460124601346014460154601646017460184601946020460214602246023460244602546026460274602846029460304603146032460334603446035460364603746038460394604046041460424604346044460454604646047460484604946050460514605246053460544605546056460574605846059460604606146062460634606446065460664606746068460694607046071460724607346074460754607646077460784607946080460814608246083460844608546086460874608846089460904609146092460934609446095460964609746098460994610046101461024610346104461054610646107461084610946110461114611246113461144611546116461174611846119461204612146122461234612446125461264612746128461294613046131461324613346134461354613646137461384613946140461414614246143461444614546146461474614846149461504615146152461534615446155461564615746158461594616046161461624616346164461654616646167461684616946170461714617246173461744617546176461774617846179461804618146182461834618446185461864618746188461894619046191461924619346194461954619646197461984619946200462014620246203462044620546206462074620846209462104621146212462134621446215462164621746218462194622046221462224622346224462254622646227462284622946230462314623246233462344623546236462374623846239462404624146242462434624446245462464624746248462494625046251462524625346254462554625646257462584625946260462614626246263462644626546266462674626846269462704627146272462734627446275462764627746278462794628046281462824628346284462854628646287462884628946290462914629246293462944629546296462974629846299463004630146302463034630446305463064630746308463094631046311463124631346314463154631646317463184631946320463214632246323463244632546326463274632846329463304633146332463334633446335463364633746338463394634046341463424634346344463454634646347463484634946350463514635246353463544635546356463574635846359463604636146362463634636446365463664636746368463694637046371463724637346374463754637646377463784637946380463814638246383463844638546386463874638846389463904639146392463934639446395463964639746398463994640046401464024640346404464054640646407464084640946410464114641246413464144641546416464174641846419464204642146422464234642446425464264642746428464294643046431464324643346434464354643646437464384643946440464414644246443464444644546446464474644846449464504645146452464534645446455464564645746458464594646046461464624646346464464654646646467464684646946470464714647246473464744647546476464774647846479464804648146482464834648446485464864648746488464894649046491464924649346494464954649646497464984649946500465014650246503465044650546506465074650846509465104651146512465134651446515465164651746518465194652046521465224652346524465254652646527465284652946530465314653246533465344653546536465374653846539465404654146542465434654446545465464654746548465494655046551465524655346554465554655646557465584655946560465614656246563465644656546566465674656846569465704657146572465734657446575465764657746578465794658046581465824658346584465854658646587465884658946590465914659246593465944659546596465974659846599466004660146602466034660446605466064660746608466094661046611466124661346614466154661646617466184661946620466214662246623466244662546626466274662846629466304663146632466334663446635466364663746638466394664046641466424664346644466454664646647466484664946650466514665246653466544665546656466574665846659466604666146662466634666446665466664666746668466694667046671466724667346674466754667646677466784667946680466814668246683466844668546686466874668846689466904669146692466934669446695466964669746698466994670046701467024670346704467054670646707467084670946710467114671246713467144671546716467174671846719467204672146722467234672446725467264672746728467294673046731467324673346734467354673646737467384673946740467414674246743467444674546746467474674846749467504675146752467534675446755467564675746758467594676046761467624676346764467654676646767467684676946770467714677246773467744677546776467774677846779467804678146782467834678446785467864678746788467894679046791467924679346794467954679646797467984679946800468014680246803468044680546806468074680846809468104681146812468134681446815468164681746818468194682046821468224682346824468254682646827468284682946830468314683246833468344683546836468374683846839468404684146842468434684446845468464684746848468494685046851468524685346854468554685646857468584685946860468614686246863468644686546866468674686846869468704687146872468734687446875468764687746878468794688046881468824688346884468854688646887468884688946890468914689246893468944689546896468974689846899469004690146902469034690446905469064690746908469094691046911469124691346914469154691646917469184691946920469214692246923469244692546926469274692846929469304693146932469334693446935469364693746938469394694046941469424694346944469454694646947469484694946950469514695246953469544695546956469574695846959469604696146962469634696446965469664696746968469694697046971469724697346974469754697646977469784697946980469814698246983469844698546986469874698846989469904699146992469934699446995469964699746998469994700047001470024700347004470054700647007470084700947010470114701247013470144701547016470174701847019470204702147022470234702447025470264702747028470294703047031470324703347034470354703647037470384703947040470414704247043470444704547046470474704847049470504705147052470534705447055470564705747058470594706047061470624706347064470654706647067470684706947070470714707247073470744707547076470774707847079470804708147082470834708447085470864708747088470894709047091470924709347094470954709647097470984709947100471014710247103471044710547106471074710847109471104711147112471134711447115471164711747118471194712047121471224712347124471254712647127471284712947130471314713247133471344713547136471374713847139471404714147142471434714447145471464714747148471494715047151471524715347154471554715647157471584715947160471614716247163471644716547166471674716847169471704717147172471734717447175471764717747178471794718047181471824718347184471854718647187471884718947190471914719247193471944719547196471974719847199472004720147202472034720447205472064720747208472094721047211472124721347214472154721647217472184721947220472214722247223472244722547226472274722847229472304723147232472334723447235472364723747238472394724047241472424724347244472454724647247472484724947250472514725247253472544725547256472574725847259472604726147262472634726447265472664726747268472694727047271472724727347274472754727647277472784727947280472814728247283472844728547286472874728847289472904729147292472934729447295472964729747298472994730047301473024730347304473054730647307473084730947310473114731247313473144731547316473174731847319473204732147322473234732447325473264732747328473294733047331473324733347334473354733647337473384733947340473414734247343473444734547346473474734847349473504735147352473534735447355473564735747358473594736047361473624736347364473654736647367473684736947370473714737247373473744737547376473774737847379473804738147382473834738447385473864738747388473894739047391473924739347394473954739647397473984739947400474014740247403474044740547406474074740847409474104741147412474134741447415474164741747418474194742047421474224742347424474254742647427474284742947430474314743247433474344743547436474374743847439474404744147442474434744447445474464744747448474494745047451474524745347454474554745647457474584745947460474614746247463474644746547466474674746847469474704747147472474734747447475474764747747478474794748047481474824748347484474854748647487474884748947490474914749247493474944749547496474974749847499475004750147502475034750447505475064750747508475094751047511475124751347514475154751647517475184751947520475214752247523475244752547526475274752847529475304753147532475334753447535475364753747538475394754047541475424754347544475454754647547475484754947550475514755247553475544755547556475574755847559475604756147562475634756447565475664756747568475694757047571475724757347574475754757647577475784757947580475814758247583475844758547586475874758847589475904759147592475934759447595475964759747598475994760047601476024760347604476054760647607476084760947610476114761247613476144761547616476174761847619476204762147622476234762447625476264762747628476294763047631476324763347634476354763647637476384763947640476414764247643476444764547646476474764847649476504765147652476534765447655476564765747658476594766047661476624766347664476654766647667476684766947670476714767247673476744767547676476774767847679476804768147682476834768447685476864768747688476894769047691476924769347694476954769647697476984769947700477014770247703477044770547706477074770847709477104771147712477134771447715477164771747718477194772047721477224772347724477254772647727477284772947730477314773247733477344773547736477374773847739477404774147742477434774447745477464774747748477494775047751477524775347754477554775647757477584775947760477614776247763477644776547766477674776847769477704777147772477734777447775477764777747778477794778047781477824778347784477854778647787477884778947790477914779247793477944779547796477974779847799478004780147802478034780447805478064780747808478094781047811478124781347814478154781647817478184781947820478214782247823478244782547826478274782847829478304783147832478334783447835478364783747838478394784047841478424784347844478454784647847478484784947850478514785247853478544785547856478574785847859478604786147862478634786447865478664786747868478694787047871478724787347874478754787647877478784787947880478814788247883478844788547886478874788847889478904789147892478934789447895478964789747898478994790047901479024790347904479054790647907479084790947910479114791247913479144791547916479174791847919479204792147922479234792447925479264792747928479294793047931479324793347934479354793647937479384793947940479414794247943479444794547946479474794847949479504795147952479534795447955479564795747958479594796047961479624796347964479654796647967479684796947970479714797247973479744797547976479774797847979479804798147982479834798447985479864798747988479894799047991479924799347994479954799647997479984799948000480014800248003480044800548006480074800848009480104801148012480134801448015480164801748018480194802048021480224802348024480254802648027480284802948030480314803248033480344803548036480374803848039480404804148042480434804448045480464804748048480494805048051480524805348054480554805648057480584805948060480614806248063480644806548066480674806848069480704807148072480734807448075480764807748078480794808048081480824808348084480854808648087480884808948090480914809248093480944809548096480974809848099481004810148102481034810448105481064810748108481094811048111481124811348114481154811648117481184811948120481214812248123481244812548126481274812848129481304813148132481334813448135481364813748138481394814048141481424814348144481454814648147481484814948150481514815248153481544815548156481574815848159481604816148162481634816448165481664816748168481694817048171481724817348174481754817648177481784817948180481814818248183481844818548186481874818848189481904819148192481934819448195481964819748198481994820048201482024820348204482054820648207482084820948210482114821248213482144821548216482174821848219482204822148222482234822448225482264822748228482294823048231482324823348234482354823648237482384823948240482414824248243482444824548246482474824848249482504825148252482534825448255482564825748258482594826048261482624826348264482654826648267482684826948270482714827248273482744827548276482774827848279482804828148282482834828448285482864828748288482894829048291482924829348294482954829648297482984829948300483014830248303483044830548306483074830848309483104831148312483134831448315483164831748318483194832048321483224832348324483254832648327483284832948330483314833248333483344833548336483374833848339483404834148342483434834448345483464834748348483494835048351483524835348354483554835648357483584835948360483614836248363483644836548366483674836848369483704837148372483734837448375483764837748378483794838048381483824838348384483854838648387483884838948390483914839248393483944839548396483974839848399484004840148402484034840448405484064840748408484094841048411484124841348414484154841648417484184841948420484214842248423484244842548426484274842848429484304843148432484334843448435484364843748438484394844048441484424844348444484454844648447484484844948450484514845248453484544845548456484574845848459484604846148462484634846448465484664846748468484694847048471484724847348474484754847648477484784847948480484814848248483484844848548486484874848848489484904849148492484934849448495484964849748498484994850048501485024850348504485054850648507485084850948510485114851248513485144851548516485174851848519485204852148522485234852448525485264852748528485294853048531485324853348534485354853648537485384853948540485414854248543485444854548546485474854848549485504855148552485534855448555485564855748558485594856048561485624856348564485654856648567485684856948570485714857248573485744857548576485774857848579485804858148582485834858448585485864858748588485894859048591485924859348594485954859648597485984859948600486014860248603486044860548606486074860848609486104861148612486134861448615486164861748618486194862048621486224862348624486254862648627486284862948630486314863248633486344863548636486374863848639486404864148642486434864448645486464864748648486494865048651486524865348654486554865648657486584865948660486614866248663486644866548666486674866848669486704867148672486734867448675486764867748678486794868048681486824868348684486854868648687486884868948690486914869248693486944869548696486974869848699487004870148702487034870448705487064870748708487094871048711487124871348714487154871648717487184871948720487214872248723487244872548726487274872848729487304873148732487334873448735487364873748738487394874048741487424874348744487454874648747487484874948750487514875248753487544875548756487574875848759487604876148762487634876448765487664876748768487694877048771487724877348774487754877648777487784877948780487814878248783487844878548786487874878848789487904879148792487934879448795487964879748798487994880048801488024880348804488054880648807488084880948810488114881248813488144881548816488174881848819488204882148822488234882448825488264882748828488294883048831488324883348834488354883648837488384883948840488414884248843488444884548846488474884848849488504885148852488534885448855488564885748858488594886048861488624886348864488654886648867488684886948870488714887248873488744887548876488774887848879488804888148882488834888448885488864888748888488894889048891488924889348894488954889648897488984889948900489014890248903489044890548906489074890848909489104891148912489134891448915489164891748918489194892048921489224892348924489254892648927489284892948930489314893248933489344893548936489374893848939489404894148942489434894448945489464894748948489494895048951489524895348954489554895648957489584895948960489614896248963489644896548966489674896848969489704897148972489734897448975489764897748978489794898048981489824898348984489854898648987489884898948990489914899248993489944899548996489974899848999490004900149002490034900449005490064900749008490094901049011490124901349014490154901649017490184901949020490214902249023490244902549026490274902849029490304903149032490334903449035490364903749038490394904049041490424904349044490454904649047490484904949050490514905249053490544905549056490574905849059490604906149062490634906449065490664906749068490694907049071490724907349074490754907649077490784907949080490814908249083490844908549086490874908849089490904909149092490934909449095490964909749098490994910049101491024910349104491054910649107491084910949110491114911249113491144911549116491174911849119491204912149122491234912449125491264912749128491294913049131491324913349134491354913649137491384913949140491414914249143491444914549146491474914849149491504915149152491534915449155491564915749158491594916049161491624916349164491654916649167491684916949170491714917249173491744917549176491774917849179491804918149182491834918449185491864918749188491894919049191491924919349194491954919649197491984919949200492014920249203492044920549206492074920849209492104921149212492134921449215492164921749218492194922049221492224922349224492254922649227492284922949230492314923249233492344923549236492374923849239492404924149242492434924449245492464924749248492494925049251492524925349254492554925649257492584925949260492614926249263492644926549266492674926849269492704927149272492734927449275492764927749278492794928049281492824928349284492854928649287492884928949290492914929249293492944929549296492974929849299493004930149302493034930449305493064930749308493094931049311493124931349314493154931649317493184931949320493214932249323493244932549326493274932849329493304933149332493334933449335493364933749338493394934049341493424934349344493454934649347493484934949350493514935249353493544935549356493574935849359493604936149362493634936449365493664936749368493694937049371493724937349374493754937649377493784937949380493814938249383493844938549386493874938849389493904939149392493934939449395493964939749398493994940049401494024940349404494054940649407494084940949410494114941249413494144941549416494174941849419494204942149422494234942449425494264942749428494294943049431494324943349434494354943649437494384943949440494414944249443494444944549446494474944849449494504945149452494534945449455494564945749458494594946049461494624946349464494654946649467494684946949470494714947249473494744947549476494774947849479494804948149482494834948449485494864948749488494894949049491494924949349494494954949649497494984949949500495014950249503495044950549506495074950849509495104951149512495134951449515495164951749518495194952049521495224952349524495254952649527495284952949530495314953249533495344953549536495374953849539495404954149542495434954449545495464954749548495494955049551495524955349554495554955649557495584955949560495614956249563495644956549566495674956849569495704957149572495734957449575495764957749578495794958049581495824958349584495854958649587495884958949590495914959249593495944959549596495974959849599496004960149602496034960449605496064960749608496094961049611496124961349614496154961649617496184961949620496214962249623496244962549626496274962849629496304963149632496334963449635496364963749638496394964049641496424964349644496454964649647496484964949650496514965249653496544965549656496574965849659496604966149662496634966449665496664966749668496694967049671496724967349674496754967649677496784967949680496814968249683496844968549686496874968849689496904969149692496934969449695496964969749698496994970049701497024970349704497054970649707497084970949710497114971249713497144971549716497174971849719497204972149722497234972449725497264972749728497294973049731497324973349734497354973649737497384973949740497414974249743497444974549746497474974849749497504975149752497534975449755497564975749758497594976049761497624976349764497654976649767497684976949770497714977249773497744977549776497774977849779497804978149782497834978449785497864978749788497894979049791497924979349794497954979649797497984979949800498014980249803498044980549806498074980849809498104981149812498134981449815498164981749818498194982049821498224982349824498254982649827498284982949830498314983249833498344983549836498374983849839498404984149842498434984449845498464984749848498494985049851498524985349854498554985649857498584985949860498614986249863498644986549866498674986849869498704987149872498734987449875498764987749878498794988049881498824988349884498854988649887498884988949890498914989249893498944989549896498974989849899499004990149902499034990449905499064990749908499094991049911499124991349914499154991649917499184991949920499214992249923499244992549926499274992849929499304993149932499334993449935499364993749938499394994049941499424994349944499454994649947499484994949950499514995249953499544995549956499574995849959499604996149962499634996449965499664996749968499694997049971499724997349974499754997649977499784997949980499814998249983499844998549986499874998849989499904999149992499934999449995499964999749998499995000050001500025000350004500055000650007500085000950010500115001250013500145001550016500175001850019500205002150022500235002450025500265002750028500295003050031500325003350034500355003650037500385003950040500415004250043500445004550046500475004850049500505005150052500535005450055500565005750058500595006050061500625006350064500655006650067500685006950070500715007250073500745007550076500775007850079500805008150082500835008450085500865008750088500895009050091500925009350094500955009650097500985009950100501015010250103501045010550106501075010850109501105011150112501135011450115501165011750118501195012050121501225012350124501255012650127501285012950130501315013250133501345013550136501375013850139501405014150142501435014450145501465014750148501495015050151501525015350154501555015650157501585015950160501615016250163501645016550166501675016850169501705017150172501735017450175501765017750178501795018050181501825018350184501855018650187501885018950190501915019250193501945019550196501975019850199502005020150202502035020450205502065020750208502095021050211502125021350214502155021650217502185021950220502215022250223502245022550226502275022850229502305023150232502335023450235502365023750238502395024050241502425024350244502455024650247502485024950250502515025250253502545025550256502575025850259502605026150262502635026450265502665026750268502695027050271502725027350274502755027650277502785027950280502815028250283502845028550286502875028850289502905029150292502935029450295502965029750298502995030050301503025030350304503055030650307503085030950310503115031250313503145031550316503175031850319503205032150322503235032450325503265032750328503295033050331503325033350334503355033650337503385033950340503415034250343503445034550346503475034850349503505035150352503535035450355503565035750358503595036050361503625036350364503655036650367503685036950370503715037250373503745037550376503775037850379503805038150382503835038450385503865038750388503895039050391503925039350394503955039650397503985039950400504015040250403504045040550406504075040850409504105041150412504135041450415504165041750418504195042050421504225042350424504255042650427504285042950430504315043250433504345043550436504375043850439504405044150442504435044450445504465044750448504495045050451504525045350454504555045650457504585045950460504615046250463504645046550466504675046850469504705047150472504735047450475504765047750478504795048050481504825048350484504855048650487504885048950490504915049250493504945049550496504975049850499505005050150502505035050450505505065050750508505095051050511505125051350514505155051650517505185051950520505215052250523505245052550526505275052850529505305053150532505335053450535505365053750538505395054050541505425054350544505455054650547505485054950550505515055250553505545055550556505575055850559505605056150562505635056450565505665056750568505695057050571505725057350574505755057650577505785057950580505815058250583505845058550586505875058850589505905059150592505935059450595505965059750598505995060050601506025060350604506055060650607506085060950610506115061250613506145061550616506175061850619506205062150622506235062450625506265062750628506295063050631506325063350634506355063650637506385063950640506415064250643506445064550646506475064850649506505065150652506535065450655506565065750658506595066050661506625066350664506655066650667506685066950670506715067250673506745067550676506775067850679506805068150682506835068450685506865068750688506895069050691506925069350694506955069650697506985069950700507015070250703507045070550706507075070850709507105071150712507135071450715507165071750718507195072050721507225072350724507255072650727507285072950730507315073250733507345073550736507375073850739507405074150742507435074450745507465074750748507495075050751507525075350754507555075650757507585075950760507615076250763507645076550766507675076850769507705077150772507735077450775507765077750778507795078050781507825078350784507855078650787507885078950790507915079250793507945079550796507975079850799508005080150802508035080450805508065080750808508095081050811508125081350814508155081650817508185081950820508215082250823508245082550826508275082850829508305083150832508335083450835508365083750838508395084050841508425084350844508455084650847508485084950850508515085250853508545085550856508575085850859508605086150862508635086450865508665086750868508695087050871508725087350874508755087650877508785087950880508815088250883508845088550886508875088850889508905089150892508935089450895508965089750898508995090050901509025090350904509055090650907509085090950910509115091250913509145091550916509175091850919509205092150922509235092450925509265092750928509295093050931509325093350934509355093650937509385093950940509415094250943509445094550946509475094850949509505095150952509535095450955509565095750958509595096050961509625096350964509655096650967509685096950970509715097250973509745097550976509775097850979509805098150982509835098450985509865098750988509895099050991509925099350994509955099650997509985099951000510015100251003510045100551006510075100851009510105101151012510135101451015510165101751018510195102051021510225102351024510255102651027510285102951030510315103251033510345103551036510375103851039510405104151042510435104451045510465104751048510495105051051510525105351054510555105651057510585105951060510615106251063510645106551066510675106851069510705107151072510735107451075510765107751078510795108051081510825108351084510855108651087510885108951090510915109251093510945109551096510975109851099511005110151102511035110451105511065110751108511095111051111511125111351114511155111651117511185111951120511215112251123511245112551126511275112851129511305113151132511335113451135511365113751138511395114051141511425114351144511455114651147511485114951150511515115251153511545115551156511575115851159511605116151162511635116451165511665116751168511695117051171511725117351174511755117651177511785117951180511815118251183511845118551186511875118851189511905119151192511935119451195511965119751198511995120051201512025120351204512055120651207512085120951210512115121251213512145121551216512175121851219512205122151222512235122451225512265122751228512295123051231512325123351234512355123651237512385123951240512415124251243512445124551246512475124851249512505125151252512535125451255512565125751258512595126051261512625126351264512655126651267512685126951270512715127251273512745127551276512775127851279512805128151282512835128451285512865128751288512895129051291512925129351294512955129651297512985129951300513015130251303513045130551306513075130851309513105131151312513135131451315513165131751318513195132051321513225132351324513255132651327513285132951330513315133251333513345133551336513375133851339513405134151342513435134451345513465134751348513495135051351513525135351354513555135651357513585135951360513615136251363513645136551366513675136851369513705137151372513735137451375513765137751378513795138051381513825138351384513855138651387513885138951390513915139251393513945139551396513975139851399514005140151402514035140451405514065140751408514095141051411514125141351414514155141651417514185141951420514215142251423514245142551426514275142851429514305143151432514335143451435514365143751438514395144051441514425144351444514455144651447514485144951450514515145251453514545145551456514575145851459514605146151462514635146451465514665146751468514695147051471514725147351474514755147651477514785147951480514815148251483514845148551486514875148851489514905149151492514935149451495514965149751498514995150051501515025150351504515055150651507515085150951510515115151251513515145151551516515175151851519515205152151522515235152451525515265152751528515295153051531515325153351534515355153651537515385153951540515415154251543515445154551546515475154851549515505155151552515535155451555515565155751558515595156051561515625156351564515655156651567515685156951570515715157251573515745157551576515775157851579515805158151582515835158451585515865158751588515895159051591515925159351594515955159651597515985159951600516015160251603516045160551606516075160851609516105161151612516135161451615516165161751618516195162051621516225162351624516255162651627516285162951630516315163251633516345163551636516375163851639516405164151642516435164451645516465164751648516495165051651516525165351654516555165651657516585165951660516615166251663516645166551666516675166851669516705167151672516735167451675516765167751678516795168051681516825168351684516855168651687516885168951690516915169251693516945169551696516975169851699517005170151702517035170451705517065170751708517095171051711517125171351714517155171651717517185171951720517215172251723517245172551726517275172851729517305173151732517335173451735517365173751738517395174051741517425174351744517455174651747517485174951750517515175251753517545175551756517575175851759517605176151762517635176451765517665176751768517695177051771517725177351774517755177651777517785177951780517815178251783517845178551786517875178851789517905179151792517935179451795517965179751798517995180051801518025180351804518055180651807518085180951810518115181251813518145181551816518175181851819518205182151822518235182451825518265182751828518295183051831518325183351834518355183651837518385183951840518415184251843518445184551846518475184851849518505185151852518535185451855518565185751858518595186051861518625186351864518655186651867518685186951870518715187251873518745187551876518775187851879518805188151882518835188451885518865188751888518895189051891518925189351894518955189651897518985189951900519015190251903519045190551906519075190851909519105191151912519135191451915519165191751918519195192051921519225192351924519255192651927519285192951930519315193251933519345193551936519375193851939519405194151942519435194451945519465194751948519495195051951519525195351954519555195651957519585195951960519615196251963519645196551966519675196851969519705197151972519735197451975519765197751978519795198051981519825198351984519855198651987519885198951990519915199251993519945199551996519975199851999520005200152002520035200452005520065200752008520095201052011520125201352014520155201652017520185201952020520215202252023520245202552026520275202852029520305203152032520335203452035520365203752038520395204052041520425204352044520455204652047520485204952050520515205252053520545205552056520575205852059520605206152062520635206452065520665206752068520695207052071520725207352074520755207652077520785207952080520815208252083520845208552086520875208852089520905209152092520935209452095520965209752098520995210052101521025210352104521055210652107521085210952110521115211252113521145211552116521175211852119521205212152122521235212452125521265212752128521295213052131521325213352134521355213652137521385213952140521415214252143521445214552146521475214852149521505215152152521535215452155521565215752158521595216052161521625216352164521655216652167521685216952170521715217252173521745217552176521775217852179521805218152182521835218452185521865218752188521895219052191521925219352194521955219652197521985219952200522015220252203522045220552206522075220852209522105221152212522135221452215522165221752218522195222052221522225222352224522255222652227522285222952230522315223252233522345223552236522375223852239522405224152242522435224452245522465224752248522495225052251522525225352254522555225652257522585225952260522615226252263522645226552266522675226852269522705227152272522735227452275522765227752278522795228052281522825228352284522855228652287522885228952290522915229252293522945229552296522975229852299523005230152302523035230452305523065230752308523095231052311523125231352314523155231652317523185231952320523215232252323523245232552326523275232852329523305233152332523335233452335523365233752338523395234052341523425234352344523455234652347523485234952350523515235252353523545235552356523575235852359523605236152362523635236452365523665236752368523695237052371523725237352374523755237652377523785237952380523815238252383523845238552386523875238852389523905239152392523935239452395523965239752398523995240052401524025240352404524055240652407524085240952410524115241252413524145241552416524175241852419524205242152422524235242452425524265242752428524295243052431524325243352434524355243652437524385243952440524415244252443524445244552446524475244852449524505245152452524535245452455524565245752458524595246052461524625246352464524655246652467524685246952470524715247252473524745247552476524775247852479524805248152482524835248452485524865248752488524895249052491524925249352494524955249652497524985249952500525015250252503525045250552506525075250852509525105251152512525135251452515525165251752518525195252052521525225252352524525255252652527525285252952530525315253252533525345253552536525375253852539525405254152542525435254452545525465254752548525495255052551525525255352554525555255652557525585255952560525615256252563525645256552566525675256852569525705257152572525735257452575525765257752578525795258052581525825258352584525855258652587525885258952590525915259252593525945259552596525975259852599526005260152602526035260452605526065260752608526095261052611526125261352614526155261652617526185261952620526215262252623526245262552626526275262852629526305263152632526335263452635526365263752638526395264052641526425264352644526455264652647526485264952650526515265252653526545265552656526575265852659526605266152662526635266452665526665266752668526695267052671526725267352674526755267652677526785267952680526815268252683526845268552686526875268852689526905269152692526935269452695526965269752698526995270052701527025270352704527055270652707527085270952710527115271252713527145271552716527175271852719527205272152722527235272452725527265272752728527295273052731527325273352734527355273652737527385273952740527415274252743527445274552746527475274852749527505275152752527535275452755527565275752758527595276052761527625276352764527655276652767527685276952770527715277252773527745277552776527775277852779527805278152782527835278452785527865278752788527895279052791527925279352794527955279652797527985279952800528015280252803528045280552806528075280852809528105281152812528135281452815528165281752818528195282052821528225282352824528255282652827528285282952830528315283252833528345283552836528375283852839528405284152842528435284452845528465284752848528495285052851528525285352854528555285652857528585285952860528615286252863528645286552866528675286852869528705287152872528735287452875528765287752878528795288052881528825288352884528855288652887528885288952890528915289252893528945289552896528975289852899529005290152902529035290452905529065290752908529095291052911529125291352914529155291652917529185291952920529215292252923529245292552926529275292852929529305293152932529335293452935529365293752938529395294052941529425294352944529455294652947529485294952950529515295252953529545295552956529575295852959529605296152962529635296452965529665296752968529695297052971529725297352974529755297652977529785297952980529815298252983529845298552986529875298852989529905299152992529935299452995529965299752998529995300053001530025300353004530055300653007530085300953010530115301253013530145301553016530175301853019530205302153022530235302453025530265302753028530295303053031530325303353034530355303653037530385303953040530415304253043530445304553046530475304853049530505305153052530535305453055530565305753058530595306053061530625306353064530655306653067530685306953070530715307253073530745307553076530775307853079530805308153082530835308453085530865308753088530895309053091530925309353094530955309653097530985309953100531015310253103531045310553106531075310853109531105311153112531135311453115531165311753118531195312053121531225312353124531255312653127531285312953130531315313253133531345313553136531375313853139531405314153142531435314453145531465314753148531495315053151531525315353154531555315653157531585315953160531615316253163531645316553166531675316853169531705317153172531735317453175531765317753178531795318053181531825318353184531855318653187531885318953190531915319253193531945319553196531975319853199532005320153202532035320453205532065320753208532095321053211532125321353214532155321653217532185321953220532215322253223532245322553226532275322853229532305323153232532335323453235532365323753238532395324053241532425324353244532455324653247532485324953250532515325253253532545325553256532575325853259532605326153262532635326453265532665326753268532695327053271532725327353274532755327653277532785327953280532815328253283532845328553286532875328853289532905329153292532935329453295532965329753298532995330053301533025330353304533055330653307533085330953310533115331253313533145331553316533175331853319533205332153322533235332453325533265332753328533295333053331533325333353334533355333653337533385333953340533415334253343533445334553346533475334853349533505335153352533535335453355533565335753358533595336053361533625336353364533655336653367533685336953370533715337253373533745337553376533775337853379533805338153382533835338453385533865338753388533895339053391533925339353394533955339653397533985339953400534015340253403534045340553406534075340853409534105341153412534135341453415534165341753418534195342053421534225342353424534255342653427534285342953430534315343253433534345343553436534375343853439534405344153442534435344453445534465344753448534495345053451534525345353454534555345653457534585345953460534615346253463534645346553466534675346853469534705347153472534735347453475534765347753478534795348053481534825348353484534855348653487534885348953490534915349253493534945349553496534975349853499535005350153502535035350453505535065350753508535095351053511535125351353514535155351653517535185351953520535215352253523535245352553526535275352853529535305353153532535335353453535535365353753538535395354053541535425354353544535455354653547535485354953550535515355253553535545355553556535575355853559535605356153562535635356453565535665356753568535695357053571535725357353574535755357653577535785357953580535815358253583535845358553586535875358853589535905359153592535935359453595535965359753598535995360053601536025360353604536055360653607536085360953610536115361253613536145361553616536175361853619536205362153622536235362453625536265362753628536295363053631536325363353634536355363653637536385363953640536415364253643536445364553646536475364853649536505365153652536535365453655536565365753658536595366053661536625366353664536655366653667536685366953670536715367253673536745367553676536775367853679536805368153682536835368453685536865368753688536895369053691536925369353694536955369653697536985369953700537015370253703537045370553706537075370853709537105371153712537135371453715537165371753718537195372053721537225372353724537255372653727537285372953730537315373253733537345373553736537375373853739537405374153742537435374453745537465374753748537495375053751537525375353754537555375653757537585375953760537615376253763537645376553766537675376853769537705377153772537735377453775537765377753778537795378053781537825378353784537855378653787537885378953790537915379253793537945379553796537975379853799538005380153802538035380453805538065380753808538095381053811538125381353814538155381653817538185381953820538215382253823538245382553826538275382853829538305383153832538335383453835538365383753838538395384053841538425384353844538455384653847538485384953850538515385253853538545385553856538575385853859538605386153862538635386453865538665386753868538695387053871538725387353874538755387653877538785387953880538815388253883538845388553886538875388853889538905389153892538935389453895538965389753898538995390053901539025390353904539055390653907539085390953910539115391253913539145391553916539175391853919539205392153922539235392453925539265392753928539295393053931539325393353934539355393653937539385393953940539415394253943539445394553946539475394853949539505395153952539535395453955539565395753958539595396053961539625396353964539655396653967539685396953970539715397253973539745397553976539775397853979539805398153982539835398453985539865398753988539895399053991539925399353994539955399653997539985399954000540015400254003540045400554006540075400854009540105401154012540135401454015540165401754018540195402054021540225402354024540255402654027540285402954030540315403254033540345403554036540375403854039540405404154042540435404454045540465404754048540495405054051540525405354054540555405654057540585405954060540615406254063540645406554066540675406854069540705407154072540735407454075540765407754078540795408054081540825408354084540855408654087540885408954090540915409254093540945409554096540975409854099541005410154102541035410454105541065410754108541095411054111541125411354114541155411654117541185411954120541215412254123541245412554126541275412854129541305413154132541335413454135541365413754138541395414054141541425414354144541455414654147541485414954150541515415254153541545415554156541575415854159541605416154162541635416454165541665416754168541695417054171541725417354174541755417654177541785417954180541815418254183541845418554186541875418854189541905419154192541935419454195541965419754198541995420054201542025420354204542055420654207542085420954210542115421254213542145421554216542175421854219542205422154222542235422454225542265422754228542295423054231542325423354234542355423654237542385423954240542415424254243542445424554246542475424854249542505425154252542535425454255542565425754258542595426054261542625426354264542655426654267542685426954270542715427254273542745427554276542775427854279542805428154282542835428454285542865428754288542895429054291542925429354294542955429654297542985429954300543015430254303543045430554306543075430854309543105431154312543135431454315543165431754318543195432054321543225432354324543255432654327543285432954330543315433254333543345433554336543375433854339543405434154342543435434454345543465434754348543495435054351543525435354354543555435654357543585435954360543615436254363543645436554366543675436854369543705437154372543735437454375543765437754378543795438054381543825438354384543855438654387543885438954390543915439254393543945439554396543975439854399544005440154402544035440454405544065440754408544095441054411544125441354414544155441654417544185441954420544215442254423544245442554426544275442854429544305443154432544335443454435544365443754438544395444054441544425444354444544455444654447544485444954450544515445254453544545445554456544575445854459544605446154462544635446454465544665446754468544695447054471544725447354474544755447654477544785447954480544815448254483544845448554486544875448854489544905449154492544935449454495544965449754498544995450054501545025450354504545055450654507545085450954510545115451254513545145451554516545175451854519545205452154522545235452454525545265452754528545295453054531545325453354534545355453654537545385453954540545415454254543545445454554546545475454854549545505455154552545535455454555545565455754558545595456054561545625456354564545655456654567545685456954570545715457254573545745457554576545775457854579545805458154582545835458454585545865458754588545895459054591545925459354594545955459654597545985459954600546015460254603546045460554606546075460854609546105461154612546135461454615546165461754618546195462054621546225462354624546255462654627546285462954630546315463254633546345463554636546375463854639546405464154642546435464454645546465464754648546495465054651546525465354654546555465654657546585465954660546615466254663546645466554666546675466854669546705467154672546735467454675546765467754678546795468054681546825468354684546855468654687546885468954690546915469254693546945469554696546975469854699547005470154702547035470454705547065470754708547095471054711547125471354714547155471654717547185471954720547215472254723547245472554726547275472854729547305473154732547335473454735547365473754738547395474054741547425474354744547455474654747547485474954750547515475254753547545475554756547575475854759547605476154762547635476454765547665476754768547695477054771547725477354774547755477654777547785477954780547815478254783547845478554786547875478854789547905479154792547935479454795547965479754798547995480054801548025480354804548055480654807548085480954810548115481254813548145481554816548175481854819548205482154822548235482454825548265482754828548295483054831548325483354834548355483654837548385483954840548415484254843548445484554846548475484854849548505485154852548535485454855548565485754858548595486054861548625486354864548655486654867548685486954870548715487254873548745487554876548775487854879548805488154882548835488454885548865488754888548895489054891548925489354894548955489654897548985489954900549015490254903549045490554906549075490854909549105491154912549135491454915549165491754918549195492054921549225492354924549255492654927549285492954930549315493254933549345493554936549375493854939549405494154942549435494454945549465494754948549495495054951549525495354954549555495654957549585495954960549615496254963549645496554966549675496854969549705497154972549735497454975549765497754978549795498054981549825498354984549855498654987549885498954990549915499254993549945499554996549975499854999550005500155002550035500455005550065500755008550095501055011550125501355014550155501655017550185501955020550215502255023550245502555026550275502855029550305503155032550335503455035550365503755038550395504055041550425504355044550455504655047550485504955050550515505255053550545505555056550575505855059550605506155062550635506455065550665506755068550695507055071550725507355074550755507655077550785507955080550815508255083550845508555086550875508855089550905509155092550935509455095550965509755098550995510055101551025510355104551055510655107551085510955110551115511255113551145511555116551175511855119551205512155122551235512455125551265512755128551295513055131551325513355134551355513655137551385513955140551415514255143551445514555146551475514855149551505515155152551535515455155551565515755158551595516055161551625516355164551655516655167551685516955170551715517255173551745517555176551775517855179551805518155182551835518455185551865518755188551895519055191551925519355194551955519655197551985519955200552015520255203552045520555206552075520855209552105521155212552135521455215552165521755218552195522055221552225522355224552255522655227552285522955230552315523255233552345523555236552375523855239552405524155242552435524455245552465524755248552495525055251552525525355254552555525655257552585525955260552615526255263552645526555266552675526855269552705527155272552735527455275552765527755278552795528055281552825528355284552855528655287552885528955290552915529255293552945529555296552975529855299553005530155302553035530455305553065530755308553095531055311553125531355314553155531655317553185531955320553215532255323553245532555326553275532855329553305533155332553335533455335553365533755338553395534055341553425534355344553455534655347553485534955350553515535255353553545535555356553575535855359553605536155362553635536455365553665536755368553695537055371553725537355374553755537655377553785537955380553815538255383553845538555386553875538855389553905539155392553935539455395553965539755398553995540055401554025540355404554055540655407554085540955410554115541255413554145541555416554175541855419554205542155422554235542455425554265542755428554295543055431554325543355434554355543655437554385543955440554415544255443554445544555446554475544855449554505545155452554535545455455554565545755458554595546055461554625546355464554655546655467554685546955470554715547255473554745547555476554775547855479554805548155482554835548455485554865548755488554895549055491554925549355494554955549655497554985549955500555015550255503555045550555506555075550855509555105551155512555135551455515555165551755518555195552055521555225552355524555255552655527555285552955530555315553255533555345553555536555375553855539555405554155542555435554455545555465554755548555495555055551555525555355554555555555655557555585555955560555615556255563555645556555566555675556855569555705557155572555735557455575555765557755578555795558055581555825558355584555855558655587555885558955590555915559255593555945559555596555975559855599556005560155602556035560455605556065560755608556095561055611556125561355614556155561655617556185561955620556215562255623556245562555626556275562855629556305563155632556335563455635556365563755638556395564055641556425564355644556455564655647556485564955650556515565255653556545565555656556575565855659556605566155662556635566455665556665566755668556695567055671556725567355674556755567655677556785567955680556815568255683556845568555686556875568855689556905569155692556935569455695556965569755698556995570055701557025570355704557055570655707557085570955710557115571255713557145571555716557175571855719557205572155722557235572455725557265572755728557295573055731557325573355734557355573655737557385573955740557415574255743557445574555746557475574855749557505575155752557535575455755557565575755758557595576055761557625576355764557655576655767557685576955770557715577255773557745577555776557775577855779557805578155782557835578455785557865578755788557895579055791557925579355794557955579655797557985579955800558015580255803558045580555806558075580855809558105581155812558135581455815558165581755818558195582055821558225582355824558255582655827558285582955830558315583255833558345583555836558375583855839558405584155842558435584455845558465584755848558495585055851558525585355854558555585655857558585585955860558615586255863558645586555866558675586855869558705587155872558735587455875558765587755878558795588055881558825588355884558855588655887558885588955890558915589255893558945589555896558975589855899559005590155902559035590455905559065590755908559095591055911559125591355914559155591655917559185591955920559215592255923559245592555926559275592855929559305593155932559335593455935559365593755938559395594055941559425594355944559455594655947559485594955950559515595255953559545595555956559575595855959559605596155962559635596455965559665596755968559695597055971559725597355974559755597655977559785597955980559815598255983559845598555986559875598855989559905599155992559935599455995559965599755998559995600056001560025600356004560055600656007560085600956010560115601256013560145601556016560175601856019560205602156022560235602456025560265602756028560295603056031560325603356034560355603656037560385603956040560415604256043560445604556046560475604856049560505605156052560535605456055560565605756058560595606056061560625606356064560655606656067560685606956070560715607256073560745607556076560775607856079560805608156082560835608456085560865608756088560895609056091560925609356094560955609656097560985609956100561015610256103561045610556106561075610856109561105611156112561135611456115561165611756118561195612056121561225612356124561255612656127561285612956130561315613256133561345613556136561375613856139561405614156142561435614456145561465614756148561495615056151561525615356154561555615656157561585615956160561615616256163561645616556166561675616856169561705617156172561735617456175561765617756178561795618056181561825618356184561855618656187561885618956190561915619256193561945619556196561975619856199562005620156202562035620456205562065620756208562095621056211562125621356214562155621656217562185621956220562215622256223562245622556226562275622856229562305623156232562335623456235562365623756238562395624056241562425624356244562455624656247562485624956250562515625256253562545625556256562575625856259562605626156262562635626456265562665626756268562695627056271562725627356274562755627656277562785627956280562815628256283562845628556286562875628856289562905629156292562935629456295562965629756298562995630056301563025630356304563055630656307563085630956310563115631256313563145631556316563175631856319563205632156322563235632456325563265632756328563295633056331563325633356334563355633656337563385633956340563415634256343563445634556346563475634856349563505635156352563535635456355563565635756358563595636056361563625636356364563655636656367563685636956370563715637256373563745637556376563775637856379563805638156382563835638456385563865638756388563895639056391563925639356394563955639656397563985639956400564015640256403564045640556406564075640856409564105641156412564135641456415564165641756418564195642056421564225642356424564255642656427564285642956430564315643256433564345643556436564375643856439564405644156442564435644456445564465644756448564495645056451564525645356454564555645656457564585645956460564615646256463564645646556466564675646856469564705647156472564735647456475564765647756478564795648056481564825648356484564855648656487564885648956490564915649256493564945649556496564975649856499565005650156502565035650456505565065650756508565095651056511565125651356514565155651656517565185651956520565215652256523565245652556526565275652856529565305653156532565335653456535565365653756538565395654056541565425654356544565455654656547565485654956550565515655256553565545655556556565575655856559565605656156562565635656456565565665656756568565695657056571565725657356574565755657656577565785657956580565815658256583565845658556586565875658856589565905659156592565935659456595565965659756598565995660056601566025660356604566055660656607566085660956610566115661256613566145661556616566175661856619566205662156622566235662456625566265662756628566295663056631566325663356634566355663656637566385663956640566415664256643566445664556646566475664856649566505665156652566535665456655566565665756658566595666056661566625666356664566655666656667566685666956670566715667256673566745667556676566775667856679566805668156682566835668456685566865668756688566895669056691566925669356694566955669656697566985669956700567015670256703567045670556706567075670856709567105671156712567135671456715567165671756718567195672056721567225672356724567255672656727567285672956730567315673256733567345673556736567375673856739567405674156742567435674456745567465674756748567495675056751567525675356754567555675656757567585675956760567615676256763567645676556766567675676856769567705677156772567735677456775567765677756778567795678056781567825678356784567855678656787567885678956790567915679256793567945679556796567975679856799568005680156802568035680456805568065680756808568095681056811568125681356814568155681656817568185681956820568215682256823568245682556826568275682856829568305683156832568335683456835568365683756838568395684056841568425684356844568455684656847568485684956850568515685256853568545685556856568575685856859568605686156862568635686456865568665686756868568695687056871568725687356874568755687656877568785687956880568815688256883568845688556886568875688856889568905689156892568935689456895568965689756898568995690056901569025690356904569055690656907569085690956910569115691256913569145691556916569175691856919569205692156922569235692456925569265692756928569295693056931569325693356934569355693656937569385693956940569415694256943569445694556946569475694856949569505695156952569535695456955569565695756958569595696056961569625696356964569655696656967569685696956970569715697256973569745697556976569775697856979569805698156982569835698456985569865698756988569895699056991569925699356994569955699656997569985699957000570015700257003570045700557006570075700857009570105701157012570135701457015570165701757018570195702057021570225702357024570255702657027570285702957030570315703257033570345703557036570375703857039570405704157042570435704457045570465704757048570495705057051570525705357054570555705657057570585705957060570615706257063570645706557066570675706857069570705707157072570735707457075570765707757078570795708057081570825708357084570855708657087570885708957090570915709257093570945709557096570975709857099571005710157102571035710457105571065710757108571095711057111571125711357114571155711657117571185711957120571215712257123571245712557126571275712857129571305713157132571335713457135571365713757138571395714057141571425714357144571455714657147571485714957150571515715257153571545715557156571575715857159571605716157162571635716457165571665716757168571695717057171571725717357174571755717657177571785717957180571815718257183571845718557186571875718857189571905719157192571935719457195571965719757198571995720057201572025720357204572055720657207572085720957210572115721257213572145721557216572175721857219572205722157222572235722457225572265722757228572295723057231572325723357234572355723657237572385723957240572415724257243572445724557246572475724857249572505725157252572535725457255572565725757258572595726057261572625726357264572655726657267572685726957270572715727257273572745727557276572775727857279572805728157282572835728457285572865728757288572895729057291572925729357294572955729657297572985729957300573015730257303573045730557306573075730857309573105731157312573135731457315573165731757318573195732057321573225732357324573255732657327573285732957330573315733257333573345733557336573375733857339573405734157342573435734457345573465734757348573495735057351573525735357354573555735657357573585735957360573615736257363573645736557366573675736857369573705737157372573735737457375573765737757378573795738057381573825738357384573855738657387573885738957390573915739257393573945739557396573975739857399574005740157402574035740457405574065740757408574095741057411574125741357414574155741657417574185741957420574215742257423574245742557426574275742857429574305743157432574335743457435574365743757438574395744057441574425744357444574455744657447574485744957450574515745257453574545745557456574575745857459574605746157462574635746457465574665746757468574695747057471574725747357474574755747657477574785747957480574815748257483574845748557486574875748857489574905749157492574935749457495574965749757498574995750057501575025750357504575055750657507575085750957510575115751257513575145751557516575175751857519575205752157522575235752457525575265752757528575295753057531575325753357534575355753657537575385753957540575415754257543575445754557546575475754857549575505755157552575535755457555575565755757558575595756057561575625756357564575655756657567575685756957570575715757257573575745757557576575775757857579575805758157582575835758457585575865758757588575895759057591575925759357594575955759657597575985759957600576015760257603576045760557606576075760857609576105761157612576135761457615576165761757618576195762057621576225762357624576255762657627576285762957630576315763257633576345763557636576375763857639576405764157642576435764457645576465764757648576495765057651576525765357654576555765657657576585765957660576615766257663576645766557666576675766857669576705767157672576735767457675576765767757678576795768057681576825768357684576855768657687576885768957690576915769257693576945769557696576975769857699577005770157702577035770457705577065770757708577095771057711577125771357714577155771657717577185771957720577215772257723577245772557726577275772857729577305773157732577335773457735577365773757738577395774057741577425774357744577455774657747577485774957750577515775257753577545775557756577575775857759577605776157762577635776457765577665776757768577695777057771577725777357774577755777657777577785777957780577815778257783577845778557786577875778857789577905779157792577935779457795577965779757798577995780057801578025780357804578055780657807578085780957810578115781257813578145781557816578175781857819578205782157822578235782457825578265782757828578295783057831578325783357834578355783657837578385783957840578415784257843578445784557846578475784857849578505785157852578535785457855578565785757858578595786057861578625786357864578655786657867578685786957870578715787257873578745787557876578775787857879578805788157882578835788457885578865788757888578895789057891578925789357894578955789657897578985789957900579015790257903579045790557906579075790857909579105791157912579135791457915579165791757918579195792057921579225792357924579255792657927579285792957930579315793257933579345793557936579375793857939579405794157942579435794457945579465794757948579495795057951579525795357954579555795657957579585795957960579615796257963579645796557966579675796857969579705797157972579735797457975579765797757978579795798057981579825798357984579855798657987579885798957990579915799257993579945799557996579975799857999580005800158002580035800458005580065800758008580095801058011580125801358014580155801658017580185801958020580215802258023580245802558026580275802858029580305803158032580335803458035580365803758038580395804058041580425804358044580455804658047580485804958050580515805258053580545805558056580575805858059580605806158062580635806458065580665806758068580695807058071580725807358074580755807658077580785807958080580815808258083580845808558086580875808858089580905809158092580935809458095580965809758098580995810058101581025810358104581055810658107581085810958110581115811258113581145811558116581175811858119581205812158122581235812458125581265812758128581295813058131581325813358134581355813658137581385813958140581415814258143581445814558146581475814858149581505815158152581535815458155581565815758158581595816058161581625816358164581655816658167581685816958170581715817258173581745817558176581775817858179581805818158182581835818458185581865818758188581895819058191581925819358194581955819658197581985819958200582015820258203582045820558206582075820858209582105821158212582135821458215582165821758218582195822058221582225822358224582255822658227582285822958230582315823258233582345823558236582375823858239582405824158242582435824458245582465824758248582495825058251582525825358254582555825658257582585825958260582615826258263582645826558266582675826858269582705827158272582735827458275582765827758278582795828058281582825828358284582855828658287582885828958290582915829258293582945829558296582975829858299583005830158302583035830458305583065830758308583095831058311583125831358314583155831658317583185831958320583215832258323583245832558326583275832858329583305833158332583335833458335583365833758338583395834058341583425834358344583455834658347583485834958350583515835258353583545835558356583575835858359583605836158362583635836458365583665836758368583695837058371583725837358374583755837658377583785837958380583815838258383583845838558386583875838858389583905839158392583935839458395583965839758398583995840058401584025840358404584055840658407584085840958410584115841258413584145841558416584175841858419584205842158422584235842458425584265842758428584295843058431584325843358434584355843658437584385843958440584415844258443584445844558446584475844858449584505845158452584535845458455584565845758458584595846058461584625846358464584655846658467584685846958470584715847258473584745847558476584775847858479584805848158482584835848458485584865848758488584895849058491584925849358494584955849658497584985849958500585015850258503585045850558506585075850858509585105851158512585135851458515585165851758518585195852058521585225852358524585255852658527585285852958530585315853258533585345853558536585375853858539585405854158542585435854458545585465854758548585495855058551585525855358554585555855658557585585855958560585615856258563585645856558566585675856858569585705857158572585735857458575585765857758578585795858058581585825858358584585855858658587585885858958590585915859258593585945859558596585975859858599586005860158602586035860458605586065860758608586095861058611586125861358614586155861658617586185861958620586215862258623586245862558626586275862858629586305863158632586335863458635586365863758638586395864058641586425864358644586455864658647586485864958650586515865258653586545865558656586575865858659586605866158662586635866458665586665866758668586695867058671586725867358674586755867658677586785867958680586815868258683586845868558686586875868858689586905869158692586935869458695586965869758698586995870058701587025870358704587055870658707587085870958710587115871258713587145871558716587175871858719587205872158722587235872458725587265872758728587295873058731587325873358734587355873658737587385873958740587415874258743587445874558746587475874858749587505875158752587535875458755587565875758758587595876058761587625876358764587655876658767587685876958770587715877258773587745877558776587775877858779587805878158782587835878458785587865878758788587895879058791587925879358794587955879658797587985879958800588015880258803588045880558806588075880858809588105881158812588135881458815588165881758818588195882058821588225882358824588255882658827588285882958830588315883258833588345883558836588375883858839588405884158842588435884458845588465884758848588495885058851588525885358854588555885658857588585885958860588615886258863588645886558866588675886858869588705887158872588735887458875588765887758878588795888058881588825888358884588855888658887588885888958890588915889258893588945889558896588975889858899589005890158902589035890458905589065890758908589095891058911589125891358914589155891658917589185891958920589215892258923589245892558926589275892858929589305893158932589335893458935589365893758938589395894058941589425894358944589455894658947589485894958950589515895258953589545895558956589575895858959589605896158962589635896458965589665896758968589695897058971589725897358974589755897658977589785897958980589815898258983589845898558986589875898858989589905899158992589935899458995589965899758998589995900059001590025900359004590055900659007590085900959010590115901259013590145901559016590175901859019590205902159022590235902459025590265902759028590295903059031590325903359034590355903659037590385903959040590415904259043590445904559046590475904859049590505905159052590535905459055590565905759058590595906059061590625906359064590655906659067590685906959070590715907259073590745907559076590775907859079590805908159082590835908459085590865908759088590895909059091590925909359094590955909659097590985909959100591015910259103591045910559106591075910859109591105911159112591135911459115591165911759118591195912059121591225912359124591255912659127591285912959130591315913259133591345913559136591375913859139591405914159142591435914459145591465914759148591495915059151591525915359154591555915659157591585915959160591615916259163591645916559166591675916859169591705917159172591735917459175591765917759178591795918059181591825918359184591855918659187591885918959190591915919259193591945919559196591975919859199592005920159202592035920459205592065920759208592095921059211592125921359214592155921659217592185921959220592215922259223592245922559226592275922859229592305923159232592335923459235592365923759238592395924059241592425924359244592455924659247592485924959250592515925259253592545925559256592575925859259592605926159262592635926459265592665926759268592695927059271592725927359274592755927659277592785927959280592815928259283592845928559286592875928859289592905929159292592935929459295592965929759298592995930059301593025930359304593055930659307593085930959310593115931259313593145931559316593175931859319593205932159322593235932459325593265932759328593295933059331593325933359334593355933659337593385933959340593415934259343593445934559346593475934859349593505935159352593535935459355593565935759358593595936059361593625936359364593655936659367593685936959370593715937259373593745937559376593775937859379593805938159382593835938459385593865938759388593895939059391593925939359394593955939659397593985939959400594015940259403594045940559406594075940859409594105941159412594135941459415594165941759418594195942059421594225942359424594255942659427594285942959430594315943259433594345943559436594375943859439594405944159442594435944459445594465944759448594495945059451594525945359454594555945659457594585945959460594615946259463594645946559466594675946859469594705947159472594735947459475594765947759478594795948059481594825948359484594855948659487594885948959490594915949259493594945949559496594975949859499595005950159502595035950459505595065950759508595095951059511595125951359514595155951659517595185951959520595215952259523595245952559526595275952859529595305953159532595335953459535595365953759538595395954059541595425954359544595455954659547595485954959550595515955259553595545955559556595575955859559595605956159562595635956459565595665956759568595695957059571595725957359574595755957659577595785957959580595815958259583595845958559586595875958859589595905959159592595935959459595595965959759598595995960059601596025960359604596055960659607596085960959610596115961259613596145961559616596175961859619596205962159622596235962459625596265962759628596295963059631596325963359634596355963659637596385963959640596415964259643596445964559646596475964859649596505965159652596535965459655596565965759658596595966059661596625966359664596655966659667596685966959670596715967259673596745967559676596775967859679596805968159682596835968459685596865968759688596895969059691596925969359694596955969659697596985969959700597015970259703597045970559706597075970859709597105971159712597135971459715597165971759718597195972059721597225972359724597255972659727597285972959730597315973259733597345973559736597375973859739597405974159742597435974459745597465974759748597495975059751597525975359754597555975659757597585975959760597615976259763597645976559766597675976859769597705977159772597735977459775597765977759778597795978059781597825978359784597855978659787597885978959790597915979259793597945979559796597975979859799598005980159802598035980459805598065980759808598095981059811598125981359814598155981659817598185981959820598215982259823598245982559826598275982859829598305983159832598335983459835598365983759838598395984059841598425984359844598455984659847598485984959850598515985259853598545985559856598575985859859598605986159862598635986459865598665986759868598695987059871598725987359874598755987659877598785987959880598815988259883598845988559886598875988859889598905989159892598935989459895598965989759898598995990059901599025990359904599055990659907599085990959910599115991259913599145991559916599175991859919599205992159922599235992459925599265992759928599295993059931599325993359934599355993659937599385993959940599415994259943599445994559946599475994859949599505995159952599535995459955599565995759958599595996059961599625996359964599655996659967599685996959970599715997259973599745997559976599775997859979599805998159982599835998459985599865998759988599895999059991599925999359994599955999659997599985999960000600016000260003600046000560006600076000860009600106001160012600136001460015600166001760018600196002060021600226002360024600256002660027600286002960030600316003260033600346003560036600376003860039600406004160042600436004460045600466004760048600496005060051600526005360054600556005660057600586005960060600616006260063600646006560066600676006860069600706007160072600736007460075600766007760078600796008060081600826008360084600856008660087600886008960090600916009260093600946009560096600976009860099601006010160102601036010460105601066010760108601096011060111601126011360114601156011660117601186011960120601216012260123601246012560126601276012860129601306013160132601336013460135601366013760138601396014060141601426014360144601456014660147601486014960150601516015260153601546015560156601576015860159601606016160162601636016460165601666016760168601696017060171601726017360174601756017660177601786017960180601816018260183601846018560186601876018860189601906019160192601936019460195601966019760198601996020060201602026020360204602056020660207602086020960210602116021260213602146021560216602176021860219602206022160222602236022460225602266022760228602296023060231602326023360234602356023660237602386023960240602416024260243602446024560246602476024860249602506025160252602536025460255602566025760258602596026060261602626026360264602656026660267602686026960270602716027260273602746027560276602776027860279602806028160282602836028460285602866028760288602896029060291602926029360294602956029660297602986029960300603016030260303603046030560306603076030860309603106031160312603136031460315603166031760318603196032060321603226032360324603256032660327603286032960330603316033260333603346033560336603376033860339603406034160342603436034460345603466034760348603496035060351603526035360354603556035660357603586035960360603616036260363603646036560366603676036860369603706037160372603736037460375603766037760378603796038060381603826038360384603856038660387603886038960390603916039260393603946039560396603976039860399604006040160402604036040460405604066040760408604096041060411604126041360414604156041660417604186041960420604216042260423604246042560426604276042860429604306043160432604336043460435604366043760438604396044060441604426044360444604456044660447604486044960450604516045260453604546045560456604576045860459604606046160462604636046460465604666046760468604696047060471604726047360474604756047660477604786047960480604816048260483604846048560486604876048860489604906049160492604936049460495604966049760498604996050060501605026050360504605056050660507605086050960510605116051260513605146051560516605176051860519605206052160522605236052460525605266052760528605296053060531605326053360534605356053660537605386053960540605416054260543605446054560546605476054860549605506055160552605536055460555605566055760558605596056060561605626056360564605656056660567605686056960570605716057260573605746057560576605776057860579605806058160582605836058460585605866058760588605896059060591605926059360594605956059660597605986059960600606016060260603606046060560606606076060860609606106061160612606136061460615606166061760618606196062060621606226062360624606256062660627606286062960630606316063260633606346063560636606376063860639606406064160642606436064460645606466064760648606496065060651606526065360654606556065660657606586065960660606616066260663606646066560666606676066860669606706067160672606736067460675606766067760678606796068060681606826068360684606856068660687606886068960690606916069260693606946069560696606976069860699607006070160702607036070460705607066070760708607096071060711607126071360714607156071660717607186071960720607216072260723607246072560726607276072860729607306073160732607336073460735607366073760738607396074060741607426074360744607456074660747607486074960750607516075260753607546075560756607576075860759607606076160762607636076460765607666076760768607696077060771607726077360774607756077660777607786077960780607816078260783607846078560786607876078860789607906079160792607936079460795607966079760798607996080060801608026080360804608056080660807608086080960810608116081260813608146081560816608176081860819608206082160822608236082460825608266082760828608296083060831608326083360834608356083660837608386083960840608416084260843608446084560846608476084860849608506085160852608536085460855608566085760858608596086060861608626086360864608656086660867608686086960870608716087260873608746087560876608776087860879608806088160882608836088460885608866088760888608896089060891608926089360894608956089660897608986089960900609016090260903609046090560906609076090860909609106091160912609136091460915609166091760918609196092060921609226092360924609256092660927609286092960930609316093260933609346093560936609376093860939609406094160942609436094460945609466094760948609496095060951609526095360954609556095660957609586095960960609616096260963609646096560966609676096860969609706097160972609736097460975609766097760978609796098060981609826098360984609856098660987609886098960990609916099260993609946099560996609976099860999610006100161002610036100461005610066100761008610096101061011610126101361014610156101661017610186101961020610216102261023610246102561026610276102861029610306103161032610336103461035610366103761038610396104061041610426104361044610456104661047610486104961050610516105261053610546105561056610576105861059610606106161062610636106461065610666106761068610696107061071610726107361074610756107661077610786107961080610816108261083610846108561086610876108861089610906109161092610936109461095610966109761098610996110061101611026110361104611056110661107611086110961110611116111261113611146111561116611176111861119611206112161122611236112461125611266112761128611296113061131611326113361134611356113661137611386113961140611416114261143611446114561146611476114861149611506115161152611536115461155611566115761158611596116061161611626116361164611656116661167611686116961170611716117261173611746117561176611776117861179611806118161182611836118461185611866118761188611896119061191611926119361194611956119661197611986119961200612016120261203 |
- /*
- * QuickJS Javascript Engine
- *
- * Copyright (c) 2017-2025 Fabrice Bellard
- * Copyright (c) 2017-2025 Charlie Gordon
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <stdarg.h>
- #include <inttypes.h>
- #include <string.h>
- #include <assert.h>
- #include <sys/time.h>
- #include <time.h>
- #ifndef __nebos__
- #include <fenv.h>
- #endif
- #include <math.h>
- #if defined(__APPLE__)
- #include <malloc/malloc.h>
- #elif defined(__linux__) || defined(__GLIBC__)
- #include <malloc.h>
- #elif defined(__FreeBSD__)
- #include <malloc_np.h>
- #elif defined(__nebos__)
- #include <alloca.h>
- #endif
- #include "cutils.h"
- #include "list.h"
- #include "quickjs.h"
- #include "libregexp.h"
- #include "libunicode.h"
- #include "dtoa.h"
- #define OPTIMIZE 1
- #define SHORT_OPCODES 1
- #if defined(__EMSCRIPTEN__)
- #define DIRECT_DISPATCH 0
- #else
- #define DIRECT_DISPATCH 1
- #endif
- #if defined(__APPLE__)
- #define MALLOC_OVERHEAD 0
- #else
- #define MALLOC_OVERHEAD 8
- #endif
- #if !defined(_WIN32)
- /* define it if printf uses the RNDN rounding mode instead of RNDNA */
- #define CONFIG_PRINTF_RNDN
- #endif
- /* define to include Atomics.* operations which depend on the OS
- threads */
- #if !defined(__EMSCRIPTEN__) && !defined(__nebos__)
- #define CONFIG_ATOMICS
- #endif
- #if !defined(__EMSCRIPTEN__)
- /* enable stack limitation */
- #define CONFIG_STACK_CHECK
- #endif
- /* dump object free */
- //#define DUMP_FREE
- //#define DUMP_CLOSURE
- /* dump the bytecode of the compiled functions: combination of bits
- 1: dump pass 3 final byte code
- 2: dump pass 2 code
- 4: dump pass 1 code
- 8: dump stdlib functions
- 16: dump bytecode in hex
- 32: dump line number table
- 64: dump compute_stack_size
- */
- //#define DUMP_BYTECODE (1)
- /* dump the occurence of the automatic GC */
- //#define DUMP_GC
- /* dump objects freed by the garbage collector */
- //#define DUMP_GC_FREE
- /* dump objects leaking when freeing the runtime */
- //#define DUMP_LEAKS 1
- /* dump memory usage before running the garbage collector */
- //#define DUMP_MEM
- //#define DUMP_OBJECTS /* dump objects in JS_FreeContext */
- //#define DUMP_ATOMS /* dump atoms in JS_FreeContext */
- //#define DUMP_SHAPES /* dump shapes in JS_FreeContext */
- //#define DUMP_MODULE_RESOLVE
- //#define DUMP_MODULE_EXEC
- //#define DUMP_PROMISE
- //#define DUMP_READ_OBJECT
- //#define DUMP_ROPE_REBALANCE
- /* add asm labels to each opcode so that it is easier to see the generated code */
- //#define OPCODE_ASM_LABEL
- /* test the GC by forcing it before each object allocation */
- //#define FORCE_GC_AT_MALLOC
- #ifdef CONFIG_ATOMICS
- #include <pthread.h>
- #include <stdatomic.h>
- #include <errno.h>
- #endif
- enum {
- /* classid tag */ /* union usage | properties */
- JS_CLASS_OBJECT = 1, /* must be first */
- JS_CLASS_ARRAY, /* u.array | length */
- JS_CLASS_ERROR,
- JS_CLASS_NUMBER, /* u.object_data */
- JS_CLASS_STRING, /* u.object_data */
- JS_CLASS_BOOLEAN, /* u.object_data */
- JS_CLASS_SYMBOL, /* u.object_data */
- JS_CLASS_ARGUMENTS, /* u.array | length */
- JS_CLASS_MAPPED_ARGUMENTS, /* u.array | length */
- JS_CLASS_DATE, /* u.object_data */
- JS_CLASS_MODULE_NS,
- JS_CLASS_C_FUNCTION, /* u.cfunc */
- JS_CLASS_BYTECODE_FUNCTION, /* u.func */
- JS_CLASS_BOUND_FUNCTION, /* u.bound_function */
- JS_CLASS_C_FUNCTION_DATA, /* u.c_function_data_record */
- JS_CLASS_GENERATOR_FUNCTION, /* u.func */
- JS_CLASS_FOR_IN_ITERATOR, /* u.for_in_iterator */
- JS_CLASS_REGEXP, /* u.regexp */
- JS_CLASS_ARRAY_BUFFER, /* u.array_buffer */
- JS_CLASS_SHARED_ARRAY_BUFFER, /* u.array_buffer */
- JS_CLASS_UINT8C_ARRAY, /* u.array (typed_array) */
- JS_CLASS_INT8_ARRAY, /* u.array (typed_array) */
- JS_CLASS_UINT8_ARRAY, /* u.array (typed_array) */
- JS_CLASS_INT16_ARRAY, /* u.array (typed_array) */
- JS_CLASS_UINT16_ARRAY, /* u.array (typed_array) */
- JS_CLASS_INT32_ARRAY, /* u.array (typed_array) */
- JS_CLASS_UINT32_ARRAY, /* u.array (typed_array) */
- JS_CLASS_BIG_INT64_ARRAY, /* u.array (typed_array) */
- JS_CLASS_BIG_UINT64_ARRAY, /* u.array (typed_array) */
- JS_CLASS_FLOAT16_ARRAY, /* u.array (typed_array) */
- JS_CLASS_FLOAT32_ARRAY, /* u.array (typed_array) */
- JS_CLASS_FLOAT64_ARRAY, /* u.array (typed_array) */
- JS_CLASS_DATAVIEW, /* u.typed_array */
- JS_CLASS_BIG_INT, /* u.object_data */
- JS_CLASS_MAP, /* u.map_state */
- JS_CLASS_SET, /* u.map_state */
- JS_CLASS_WEAKMAP, /* u.map_state */
- JS_CLASS_WEAKSET, /* u.map_state */
- JS_CLASS_ITERATOR, /* u.map_iterator_data */
- JS_CLASS_ITERATOR_CONCAT, /* u.iterator_concat_data */
- JS_CLASS_ITERATOR_HELPER, /* u.iterator_helper_data */
- JS_CLASS_ITERATOR_WRAP, /* u.iterator_wrap_data */
- JS_CLASS_MAP_ITERATOR, /* u.map_iterator_data */
- JS_CLASS_SET_ITERATOR, /* u.map_iterator_data */
- JS_CLASS_ARRAY_ITERATOR, /* u.array_iterator_data */
- JS_CLASS_STRING_ITERATOR, /* u.array_iterator_data */
- JS_CLASS_REGEXP_STRING_ITERATOR, /* u.regexp_string_iterator_data */
- JS_CLASS_GENERATOR, /* u.generator_data */
- JS_CLASS_GLOBAL_OBJECT, /* u.global_object */
- JS_CLASS_RAWJSON,
- JS_CLASS_PROXY, /* u.proxy_data */
- JS_CLASS_PROMISE, /* u.promise_data */
- JS_CLASS_PROMISE_RESOLVE_FUNCTION, /* u.promise_function_data */
- JS_CLASS_PROMISE_REJECT_FUNCTION, /* u.promise_function_data */
- JS_CLASS_ASYNC_FUNCTION, /* u.func */
- JS_CLASS_ASYNC_FUNCTION_RESOLVE, /* u.async_function_data */
- JS_CLASS_ASYNC_FUNCTION_REJECT, /* u.async_function_data */
- JS_CLASS_ASYNC_FROM_SYNC_ITERATOR, /* u.async_from_sync_iterator_data */
- JS_CLASS_ASYNC_GENERATOR_FUNCTION, /* u.func */
- JS_CLASS_ASYNC_GENERATOR, /* u.async_generator_data */
- JS_CLASS_WEAK_REF,
- JS_CLASS_FINALIZATION_REGISTRY,
-
- JS_CLASS_INIT_COUNT, /* last entry for predefined classes */
- };
- /* number of typed array types */
- #define JS_TYPED_ARRAY_COUNT (JS_CLASS_FLOAT64_ARRAY - JS_CLASS_UINT8C_ARRAY + 1)
- static uint8_t const typed_array_size_log2[JS_TYPED_ARRAY_COUNT];
- #define typed_array_size_log2(classid) (typed_array_size_log2[(classid)- JS_CLASS_UINT8C_ARRAY])
- typedef enum JSErrorEnum {
- JS_EVAL_ERROR,
- JS_RANGE_ERROR,
- JS_REFERENCE_ERROR,
- JS_SYNTAX_ERROR,
- JS_TYPE_ERROR,
- JS_URI_ERROR,
- JS_INTERNAL_ERROR,
- JS_AGGREGATE_ERROR,
- JS_NATIVE_ERROR_COUNT, /* number of different NativeError objects */
- } JSErrorEnum;
- /* the variable and scope indexes must fit on 16 bits. The (-1) and
- ARG_SCOPE_END values are reserved. */
- #define JS_MAX_LOCAL_VARS 65534
- #define JS_STACK_SIZE_MAX 65534
- #define JS_STRING_LEN_MAX ((1 << 30) - 1)
- /* strings <= this length are not concatenated using ropes. if too
- small, the rope memory overhead becomes high. */
- #define JS_STRING_ROPE_SHORT_LEN 512
- /* specific threshold for initial rope use */
- #define JS_STRING_ROPE_SHORT2_LEN 8192
- /* rope depth at which we rebalance */
- #define JS_STRING_ROPE_MAX_DEPTH 60
- #define __exception __attribute__((warn_unused_result))
- typedef struct JSShape JSShape;
- typedef struct JSString JSString;
- typedef struct JSString JSAtomStruct;
- typedef struct JSObject JSObject;
- #define JS_VALUE_GET_OBJ(v) ((JSObject *)JS_VALUE_GET_PTR(v))
- #define JS_VALUE_GET_STRING(v) ((JSString *)JS_VALUE_GET_PTR(v))
- #define JS_VALUE_GET_STRING_ROPE(v) ((JSStringRope *)JS_VALUE_GET_PTR(v))
- typedef enum {
- JS_GC_PHASE_NONE,
- JS_GC_PHASE_DECREF,
- JS_GC_PHASE_REMOVE_CYCLES,
- } JSGCPhaseEnum;
- typedef enum OPCodeEnum OPCodeEnum;
- /* JS malloc */
- #define JS_MALLOC_ALIGN 8
- #define JS_MALLOC_ARENA_SIZE 4096
- #define JS_MALLOC_BLOCK_SIZE_COUNT 31
- #define JS_MALLOC_MIN_SMALL_SIZE 16
- #define JS_MALLOC_MAX_SMALL_SIZE 512
- #if defined(__SANITIZE_ADDRESS__)
- /* use the host malloc() for all allocations */
- #define JS_MALLOC_LARGE_BLOCKS_ONLY 1
- #else
- #define JS_MALLOC_LARGE_BLOCKS_ONLY 0
- #endif
- /* allow iteration among the allocated blocks. Currently not used. May
- be used to suppress the memory overhead of JSGCObjectHeader */
- //#define JS_MALLOC_USE_ITER
- #define FREE_NIL 0xffff
- /* 8 byte header */
- /* Notes:
- - the header is necessary at least to recover a pointer to
- JSMallocArena because we don't want to enforce a page
- alignment on the system malloc().
- - could store the block offset instead of (block_idx,
- block_size_idx), but it would require a division to recover the block
- index.
- */
- typedef struct JSMallocBlockHeader {
- union {
- uint16_t block_idx; /* FREE_NIL if large block */
- uint16_t free_next; /* FREE_NIL if none */
- } u;
- uint8_t block_size_idx;
- uint8_t gc_obj_type : 7;
- uint8_t mark : 1;
- int ref_count;
- __attribute__((aligned(JS_MALLOC_ALIGN))) uint8_t user_data[];
- } JSMallocBlockHeader;
- typedef struct JSMallocLargeBlockHeader {
- #ifdef JS_MALLOC_USE_ITER
- struct list_head link;
- #endif
- JSMallocBlockHeader header;
- } JSMallocLargeBlockHeader;
- typedef struct {
- struct list_head free_link;
- struct list_head link;
- uint8_t block_size_idx;
- uint16_t n_used_blocks; /* number of allocated blocks */
- uint16_t n_blocks; /* total number of blocks */
- uint16_t first_free_block; /* FREE_NIL if none */
- #ifdef JS_MALLOC_USE_ITER
- /* bit set to 1 for allocated block */
- uint32_t bitmap[((JS_MALLOC_ARENA_SIZE / JS_MALLOC_MIN_SMALL_SIZE) + 31) / 32];
- #endif
- /* n_blocks memory blocks of identical size */
- __attribute__((aligned(JS_MALLOC_ALIGN))) uint8_t blocks[];
- } JSMallocArena;
- typedef struct {
- struct list_head arena_list[JS_MALLOC_BLOCK_SIZE_COUNT]; /* list of JSMallocArena.link (all arenas) */
- struct list_head free_arena_list[JS_MALLOC_BLOCK_SIZE_COUNT]; /* list of JSMallocArena.free_link (arenas where n_used_blocks < n_blocks) */
- #ifdef JS_MALLOC_USE_ITER
- struct list_head large_block_list; /* list of JSMallocLargeBlockHeader.link */
- #endif
- __attribute__((aligned(JS_MALLOC_ALIGN))) uint8_t zero_size_block[sizeof(JSMallocBlockHeader)];
- /* callbacks to the host malloc */
- JSMallocFunctions mf;
- JSMallocState malloc_state;
- } JSMallocContext;
- /* end JS Malloc */
- struct JSRuntime {
- JSMallocContext malloc_ctx;
- const char *rt_info;
- int atom_hash_size; /* power of two */
- int atom_count;
- int atom_size;
- int atom_count_resize; /* resize hash table at this count */
- uint32_t *atom_hash;
- JSAtomStruct **atom_array;
- int atom_free_index; /* 0 = none */
- int class_count; /* size of class_array */
- JSClass *class_array;
- struct list_head context_list; /* list of JSContext.link */
- /* list of JSGCObjectHeader.link. List of allocated GC objects (used
- by the garbage collector) */
- struct list_head gc_obj_list;
- /* list of JSGCObjectHeader.link. Used during JS_FreeValueRT() */
- struct list_head gc_zero_ref_count_list;
- struct list_head tmp_obj_list; /* used during GC */
- JSGCPhaseEnum gc_phase : 8;
- size_t malloc_gc_threshold;
- struct list_head weakref_list; /* list of JSWeakRefHeader.link */
- #ifdef DUMP_LEAKS
- struct list_head string_list; /* list of JSString.link */
- #endif
- /* stack limitation */
- uintptr_t stack_size; /* in bytes, 0 if no limit */
- uintptr_t stack_top;
- uintptr_t stack_limit; /* lower stack limit */
- JSValue current_exception;
- /* true if the current exception cannot be catched */
- BOOL current_exception_is_uncatchable : 8;
- /* true if inside an out of memory error, to avoid recursing */
- BOOL in_out_of_memory : 8;
- struct JSStackFrame *current_stack_frame;
- JSInterruptHandler *interrupt_handler;
- void *interrupt_opaque;
- JSHostPromiseRejectionTracker *host_promise_rejection_tracker;
- void *host_promise_rejection_tracker_opaque;
- struct list_head job_list; /* list of JSJobEntry.link */
- JSModuleNormalizeFunc *module_normalize_func;
- BOOL module_loader_has_attr;
- union {
- JSModuleLoaderFunc *module_loader_func;
- JSModuleLoaderFunc2 *module_loader_func2;
- } u;
- JSModuleCheckSupportedImportAttributes *module_check_attrs;
- void *module_loader_opaque;
- /* timestamp for internal use in module evaluation */
- int64_t module_async_evaluation_next_timestamp;
- BOOL can_block : 8; /* TRUE if Atomics.wait can block */
- /* used to allocate, free and clone SharedArrayBuffers */
- JSSharedArrayBufferFunctions sab_funcs;
- /* see JS_SetStripInfo() */
- uint8_t strip_flags;
-
- /* Shape hash table */
- int shape_hash_bits;
- int shape_hash_size;
- int shape_hash_count; /* number of hashed shapes */
- JSShape **shape_hash;
- void *user_opaque;
- };
- struct JSClass {
- uint32_t class_id; /* 0 means free entry */
- JSAtom class_name;
- JSClassFinalizer *finalizer;
- JSClassGCMark *gc_mark;
- JSClassCall *call;
- /* pointers for exotic behavior, can be NULL if none are present */
- const JSClassExoticMethods *exotic;
- };
- #define JS_MODE_STRICT (1 << 0)
- #define JS_MODE_ASYNC (1 << 2) /* async function */
- #define JS_MODE_BACKTRACE_BARRIER (1 << 3) /* stop backtrace before this frame */
- typedef struct JSStackFrame {
- struct JSStackFrame *prev_frame; /* NULL if first stack frame */
- JSValue cur_func; /* current function, JS_UNDEFINED if the frame is detached */
- JSValue *arg_buf; /* arguments */
- JSValue *var_buf; /* variables */
- struct JSVarRef **var_refs; /* references to arguments or local variables */
- const uint8_t *cur_pc; /* only used in bytecode functions : PC of the
- instruction after the call */
- int arg_count;
- int js_mode; /* not supported for C functions */
- /* only used in generators. Current stack pointer value. NULL if
- the function is running. */
- JSValue *cur_sp;
- } JSStackFrame;
- typedef enum {
- JS_GC_OBJ_TYPE_JS_OBJECT,
- JS_GC_OBJ_TYPE_FUNCTION_BYTECODE,
- JS_GC_OBJ_TYPE_SHAPE,
- JS_GC_OBJ_TYPE_VAR_REF,
- JS_GC_OBJ_TYPE_ASYNC_FUNCTION,
- JS_GC_OBJ_TYPE_JS_CONTEXT,
- JS_GC_OBJ_TYPE_MODULE,
- } JSGCObjectTypeEnum;
- /* header for GC objects. GC objects are C data structures with a
- reference count that can reference other GC objects. JS Objects are
- a particular type of GC object. */
- struct JSGCObjectHeader {
- struct list_head link;
- };
- typedef enum {
- JS_WEAKREF_TYPE_MAP,
- JS_WEAKREF_TYPE_WEAKREF,
- JS_WEAKREF_TYPE_FINREC,
- } JSWeakRefHeaderTypeEnum;
- typedef struct {
- struct list_head link;
- JSWeakRefHeaderTypeEnum weakref_type;
- } JSWeakRefHeader;
- typedef struct JSVarRef {
- JSGCObjectHeader header; /* must come first */
- uint8_t is_detached;
- uint8_t is_lexical; /* only used with global variables */
- uint8_t is_const; /* only used with global variables */
- JSValue *pvalue; /* pointer to the value, either on the stack or
- to 'value' */
- union {
- JSValue value; /* used when is_detached = TRUE */
- struct {
- uint16_t var_ref_idx; /* index in JSStackFrame.var_refs[] */
- JSStackFrame *stack_frame;
- }; /* used when is_detached = FALSE */
- };
- } JSVarRef;
- /* bigint */
- #if JS_LIMB_BITS == 32
- typedef int32_t js_slimb_t;
- typedef uint32_t js_limb_t;
- typedef int64_t js_sdlimb_t;
- typedef uint64_t js_dlimb_t;
- #define JS_LIMB_DIGITS 9
- #else
- typedef __int128 int128_t;
- typedef unsigned __int128 uint128_t;
- typedef int64_t js_slimb_t;
- typedef uint64_t js_limb_t;
- typedef int128_t js_sdlimb_t;
- typedef uint128_t js_dlimb_t;
- #define JS_LIMB_DIGITS 19
- #endif
- typedef struct JSBigInt {
- uint32_t len; /* number of limbs, >= 1 */
- js_limb_t tab[]; /* two's complement representation, always
- normalized so that 'len' is the minimum
- possible length >= 1 */
- } JSBigInt;
- /* this bigint structure can hold a 64 bit integer */
- typedef struct {
- js_limb_t big_int_buf[sizeof(JSBigInt) / sizeof(js_limb_t)]; /* for JSBigInt */
- /* must come just after */
- js_limb_t tab[(64 + JS_LIMB_BITS - 1) / JS_LIMB_BITS];
- } JSBigIntBuf;
-
- typedef enum {
- JS_AUTOINIT_ID_PROTOTYPE,
- JS_AUTOINIT_ID_MODULE_NS,
- JS_AUTOINIT_ID_PROP,
- } JSAutoInitIDEnum;
- /* must be large enough to have a negligible runtime cost and small
- enough to call the interrupt callback often. */
- #define JS_INTERRUPT_COUNTER_INIT 10000
- struct JSContext {
- JSGCObjectHeader header; /* must come first */
- JSRuntime *rt;
- struct list_head link;
- uint16_t binary_object_count;
- int binary_object_size;
-
- JSShape *array_shape; /* initial shape for Array objects */
- JSShape *arguments_shape; /* shape for arguments objects */
- JSShape *mapped_arguments_shape; /* shape for mapped arguments objects */
- JSShape *regexp_shape; /* shape for regexp objects */
- JSShape *regexp_result_shape; /* shape for regexp result objects */
- JSValue *class_proto;
- JSValue function_proto;
- JSValue function_ctor;
- JSValue array_ctor;
- JSValue regexp_ctor;
- JSValue promise_ctor;
- JSValue native_error_proto[JS_NATIVE_ERROR_COUNT];
- JSValue iterator_ctor;
- JSValue async_iterator_proto;
- JSValue array_proto_values;
- JSValue throw_type_error;
- JSValue eval_obj;
- JSValue global_obj; /* global object */
- JSValue global_var_obj; /* contains the global let/const definitions */
- uint64_t random_state;
- /* when the counter reaches zero, JSRutime.interrupt_handler is called */
- int interrupt_counter;
- struct list_head loaded_modules; /* list of JSModuleDef.link */
- /* if NULL, RegExp compilation is not supported */
- JSValue (*compile_regexp)(JSContext *ctx, JSValueConst pattern,
- JSValueConst flags);
- /* if NULL, eval is not supported */
- JSValue (*eval_internal)(JSContext *ctx, JSValueConst this_obj,
- const char *input, size_t input_len,
- const char *filename, int flags, int scope_idx);
- void *user_opaque;
- };
- typedef union JSFloat64Union {
- double d;
- uint64_t u64;
- uint32_t u32[2];
- } JSFloat64Union;
- enum {
- JS_ATOM_TYPE_STRING = 1,
- JS_ATOM_TYPE_GLOBAL_SYMBOL,
- JS_ATOM_TYPE_SYMBOL,
- JS_ATOM_TYPE_PRIVATE,
- };
- typedef enum {
- JS_ATOM_KIND_STRING,
- JS_ATOM_KIND_SYMBOL,
- JS_ATOM_KIND_PRIVATE,
- } JSAtomKindEnum;
- #define JS_ATOM_HASH_MASK ((1 << 30) - 1)
- #define JS_ATOM_HASH_PRIVATE JS_ATOM_HASH_MASK
- struct JSString {
- uint32_t len : 31;
- uint8_t is_wide_char : 1; /* 0 = 8 bits, 1 = 16 bits characters */
- /* for JS_ATOM_TYPE_SYMBOL: hash = weakref_count, atom_type = 3,
- for JS_ATOM_TYPE_PRIVATE: hash = JS_ATOM_HASH_PRIVATE, atom_type = 3
- XXX: could change encoding to have one more bit in hash */
- uint32_t hash : 30;
- uint8_t atom_type : 2; /* != 0 if atom, JS_ATOM_TYPE_x */
- uint32_t hash_next; /* atom_index for JS_ATOM_TYPE_SYMBOL */
- #ifdef DUMP_LEAKS
- struct list_head link; /* string list */
- #endif
- union {
- uint8_t str8[0]; /* 8 bit strings will get an extra null terminator */
- uint16_t str16[0];
- } u;
- };
- typedef struct JSStringRope {
- uint32_t len;
- uint8_t is_wide_char; /* 0 = 8 bits, 1 = 16 bits characters */
- uint8_t depth; /* max depth of the rope tree */
- /* XXX: could reduce memory usage by using a direct pointer with
- bit 0 to select rope or string */
- JSValue left;
- JSValue right; /* might be the empty string */
- } JSStringRope;
- typedef enum {
- JS_CLOSURE_LOCAL, /* 'var_idx' is the index of a local variable in the parent function */
- JS_CLOSURE_ARG, /* 'var_idx' is the index of a argument variable in the parent function */
- JS_CLOSURE_REF, /* 'var_idx' is the index of a closure variable in the parent function */
- JS_CLOSURE_GLOBAL_REF, /* 'var_idx' in the index of a closure
- variable in the parent function
- referencing a global variable */
- JS_CLOSURE_GLOBAL_DECL, /* global variable declaration (eval code only) */
- JS_CLOSURE_GLOBAL, /* global variable (eval code only) */
- JS_CLOSURE_MODULE_DECL, /* definition of a module variable (eval code only) */
- JS_CLOSURE_MODULE_IMPORT, /* definition of a module import (eval code only) */
- } JSClosureTypeEnum;
- typedef struct JSClosureVar {
- JSClosureTypeEnum closure_type : 3;
- uint8_t is_lexical : 1; /* lexical variable */
- uint8_t is_const : 1; /* const variable (is_lexical = 1 if is_const = 1 */
- uint8_t var_kind : 4; /* see JSVarKindEnum */
- uint16_t var_idx; /* is_local = TRUE: index to a normal variable of the
- parent function. otherwise: index to a closure
- variable of the parent function */
- JSAtom var_name;
- } JSClosureVar;
- #define ARG_SCOPE_INDEX 1
- #define ARG_SCOPE_END (-2)
- typedef enum {
- /* XXX: add more variable kinds here instead of using bit fields */
- JS_VAR_NORMAL,
- JS_VAR_FUNCTION_DECL, /* lexical var with function declaration */
- JS_VAR_NEW_FUNCTION_DECL, /* lexical var with async/generator
- function declaration */
- JS_VAR_CATCH,
- JS_VAR_FUNCTION_NAME, /* function expression name */
- JS_VAR_PRIVATE_FIELD,
- JS_VAR_PRIVATE_METHOD,
- JS_VAR_PRIVATE_GETTER,
- JS_VAR_PRIVATE_SETTER, /* must come after JS_VAR_PRIVATE_GETTER */
- JS_VAR_PRIVATE_GETTER_SETTER, /* must come after JS_VAR_PRIVATE_SETTER */
- JS_VAR_GLOBAL_FUNCTION_DECL, /* global function definition, only in JSVarDef */
- } JSVarKindEnum;
- typedef struct JSBytecodeVarDef {
- JSAtom var_name;
- /* index into JSFunctionBytecode.vars of the next variable in the same or
- enclosing lexical scope
- */
- int scope_next; /* XXX: store on 16 bits */
- uint8_t is_const : 1;
- uint8_t is_lexical : 1;
- uint8_t is_captured : 1; /* XXX: could remove and use a var_ref_idx value */
- uint8_t has_scope: 1; /* true if JSVarDef.scope_level != 0 */
- uint8_t var_kind : 4; /* see JSVarKindEnum */
- /* If is_captured = TRUE, provides, the index of the corresponding
- JSVarRef on stack. It would be more compact to have a separate
- table with the corresponding inverted table but it requires
- more modifications in the code. */
- uint16_t var_ref_idx;
- } JSBytecodeVarDef;
- /* for the encoding of the pc2line table */
- #define PC2LINE_BASE (-1)
- #define PC2LINE_RANGE 5
- #define PC2LINE_OP_FIRST 1
- #define PC2LINE_DIFF_PC_MAX ((255 - PC2LINE_OP_FIRST) / PC2LINE_RANGE)
- typedef enum JSFunctionKindEnum {
- JS_FUNC_NORMAL = 0,
- JS_FUNC_GENERATOR = (1 << 0),
- JS_FUNC_ASYNC = (1 << 1),
- JS_FUNC_ASYNC_GENERATOR = (JS_FUNC_GENERATOR | JS_FUNC_ASYNC),
- } JSFunctionKindEnum;
- typedef struct JSFunctionBytecode {
- JSGCObjectHeader header; /* must come first */
- uint8_t js_mode;
- uint8_t has_prototype : 1; /* true if a prototype field is necessary */
- uint8_t has_simple_parameter_list : 1;
- uint8_t is_derived_class_constructor : 1;
- /* true if home_object needs to be initialized */
- uint8_t need_home_object : 1;
- uint8_t func_kind : 2;
- uint8_t new_target_allowed : 1;
- uint8_t super_call_allowed : 1;
- uint8_t super_allowed : 1;
- uint8_t arguments_allowed : 1;
- uint8_t has_debug : 1;
- uint8_t read_only_bytecode : 1;
- uint8_t is_direct_or_indirect_eval : 1; /* used by JS_GetScriptOrModuleName() */
- /* XXX: 10 bits available */
- uint8_t *byte_code_buf; /* (self pointer) */
- int byte_code_len;
- JSAtom func_name;
- JSBytecodeVarDef *vardefs; /* arguments + local variables (arg_count + var_count) (self pointer) */
- JSClosureVar *closure_var; /* list of variables in the closure (self pointer) */
- uint16_t arg_count;
- uint16_t var_count;
- uint16_t defined_arg_count; /* for length function property */
- uint16_t stack_size; /* maximum stack size */
- uint16_t var_ref_count; /* number of local variable references */
- JSContext *realm; /* function realm */
- JSValue *cpool; /* constant pool (self pointer) */
- int cpool_count;
- int closure_var_count;
- struct {
- /* debug info, move to separate structure to save memory? */
- JSAtom filename;
- int source_len;
- int pc2line_len;
- uint8_t *pc2line_buf;
- char *source;
- } debug;
- } JSFunctionBytecode;
- typedef struct JSBoundFunction {
- JSValue func_obj;
- JSValue this_val;
- int argc;
- JSValue argv[0];
- } JSBoundFunction;
- typedef enum JSIteratorKindEnum {
- JS_ITERATOR_KIND_KEY,
- JS_ITERATOR_KIND_VALUE,
- JS_ITERATOR_KIND_KEY_AND_VALUE,
- } JSIteratorKindEnum;
- typedef struct JSForInIterator {
- JSValue obj;
- uint32_t idx;
- uint32_t atom_count;
- uint8_t in_prototype_chain;
- uint8_t is_array;
- JSPropertyEnum *tab_atom; /* is_array = FALSE */
- } JSForInIterator;
- typedef struct JSRegExp {
- JSString *pattern;
- JSString *bytecode; /* also contains the flags */
- } JSRegExp;
- typedef struct JSProxyData {
- JSValue target;
- JSValue handler;
- uint8_t is_func;
- uint8_t is_revoked;
- } JSProxyData;
- typedef struct JSArrayBuffer {
- int byte_length; /* 0 if detached */
- int max_byte_length; /* -1 if not resizable; >= byte_length otherwise */
- uint8_t detached;
- uint8_t shared; /* if shared, the array buffer cannot be detached */
- uint8_t *data; /* NULL if detached */
- struct list_head array_list;
- void *opaque;
- JSFreeArrayBufferDataFunc *free_func;
- } JSArrayBuffer;
- typedef struct JSTypedArray {
- struct list_head link; /* link to arraybuffer */
- JSObject *obj; /* back pointer to the TypedArray/DataView object */
- JSObject *buffer; /* based array buffer */
- uint32_t offset; /* byte offset in the array buffer */
- uint32_t length; /* byte length in the array buffer */
- BOOL track_rab; /* auto-track length of backing array buffer */
- } JSTypedArray;
- typedef struct JSGlobalObject {
- JSValue uninitialized_vars; /* hidden object containing the list of uninitialized variables */
- } JSGlobalObject;
- typedef struct JSAsyncFunctionState {
- JSGCObjectHeader header;
- JSValue this_val; /* 'this' argument */
- int argc; /* number of function arguments */
- BOOL throw_flag; /* used to throw an exception in JS_CallInternal() */
- BOOL is_completed; /* TRUE if the function has returned. The stack
- frame is no longer valid */
- JSValue resolving_funcs[2]; /* only used in JS async functions */
- JSStackFrame frame;
- /* arg_buf, var_buf, stack_buf and var_refs follow */
- } JSAsyncFunctionState;
- typedef enum {
- /* binary operators */
- JS_OVOP_ADD,
- JS_OVOP_SUB,
- JS_OVOP_MUL,
- JS_OVOP_DIV,
- JS_OVOP_MOD,
- JS_OVOP_POW,
- JS_OVOP_OR,
- JS_OVOP_AND,
- JS_OVOP_XOR,
- JS_OVOP_SHL,
- JS_OVOP_SAR,
- JS_OVOP_SHR,
- JS_OVOP_EQ,
- JS_OVOP_LESS,
- JS_OVOP_BINARY_COUNT,
- /* unary operators */
- JS_OVOP_POS = JS_OVOP_BINARY_COUNT,
- JS_OVOP_NEG,
- JS_OVOP_INC,
- JS_OVOP_DEC,
- JS_OVOP_NOT,
- JS_OVOP_COUNT,
- } JSOverloadableOperatorEnum;
- typedef struct {
- uint32_t operator_index;
- JSObject *ops[JS_OVOP_BINARY_COUNT]; /* self operators */
- } JSBinaryOperatorDefEntry;
- typedef struct {
- int count;
- JSBinaryOperatorDefEntry *tab;
- } JSBinaryOperatorDef;
- typedef struct {
- uint32_t operator_counter;
- BOOL is_primitive; /* OperatorSet for a primitive type */
- /* NULL if no operator is defined */
- JSObject *self_ops[JS_OVOP_COUNT]; /* self operators */
- JSBinaryOperatorDef left;
- JSBinaryOperatorDef right;
- } JSOperatorSetData;
- typedef struct JSReqModuleEntry {
- JSAtom module_name;
- JSModuleDef *module; /* used using resolution */
- JSValue attributes; /* JS_UNDEFINED or an object contains the attributes as key/value */
- } JSReqModuleEntry;
- typedef enum JSExportTypeEnum {
- JS_EXPORT_TYPE_LOCAL,
- JS_EXPORT_TYPE_INDIRECT,
- } JSExportTypeEnum;
- typedef struct JSExportEntry {
- union {
- struct {
- int var_idx; /* closure variable index */
- JSVarRef *var_ref; /* if != NULL, reference to the variable */
- } local; /* for local export */
- int req_module_idx; /* module for indirect export */
- } u;
- JSExportTypeEnum export_type;
- JSAtom local_name; /* '*' if export ns from. not used for local
- export after compilation */
- JSAtom export_name; /* exported variable name */
- } JSExportEntry;
- typedef struct JSStarExportEntry {
- int req_module_idx; /* in req_module_entries */
- } JSStarExportEntry;
- typedef struct JSImportEntry {
- int var_idx; /* closure variable index */
- BOOL is_star; /* import_name = '*' is a valid import name, so need a flag */
- JSAtom import_name;
- int req_module_idx; /* in req_module_entries */
- } JSImportEntry;
- typedef enum {
- JS_MODULE_STATUS_UNLINKED,
- JS_MODULE_STATUS_LINKING,
- JS_MODULE_STATUS_LINKED,
- JS_MODULE_STATUS_EVALUATING,
- JS_MODULE_STATUS_EVALUATING_ASYNC,
- JS_MODULE_STATUS_EVALUATED,
- } JSModuleStatus;
- struct JSModuleDef {
- JSGCObjectHeader header; /* must come first */
- JSAtom module_name;
- struct list_head link;
- JSReqModuleEntry *req_module_entries;
- int req_module_entries_count;
- int req_module_entries_size;
- JSExportEntry *export_entries;
- int export_entries_count;
- int export_entries_size;
- JSStarExportEntry *star_export_entries;
- int star_export_entries_count;
- int star_export_entries_size;
- JSImportEntry *import_entries;
- int import_entries_count;
- int import_entries_size;
- JSValue module_ns;
- JSValue func_obj; /* only used for JS modules */
- JSModuleInitFunc *init_func; /* only used for C modules */
- BOOL has_tla : 8; /* true if func_obj contains await */
- BOOL resolved : 8;
- BOOL func_created : 8;
- JSModuleStatus status : 8;
- /* temp use during js_module_link() & js_module_evaluate() */
- int dfs_index, dfs_ancestor_index;
- JSModuleDef *stack_prev;
- /* temp use during js_module_evaluate() */
- JSModuleDef **async_parent_modules;
- int async_parent_modules_count;
- int async_parent_modules_size;
- int pending_async_dependencies;
- BOOL async_evaluation; /* true: async_evaluation_timestamp corresponds to [[AsyncEvaluationOrder]]
- false: [[AsyncEvaluationOrder]] is UNSET or DONE */
- int64_t async_evaluation_timestamp;
- JSModuleDef *cycle_root;
- JSValue promise; /* corresponds to spec field: capability */
- JSValue resolving_funcs[2]; /* corresponds to spec field: capability */
- /* true if evaluation yielded an exception. It is saved in
- eval_exception */
- BOOL eval_has_exception : 8;
- JSValue eval_exception;
- JSValue meta_obj; /* for import.meta */
- JSValue private_value; /* private value for C modules */
- };
- typedef struct JSJobEntry {
- struct list_head link;
- JSContext *realm;
- JSJobFunc *job_func;
- int argc;
- JSValue argv[0];
- } JSJobEntry;
- typedef struct JSProperty {
- union {
- JSValue value; /* JS_PROP_NORMAL */
- struct { /* JS_PROP_GETSET */
- JSObject *getter; /* NULL if undefined */
- JSObject *setter; /* NULL if undefined */
- } getset;
- JSVarRef *var_ref; /* JS_PROP_VARREF */
- struct { /* JS_PROP_AUTOINIT */
- /* in order to use only 2 pointers, we compress the realm
- and the init function pointer */
- uintptr_t realm_and_id; /* realm and init_id (JS_AUTOINIT_ID_x)
- in the 2 low bits */
- void *opaque;
- } init;
- } u;
- } JSProperty;
- #define JS_PROP_INITIAL_SIZE 2
- #define JS_PROP_INITIAL_HASH_SIZE 4 /* must be a power of two */
- typedef struct JSShapeProperty {
- uint32_t hash_next : 26; /* 0 if last in list */
- uint32_t flags : 6; /* JS_PROP_XXX */
- JSAtom atom; /* JS_ATOM_NULL = free property entry */
- } JSShapeProperty;
- struct JSShape {
- JSGCObjectHeader header;
- /* true if the shape is inserted in the shape hash table. If not,
- JSShape.hash is not valid */
- uint8_t is_hashed;
- uint32_t hash; /* current hash value */
- uint32_t prop_hash_mask; /* >= 2 */
- int prop_size; /* allocated properties */
- int prop_count; /* include deleted properties */
- int deleted_prop_count;
- JSShape *shape_hash_next; /* in JSRuntime.shape_hash[h] list */
- JSObject *proto;
- uint32_t hash_table[]; /* prop_hash_mask + 1 elements */
- /* followed by JSShapeProperty prop[prop_size]; */
- };
- struct JSObject {
- JSGCObjectHeader header;
- /* TRUE if the array prototype is "normal":
- - no small index properties which are get/set or non writable
- - its prototype is Object.prototype
- - Object.prototype has no small index properties which are get/set or non writable
- - the prototype of Object.prototype is null (always true as it is immutable)
- */
- uint8_t is_std_array_prototype : 1;
-
- uint8_t extensible : 1;
- uint8_t free_mark : 1; /* only used when freeing objects with cycles */
- uint8_t is_exotic : 1; /* TRUE if object has exotic property handlers */
- uint8_t fast_array : 1; /* TRUE if u.array is used for get/put (for JS_CLASS_ARRAY, JS_CLASS_ARGUMENTS, JS_CLASS_MAPPED_ARGUMENTS and typed arrays) */
- uint8_t is_constructor : 1; /* TRUE if object is a constructor function */
- uint8_t has_immutable_prototype : 1; /* cannot modify the prototype */
- uint8_t tmp_mark : 1; /* used in JS_WriteObjectRec() */
- uint8_t is_HTMLDDA : 1; /* specific annex B IsHtmlDDA behavior */
- uint16_t class_id; /* see JS_CLASS_x */
- /* count the number of weak references to this object. The object
- structure is freed only if header.ref_count = 0 and
- weakref_count = 0 */
- uint32_t weakref_count;
- JSShape *shape; /* prototype and property names + flag */
- JSProperty *prop; /* array of properties */
- union {
- void *opaque;
- struct JSBoundFunction *bound_function; /* JS_CLASS_BOUND_FUNCTION */
- struct JSCFunctionDataRecord *c_function_data_record; /* JS_CLASS_C_FUNCTION_DATA */
- struct JSForInIterator *for_in_iterator; /* JS_CLASS_FOR_IN_ITERATOR */
- struct JSArrayBuffer *array_buffer; /* JS_CLASS_ARRAY_BUFFER, JS_CLASS_SHARED_ARRAY_BUFFER */
- struct JSTypedArray *typed_array; /* JS_CLASS_UINT8C_ARRAY..JS_CLASS_DATAVIEW */
- struct JSMapState *map_state; /* JS_CLASS_MAP..JS_CLASS_WEAKSET */
- struct JSMapIteratorData *map_iterator_data; /* JS_CLASS_MAP_ITERATOR, JS_CLASS_SET_ITERATOR */
- struct JSArrayIteratorData *array_iterator_data; /* JS_CLASS_ARRAY_ITERATOR, JS_CLASS_STRING_ITERATOR */
- struct JSRegExpStringIteratorData *regexp_string_iterator_data; /* JS_CLASS_REGEXP_STRING_ITERATOR */
- struct JSGeneratorData *generator_data; /* JS_CLASS_GENERATOR */
- struct JSIteratorConcatData *iterator_concat_data; /* JS_CLASS_ITERATOR_CONCAT */
- struct JSIteratorHelperData *iterator_helper_data; /* JS_CLASS_ITERATOR_HELPER */
- struct JSIteratorWrapData *iterator_wrap_data; /* JS_CLASS_ITERATOR_WRAP */
- struct JSProxyData *proxy_data; /* JS_CLASS_PROXY */
- struct JSPromiseData *promise_data; /* JS_CLASS_PROMISE */
- struct JSPromiseFunctionData *promise_function_data; /* JS_CLASS_PROMISE_RESOLVE_FUNCTION, JS_CLASS_PROMISE_REJECT_FUNCTION */
- struct JSAsyncFunctionState *async_function_data; /* JS_CLASS_ASYNC_FUNCTION_RESOLVE, JS_CLASS_ASYNC_FUNCTION_REJECT */
- struct JSAsyncFromSyncIteratorData *async_from_sync_iterator_data; /* JS_CLASS_ASYNC_FROM_SYNC_ITERATOR */
- struct JSAsyncGeneratorData *async_generator_data; /* JS_CLASS_ASYNC_GENERATOR */
- struct { /* JS_CLASS_BYTECODE_FUNCTION: 12/24 bytes */
- /* also used by JS_CLASS_GENERATOR_FUNCTION, JS_CLASS_ASYNC_FUNCTION and JS_CLASS_ASYNC_GENERATOR_FUNCTION */
- struct JSFunctionBytecode *function_bytecode;
- JSVarRef **var_refs;
- JSObject *home_object; /* for 'super' access */
- } func;
- struct { /* JS_CLASS_C_FUNCTION: 12/20 bytes */
- JSContext *realm;
- JSCFunctionType c_function;
- uint8_t length;
- uint8_t cproto;
- int16_t magic;
- } cfunc;
- /* array part for fast arrays and typed arrays */
- struct { /* JS_CLASS_ARRAY, JS_CLASS_ARGUMENTS, JS_CLASS_MAPPED_ARGUMENTS, JS_CLASS_UINT8C_ARRAY..JS_CLASS_FLOAT64_ARRAY */
- union {
- uint32_t size; /* JS_CLASS_ARRAY */
- struct JSTypedArray *typed_array; /* JS_CLASS_UINT8C_ARRAY..JS_CLASS_FLOAT64_ARRAY */
- } u1;
- union {
- JSValue *values; /* JS_CLASS_ARRAY, JS_CLASS_ARGUMENTS */
- JSVarRef **var_refs; /* JS_CLASS_MAPPED_ARGUMENTS */
- void *ptr; /* JS_CLASS_UINT8C_ARRAY..JS_CLASS_FLOAT64_ARRAY */
- int8_t *int8_ptr; /* JS_CLASS_INT8_ARRAY */
- uint8_t *uint8_ptr; /* JS_CLASS_UINT8_ARRAY, JS_CLASS_UINT8C_ARRAY */
- int16_t *int16_ptr; /* JS_CLASS_INT16_ARRAY */
- uint16_t *uint16_ptr; /* JS_CLASS_UINT16_ARRAY */
- int32_t *int32_ptr; /* JS_CLASS_INT32_ARRAY */
- uint32_t *uint32_ptr; /* JS_CLASS_UINT32_ARRAY */
- int64_t *int64_ptr; /* JS_CLASS_INT64_ARRAY */
- uint64_t *uint64_ptr; /* JS_CLASS_UINT64_ARRAY */
- uint16_t *fp16_ptr; /* JS_CLASS_FLOAT16_ARRAY */
- float *float_ptr; /* JS_CLASS_FLOAT32_ARRAY */
- double *double_ptr; /* JS_CLASS_FLOAT64_ARRAY */
- } u;
- uint32_t count; /* <= 2^31-1. 0 for a detached typed array */
- } array; /* 12/20 bytes */
- JSRegExp regexp; /* JS_CLASS_REGEXP: 8/16 bytes */
- JSValue object_data; /* for JS_SetObjectData(): 8/16/16 bytes */
- JSGlobalObject global_object;
- } u;
- };
- typedef struct JSMapRecord {
- int ref_count; /* used during enumeration to avoid freeing the record */
- BOOL empty : 8; /* TRUE if the record is deleted */
- struct list_head link;
- struct JSMapRecord *hash_next;
- JSValue key;
- JSValue value;
- } JSMapRecord;
- typedef struct JSMapState {
- BOOL is_weak; /* TRUE if WeakSet/WeakMap */
- struct list_head records; /* list of JSMapRecord.link */
- uint32_t record_count;
- JSMapRecord **hash_table;
- int hash_bits;
- uint32_t hash_size; /* = 2 ^ hash_bits */
- uint32_t record_count_threshold; /* count at which a hash table
- resize is needed */
- JSWeakRefHeader weakref_header; /* only used if is_weak = TRUE */
- } JSMapState;
- enum {
- __JS_ATOM_NULL = JS_ATOM_NULL,
- #define DEF(name, str) JS_ATOM_ ## name,
- #include "quickjs-atom.h"
- #undef DEF
- JS_ATOM_END,
- };
- #define JS_ATOM_LAST_KEYWORD JS_ATOM_super
- #define JS_ATOM_LAST_STRICT_KEYWORD JS_ATOM_yield
- static const char js_atom_init[] =
- #define DEF(name, str) str "\0"
- #include "quickjs-atom.h"
- #undef DEF
- ;
- typedef enum OPCodeFormat {
- #define FMT(f) OP_FMT_ ## f,
- #define DEF(id, size, n_pop, n_push, f)
- #include "quickjs-opcode.h"
- #undef DEF
- #undef FMT
- } OPCodeFormat;
- enum OPCodeEnum {
- #define FMT(f)
- #define DEF(id, size, n_pop, n_push, f) OP_ ## id,
- #define def(id, size, n_pop, n_push, f)
- #include "quickjs-opcode.h"
- #undef def
- #undef DEF
- #undef FMT
- OP_COUNT, /* excluding temporary opcodes */
- /* temporary opcodes : overlap with the short opcodes */
- OP_TEMP_START = OP_nop + 1,
- OP___dummy = OP_TEMP_START - 1,
- #define FMT(f)
- #define DEF(id, size, n_pop, n_push, f)
- #define def(id, size, n_pop, n_push, f) OP_ ## id,
- #include "quickjs-opcode.h"
- #undef def
- #undef DEF
- #undef FMT
- OP_TEMP_END,
- };
- static int JS_InitAtoms(JSRuntime *rt);
- static JSAtom __JS_NewAtomInit(JSRuntime *rt, const char *str, int len,
- int atom_type);
- static void JS_FreeAtomStruct(JSRuntime *rt, JSAtomStruct *p);
- static void free_function_bytecode(JSRuntime *rt, JSFunctionBytecode *b);
- static JSValue js_call_c_function(JSContext *ctx, JSValueConst func_obj,
- JSValueConst this_obj,
- int argc, JSValueConst *argv, int flags);
- static JSValue js_call_bound_function(JSContext *ctx, JSValueConst func_obj,
- JSValueConst this_obj,
- int argc, JSValueConst *argv, int flags);
- static JSValue JS_CallInternal(JSContext *ctx, JSValueConst func_obj,
- JSValueConst this_obj, JSValueConst new_target,
- int argc, JSValue *argv, int flags);
- static JSValue JS_CallConstructorInternal(JSContext *ctx,
- JSValueConst func_obj,
- JSValueConst new_target,
- int argc, JSValue *argv, int flags);
- static JSValue JS_CallFree(JSContext *ctx, JSValue func_obj, JSValueConst this_obj,
- int argc, JSValueConst *argv);
- static JSValue JS_InvokeFree(JSContext *ctx, JSValue this_val, JSAtom atom,
- int argc, JSValueConst *argv);
- static __exception int JS_ToArrayLengthFree(JSContext *ctx, uint32_t *plen,
- JSValue val, BOOL is_array_ctor);
- static JSValue JS_EvalObject(JSContext *ctx, JSValueConst this_obj,
- JSValueConst val, int flags, int scope_idx);
- JSValue __attribute__((format(printf, 2, 3))) JS_ThrowInternalError(JSContext *ctx, const char *fmt, ...);
- static __maybe_unused void JS_DumpAtoms(JSRuntime *rt);
- static __maybe_unused void JS_DumpString(JSRuntime *rt, const JSString *p);
- static __maybe_unused void JS_DumpObjectHeader(JSRuntime *rt);
- static __maybe_unused void JS_DumpObject(JSRuntime *rt, JSObject *p);
- static __maybe_unused void JS_DumpGCObject(JSRuntime *rt, JSGCObjectHeader *p);
- static __maybe_unused void JS_DumpAtom(JSContext *ctx, const char *str, JSAtom atom);
- static __maybe_unused void JS_DumpValueRT(JSRuntime *rt, const char *str, JSValueConst val);
- static __maybe_unused void JS_DumpValue(JSContext *ctx, const char *str, JSValueConst val);
- static __maybe_unused void JS_DumpShapes(JSRuntime *rt);
- static void js_dump_value_write(void *opaque, const char *buf, size_t len);
- static JSValue js_function_apply(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic);
- static void js_array_finalizer(JSRuntime *rt, JSValue val);
- static void js_array_mark(JSRuntime *rt, JSValueConst val, JS_MarkFunc *mark_func);
- static void js_mapped_arguments_finalizer(JSRuntime *rt, JSValue val);
- static void js_mapped_arguments_mark(JSRuntime *rt, JSValueConst val, JS_MarkFunc *mark_func);
- static void js_object_data_finalizer(JSRuntime *rt, JSValue val);
- static void js_object_data_mark(JSRuntime *rt, JSValueConst val, JS_MarkFunc *mark_func);
- static void js_c_function_finalizer(JSRuntime *rt, JSValue val);
- static void js_c_function_mark(JSRuntime *rt, JSValueConst val, JS_MarkFunc *mark_func);
- static void js_bytecode_function_finalizer(JSRuntime *rt, JSValue val);
- static void js_bytecode_function_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func);
- static void js_bound_function_finalizer(JSRuntime *rt, JSValue val);
- static void js_bound_function_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func);
- static void js_for_in_iterator_finalizer(JSRuntime *rt, JSValue val);
- static void js_for_in_iterator_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func);
- static void js_regexp_finalizer(JSRuntime *rt, JSValue val);
- static void js_array_buffer_finalizer(JSRuntime *rt, JSValue val);
- static void js_typed_array_finalizer(JSRuntime *rt, JSValue val);
- static void js_typed_array_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func);
- static void js_proxy_finalizer(JSRuntime *rt, JSValue val);
- static void js_proxy_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func);
- static void js_map_finalizer(JSRuntime *rt, JSValue val);
- static void js_map_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func);
- static void js_map_iterator_finalizer(JSRuntime *rt, JSValue val);
- static void js_map_iterator_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func);
- static void js_array_iterator_finalizer(JSRuntime *rt, JSValue val);
- static void js_array_iterator_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func);
- static void js_iterator_concat_finalizer(JSRuntime *rt, JSValue val);
- static void js_iterator_concat_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func);
- static void js_iterator_helper_finalizer(JSRuntime *rt, JSValue val);
- static void js_iterator_helper_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func);
- static void js_iterator_wrap_finalizer(JSRuntime *rt, JSValue val);
- static void js_iterator_wrap_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func);
- static void js_regexp_string_iterator_finalizer(JSRuntime *rt, JSValue val);
- static void js_regexp_string_iterator_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func);
- static void js_generator_finalizer(JSRuntime *rt, JSValue obj);
- static void js_generator_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func);
- static void js_global_object_finalizer(JSRuntime *rt, JSValue obj);
- static void js_global_object_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func);
- static void js_promise_finalizer(JSRuntime *rt, JSValue val);
- static void js_promise_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func);
- static void js_promise_resolve_function_finalizer(JSRuntime *rt, JSValue val);
- static void js_promise_resolve_function_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func);
- #define HINT_STRING 0
- #define HINT_NUMBER 1
- #define HINT_NONE 2
- #define HINT_FORCE_ORDINARY (1 << 4) // don't try Symbol.toPrimitive
- static JSValue JS_ToPrimitiveFree(JSContext *ctx, JSValue val, int hint);
- static JSValue JS_ToStringFree(JSContext *ctx, JSValue val);
- static int JS_ToBoolFree(JSContext *ctx, JSValue val);
- static int JS_ToInt32Free(JSContext *ctx, int32_t *pres, JSValue val);
- static int JS_ToFloat64Free(JSContext *ctx, double *pres, JSValue val);
- static int JS_ToUint8ClampFree(JSContext *ctx, int32_t *pres, JSValue val);
- static JSValue js_new_string8_len(JSContext *ctx, const char *buf, int len);
- static JSValue js_compile_regexp(JSContext *ctx, JSValueConst pattern,
- JSValueConst flags);
- static JSValue JS_NewRegexp(JSContext *ctx, JSValue pattern, JSValue bc);
- static void gc_decref(JSRuntime *rt);
- static int JS_NewClass1(JSRuntime *rt, JSClassID class_id,
- const JSClassDef *class_def, JSAtom name);
- typedef enum JSStrictEqModeEnum {
- JS_EQ_STRICT,
- JS_EQ_SAME_VALUE,
- JS_EQ_SAME_VALUE_ZERO,
- } JSStrictEqModeEnum;
- static BOOL js_strict_eq2(JSContext *ctx, JSValue op1, JSValue op2,
- JSStrictEqModeEnum eq_mode);
- static BOOL js_strict_eq(JSContext *ctx, JSValueConst op1, JSValueConst op2);
- static BOOL js_same_value(JSContext *ctx, JSValueConst op1, JSValueConst op2);
- static BOOL js_same_value_zero(JSContext *ctx, JSValueConst op1, JSValueConst op2);
- static JSValue JS_ToObject(JSContext *ctx, JSValueConst val);
- static JSValue JS_ToObjectFree(JSContext *ctx, JSValue val);
- static JSProperty *add_property(JSContext *ctx,
- JSObject *p, JSAtom prop, int prop_flags);
- static void free_property(JSRuntime *rt, JSProperty *pr, int prop_flags);
- static int JS_ToBigInt64Free(JSContext *ctx, int64_t *pres, JSValue val);
- JSValue JS_ThrowOutOfMemory(JSContext *ctx);
- static JSValue JS_ThrowTypeErrorRevokedProxy(JSContext *ctx);
- static int js_resolve_proxy(JSContext *ctx, JSValueConst *pval, int throw_exception);
- static int JS_CreateProperty(JSContext *ctx, JSObject *p,
- JSAtom prop, JSValueConst val,
- JSValueConst getter, JSValueConst setter,
- int flags);
- static int js_string_memcmp(const JSString *p1, int pos1, const JSString *p2,
- int pos2, int len);
- static JSValue js_array_buffer_constructor3(JSContext *ctx,
- JSValueConst new_target,
- uint64_t len, uint64_t *max_len,
- JSClassID class_id,
- uint8_t *buf,
- JSFreeArrayBufferDataFunc *free_func,
- void *opaque, BOOL alloc_flag);
- static void js_array_buffer_free(JSRuntime *rt, void *opaque, void *ptr);
- static JSArrayBuffer *js_get_array_buffer(JSContext *ctx, JSValueConst obj);
- static BOOL array_buffer_is_resizable(const JSArrayBuffer *abuf);
- static JSValue js_typed_array_constructor(JSContext *ctx,
- JSValueConst this_val,
- int argc, JSValueConst *argv,
- int classid);
- static JSValue js_typed_array_constructor_ta(JSContext *ctx,
- JSValueConst new_target,
- JSValueConst src_obj,
- int classid, uint32_t len);
- static BOOL typed_array_is_oob(JSObject *p);
- static int js_typed_array_get_length_unsafe(JSContext *ctx, JSValueConst obj);
- static JSValue JS_ThrowTypeErrorDetachedArrayBuffer(JSContext *ctx);
- static JSValue JS_ThrowTypeErrorArrayBufferOOB(JSContext *ctx);
- static JSVarRef *js_create_var_ref(JSContext *ctx, BOOL is_lexical);
- static JSVarRef *get_var_ref(JSContext *ctx, JSStackFrame *sf, int var_idx,
- BOOL is_arg);
- static void __async_func_free(JSRuntime *rt, JSAsyncFunctionState *s);
- static void async_func_free(JSRuntime *rt, JSAsyncFunctionState *s);
- static JSValue js_generator_function_call(JSContext *ctx, JSValueConst func_obj,
- JSValueConst this_obj,
- int argc, JSValueConst *argv,
- int flags);
- static void js_async_function_resolve_finalizer(JSRuntime *rt, JSValue val);
- static void js_async_function_resolve_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func);
- static JSValue JS_EvalInternal(JSContext *ctx, JSValueConst this_obj,
- const char *input, size_t input_len,
- const char *filename, int flags, int scope_idx);
- static void js_free_module_def(JSRuntime *rt, JSModuleDef *m);
- static void js_mark_module_def(JSRuntime *rt, JSModuleDef *m,
- JS_MarkFunc *mark_func);
- static JSValue js_import_meta(JSContext *ctx);
- static JSValue js_dynamic_import(JSContext *ctx, JSValueConst specifier, JSValueConst options);
- static void free_var_ref(JSRuntime *rt, JSVarRef *var_ref);
- static JSValue js_new_promise_capability(JSContext *ctx,
- JSValue *resolving_funcs,
- JSValueConst ctor);
- static __exception int perform_promise_then(JSContext *ctx,
- JSValueConst promise,
- JSValueConst *resolve_reject,
- JSValueConst *cap_resolving_funcs);
- static JSValue js_promise_resolve(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic);
- static JSValue js_promise_then(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv);
- static BOOL js_string_eq(JSContext *ctx,
- const JSString *p1, const JSString *p2);
- static int js_string_compare(JSContext *ctx,
- const JSString *p1, const JSString *p2);
- static JSValue JS_ToNumber(JSContext *ctx, JSValueConst val);
- static int JS_SetPropertyValue(JSContext *ctx, JSValueConst this_obj,
- JSValue prop, JSValue val, int flags);
- static int JS_NumberIsInteger(JSContext *ctx, JSValueConst val);
- static BOOL JS_NumberIsNegativeOrMinusZero(JSContext *ctx, JSValueConst val);
- static JSValue JS_ToNumberFree(JSContext *ctx, JSValue val);
- static int JS_GetOwnPropertyInternal(JSContext *ctx, JSPropertyDescriptor *desc,
- JSObject *p, JSAtom prop);
- static void js_free_desc(JSContext *ctx, JSPropertyDescriptor *desc);
- static int JS_AddIntrinsicBasicObjects(JSContext *ctx);
- static void js_free_shape(JSRuntime *rt, JSShape *sh);
- static void js_free_shape_null(JSRuntime *rt, JSShape *sh);
- static int js_shape_prepare_update(JSContext *ctx, JSObject *p,
- JSShapeProperty **pprs);
- static int init_shape_hash(JSRuntime *rt);
- static __exception int js_get_length32(JSContext *ctx, uint32_t *pres,
- JSValueConst obj);
- static __exception int js_get_length64(JSContext *ctx, int64_t *pres,
- JSValueConst obj);
- static void free_arg_list(JSContext *ctx, JSValue *tab, uint32_t len);
- static JSValue *build_arg_list(JSContext *ctx, uint32_t *plen,
- JSValueConst array_arg);
- static BOOL js_get_fast_array(JSContext *ctx, JSValueConst obj,
- JSValue **arrpp, uint32_t *countp);
- static JSValue JS_CreateAsyncFromSyncIterator(JSContext *ctx,
- JSValueConst sync_iter);
- static void js_c_function_data_finalizer(JSRuntime *rt, JSValue val);
- static void js_c_function_data_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func);
- static JSValue js_c_function_data_call(JSContext *ctx, JSValueConst func_obj,
- JSValueConst this_val,
- int argc, JSValueConst *argv, int flags);
- static JSAtom js_symbol_to_atom(JSContext *ctx, JSValue val);
- static void add_gc_object(JSRuntime *rt, JSGCObjectHeader *h,
- JSGCObjectTypeEnum type);
- static void remove_gc_object(JSGCObjectHeader *h);
- static JSValue js_instantiate_prototype(JSContext *ctx, JSObject *p, JSAtom atom, void *opaque);
- static JSValue js_module_ns_autoinit(JSContext *ctx, JSObject *p, JSAtom atom,
- void *opaque);
- static JSValue JS_InstantiateFunctionListItem2(JSContext *ctx, JSObject *p,
- JSAtom atom, void *opaque);
- static JSValue js_object_groupBy(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int is_map);
- static void map_delete_weakrefs(JSRuntime *rt, JSWeakRefHeader *wh);
- static void weakref_delete_weakref(JSRuntime *rt, JSWeakRefHeader *wh);
- static void finrec_delete_weakref(JSRuntime *rt, JSWeakRefHeader *wh);
- static void JS_RunGCInternal(JSRuntime *rt, BOOL remove_weak_objects);
- static JSValue js_array_from_iterator(JSContext *ctx, uint32_t *plen,
- JSValueConst obj, JSValueConst method);
- static int js_string_find_invalid_codepoint(JSString *p);
- static JSValue js_regexp_toString(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv);
- static JSValue get_date_string(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic);
- static JSValue js_error_toString(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv);
- static JSVarRef *js_global_object_find_uninitialized_var(JSContext *ctx, JSObject *p,
- JSAtom atom, BOOL is_lexical);
- static int typed_array_init(JSContext *ctx, JSValueConst obj,
- JSValue buffer, uint64_t offset, uint64_t len,
- BOOL track_rab);
- static const JSClassExoticMethods js_arguments_exotic_methods;
- static const JSClassExoticMethods js_string_exotic_methods;
- static const JSClassExoticMethods js_proxy_exotic_methods;
- static const JSClassExoticMethods js_module_ns_exotic_methods;
- static JSClassID js_class_id_alloc = JS_CLASS_INIT_COUNT;
- /* JS malloc */
- /* max overhead for size >= 64: 12.5% */
- static const uint16_t js_malloc_block_sizes[JS_MALLOC_BLOCK_SIZE_COUNT] = {
- 16,
- 24,
- 32,
- 40,
- 48,
- 56,
- 64,
- 72,
- 80,
- 88,
- 96,
- 104,
- 112,
- 120,
- 128,
- 144,
- 160,
- 176,
- 192,
- 208,
- 224,
- 240,
- 256,
- 288,
- 320,
- 352,
- 384,
- 416,
- 448,
- 480,
- 512,
- };
- static int get_block_size_index(size_t size)
- {
- if (size <= 16) {
- return 0;
- } else if (size <= 128) {
- return (size + 7) / 8 - 2;
- } else if (size <= 256) {
- return (size + 15) / 16 + 6;
- } else if (size <= 512) {
- return (size + 31) / 32 + 14;
- } else {
- return JS_MALLOC_BLOCK_SIZE_COUNT;
- }
- }
- static JSMallocBlockHeader *get_zero_size_block(JSMallocContext *s)
- {
- return (JSMallocBlockHeader *)s->zero_size_block;
- }
- static void js_malloc_init(JSMallocContext *s)
- {
- int i;
- memset(s, 0, sizeof(*s));
- get_zero_size_block(s)->u.block_idx = FREE_NIL;
- for(i = 0; i < JS_MALLOC_BLOCK_SIZE_COUNT; i++) {
- init_list_head(&s->arena_list[i]);
- init_list_head(&s->free_arena_list[i]);
- }
- #ifdef JS_MALLOC_USE_ITER
- init_list_head(&s->large_block_list);
- #endif
- }
- static void *get_arena_block(JSMallocArena *ar, unsigned int idx, unsigned int block_size)
- {
- return ar->blocks + idx * block_size;
- }
- static inline JSMallocBlockHeader *js_rc(void *ptr)
- {
- return container_of(ptr, JSMallocBlockHeader, user_data);
- }
- static no_inline JSMallocArena *js_malloc_new_arena(JSMallocContext *s, int block_size_idx)
- {
- JSMallocBlockHeader *b;
- JSMallocArena *ar;
- int n_blocks, block_size, i;
- block_size = js_malloc_block_sizes[block_size_idx];
- n_blocks = (JS_MALLOC_ARENA_SIZE - sizeof(JSMallocArena)) / block_size;
- ar = s->mf.js_malloc(&s->malloc_state, sizeof(JSMallocArena) + n_blocks * block_size);
- if (!ar)
- return NULL;
- ar->block_size_idx = block_size_idx;
- ar->n_blocks = n_blocks;
- ar->n_used_blocks = 0;
- ar->first_free_block = 0;
- #ifdef JS_MALLOC_USE_ITER
- {
- int n_bitmap_words = (n_blocks + 31) / 32;
- for(i = 0; i < n_bitmap_words; i++)
- ar->bitmap[i] = 0;
- }
- #endif
- for(i = 0; i < n_blocks - 1; i++) {
- b = get_arena_block(ar, i, block_size);
- b->u.free_next = i + 1;
- b->block_size_idx = block_size_idx;
- }
- b = get_arena_block(ar, n_blocks - 1, block_size);
- b->u.free_next = FREE_NIL;
- b->block_size_idx = block_size_idx;
-
- /* add to the head */
- list_add(&ar->link, &s->arena_list[block_size_idx]);
- list_add(&ar->free_link, &s->free_arena_list[block_size_idx]);
- return ar;
- }
- static no_inline void *js_malloc_large(JSMallocContext *s, size_t size)
- {
- JSMallocLargeBlockHeader *b;
- b = s->mf.js_malloc(&s->malloc_state, sizeof(JSMallocLargeBlockHeader) + size);
- if (!b)
- return NULL;
- b->header.u.block_idx = FREE_NIL;
- b->header.block_size_idx = 0xff; /* fail safe */
- #ifdef JS_MALLOC_USE_ITER
- list_add_tail(&b->link, &s->large_block_list);
- #endif
- return b->header.user_data;
- }
- static void *__js_malloc(JSMallocContext *s, size_t size)
- {
- size_t total_size;
- if (unlikely(size == 0)) {
- JSMallocBlockHeader *b = get_zero_size_block(s);
- return b->user_data;
- } else {
- total_size = ((size + JS_MALLOC_ALIGN - 1) & ~(JS_MALLOC_ALIGN - 1)) +
- sizeof(JSMallocBlockHeader);
- if (!JS_MALLOC_LARGE_BLOCKS_ONLY &&
- total_size <= JS_MALLOC_MAX_SMALL_SIZE) {
- int block_size_idx;
- unsigned int block_idx, block_size;
- JSMallocBlockHeader *b;
- JSMallocArena *ar;
- struct list_head *el, *head;
-
- block_size_idx = get_block_size_index(total_size);
- block_size = js_malloc_block_sizes[block_size_idx];
- head = &s->free_arena_list[block_size_idx];
- el = head->next;
- if (unlikely(el == head)) {
- ar = js_malloc_new_arena(s, block_size_idx);
- if (!ar)
- return NULL;
- } else {
- ar = list_entry(el, JSMallocArena, free_link);
- }
- block_idx = ar->first_free_block;
- b = get_arena_block(ar, ar->first_free_block, block_size);
- ar->first_free_block = b->u.free_next;
- b->u.block_idx = block_idx;
- ar->n_used_blocks++;
- if (unlikely(ar->n_used_blocks == ar->n_blocks)) {
- list_del(&ar->free_link);
- }
- #ifdef JS_MALLOC_USE_ITER
- ar->bitmap[block_idx / 32] |= 1 << (block_idx % 32);
- #endif
- return b->user_data;
- } else {
- return js_malloc_large(s, size);
- }
- }
- }
- static void __js_free(JSMallocContext *s, void *ptr)
- {
- JSMallocBlockHeader *b;
- if (!ptr)
- return;
- b = container_of(ptr, JSMallocBlockHeader, user_data);
- if (unlikely(b->u.block_idx == FREE_NIL)) {
- /* large or zero size block */
- if (b == get_zero_size_block(s)) {
- /* nothing to do */
- } else {
- JSMallocLargeBlockHeader *lb = container_of(ptr, JSMallocLargeBlockHeader, header.user_data);
- #ifdef JS_MALLOC_USE_ITER
- list_del(&lb->link);
- #endif
- s->mf.js_free(&s->malloc_state, lb);
- }
- } else {
- unsigned int block_idx = b->u.block_idx;
- unsigned int block_size_idx = b->block_size_idx;
- unsigned int block_size = js_malloc_block_sizes[block_size_idx];
- JSMallocArena *ar = (JSMallocArena *)((uint8_t *)b - block_size * block_idx - sizeof(JSMallocArena));
- b->u.free_next = ar->first_free_block;
- ar->first_free_block = block_idx;
- #ifdef JS_MALLOC_USE_ITER
- ar->bitmap[block_idx / 32] &= ~(1 << (block_idx % 32));
- #endif
- /* add back to the free list if needed */
- if (unlikely(ar->n_used_blocks == ar->n_blocks)) {
- list_add(&ar->free_link, &s->free_arena_list[block_size_idx]);
- }
- ar->n_used_blocks--;
- if (unlikely(ar->n_used_blocks == 0)) {
- list_del(&ar->link);
- list_del(&ar->free_link);
- s->mf.js_free(&s->malloc_state, ar);
- }
- }
- }
- static void *__js_realloc(JSMallocContext *s, void *ptr, size_t size)
- {
- JSMallocBlockHeader *b;
- if (ptr == NULL) {
- return __js_malloc(s, size);
- } else if (size == 0) {
- __js_free(s, ptr);
- return NULL;
- }
- b = container_of(ptr, JSMallocBlockHeader, user_data);
- if (b->u.block_idx == FREE_NIL) {
- if (b == get_zero_size_block(s)) {
- return __js_malloc(s, size);
- } else {
- JSMallocLargeBlockHeader *lb, *new_lb;
- lb = container_of(ptr, JSMallocLargeBlockHeader, header.user_data);
- #ifdef JS_MALLOC_USE_ITER
- list_del(&lb->link);
- #endif
- new_lb = s->mf.js_realloc(&s->malloc_state, lb, sizeof(JSMallocLargeBlockHeader) + size);
- if (!new_lb) {
- #ifdef JS_MALLOC_USE_ITER
- /* add again in the list */
- list_add_tail(&lb->link, &s->large_block_list);
- #endif
- return NULL;
- }
- new_lb->header.u.block_idx = FREE_NIL;
- new_lb->header.block_size_idx = 0xff; /* fail safe */
- #ifdef JS_MALLOC_USE_ITER
- list_add_tail(&new_lb->link, &s->large_block_list);
- #endif
- return new_lb->header.user_data;
- }
- } else {
- unsigned int block_size_idx = b->block_size_idx;
- size_t block_size = js_malloc_block_sizes[block_size_idx];
- size_t total_size, old_size;
- void *new_ptr;
- JSMallocBlockHeader *new_b;
- total_size = ((size + JS_MALLOC_ALIGN - 1) & ~(JS_MALLOC_ALIGN - 1)) +
- sizeof(JSMallocBlockHeader);
- if (total_size <= block_size)
- return ptr;
- new_ptr = __js_malloc(s, size);
- if (!new_ptr)
- return NULL;
- new_b = container_of(new_ptr, JSMallocBlockHeader, user_data);
- /* copy the GC data */
- new_b->gc_obj_type = b->gc_obj_type;
- new_b->mark = b->mark;
- new_b->ref_count = b->ref_count;
- /* copy the data */
- old_size = block_size - sizeof(JSMallocBlockHeader);
- if (size > old_size)
- size = old_size;
- memcpy(new_ptr, ptr, size);
- __js_free(s, ptr);
- return new_ptr;
- }
- }
- static size_t __js_malloc_usable_size(JSMallocContext *s, const char *ptr)
- {
- JSMallocBlockHeader *b;
- if (!ptr)
- return 0;
- b = container_of(ptr, JSMallocBlockHeader, user_data);
- if (b->u.block_idx == FREE_NIL) {
- if (b == get_zero_size_block(s)) {
- return 0;
- } else {
- JSMallocLargeBlockHeader *lb;
- size_t size;
- lb = container_of(ptr, JSMallocLargeBlockHeader, header.user_data);
- if (s->mf.js_malloc_usable_size) {
- size = s->mf.js_malloc_usable_size(lb);
- if (size != 0)
- size -= sizeof(JSMallocLargeBlockHeader);
- return size;
- } else {
- return 0;
- }
- }
- } else {
- size_t block_size = js_malloc_block_sizes[b->block_size_idx];
- return block_size - sizeof(*b);
- }
- }
- static __maybe_unused void js_malloc_dump_arenas(JSMallocContext *s)
- {
- struct list_head *el;
- int block_size_idx;
- printf("%20s %10s %10s\n", "PTR", "BLK_SIZE", "ALLOC");
- for(block_size_idx = 0; block_size_idx < JS_MALLOC_BLOCK_SIZE_COUNT; block_size_idx++) {
- int block_size = js_malloc_block_sizes[block_size_idx];
- list_for_each(el, &s->arena_list[block_size_idx]) {
- JSMallocArena *ar = list_entry(el, JSMallocArena, link);
- printf("%20p %10u %9.1f%%\n",
- ar, block_size,
- (double)ar->n_used_blocks / ar->n_blocks * 100);
- }
- }
- }
- #ifdef JS_MALLOC_USE_ITER
- typedef void JSMallocIterFunc(void *opaque, void *ptr);
- /* iterate thru allocated blocks. The allocated block list should not
- be modified while iterating. */
- static __maybe_unused void js_malloc_iter(JSMallocContext *s, JSMallocIterFunc *iter_func, void *iter_opaque)
- {
- struct list_head *el;
- int block_size_idx;
- int i, j, n_words;
- uint32_t bmp;
-
- for(block_size_idx = 0; block_size_idx < JS_MALLOC_BLOCK_SIZE_COUNT; block_size_idx++) {
- unsigned int block_size = js_malloc_block_sizes[block_size_idx];
- list_for_each(el, &s->arena_list[block_size_idx]) {
- JSMallocArena *ar = list_entry(el, JSMallocArena, link);
- n_words = (ar->n_blocks + 31) / 32;
- for(i = 0; i < n_words; i++) {
- bmp = ar->bitmap[i];
- while (bmp != 0) {
- j = ctz32(bmp);
- bmp &= ~(1 << j);
- iter_func(iter_opaque, get_arena_block(ar, i * 32+ j, block_size));
- }
- }
- }
- }
- list_for_each(el, &s->large_block_list) {
- JSMallocLargeBlockHeader *lb = list_entry(el, JSMallocLargeBlockHeader, link);
- iter_func(iter_opaque, lb->header.user_data);
- }
- }
- #endif
- /* end JS malloc */
- static void js_trigger_gc(JSRuntime *rt, size_t size)
- {
- BOOL force_gc;
- #ifdef FORCE_GC_AT_MALLOC
- force_gc = TRUE;
- #else
- force_gc = ((rt->malloc_ctx.malloc_state.malloc_size + size) >
- rt->malloc_gc_threshold);
- #endif
- if (force_gc) {
- #ifdef DUMP_GC
- printf("GC: size=%" PRIu64 "\n",
- (uint64_t)rt->malloc_ctx.malloc_state.malloc_size);
- #endif
- JS_RunGC(rt);
- rt->malloc_gc_threshold = rt->malloc_ctx.malloc_state.malloc_size +
- (rt->malloc_ctx.malloc_state.malloc_size >> 1);
- }
- }
- void *js_malloc_rt(JSRuntime *rt, size_t size)
- {
- return __js_malloc(&rt->malloc_ctx, size);
- }
- void js_free_rt(JSRuntime *rt, void *ptr)
- {
- __js_free(&rt->malloc_ctx, ptr);
- }
- void *js_realloc_rt(JSRuntime *rt, void *ptr, size_t size)
- {
- return __js_realloc(&rt->malloc_ctx, ptr, size);
- }
- size_t js_malloc_usable_size_rt(JSRuntime *rt, const void *ptr)
- {
- return __js_malloc_usable_size(&rt->malloc_ctx, ptr);
- }
- void *js_mallocz_rt(JSRuntime *rt, size_t size)
- {
- void *ptr;
- ptr = js_malloc_rt(rt, size);
- if (unlikely(!ptr))
- return NULL;
- return memset(ptr, 0, size);
- }
- /* Throw out of memory in case of error */
- void *js_malloc(JSContext *ctx, size_t size)
- {
- void *ptr;
- ptr = js_malloc_rt(ctx->rt, size);
- if (unlikely(!ptr)) {
- JS_ThrowOutOfMemory(ctx);
- return NULL;
- }
- return ptr;
- }
- /* Throw out of memory in case of error */
- void *js_mallocz(JSContext *ctx, size_t size)
- {
- void *ptr;
- ptr = js_mallocz_rt(ctx->rt, size);
- if (unlikely(!ptr)) {
- JS_ThrowOutOfMemory(ctx);
- return NULL;
- }
- return ptr;
- }
- void js_free(JSContext *ctx, void *ptr)
- {
- js_free_rt(ctx->rt, ptr);
- }
- /* Throw out of memory in case of error */
- void *js_realloc(JSContext *ctx, void *ptr, size_t size)
- {
- void *ret;
- ret = js_realloc_rt(ctx->rt, ptr, size);
- if (unlikely(!ret && size != 0)) {
- JS_ThrowOutOfMemory(ctx);
- return NULL;
- }
- return ret;
- }
- /* store extra allocated size in *pslack if successful */
- void *js_realloc2(JSContext *ctx, void *ptr, size_t size, size_t *pslack)
- {
- void *ret;
- ret = js_realloc_rt(ctx->rt, ptr, size);
- if (unlikely(!ret && size != 0)) {
- JS_ThrowOutOfMemory(ctx);
- return NULL;
- }
- if (pslack) {
- size_t new_size = js_malloc_usable_size_rt(ctx->rt, ret);
- *pslack = (new_size > size) ? new_size - size : 0;
- }
- return ret;
- }
- size_t js_malloc_usable_size(JSContext *ctx, const void *ptr)
- {
- return js_malloc_usable_size_rt(ctx->rt, ptr);
- }
- /* Throw out of memory exception in case of error */
- char *js_strndup(JSContext *ctx, const char *s, size_t n)
- {
- char *ptr;
- ptr = js_malloc(ctx, n + 1);
- if (ptr) {
- memcpy(ptr, s, n);
- ptr[n] = '\0';
- }
- return ptr;
- }
- char *js_strdup(JSContext *ctx, const char *str)
- {
- return js_strndup(ctx, str, strlen(str));
- }
- static no_inline int js_realloc_array(JSContext *ctx, void **parray,
- int elem_size, int *psize, int req_size)
- {
- int new_size;
- size_t slack;
- void *new_array;
- /* XXX: potential arithmetic overflow */
- new_size = max_int(req_size, *psize * 3 / 2);
- new_array = js_realloc2(ctx, *parray, new_size * elem_size, &slack);
- if (!new_array)
- return -1;
- new_size += slack / elem_size;
- *psize = new_size;
- *parray = new_array;
- return 0;
- }
- /* resize the array and update its size if req_size > *psize */
- static inline int js_resize_array(JSContext *ctx, void **parray, int elem_size,
- int *psize, int req_size)
- {
- if (unlikely(req_size > *psize))
- return js_realloc_array(ctx, parray, elem_size, psize, req_size);
- else
- return 0;
- }
- static inline void js_dbuf_init(JSContext *ctx, DynBuf *s)
- {
- dbuf_init2(s, ctx->rt, (DynBufReallocFunc *)js_realloc_rt);
- }
- static void *js_realloc_bytecode_rt(void *opaque, void *ptr, size_t size)
- {
- JSRuntime *rt = opaque;
- if (size > (INT32_MAX / 2)) {
- /* the bytecode cannot be larger than 2G. Leave some slack to
- avoid some overflows. */
- return NULL;
- } else {
- return js_realloc_rt(rt, ptr, size);
- }
- }
- static inline void js_dbuf_bytecode_init(JSContext *ctx, DynBuf *s)
- {
- dbuf_init2(s, ctx->rt, js_realloc_bytecode_rt);
- }
- static inline int is_digit(int c) {
- return c >= '0' && c <= '9';
- }
- static inline int string_get(const JSString *p, int idx) {
- return p->is_wide_char ? p->u.str16[idx] : p->u.str8[idx];
- }
- typedef struct JSClassShortDef {
- JSAtom class_name;
- JSClassFinalizer *finalizer;
- JSClassGCMark *gc_mark;
- } JSClassShortDef;
- static JSClassShortDef const js_std_class_def[] = {
- { JS_ATOM_Object, NULL, NULL }, /* JS_CLASS_OBJECT */
- { JS_ATOM_Array, js_array_finalizer, js_array_mark }, /* JS_CLASS_ARRAY */
- { JS_ATOM_Error, NULL, NULL }, /* JS_CLASS_ERROR */
- { JS_ATOM_Number, js_object_data_finalizer, js_object_data_mark }, /* JS_CLASS_NUMBER */
- { JS_ATOM_String, js_object_data_finalizer, js_object_data_mark }, /* JS_CLASS_STRING */
- { JS_ATOM_Boolean, js_object_data_finalizer, js_object_data_mark }, /* JS_CLASS_BOOLEAN */
- { JS_ATOM_Symbol, js_object_data_finalizer, js_object_data_mark }, /* JS_CLASS_SYMBOL */
- { JS_ATOM_Arguments, js_array_finalizer, js_array_mark }, /* JS_CLASS_ARGUMENTS */
- { JS_ATOM_Arguments, js_mapped_arguments_finalizer, js_mapped_arguments_mark }, /* JS_CLASS_MAPPED_ARGUMENTS */
- { JS_ATOM_Date, js_object_data_finalizer, js_object_data_mark }, /* JS_CLASS_DATE */
- { JS_ATOM_Object, NULL, NULL }, /* JS_CLASS_MODULE_NS */
- { JS_ATOM_Function, js_c_function_finalizer, js_c_function_mark }, /* JS_CLASS_C_FUNCTION */
- { JS_ATOM_Function, js_bytecode_function_finalizer, js_bytecode_function_mark }, /* JS_CLASS_BYTECODE_FUNCTION */
- { JS_ATOM_Function, js_bound_function_finalizer, js_bound_function_mark }, /* JS_CLASS_BOUND_FUNCTION */
- { JS_ATOM_Function, js_c_function_data_finalizer, js_c_function_data_mark }, /* JS_CLASS_C_FUNCTION_DATA */
- { JS_ATOM_GeneratorFunction, js_bytecode_function_finalizer, js_bytecode_function_mark }, /* JS_CLASS_GENERATOR_FUNCTION */
- { JS_ATOM_ForInIterator, js_for_in_iterator_finalizer, js_for_in_iterator_mark }, /* JS_CLASS_FOR_IN_ITERATOR */
- { JS_ATOM_RegExp, js_regexp_finalizer, NULL }, /* JS_CLASS_REGEXP */
- { JS_ATOM_ArrayBuffer, js_array_buffer_finalizer, NULL }, /* JS_CLASS_ARRAY_BUFFER */
- { JS_ATOM_SharedArrayBuffer, js_array_buffer_finalizer, NULL }, /* JS_CLASS_SHARED_ARRAY_BUFFER */
- { JS_ATOM_Uint8ClampedArray, js_typed_array_finalizer, js_typed_array_mark }, /* JS_CLASS_UINT8C_ARRAY */
- { JS_ATOM_Int8Array, js_typed_array_finalizer, js_typed_array_mark }, /* JS_CLASS_INT8_ARRAY */
- { JS_ATOM_Uint8Array, js_typed_array_finalizer, js_typed_array_mark }, /* JS_CLASS_UINT8_ARRAY */
- { JS_ATOM_Int16Array, js_typed_array_finalizer, js_typed_array_mark }, /* JS_CLASS_INT16_ARRAY */
- { JS_ATOM_Uint16Array, js_typed_array_finalizer, js_typed_array_mark }, /* JS_CLASS_UINT16_ARRAY */
- { JS_ATOM_Int32Array, js_typed_array_finalizer, js_typed_array_mark }, /* JS_CLASS_INT32_ARRAY */
- { JS_ATOM_Uint32Array, js_typed_array_finalizer, js_typed_array_mark }, /* JS_CLASS_UINT32_ARRAY */
- { JS_ATOM_BigInt64Array, js_typed_array_finalizer, js_typed_array_mark }, /* JS_CLASS_BIG_INT64_ARRAY */
- { JS_ATOM_BigUint64Array, js_typed_array_finalizer, js_typed_array_mark }, /* JS_CLASS_BIG_UINT64_ARRAY */
- { JS_ATOM_Float16Array, js_typed_array_finalizer, js_typed_array_mark }, /* JS_CLASS_FLOAT16_ARRAY */
- { JS_ATOM_Float32Array, js_typed_array_finalizer, js_typed_array_mark }, /* JS_CLASS_FLOAT32_ARRAY */
- { JS_ATOM_Float64Array, js_typed_array_finalizer, js_typed_array_mark }, /* JS_CLASS_FLOAT64_ARRAY */
- { JS_ATOM_DataView, js_typed_array_finalizer, js_typed_array_mark }, /* JS_CLASS_DATAVIEW */
- { JS_ATOM_BigInt, js_object_data_finalizer, js_object_data_mark }, /* JS_CLASS_BIG_INT */
- { JS_ATOM_Map, js_map_finalizer, js_map_mark }, /* JS_CLASS_MAP */
- { JS_ATOM_Set, js_map_finalizer, js_map_mark }, /* JS_CLASS_SET */
- { JS_ATOM_WeakMap, js_map_finalizer, js_map_mark }, /* JS_CLASS_WEAKMAP */
- { JS_ATOM_WeakSet, js_map_finalizer, js_map_mark }, /* JS_CLASS_WEAKSET */
- { JS_ATOM_Iterator, NULL, NULL }, /* JS_CLASS_ITERATOR */
- { JS_ATOM_IteratorConcat, js_iterator_concat_finalizer, js_iterator_concat_mark }, /* JS_CLASS_ITERATOR_CONCAT */
- { JS_ATOM_IteratorHelper, js_iterator_helper_finalizer, js_iterator_helper_mark }, /* JS_CLASS_ITERATOR_HELPER */
- { JS_ATOM_IteratorWrap, js_iterator_wrap_finalizer, js_iterator_wrap_mark }, /* JS_CLASS_ITERATOR_WRAP */
- { JS_ATOM_Map_Iterator, js_map_iterator_finalizer, js_map_iterator_mark }, /* JS_CLASS_MAP_ITERATOR */
- { JS_ATOM_Set_Iterator, js_map_iterator_finalizer, js_map_iterator_mark }, /* JS_CLASS_SET_ITERATOR */
- { JS_ATOM_Array_Iterator, js_array_iterator_finalizer, js_array_iterator_mark }, /* JS_CLASS_ARRAY_ITERATOR */
- { JS_ATOM_String_Iterator, js_array_iterator_finalizer, js_array_iterator_mark }, /* JS_CLASS_STRING_ITERATOR */
- { JS_ATOM_RegExp_String_Iterator, js_regexp_string_iterator_finalizer, js_regexp_string_iterator_mark }, /* JS_CLASS_REGEXP_STRING_ITERATOR */
- { JS_ATOM_Generator, js_generator_finalizer, js_generator_mark }, /* JS_CLASS_GENERATOR */
- { JS_ATOM_Object, js_global_object_finalizer, js_global_object_mark }, /* JS_CLASS_GLOBAL_OBJECT */
- { JS_ATOM_Object, NULL, NULL }, /* JS_CLASS_RAWJSON */
- };
- static int init_class_range(JSRuntime *rt, JSClassShortDef const *tab,
- int start, int count)
- {
- JSClassDef cm_s, *cm = &cm_s;
- int i, class_id;
- for(i = 0; i < count; i++) {
- class_id = i + start;
- memset(cm, 0, sizeof(*cm));
- cm->finalizer = tab[i].finalizer;
- cm->gc_mark = tab[i].gc_mark;
- if (JS_NewClass1(rt, class_id, cm, tab[i].class_name) < 0)
- return -1;
- }
- return 0;
- }
- #if !defined(CONFIG_STACK_CHECK)
- /* no stack limitation */
- static inline uintptr_t js_get_stack_pointer(void)
- {
- return 0;
- }
- static inline BOOL js_check_stack_overflow(JSRuntime *rt, size_t alloca_size)
- {
- return FALSE;
- }
- #else
- /* Note: OS and CPU dependent */
- static inline uintptr_t js_get_stack_pointer(void)
- {
- return (uintptr_t)__builtin_frame_address(0);
- }
- static inline BOOL js_check_stack_overflow(JSRuntime *rt, size_t alloca_size)
- {
- uintptr_t sp;
- sp = js_get_stack_pointer() - alloca_size;
- return unlikely(sp < rt->stack_limit);
- }
- #endif
- JSRuntime *JS_NewRuntime2(const JSMallocFunctions *mf, void *opaque)
- {
- JSRuntime *rt;
- JSMallocState ms;
- memset(&ms, 0, sizeof(ms));
- ms.opaque = opaque;
- ms.malloc_limit = -1;
- rt = mf->js_malloc(&ms, sizeof(JSRuntime));
- if (!rt)
- return NULL;
- memset(rt, 0, sizeof(*rt));
- js_malloc_init(&rt->malloc_ctx);
- rt->malloc_ctx.mf = *mf;
- rt->malloc_ctx.malloc_state = ms;
- rt->malloc_gc_threshold = 256 * 1024;
- init_list_head(&rt->context_list);
- init_list_head(&rt->gc_obj_list);
- init_list_head(&rt->gc_zero_ref_count_list);
- rt->gc_phase = JS_GC_PHASE_NONE;
- init_list_head(&rt->weakref_list);
- #ifdef DUMP_LEAKS
- init_list_head(&rt->string_list);
- #endif
- init_list_head(&rt->job_list);
- if (JS_InitAtoms(rt))
- goto fail;
- /* create the object, array and function classes */
- if (init_class_range(rt, js_std_class_def, JS_CLASS_OBJECT,
- countof(js_std_class_def)) < 0)
- goto fail;
- rt->class_array[JS_CLASS_ARGUMENTS].exotic = &js_arguments_exotic_methods;
- rt->class_array[JS_CLASS_MAPPED_ARGUMENTS].exotic = &js_arguments_exotic_methods;
- rt->class_array[JS_CLASS_STRING].exotic = &js_string_exotic_methods;
- rt->class_array[JS_CLASS_MODULE_NS].exotic = &js_module_ns_exotic_methods;
- rt->class_array[JS_CLASS_C_FUNCTION].call = js_call_c_function;
- rt->class_array[JS_CLASS_C_FUNCTION_DATA].call = js_c_function_data_call;
- rt->class_array[JS_CLASS_BOUND_FUNCTION].call = js_call_bound_function;
- rt->class_array[JS_CLASS_GENERATOR_FUNCTION].call = js_generator_function_call;
- if (init_shape_hash(rt))
- goto fail;
- rt->stack_size = JS_DEFAULT_STACK_SIZE;
- JS_UpdateStackTop(rt);
- rt->current_exception = JS_UNINITIALIZED;
- return rt;
- fail:
- JS_FreeRuntime(rt);
- return NULL;
- }
- void *JS_GetRuntimeOpaque(JSRuntime *rt)
- {
- return rt->user_opaque;
- }
- void JS_SetRuntimeOpaque(JSRuntime *rt, void *opaque)
- {
- rt->user_opaque = opaque;
- }
- /* default memory allocation functions with memory limitation */
- static size_t js_def_malloc_usable_size(const void *ptr)
- {
- #if defined(__APPLE__)
- return malloc_size(ptr);
- #elif defined(_WIN32)
- return _msize((void *)ptr);
- #elif defined(__EMSCRIPTEN__)
- return 0;
- #elif defined(__linux__) || defined(__GLIBC__)
- return malloc_usable_size((void *)ptr);
- #else
- /* change this to `return 0;` if compilation fails */
- return malloc_usable_size((void *)ptr);
- #endif
- }
- static void *js_def_malloc(JSMallocState *s, size_t size)
- {
- void *ptr;
- /* Do not allocate zero bytes: behavior is platform dependent */
- assert(size != 0);
- if (unlikely(s->malloc_size + size > s->malloc_limit))
- return NULL;
- ptr = malloc(size);
- if (!ptr)
- return NULL;
- s->malloc_count++;
- s->malloc_size += js_def_malloc_usable_size(ptr) + MALLOC_OVERHEAD;
- return ptr;
- }
- static void js_def_free(JSMallocState *s, void *ptr)
- {
- if (!ptr)
- return;
- s->malloc_count--;
- s->malloc_size -= js_def_malloc_usable_size(ptr) + MALLOC_OVERHEAD;
- free(ptr);
- }
- static void *js_def_realloc(JSMallocState *s, void *ptr, size_t size)
- {
- size_t old_size;
- if (!ptr) {
- if (size == 0)
- return NULL;
- return js_def_malloc(s, size);
- }
- old_size = js_def_malloc_usable_size(ptr);
- if (size == 0) {
- s->malloc_count--;
- s->malloc_size -= old_size + MALLOC_OVERHEAD;
- free(ptr);
- return NULL;
- }
- if (s->malloc_size + size - old_size > s->malloc_limit)
- return NULL;
- ptr = realloc(ptr, size);
- if (!ptr)
- return NULL;
- s->malloc_size += js_def_malloc_usable_size(ptr) - old_size;
- return ptr;
- }
- static const JSMallocFunctions def_malloc_funcs = {
- js_def_malloc,
- js_def_free,
- js_def_realloc,
- js_def_malloc_usable_size,
- };
- JSRuntime *JS_NewRuntime(void)
- {
- return JS_NewRuntime2(&def_malloc_funcs, NULL);
- }
- void JS_SetMemoryLimit(JSRuntime *rt, size_t limit)
- {
- rt->malloc_ctx.malloc_state.malloc_limit = limit;
- }
- /* use -1 to disable automatic GC */
- void JS_SetGCThreshold(JSRuntime *rt, size_t gc_threshold)
- {
- rt->malloc_gc_threshold = gc_threshold;
- }
- #define malloc(s) malloc_is_forbidden(s)
- #define free(p) free_is_forbidden(p)
- #define realloc(p,s) realloc_is_forbidden(p,s)
- void JS_SetInterruptHandler(JSRuntime *rt, JSInterruptHandler *cb, void *opaque)
- {
- rt->interrupt_handler = cb;
- rt->interrupt_opaque = opaque;
- }
- void JS_SetCanBlock(JSRuntime *rt, BOOL can_block)
- {
- rt->can_block = can_block;
- }
- void JS_SetSharedArrayBufferFunctions(JSRuntime *rt,
- const JSSharedArrayBufferFunctions *sf)
- {
- rt->sab_funcs = *sf;
- }
- void JS_SetStripInfo(JSRuntime *rt, int flags)
- {
- rt->strip_flags = flags;
- }
- int JS_GetStripInfo(JSRuntime *rt)
- {
- return rt->strip_flags;
- }
- static int JS_EnqueueJob2(JSContext *ctx, JSJobFunc *job_func,
- int argc, JSValueConst *argv, BOOL no_exception)
- {
- JSRuntime *rt = ctx->rt;
- JSJobEntry *e;
- int i;
- if (no_exception)
- e = js_malloc_rt(ctx->rt, sizeof(*e) + argc * sizeof(JSValue));
- else
- e = js_malloc(ctx, sizeof(*e) + argc * sizeof(JSValue));
- if (!e)
- return -1;
- e->realm = JS_DupContext(ctx);
- e->job_func = job_func;
- e->argc = argc;
- for(i = 0; i < argc; i++) {
- e->argv[i] = JS_DupValue(ctx, argv[i]);
- }
- list_add_tail(&e->link, &rt->job_list);
- return 0;
- }
- /* return 0 if OK, < 0 if exception */
- int JS_EnqueueJob(JSContext *ctx, JSJobFunc *job_func,
- int argc, JSValueConst *argv)
- {
- return JS_EnqueueJob2(ctx, job_func, argc, argv, FALSE);
- }
- BOOL JS_IsJobPending(JSRuntime *rt)
- {
- return !list_empty(&rt->job_list);
- }
- /* return < 0 if exception, 0 if no job pending, 1 if a job was
- executed successfully. The context of the job is stored in '*pctx'
- if pctx != NULL. It may be NULL if the context was already
- destroyed or if no job was pending. The 'pctx' parameter is now
- absolete. */
- int JS_ExecutePendingJob(JSRuntime *rt, JSContext **pctx)
- {
- JSContext *ctx;
- JSJobEntry *e;
- JSValue res;
- int i, ret;
- if (list_empty(&rt->job_list)) {
- if (pctx)
- *pctx = NULL;
- return 0;
- }
- /* get the first pending job and execute it */
- e = list_entry(rt->job_list.next, JSJobEntry, link);
- list_del(&e->link);
- ctx = e->realm;
- res = e->job_func(ctx, e->argc, (JSValueConst *)e->argv);
- for(i = 0; i < e->argc; i++)
- JS_FreeValue(ctx, e->argv[i]);
- if (JS_IsException(res))
- ret = -1;
- else
- ret = 1;
- JS_FreeValue(ctx, res);
- js_free(ctx, e);
- if (pctx) {
- if (js_rc(ctx)->ref_count > 1)
- *pctx = ctx;
- else
- *pctx = NULL;
- }
- JS_FreeContext(ctx);
- return ret;
- }
- static inline uint32_t atom_get_free(const JSAtomStruct *p)
- {
- return (uintptr_t)p >> 1;
- }
- static inline BOOL atom_is_free(const JSAtomStruct *p)
- {
- return (uintptr_t)p & 1;
- }
- static inline JSAtomStruct *atom_set_free(uint32_t v)
- {
- return (JSAtomStruct *)(((uintptr_t)v << 1) | 1);
- }
- /* Note: the string contents are uninitialized */
- static JSString *js_alloc_string_rt(JSRuntime *rt, int max_len, int is_wide_char)
- {
- JSString *str;
- str = js_malloc_rt(rt, sizeof(JSString) + (max_len << is_wide_char) + 1 - is_wide_char);
- if (unlikely(!str))
- return NULL;
- js_rc(str)->ref_count = 1;
- str->is_wide_char = is_wide_char;
- str->len = max_len;
- str->atom_type = 0;
- str->hash = 0; /* optional but costless */
- str->hash_next = 0; /* optional */
- #ifdef DUMP_LEAKS
- list_add_tail(&str->link, &rt->string_list);
- #endif
- return str;
- }
- static JSString *js_alloc_string(JSContext *ctx, int max_len, int is_wide_char)
- {
- JSString *p;
- p = js_alloc_string_rt(ctx->rt, max_len, is_wide_char);
- if (unlikely(!p)) {
- JS_ThrowOutOfMemory(ctx);
- return NULL;
- }
- return p;
- }
- /* same as JS_FreeValueRT() but faster */
- static inline void js_free_string(JSRuntime *rt, JSString *str)
- {
- if (--js_rc(str)->ref_count <= 0) {
- if (str->atom_type) {
- JS_FreeAtomStruct(rt, str);
- } else {
- #ifdef DUMP_LEAKS
- list_del(&str->link);
- #endif
- js_free_rt(rt, str);
- }
- }
- }
- void JS_SetRuntimeInfo(JSRuntime *rt, const char *s)
- {
- if (rt)
- rt->rt_info = s;
- }
- void JS_FreeRuntime(JSRuntime *rt)
- {
- struct list_head *el, *el1;
- int i;
- JS_FreeValueRT(rt, rt->current_exception);
- list_for_each_safe(el, el1, &rt->job_list) {
- JSJobEntry *e = list_entry(el, JSJobEntry, link);
- for(i = 0; i < e->argc; i++)
- JS_FreeValueRT(rt, e->argv[i]);
- JS_FreeContext(e->realm);
- js_free_rt(rt, e);
- }
- init_list_head(&rt->job_list);
- /* don't remove the weak objects to avoid create new jobs with
- FinalizationRegistry */
- JS_RunGCInternal(rt, FALSE);
- #ifdef DUMP_LEAKS
- /* leaking objects */
- {
- BOOL header_done;
- JSGCObjectHeader *p;
- int count;
- /* remove the internal refcounts to display only the object
- referenced externally */
- list_for_each(el, &rt->gc_obj_list) {
- p = list_entry(el, JSGCObjectHeader, link);
- js_rc(p)->mark = 0;
- }
- gc_decref(rt);
- header_done = FALSE;
- list_for_each(el, &rt->gc_obj_list) {
- p = list_entry(el, JSGCObjectHeader, link);
- if (js_rc(p)->ref_count != 0) {
- if (!header_done) {
- printf("Object leaks:\n");
- JS_DumpObjectHeader(rt);
- header_done = TRUE;
- }
- JS_DumpGCObject(rt, p);
- }
- }
- count = 0;
- list_for_each(el, &rt->gc_obj_list) {
- p = list_entry(el, JSGCObjectHeader, link);
- if (js_rc(p)->ref_count == 0) {
- count++;
- }
- }
- if (count != 0)
- printf("Secondary object leaks: %d\n", count);
- }
- #endif
- assert(list_empty(&rt->gc_obj_list));
- assert(list_empty(&rt->weakref_list));
- /* free the classes */
- for(i = 0; i < rt->class_count; i++) {
- JSClass *cl = &rt->class_array[i];
- if (cl->class_id != 0) {
- JS_FreeAtomRT(rt, cl->class_name);
- }
- }
- js_free_rt(rt, rt->class_array);
- #ifdef DUMP_LEAKS
- /* only the atoms defined in JS_InitAtoms() should be left */
- {
- BOOL header_done = FALSE;
- for(i = 0; i < rt->atom_size; i++) {
- JSAtomStruct *p = rt->atom_array[i];
- if (!atom_is_free(p) /* && p->str*/) {
- if (i >= JS_ATOM_END || js_rc(p)->ref_count != 1) {
- if (!header_done) {
- header_done = TRUE;
- if (rt->rt_info) {
- printf("%s:1: atom leakage:", rt->rt_info);
- } else {
- printf("Atom leaks:\n"
- " %6s %6s %s\n",
- "ID", "REFCNT", "NAME");
- }
- }
- if (rt->rt_info) {
- printf(" ");
- } else {
- printf(" %6u %6u ", i, js_rc(p)->ref_count);
- }
- switch (p->atom_type) {
- case JS_ATOM_TYPE_STRING:
- JS_DumpString(rt, p);
- break;
- case JS_ATOM_TYPE_GLOBAL_SYMBOL:
- printf("Symbol.for(");
- JS_DumpString(rt, p);
- printf(")");
- break;
- case JS_ATOM_TYPE_SYMBOL:
- if (p->hash != JS_ATOM_HASH_PRIVATE) {
- printf("Symbol(");
- JS_DumpString(rt, p);
- printf(")");
- } else {
- printf("Private(");
- JS_DumpString(rt, p);
- printf(")");
- }
- break;
- }
- if (rt->rt_info) {
- printf(":%u", js_rc(p)->ref_count);
- } else {
- printf("\n");
- }
- }
- }
- }
- if (rt->rt_info && header_done)
- printf("\n");
- }
- #endif
- /* free the atoms */
- for(i = 0; i < rt->atom_size; i++) {
- JSAtomStruct *p = rt->atom_array[i];
- if (!atom_is_free(p)) {
- #ifdef DUMP_LEAKS
- list_del(&p->link);
- #endif
- js_free_rt(rt, p);
- }
- }
- js_free_rt(rt, rt->atom_array);
- js_free_rt(rt, rt->atom_hash);
- js_free_rt(rt, rt->shape_hash);
- #ifdef DUMP_LEAKS
- if (!list_empty(&rt->string_list)) {
- if (rt->rt_info) {
- printf("%s:1: string leakage:", rt->rt_info);
- } else {
- printf("String leaks:\n"
- " %6s %s\n",
- "REFCNT", "VALUE");
- }
- list_for_each_safe(el, el1, &rt->string_list) {
- JSString *str = list_entry(el, JSString, link);
- if (rt->rt_info) {
- printf(" ");
- } else {
- printf(" %6u ", js_rc(str)->ref_count);
- }
- JS_DumpString(rt, str);
- if (rt->rt_info) {
- printf(":%u", js_rc(str)->ref_count);
- } else {
- printf("\n");
- }
- list_del(&str->link);
- js_free_rt(rt, str);
- }
- if (rt->rt_info)
- printf("\n");
- }
- {
- JSMallocState *s = &rt->malloc_ctx.malloc_state;
- if (s->malloc_count > 1) {
- if (rt->rt_info)
- printf("%s:1: ", rt->rt_info);
- printf("Memory leak: %"PRIu64" bytes lost in %"PRIu64" block%s\n",
- (uint64_t)(s->malloc_size - sizeof(JSRuntime)),
- (uint64_t)(s->malloc_count - 1), &"s"[s->malloc_count == 2]);
- }
- }
- #endif
- {
- JSMallocState ms = rt->malloc_ctx.malloc_state;
- rt->malloc_ctx.mf.js_free(&ms, rt);
- }
- }
- JSContext *JS_NewContextRaw(JSRuntime *rt)
- {
- JSContext *ctx;
- int i;
- ctx = js_mallocz_rt(rt, sizeof(JSContext));
- if (!ctx)
- return NULL;
- js_rc(ctx)->ref_count = 1;
- add_gc_object(rt, &ctx->header, JS_GC_OBJ_TYPE_JS_CONTEXT);
- ctx->class_proto = js_malloc_rt(rt, sizeof(ctx->class_proto[0]) *
- rt->class_count);
- if (!ctx->class_proto) {
- js_free_rt(rt, ctx);
- return NULL;
- }
- ctx->rt = rt;
- list_add_tail(&ctx->link, &rt->context_list);
- for(i = 0; i < rt->class_count; i++)
- ctx->class_proto[i] = JS_NULL;
- ctx->array_ctor = JS_NULL;
- ctx->iterator_ctor = JS_NULL;
- ctx->regexp_ctor = JS_NULL;
- ctx->promise_ctor = JS_NULL;
- init_list_head(&ctx->loaded_modules);
- if (JS_AddIntrinsicBasicObjects(ctx)) {
- JS_FreeContext(ctx);
- return NULL;
- }
- return ctx;
- }
- JSContext *JS_NewContext(JSRuntime *rt)
- {
- JSContext *ctx;
- ctx = JS_NewContextRaw(rt);
- if (!ctx)
- return NULL;
- if (JS_AddIntrinsicBaseObjects(ctx) ||
- JS_AddIntrinsicDate(ctx) ||
- JS_AddIntrinsicEval(ctx) ||
- JS_AddIntrinsicStringNormalize(ctx) ||
- JS_AddIntrinsicRegExp(ctx) ||
- JS_AddIntrinsicJSON(ctx) ||
- JS_AddIntrinsicProxy(ctx) ||
- JS_AddIntrinsicMapSet(ctx) ||
- JS_AddIntrinsicTypedArrays(ctx) ||
- JS_AddIntrinsicPromise(ctx) ||
- JS_AddIntrinsicWeakRef(ctx)) {
- JS_FreeContext(ctx);
- return NULL;
- }
- return ctx;
- }
- void *JS_GetContextOpaque(JSContext *ctx)
- {
- return ctx->user_opaque;
- }
- void JS_SetContextOpaque(JSContext *ctx, void *opaque)
- {
- ctx->user_opaque = opaque;
- }
- /* set the new value and free the old value after (freeing the value
- can reallocate the object data) */
- static inline void set_value(JSContext *ctx, JSValue *pval, JSValue new_val)
- {
- JSValue old_val;
- old_val = *pval;
- *pval = new_val;
- JS_FreeValue(ctx, old_val);
- }
- void JS_SetClassProto(JSContext *ctx, JSClassID class_id, JSValue obj)
- {
- JSRuntime *rt = ctx->rt;
- assert(class_id < rt->class_count);
- set_value(ctx, &ctx->class_proto[class_id], obj);
- }
- JSValue JS_GetClassProto(JSContext *ctx, JSClassID class_id)
- {
- JSRuntime *rt = ctx->rt;
- assert(class_id < rt->class_count);
- return JS_DupValue(ctx, ctx->class_proto[class_id]);
- }
- typedef enum JSFreeModuleEnum {
- JS_FREE_MODULE_ALL,
- JS_FREE_MODULE_NOT_RESOLVED,
- } JSFreeModuleEnum;
- /* XXX: would be more efficient with separate module lists */
- static void js_free_modules(JSContext *ctx, JSFreeModuleEnum flag)
- {
- struct list_head *el, *el1;
- list_for_each_safe(el, el1, &ctx->loaded_modules) {
- JSModuleDef *m = list_entry(el, JSModuleDef, link);
- if (flag == JS_FREE_MODULE_ALL ||
- (flag == JS_FREE_MODULE_NOT_RESOLVED && !m->resolved)) {
- /* warning: the module may be referenced elsewhere. It
- could be simpler to use an array instead of a list for
- 'ctx->loaded_modules' */
- list_del(&m->link);
- m->link.prev = NULL;
- m->link.next = NULL;
- JS_FreeValue(ctx, JS_MKPTR(JS_TAG_MODULE, m));
- }
- }
- }
- JSContext *JS_DupContext(JSContext *ctx)
- {
- js_rc(ctx)->ref_count++;
- return ctx;
- }
- /* used by the GC */
- static void JS_MarkContext(JSRuntime *rt, JSContext *ctx,
- JS_MarkFunc *mark_func)
- {
- int i;
- struct list_head *el;
- list_for_each(el, &ctx->loaded_modules) {
- JSModuleDef *m = list_entry(el, JSModuleDef, link);
- JS_MarkValue(rt, JS_MKPTR(JS_TAG_MODULE, m), mark_func);
- }
- JS_MarkValue(rt, ctx->global_obj, mark_func);
- JS_MarkValue(rt, ctx->global_var_obj, mark_func);
- JS_MarkValue(rt, ctx->throw_type_error, mark_func);
- JS_MarkValue(rt, ctx->eval_obj, mark_func);
- JS_MarkValue(rt, ctx->array_proto_values, mark_func);
- for(i = 0; i < JS_NATIVE_ERROR_COUNT; i++) {
- JS_MarkValue(rt, ctx->native_error_proto[i], mark_func);
- }
- for(i = 0; i < rt->class_count; i++) {
- JS_MarkValue(rt, ctx->class_proto[i], mark_func);
- }
- JS_MarkValue(rt, ctx->iterator_ctor, mark_func);
- JS_MarkValue(rt, ctx->async_iterator_proto, mark_func);
- JS_MarkValue(rt, ctx->promise_ctor, mark_func);
- JS_MarkValue(rt, ctx->array_ctor, mark_func);
- JS_MarkValue(rt, ctx->regexp_ctor, mark_func);
- JS_MarkValue(rt, ctx->function_ctor, mark_func);
- JS_MarkValue(rt, ctx->function_proto, mark_func);
- if (ctx->array_shape)
- mark_func(rt, &ctx->array_shape->header);
- if (ctx->arguments_shape)
- mark_func(rt, &ctx->arguments_shape->header);
- if (ctx->mapped_arguments_shape)
- mark_func(rt, &ctx->mapped_arguments_shape->header);
- if (ctx->regexp_shape)
- mark_func(rt, &ctx->regexp_shape->header);
- if (ctx->regexp_result_shape)
- mark_func(rt, &ctx->regexp_result_shape->header);
- }
- void JS_FreeContext(JSContext *ctx)
- {
- JSRuntime *rt = ctx->rt;
- int i;
- if (--js_rc(ctx)->ref_count > 0)
- return;
- assert(js_rc(ctx)->ref_count == 0);
- #ifdef DUMP_ATOMS
- JS_DumpAtoms(ctx->rt);
- #endif
- #ifdef DUMP_SHAPES
- JS_DumpShapes(ctx->rt);
- #endif
- #ifdef DUMP_OBJECTS
- {
- struct list_head *el;
- JSGCObjectHeader *p;
- printf("JSObjects: {\n");
- JS_DumpObjectHeader(ctx->rt);
- list_for_each(el, &rt->gc_obj_list) {
- p = list_entry(el, JSGCObjectHeader, link);
- JS_DumpGCObject(rt, p);
- }
- printf("}\n");
- }
- #endif
- #ifdef DUMP_MEM
- {
- JSMemoryUsage stats;
- JS_ComputeMemoryUsage(rt, &stats);
- JS_DumpMemoryUsage(stdout, &stats, rt);
- }
- #endif
- js_free_modules(ctx, JS_FREE_MODULE_ALL);
- JS_FreeValue(ctx, ctx->global_obj);
- JS_FreeValue(ctx, ctx->global_var_obj);
- JS_FreeValue(ctx, ctx->throw_type_error);
- JS_FreeValue(ctx, ctx->eval_obj);
- JS_FreeValue(ctx, ctx->array_proto_values);
- for(i = 0; i < JS_NATIVE_ERROR_COUNT; i++) {
- JS_FreeValue(ctx, ctx->native_error_proto[i]);
- }
- for(i = 0; i < rt->class_count; i++) {
- JS_FreeValue(ctx, ctx->class_proto[i]);
- }
- js_free_rt(rt, ctx->class_proto);
- JS_FreeValue(ctx, ctx->iterator_ctor);
- JS_FreeValue(ctx, ctx->async_iterator_proto);
- JS_FreeValue(ctx, ctx->promise_ctor);
- JS_FreeValue(ctx, ctx->array_ctor);
- JS_FreeValue(ctx, ctx->regexp_ctor);
- JS_FreeValue(ctx, ctx->function_ctor);
- JS_FreeValue(ctx, ctx->function_proto);
- js_free_shape_null(ctx->rt, ctx->array_shape);
- js_free_shape_null(ctx->rt, ctx->arguments_shape);
- js_free_shape_null(ctx->rt, ctx->mapped_arguments_shape);
- js_free_shape_null(ctx->rt, ctx->regexp_shape);
- js_free_shape_null(ctx->rt, ctx->regexp_result_shape);
- list_del(&ctx->link);
- remove_gc_object(&ctx->header);
- js_free_rt(ctx->rt, ctx);
- }
- JSRuntime *JS_GetRuntime(JSContext *ctx)
- {
- return ctx->rt;
- }
- static void update_stack_limit(JSRuntime *rt)
- {
- if (rt->stack_size == 0) {
- rt->stack_limit = 0; /* no limit */
- } else {
- rt->stack_limit = rt->stack_top - rt->stack_size;
- }
- }
- void JS_SetMaxStackSize(JSRuntime *rt, size_t stack_size)
- {
- rt->stack_size = stack_size;
- update_stack_limit(rt);
- }
- void JS_UpdateStackTop(JSRuntime *rt)
- {
- rt->stack_top = js_get_stack_pointer();
- update_stack_limit(rt);
- }
- static inline BOOL is_strict_mode(JSContext *ctx)
- {
- JSStackFrame *sf = ctx->rt->current_stack_frame;
- return (sf && (sf->js_mode & JS_MODE_STRICT));
- }
- /* JSAtom support */
- #define JS_ATOM_TAG_INT (1U << 31)
- #define JS_ATOM_MAX_INT (JS_ATOM_TAG_INT - 1)
- #define JS_ATOM_MAX ((1U << 30) - 1)
- /* return the max count from the hash size */
- #define JS_ATOM_COUNT_RESIZE(n) ((n) * 2)
- static inline BOOL __JS_AtomIsConst(JSAtom v)
- {
- #if defined(DUMP_LEAKS) && DUMP_LEAKS > 1
- return (int32_t)v <= 0;
- #else
- return (int32_t)v < JS_ATOM_END;
- #endif
- }
- static inline BOOL __JS_AtomIsTaggedInt(JSAtom v)
- {
- return (v & JS_ATOM_TAG_INT) != 0;
- }
- static inline JSAtom __JS_AtomFromUInt32(uint32_t v)
- {
- return v | JS_ATOM_TAG_INT;
- }
- static inline uint32_t __JS_AtomToUInt32(JSAtom atom)
- {
- return atom & ~JS_ATOM_TAG_INT;
- }
- static inline int is_num(int c)
- {
- return c >= '0' && c <= '9';
- }
- /* return TRUE if the string is a number n with 0 <= n <= 2^32-1 */
- static inline BOOL is_num_string(uint32_t *pval, const JSString *p)
- {
- uint32_t n;
- uint64_t n64;
- int c, i, len;
- len = p->len;
- if (len == 0 || len > 10)
- return FALSE;
- c = string_get(p, 0);
- if (is_num(c)) {
- if (c == '0') {
- if (len != 1)
- return FALSE;
- n = 0;
- } else {
- n = c - '0';
- for(i = 1; i < len; i++) {
- c = string_get(p, i);
- if (!is_num(c))
- return FALSE;
- n64 = (uint64_t)n * 10 + (c - '0');
- if ((n64 >> 32) != 0)
- return FALSE;
- n = n64;
- }
- }
- *pval = n;
- return TRUE;
- } else {
- return FALSE;
- }
- }
- /* XXX: could use faster version ? */
- static inline uint32_t hash_string8(const uint8_t *str, size_t len, uint32_t h)
- {
- size_t i;
- for(i = 0; i < len; i++)
- h = h * 263 + str[i];
- return h;
- }
- static inline uint32_t hash_string16(const uint16_t *str,
- size_t len, uint32_t h)
- {
- size_t i;
- for(i = 0; i < len; i++)
- h = h * 263 + str[i];
- return h;
- }
- static uint32_t hash_string(const JSString *str, uint32_t h)
- {
- if (str->is_wide_char)
- h = hash_string16(str->u.str16, str->len, h);
- else
- h = hash_string8(str->u.str8, str->len, h);
- return h;
- }
- static uint32_t hash_string_rope(JSValueConst val, uint32_t h)
- {
- if (JS_VALUE_GET_TAG(val) == JS_TAG_STRING) {
- return hash_string(JS_VALUE_GET_STRING(val), h);
- } else {
- JSStringRope *r = JS_VALUE_GET_STRING_ROPE(val);
- h = hash_string_rope(r->left, h);
- return hash_string_rope(r->right, h);
- }
- }
- static __maybe_unused void JS_DumpChar(FILE *fo, int c, int sep)
- {
- if (c == sep || c == '\\') {
- fputc('\\', fo);
- fputc(c, fo);
- } else if (c >= ' ' && c <= 126) {
- fputc(c, fo);
- } else if (c == '\n') {
- fputc('\\', fo);
- fputc('n', fo);
- } else {
- fprintf(fo, "\\u%04x", c);
- }
- }
- static __maybe_unused void JS_DumpString(JSRuntime *rt, const JSString *p)
- {
- int i, sep;
- if (p == NULL) {
- printf("<null>");
- return;
- }
- printf("%d", js_rc((void *)p)->ref_count);
- sep = (js_rc((void *)p)->ref_count == 1) ? '\"' : '\'';
- putchar(sep);
- for(i = 0; i < p->len; i++) {
- JS_DumpChar(stdout, string_get(p, i), sep);
- }
- putchar(sep);
- }
- static __maybe_unused void JS_DumpAtoms(JSRuntime *rt)
- {
- JSAtomStruct *p;
- int h, i;
- /* This only dumps hashed atoms, not JS_ATOM_TYPE_SYMBOL atoms */
- printf("JSAtom count=%d size=%d hash_size=%d:\n",
- rt->atom_count, rt->atom_size, rt->atom_hash_size);
- printf("JSAtom hash table: {\n");
- for(i = 0; i < rt->atom_hash_size; i++) {
- h = rt->atom_hash[i];
- if (h) {
- printf(" %d:", i);
- while (h) {
- p = rt->atom_array[h];
- printf(" ");
- JS_DumpString(rt, p);
- h = p->hash_next;
- }
- printf("\n");
- }
- }
- printf("}\n");
- printf("JSAtom table: {\n");
- for(i = 0; i < rt->atom_size; i++) {
- p = rt->atom_array[i];
- if (!atom_is_free(p)) {
- printf(" %d: { %d %08x ", i, p->atom_type, p->hash);
- if (!(p->len == 0 && p->is_wide_char != 0))
- JS_DumpString(rt, p);
- printf(" %d }\n", p->hash_next);
- }
- }
- printf("}\n");
- }
- static int JS_ResizeAtomHash(JSRuntime *rt, int new_hash_size)
- {
- JSAtomStruct *p;
- uint32_t new_hash_mask, h, i, hash_next1, j, *new_hash;
- assert((new_hash_size & (new_hash_size - 1)) == 0); /* power of two */
- new_hash_mask = new_hash_size - 1;
- new_hash = js_mallocz_rt(rt, sizeof(rt->atom_hash[0]) * new_hash_size);
- if (!new_hash)
- return -1;
- for(i = 0; i < rt->atom_hash_size; i++) {
- h = rt->atom_hash[i];
- while (h != 0) {
- p = rt->atom_array[h];
- hash_next1 = p->hash_next;
- /* add in new hash table */
- j = p->hash & new_hash_mask;
- p->hash_next = new_hash[j];
- new_hash[j] = h;
- h = hash_next1;
- }
- }
- js_free_rt(rt, rt->atom_hash);
- rt->atom_hash = new_hash;
- rt->atom_hash_size = new_hash_size;
- rt->atom_count_resize = JS_ATOM_COUNT_RESIZE(new_hash_size);
- // JS_DumpAtoms(rt);
- return 0;
- }
- static int JS_InitAtoms(JSRuntime *rt)
- {
- int i, len, atom_type;
- const char *p;
- rt->atom_hash_size = 0;
- rt->atom_hash = NULL;
- rt->atom_count = 0;
- rt->atom_size = 0;
- rt->atom_free_index = 0;
- if (JS_ResizeAtomHash(rt, 512)) /* there are at least 504 predefined atoms */
- return -1;
- p = js_atom_init;
- for(i = 1; i < JS_ATOM_END; i++) {
- if (i == JS_ATOM_Private_brand)
- atom_type = JS_ATOM_TYPE_PRIVATE;
- else if (i >= JS_ATOM_Symbol_toPrimitive)
- atom_type = JS_ATOM_TYPE_SYMBOL;
- else
- atom_type = JS_ATOM_TYPE_STRING;
- len = strlen(p);
- if (__JS_NewAtomInit(rt, p, len, atom_type) == JS_ATOM_NULL)
- return -1;
- p = p + len + 1;
- }
- return 0;
- }
- static JSAtom JS_DupAtomRT(JSRuntime *rt, JSAtom v)
- {
- JSAtomStruct *p;
- if (!__JS_AtomIsConst(v)) {
- p = rt->atom_array[v];
- js_rc(p)->ref_count++;
- }
- return v;
- }
- JSAtom JS_DupAtom(JSContext *ctx, JSAtom v)
- {
- JSRuntime *rt;
- JSAtomStruct *p;
- if (!__JS_AtomIsConst(v)) {
- rt = ctx->rt;
- p = rt->atom_array[v];
- js_rc(p)->ref_count++;
- }
- return v;
- }
- static JSAtomKindEnum JS_AtomGetKind(JSContext *ctx, JSAtom v)
- {
- JSRuntime *rt;
- JSAtomStruct *p;
- rt = ctx->rt;
- if (__JS_AtomIsTaggedInt(v))
- return JS_ATOM_KIND_STRING;
- p = rt->atom_array[v];
- switch(p->atom_type) {
- case JS_ATOM_TYPE_STRING:
- return JS_ATOM_KIND_STRING;
- case JS_ATOM_TYPE_GLOBAL_SYMBOL:
- return JS_ATOM_KIND_SYMBOL;
- case JS_ATOM_TYPE_SYMBOL:
- if (p->hash == JS_ATOM_HASH_PRIVATE)
- return JS_ATOM_KIND_PRIVATE;
- else
- return JS_ATOM_KIND_SYMBOL;
- default:
- abort();
- }
- }
- static BOOL JS_AtomIsString(JSContext *ctx, JSAtom v)
- {
- return JS_AtomGetKind(ctx, v) == JS_ATOM_KIND_STRING;
- }
- static JSAtom js_get_atom_index(JSRuntime *rt, JSAtomStruct *p)
- {
- uint32_t i = p->hash_next; /* atom_index */
- if (p->atom_type != JS_ATOM_TYPE_SYMBOL) {
- JSAtomStruct *p1;
- i = rt->atom_hash[p->hash & (rt->atom_hash_size - 1)];
- p1 = rt->atom_array[i];
- while (p1 != p) {
- assert(i != 0);
- i = p1->hash_next;
- p1 = rt->atom_array[i];
- }
- }
- return i;
- }
- /* string case (internal). Return JS_ATOM_NULL if error. 'str' is
- freed. */
- static JSAtom __JS_NewAtom(JSRuntime *rt, JSString *str, int atom_type)
- {
- uint32_t h, h1, i;
- JSAtomStruct *p;
- int len;
- #if 0
- printf("__JS_NewAtom: "); JS_DumpString(rt, str); printf("\n");
- #endif
- if (atom_type < JS_ATOM_TYPE_SYMBOL) {
- /* str is not NULL */
- if (str->atom_type == atom_type) {
- /* str is the atom, return its index */
- i = js_get_atom_index(rt, str);
- /* reduce string refcount and increase atom's unless constant */
- if (__JS_AtomIsConst(i))
- js_rc(str)->ref_count--;
- return i;
- }
- /* try and locate an already registered atom */
- len = str->len;
- h = hash_string(str, atom_type);
- h &= JS_ATOM_HASH_MASK;
- h1 = h & (rt->atom_hash_size - 1);
- i = rt->atom_hash[h1];
- while (i != 0) {
- p = rt->atom_array[i];
- if (p->hash == h &&
- p->atom_type == atom_type &&
- p->len == len &&
- js_string_memcmp(p, 0, str, 0, len) == 0) {
- if (!__JS_AtomIsConst(i))
- js_rc(p)->ref_count++;
- goto done;
- }
- i = p->hash_next;
- }
- } else {
- h1 = 0; /* avoid warning */
- if (atom_type == JS_ATOM_TYPE_SYMBOL) {
- h = 0;
- } else {
- h = JS_ATOM_HASH_PRIVATE;
- atom_type = JS_ATOM_TYPE_SYMBOL;
- }
- }
- if (rt->atom_free_index == 0) {
- /* allow new atom entries */
- uint32_t new_size, start;
- JSAtomStruct **new_array;
- /* alloc new with size progression 3/2:
- 4 6 9 13 19 28 42 63 94 141 211 316 474 711 1066 1599 2398 3597 5395 8092
- preallocating space for predefined atoms (at least 504).
- */
- new_size = max_int(711, rt->atom_size * 3 / 2);
- if (new_size > JS_ATOM_MAX)
- goto fail;
- /* XXX: should use realloc2 to use slack space */
- new_array = js_realloc_rt(rt, rt->atom_array, sizeof(*new_array) * new_size);
- if (!new_array)
- goto fail;
- /* Note: the atom 0 is not used */
- start = rt->atom_size;
- if (start == 0) {
- /* JS_ATOM_NULL entry */
- p = js_mallocz_rt(rt, sizeof(JSAtomStruct));
- if (!p) {
- js_free_rt(rt, new_array);
- goto fail;
- }
- js_rc(p)->ref_count = 1; /* not refcounted */
- p->atom_type = JS_ATOM_TYPE_SYMBOL;
- #ifdef DUMP_LEAKS
- list_add_tail(&p->link, &rt->string_list);
- #endif
- new_array[0] = p;
- rt->atom_count++;
- start = 1;
- }
- rt->atom_size = new_size;
- rt->atom_array = new_array;
- rt->atom_free_index = start;
- for(i = start; i < new_size; i++) {
- uint32_t next;
- if (i == (new_size - 1))
- next = 0;
- else
- next = i + 1;
- rt->atom_array[i] = atom_set_free(next);
- }
- }
- if (str) {
- if (str->atom_type == 0) {
- p = str;
- p->atom_type = atom_type;
- } else {
- p = js_malloc_rt(rt, sizeof(JSString) +
- (str->len << str->is_wide_char) +
- 1 - str->is_wide_char);
- if (unlikely(!p))
- goto fail;
- js_rc(p)->ref_count = 1;
- p->is_wide_char = str->is_wide_char;
- p->len = str->len;
- #ifdef DUMP_LEAKS
- list_add_tail(&p->link, &rt->string_list);
- #endif
- memcpy(p->u.str8, str->u.str8, (str->len << str->is_wide_char) +
- 1 - str->is_wide_char);
- js_free_string(rt, str);
- }
- } else {
- p = js_malloc_rt(rt, sizeof(JSAtomStruct)); /* empty wide string */
- if (!p)
- return JS_ATOM_NULL;
- js_rc(p)->ref_count = 1;
- p->is_wide_char = 1; /* Hack to represent NULL as a JSString */
- p->len = 0;
- #ifdef DUMP_LEAKS
- list_add_tail(&p->link, &rt->string_list);
- #endif
- }
- /* use an already free entry */
- i = rt->atom_free_index;
- rt->atom_free_index = atom_get_free(rt->atom_array[i]);
- rt->atom_array[i] = p;
- p->hash = h;
- p->hash_next = i; /* atom_index */
- p->atom_type = atom_type;
- rt->atom_count++;
- if (atom_type != JS_ATOM_TYPE_SYMBOL) {
- p->hash_next = rt->atom_hash[h1];
- rt->atom_hash[h1] = i;
- if (unlikely(rt->atom_count >= rt->atom_count_resize))
- JS_ResizeAtomHash(rt, rt->atom_hash_size * 2);
- }
- // JS_DumpAtoms(rt);
- return i;
- fail:
- i = JS_ATOM_NULL;
- done:
- if (str)
- js_free_string(rt, str);
- return i;
- }
- /* only works with zero terminated 8 bit strings */
- static JSAtom __JS_NewAtomInit(JSRuntime *rt, const char *str, int len,
- int atom_type)
- {
- JSString *p;
- p = js_alloc_string_rt(rt, len, 0);
- if (!p)
- return JS_ATOM_NULL;
- memcpy(p->u.str8, str, len);
- p->u.str8[len] = '\0';
- return __JS_NewAtom(rt, p, atom_type);
- }
- /* Warning: str must be ASCII only */
- static JSAtom __JS_FindAtom(JSRuntime *rt, const char *str, size_t len,
- int atom_type)
- {
- uint32_t h, h1, i;
- JSAtomStruct *p;
- h = hash_string8((const uint8_t *)str, len, JS_ATOM_TYPE_STRING);
- h &= JS_ATOM_HASH_MASK;
- h1 = h & (rt->atom_hash_size - 1);
- i = rt->atom_hash[h1];
- while (i != 0) {
- p = rt->atom_array[i];
- if (p->hash == h &&
- p->atom_type == JS_ATOM_TYPE_STRING &&
- p->len == len &&
- p->is_wide_char == 0 &&
- memcmp(p->u.str8, str, len) == 0) {
- if (!__JS_AtomIsConst(i))
- js_rc(p)->ref_count++;
- return i;
- }
- i = p->hash_next;
- }
- return JS_ATOM_NULL;
- }
- static void JS_FreeAtomStruct(JSRuntime *rt, JSAtomStruct *p)
- {
- #if 0 /* JS_ATOM_NULL is not refcounted: __JS_AtomIsConst() includes 0 */
- if (unlikely(i == JS_ATOM_NULL)) {
- js_rc(p)->ref_count = INT32_MAX / 2;
- return;
- }
- #endif
- uint32_t i = p->hash_next; /* atom_index */
- if (p->atom_type != JS_ATOM_TYPE_SYMBOL) {
- JSAtomStruct *p0, *p1;
- uint32_t h0;
- h0 = p->hash & (rt->atom_hash_size - 1);
- i = rt->atom_hash[h0];
- p1 = rt->atom_array[i];
- if (p1 == p) {
- rt->atom_hash[h0] = p1->hash_next;
- } else {
- for(;;) {
- assert(i != 0);
- p0 = p1;
- i = p1->hash_next;
- p1 = rt->atom_array[i];
- if (p1 == p) {
- p0->hash_next = p1->hash_next;
- break;
- }
- }
- }
- }
- /* insert in free atom list */
- rt->atom_array[i] = atom_set_free(rt->atom_free_index);
- rt->atom_free_index = i;
- /* free the string structure */
- #ifdef DUMP_LEAKS
- list_del(&p->link);
- #endif
- if (p->atom_type == JS_ATOM_TYPE_SYMBOL &&
- p->hash != JS_ATOM_HASH_PRIVATE && p->hash != 0) {
- /* live weak references are still present on this object: keep
- it */
- } else {
- js_free_rt(rt, p);
- }
- rt->atom_count--;
- assert(rt->atom_count >= 0);
- }
- static void __JS_FreeAtom(JSRuntime *rt, uint32_t i)
- {
- JSAtomStruct *p;
- p = rt->atom_array[i];
- if (--js_rc(p)->ref_count > 0)
- return;
- JS_FreeAtomStruct(rt, p);
- }
- /* Warning: 'p' is freed */
- static JSAtom JS_NewAtomStr(JSContext *ctx, JSString *p)
- {
- JSRuntime *rt = ctx->rt;
- uint32_t n;
- if (is_num_string(&n, p)) {
- if (n <= JS_ATOM_MAX_INT) {
- js_free_string(rt, p);
- return __JS_AtomFromUInt32(n);
- }
- }
- /* XXX: should generate an exception */
- return __JS_NewAtom(rt, p, JS_ATOM_TYPE_STRING);
- }
- /* XXX: optimize */
- static size_t count_ascii(const uint8_t *buf, size_t len)
- {
- const uint8_t *p, *p_end;
- p = buf;
- p_end = buf + len;
- while (p < p_end && *p < 128)
- p++;
- return p - buf;
- }
- /* str is UTF-8 encoded */
- JSAtom JS_NewAtomLen(JSContext *ctx, const char *str, size_t len)
- {
- JSValue val;
- if (len == 0 ||
- (!is_digit(*str) &&
- count_ascii((const uint8_t *)str, len) == len)) {
- JSAtom atom = __JS_FindAtom(ctx->rt, str, len, JS_ATOM_TYPE_STRING);
- if (atom)
- return atom;
- }
- val = JS_NewStringLen(ctx, str, len);
- if (JS_IsException(val))
- return JS_ATOM_NULL;
- return JS_NewAtomStr(ctx, JS_VALUE_GET_STRING(val));
- }
- JSAtom JS_NewAtom(JSContext *ctx, const char *str)
- {
- return JS_NewAtomLen(ctx, str, strlen(str));
- }
- JSAtom JS_NewAtomUInt32(JSContext *ctx, uint32_t n)
- {
- if (n <= JS_ATOM_MAX_INT) {
- return __JS_AtomFromUInt32(n);
- } else {
- char buf[11];
- JSValue val;
- size_t len;
- len = u32toa(buf, n);
- val = js_new_string8_len(ctx, buf, len);
- if (JS_IsException(val))
- return JS_ATOM_NULL;
- return __JS_NewAtom(ctx->rt, JS_VALUE_GET_STRING(val),
- JS_ATOM_TYPE_STRING);
- }
- }
- static JSAtom JS_NewAtomInt64(JSContext *ctx, int64_t n)
- {
- if ((uint64_t)n <= JS_ATOM_MAX_INT) {
- return __JS_AtomFromUInt32((uint32_t)n);
- } else {
- char buf[24];
- JSValue val;
- size_t len;
- len = i64toa(buf, n);
- val = js_new_string8_len(ctx, buf, len);
- if (JS_IsException(val))
- return JS_ATOM_NULL;
- return __JS_NewAtom(ctx->rt, JS_VALUE_GET_STRING(val),
- JS_ATOM_TYPE_STRING);
- }
- }
- /* 'p' is freed */
- static JSValue JS_NewSymbol(JSContext *ctx, JSString *p, int atom_type)
- {
- JSRuntime *rt = ctx->rt;
- JSAtom atom;
- atom = __JS_NewAtom(rt, p, atom_type);
- if (atom == JS_ATOM_NULL)
- return JS_ThrowOutOfMemory(ctx);
- return JS_MKPTR(JS_TAG_SYMBOL, rt->atom_array[atom]);
- }
- /* descr must be a non-numeric string atom */
- static JSValue JS_NewSymbolFromAtom(JSContext *ctx, JSAtom descr,
- int atom_type)
- {
- JSRuntime *rt = ctx->rt;
- JSString *p;
- assert(!__JS_AtomIsTaggedInt(descr));
- assert(descr < rt->atom_size);
- p = rt->atom_array[descr];
- JS_DupValue(ctx, JS_MKPTR(JS_TAG_STRING, p));
- return JS_NewSymbol(ctx, p, atom_type);
- }
- #define ATOM_GET_STR_BUF_SIZE 64
- /* Should only be used for debug. */
- static const char *JS_AtomGetStrRT(JSRuntime *rt, char *buf, int buf_size,
- JSAtom atom)
- {
- if (__JS_AtomIsTaggedInt(atom)) {
- snprintf(buf, buf_size, "%u", __JS_AtomToUInt32(atom));
- } else {
- JSAtomStruct *p;
- assert(atom < rt->atom_size);
- if (atom == JS_ATOM_NULL) {
- snprintf(buf, buf_size, "<null>");
- } else {
- int i, c;
- char *q;
- JSString *str;
- q = buf;
- p = rt->atom_array[atom];
- assert(!atom_is_free(p));
- str = p;
- if (str) {
- if (!str->is_wide_char) {
- /* special case ASCII strings */
- c = 0;
- for(i = 0; i < str->len; i++) {
- c |= str->u.str8[i];
- }
- if (c < 0x80)
- return (const char *)str->u.str8;
- }
- for(i = 0; i < str->len; i++) {
- c = string_get(str, i);
- if ((q - buf) >= buf_size - UTF8_CHAR_LEN_MAX)
- break;
- if (c < 128) {
- *q++ = c;
- } else {
- q += unicode_to_utf8((uint8_t *)q, c);
- }
- }
- }
- *q = '\0';
- }
- }
- return buf;
- }
- static const char *JS_AtomGetStr(JSContext *ctx, char *buf, int buf_size, JSAtom atom)
- {
- return JS_AtomGetStrRT(ctx->rt, buf, buf_size, atom);
- }
- static JSValue __JS_AtomToValue(JSContext *ctx, JSAtom atom, BOOL force_string)
- {
- char buf[ATOM_GET_STR_BUF_SIZE];
- if (__JS_AtomIsTaggedInt(atom)) {
- size_t len = u32toa(buf, __JS_AtomToUInt32(atom));
- return js_new_string8_len(ctx, buf, len);
- } else {
- JSRuntime *rt = ctx->rt;
- JSAtomStruct *p;
- assert(atom < rt->atom_size);
- p = rt->atom_array[atom];
- if (p->atom_type == JS_ATOM_TYPE_STRING) {
- goto ret_string;
- } else if (force_string) {
- if (p->len == 0 && p->is_wide_char != 0) {
- /* no description string */
- p = rt->atom_array[JS_ATOM_empty_string];
- }
- ret_string:
- return JS_DupValue(ctx, JS_MKPTR(JS_TAG_STRING, p));
- } else {
- return JS_DupValue(ctx, JS_MKPTR(JS_TAG_SYMBOL, p));
- }
- }
- }
- JSValue JS_AtomToValue(JSContext *ctx, JSAtom atom)
- {
- return __JS_AtomToValue(ctx, atom, FALSE);
- }
- JSValue JS_AtomToString(JSContext *ctx, JSAtom atom)
- {
- return __JS_AtomToValue(ctx, atom, TRUE);
- }
- /* return TRUE if the atom is an array index (i.e. 0 <= index <=
- 2^32-2 and return its value */
- static BOOL JS_AtomIsArrayIndex(JSContext *ctx, uint32_t *pval, JSAtom atom)
- {
- if (__JS_AtomIsTaggedInt(atom)) {
- *pval = __JS_AtomToUInt32(atom);
- return TRUE;
- } else {
- JSRuntime *rt = ctx->rt;
- JSAtomStruct *p;
- uint32_t val;
- assert(atom < rt->atom_size);
- p = rt->atom_array[atom];
- if (p->atom_type == JS_ATOM_TYPE_STRING &&
- is_num_string(&val, p) && val != -1) {
- *pval = val;
- return TRUE;
- } else {
- *pval = 0;
- return FALSE;
- }
- }
- }
- /* This test must be fast if atom is not a numeric index (e.g. a
- method name). Return JS_UNDEFINED if not a numeric
- index. JS_EXCEPTION can also be returned. */
- static JSValue JS_AtomIsNumericIndex1(JSContext *ctx, JSAtom atom)
- {
- JSRuntime *rt = ctx->rt;
- JSAtomStruct *p1;
- JSString *p;
- int c, ret;
- JSValue num, str;
- if (__JS_AtomIsTaggedInt(atom))
- return JS_NewInt32(ctx, __JS_AtomToUInt32(atom));
- assert(atom < rt->atom_size);
- p1 = rt->atom_array[atom];
- if (p1->atom_type != JS_ATOM_TYPE_STRING)
- return JS_UNDEFINED;
- switch(atom) {
- case JS_ATOM_minus_zero:
- return __JS_NewFloat64(ctx, -0.0);
- case JS_ATOM_Infinity:
- return __JS_NewFloat64(ctx, INFINITY);
- case JS_ATOM_minus_Infinity:
- return __JS_NewFloat64(ctx, -INFINITY);
- case JS_ATOM_NaN:
- return __JS_NewFloat64(ctx, NAN);
- default:
- break;
- }
- p = p1;
- if (p->len == 0)
- return JS_UNDEFINED;
- c = string_get(p, 0);
- if (!is_num(c) && c != '-')
- return JS_UNDEFINED;
- /* this is ECMA CanonicalNumericIndexString primitive */
- num = JS_ToNumber(ctx, JS_MKPTR(JS_TAG_STRING, p));
- if (JS_IsException(num))
- return num;
- str = JS_ToString(ctx, num);
- if (JS_IsException(str)) {
- JS_FreeValue(ctx, num);
- return str;
- }
- ret = js_string_eq(ctx, p, JS_VALUE_GET_STRING(str));
- JS_FreeValue(ctx, str);
- if (ret) {
- return num;
- } else {
- JS_FreeValue(ctx, num);
- return JS_UNDEFINED;
- }
- }
- /* return -1 if exception or TRUE/FALSE */
- static int JS_AtomIsNumericIndex(JSContext *ctx, JSAtom atom)
- {
- JSValue num;
- num = JS_AtomIsNumericIndex1(ctx, atom);
- if (likely(JS_IsUndefined(num)))
- return FALSE;
- if (JS_IsException(num))
- return -1;
- JS_FreeValue(ctx, num);
- return TRUE;
- }
- void JS_FreeAtom(JSContext *ctx, JSAtom v)
- {
- if (!__JS_AtomIsConst(v))
- __JS_FreeAtom(ctx->rt, v);
- }
- void JS_FreeAtomRT(JSRuntime *rt, JSAtom v)
- {
- if (!__JS_AtomIsConst(v))
- __JS_FreeAtom(rt, v);
- }
- /* return TRUE if 'v' is a symbol with a string description */
- static BOOL JS_AtomSymbolHasDescription(JSContext *ctx, JSAtom v)
- {
- JSRuntime *rt;
- JSAtomStruct *p;
- rt = ctx->rt;
- if (__JS_AtomIsTaggedInt(v))
- return FALSE;
- p = rt->atom_array[v];
- return (((p->atom_type == JS_ATOM_TYPE_SYMBOL &&
- p->hash != JS_ATOM_HASH_PRIVATE) ||
- p->atom_type == JS_ATOM_TYPE_GLOBAL_SYMBOL) &&
- !(p->len == 0 && p->is_wide_char != 0));
- }
- /* free with JS_FreeCString() */
- const char *JS_AtomToCStringLen(JSContext *ctx, size_t *plen, JSAtom atom)
- {
- JSValue str;
- const char *cstr;
- str = JS_AtomToString(ctx, atom);
- if (JS_IsException(str)) {
- if (plen)
- *plen = 0;
- return NULL;
- }
- cstr = JS_ToCStringLen(ctx, plen, str);
- JS_FreeValue(ctx, str);
- return cstr;
- }
- /* return a string atom containing name concatenated with str1 */
- static JSAtom js_atom_concat_str(JSContext *ctx, JSAtom name, const char *str1)
- {
- JSValue str;
- JSAtom atom;
- const char *cstr;
- char *cstr2;
- size_t len, len1;
- str = JS_AtomToString(ctx, name);
- if (JS_IsException(str))
- return JS_ATOM_NULL;
- cstr = JS_ToCStringLen(ctx, &len, str);
- if (!cstr)
- goto fail;
- len1 = strlen(str1);
- cstr2 = js_malloc(ctx, len + len1 + 1);
- if (!cstr2)
- goto fail;
- memcpy(cstr2, cstr, len);
- memcpy(cstr2 + len, str1, len1);
- cstr2[len + len1] = '\0';
- atom = JS_NewAtomLen(ctx, cstr2, len + len1);
- js_free(ctx, cstr2);
- JS_FreeCString(ctx, cstr);
- JS_FreeValue(ctx, str);
- return atom;
- fail:
- JS_FreeCString(ctx, cstr);
- JS_FreeValue(ctx, str);
- return JS_ATOM_NULL;
- }
- static JSAtom js_atom_concat_num(JSContext *ctx, JSAtom name, uint32_t n)
- {
- char buf[16];
- size_t len;
- len = u32toa(buf, n);
- buf[len] = '\0';
- return js_atom_concat_str(ctx, name, buf);
- }
- static inline BOOL JS_IsEmptyString(JSValueConst v)
- {
- return JS_VALUE_GET_TAG(v) == JS_TAG_STRING && JS_VALUE_GET_STRING(v)->len == 0;
- }
- /* JSClass support */
- #ifdef CONFIG_ATOMICS
- static pthread_mutex_t js_class_id_mutex = PTHREAD_MUTEX_INITIALIZER;
- #endif
- /* a new class ID is allocated if *pclass_id != 0 */
- JSClassID JS_NewClassID(JSClassID *pclass_id)
- {
- JSClassID class_id;
- #ifdef CONFIG_ATOMICS
- pthread_mutex_lock(&js_class_id_mutex);
- #endif
- class_id = *pclass_id;
- if (class_id == 0) {
- class_id = js_class_id_alloc++;
- *pclass_id = class_id;
- }
- #ifdef CONFIG_ATOMICS
- pthread_mutex_unlock(&js_class_id_mutex);
- #endif
- return class_id;
- }
- JSClassID JS_GetClassID(JSValue v)
- {
- JSObject *p;
- if (JS_VALUE_GET_TAG(v) != JS_TAG_OBJECT)
- return JS_INVALID_CLASS_ID;
- p = JS_VALUE_GET_OBJ(v);
- return p->class_id;
- }
- BOOL JS_IsRegisteredClass(JSRuntime *rt, JSClassID class_id)
- {
- return (class_id < rt->class_count &&
- rt->class_array[class_id].class_id != 0);
- }
- /* create a new object internal class. Return -1 if error, 0 if
- OK. The finalizer can be NULL if none is needed. */
- static int JS_NewClass1(JSRuntime *rt, JSClassID class_id,
- const JSClassDef *class_def, JSAtom name)
- {
- int new_size, i;
- JSClass *cl, *new_class_array;
- struct list_head *el;
- if (class_id >= (1 << 16))
- return -1;
- if (class_id < rt->class_count &&
- rt->class_array[class_id].class_id != 0)
- return -1;
- if (class_id >= rt->class_count) {
- new_size = max_int(JS_CLASS_INIT_COUNT,
- max_int(class_id + 1, rt->class_count * 3 / 2));
- /* reallocate the context class prototype array, if any */
- list_for_each(el, &rt->context_list) {
- JSContext *ctx = list_entry(el, JSContext, link);
- JSValue *new_tab;
- new_tab = js_realloc_rt(rt, ctx->class_proto,
- sizeof(ctx->class_proto[0]) * new_size);
- if (!new_tab)
- return -1;
- for(i = rt->class_count; i < new_size; i++)
- new_tab[i] = JS_NULL;
- ctx->class_proto = new_tab;
- }
- /* reallocate the class array */
- new_class_array = js_realloc_rt(rt, rt->class_array,
- sizeof(JSClass) * new_size);
- if (!new_class_array)
- return -1;
- memset(new_class_array + rt->class_count, 0,
- (new_size - rt->class_count) * sizeof(JSClass));
- rt->class_array = new_class_array;
- rt->class_count = new_size;
- }
- cl = &rt->class_array[class_id];
- cl->class_id = class_id;
- cl->class_name = JS_DupAtomRT(rt, name);
- cl->finalizer = class_def->finalizer;
- cl->gc_mark = class_def->gc_mark;
- cl->call = class_def->call;
- cl->exotic = class_def->exotic;
- return 0;
- }
- int JS_NewClass(JSRuntime *rt, JSClassID class_id, const JSClassDef *class_def)
- {
- int ret, len;
- JSAtom name;
- len = strlen(class_def->class_name);
- name = __JS_FindAtom(rt, class_def->class_name, len, JS_ATOM_TYPE_STRING);
- if (name == JS_ATOM_NULL) {
- name = __JS_NewAtomInit(rt, class_def->class_name, len, JS_ATOM_TYPE_STRING);
- if (name == JS_ATOM_NULL)
- return -1;
- }
- ret = JS_NewClass1(rt, class_id, class_def, name);
- JS_FreeAtomRT(rt, name);
- return ret;
- }
- static JSValue js_new_string8_len(JSContext *ctx, const char *buf, int len)
- {
- JSString *str;
- if (len <= 0) {
- return JS_AtomToString(ctx, JS_ATOM_empty_string);
- }
- str = js_alloc_string(ctx, len, 0);
- if (!str)
- return JS_EXCEPTION;
- memcpy(str->u.str8, buf, len);
- str->u.str8[len] = '\0';
- return JS_MKPTR(JS_TAG_STRING, str);
- }
- static JSValue js_new_string8(JSContext *ctx, const char *buf)
- {
- return js_new_string8_len(ctx, buf, strlen(buf));
- }
- static JSValue js_new_string16_len(JSContext *ctx, const uint16_t *buf, int len)
- {
- JSString *str;
- str = js_alloc_string(ctx, len, 1);
- if (!str)
- return JS_EXCEPTION;
- memcpy(str->u.str16, buf, len * 2);
- return JS_MKPTR(JS_TAG_STRING, str);
- }
- static JSValue js_new_string_char(JSContext *ctx, uint16_t c)
- {
- if (c < 0x100) {
- uint8_t ch8 = c;
- return js_new_string8_len(ctx, (const char *)&ch8, 1);
- } else {
- uint16_t ch16 = c;
- return js_new_string16_len(ctx, &ch16, 1);
- }
- }
- static JSValue js_sub_string(JSContext *ctx, JSString *p, int start, int end)
- {
- int len = end - start;
- if (start == 0 && end == p->len) {
- return JS_DupValue(ctx, JS_MKPTR(JS_TAG_STRING, p));
- }
- if (p->is_wide_char && len > 0) {
- JSString *str;
- int i;
- uint16_t c = 0;
- for (i = start; i < end; i++) {
- c |= p->u.str16[i];
- }
- if (c > 0xFF)
- return js_new_string16_len(ctx, p->u.str16 + start, len);
- str = js_alloc_string(ctx, len, 0);
- if (!str)
- return JS_EXCEPTION;
- for (i = 0; i < len; i++) {
- str->u.str8[i] = p->u.str16[start + i];
- }
- str->u.str8[len] = '\0';
- return JS_MKPTR(JS_TAG_STRING, str);
- } else {
- return js_new_string8_len(ctx, (const char *)(p->u.str8 + start), len);
- }
- }
- typedef struct StringBuffer {
- JSContext *ctx;
- JSString *str;
- int len;
- int size;
- int is_wide_char;
- int error_status;
- } StringBuffer;
- /* It is valid to call string_buffer_end() and all string_buffer functions even
- if string_buffer_init() or another string_buffer function returns an error.
- If the error_status is set, string_buffer_end() returns JS_EXCEPTION.
- */
- static int string_buffer_init2(JSContext *ctx, StringBuffer *s, int size,
- int is_wide)
- {
- s->ctx = ctx;
- s->size = size;
- s->len = 0;
- s->is_wide_char = is_wide;
- s->error_status = 0;
- s->str = js_alloc_string(ctx, size, is_wide);
- if (unlikely(!s->str)) {
- s->size = 0;
- return s->error_status = -1;
- }
- #ifdef DUMP_LEAKS
- /* the StringBuffer may reallocate the JSString, only link it at the end */
- list_del(&s->str->link);
- #endif
- return 0;
- }
- static inline int string_buffer_init(JSContext *ctx, StringBuffer *s, int size)
- {
- return string_buffer_init2(ctx, s, size, 0);
- }
- static void string_buffer_free(StringBuffer *s)
- {
- js_free(s->ctx, s->str);
- s->str = NULL;
- }
- static int string_buffer_set_error(StringBuffer *s)
- {
- js_free(s->ctx, s->str);
- s->str = NULL;
- s->size = 0;
- s->len = 0;
- return s->error_status = -1;
- }
- static no_inline int string_buffer_widen(StringBuffer *s, int size)
- {
- JSString *str;
- size_t slack;
- int i;
- if (s->error_status)
- return -1;
- str = js_realloc2(s->ctx, s->str, sizeof(JSString) + (size << 1), &slack);
- if (!str)
- return string_buffer_set_error(s);
- size += slack >> 1;
- for(i = s->len; i-- > 0;) {
- str->u.str16[i] = str->u.str8[i];
- }
- s->is_wide_char = 1;
- s->size = size;
- s->str = str;
- return 0;
- }
- static no_inline int string_buffer_realloc(StringBuffer *s, int new_len, int c)
- {
- JSString *new_str;
- int new_size;
- size_t new_size_bytes, slack;
- if (s->error_status)
- return -1;
- if (new_len > JS_STRING_LEN_MAX) {
- JS_ThrowInternalError(s->ctx, "string too long");
- return string_buffer_set_error(s);
- }
- new_size = min_int(max_int(new_len, s->size * 3 / 2), JS_STRING_LEN_MAX);
- if (!s->is_wide_char && c >= 0x100) {
- return string_buffer_widen(s, new_size);
- }
- new_size_bytes = sizeof(JSString) + (new_size << s->is_wide_char) + 1 - s->is_wide_char;
- new_str = js_realloc2(s->ctx, s->str, new_size_bytes, &slack);
- if (!new_str)
- return string_buffer_set_error(s);
- new_size = min_int(new_size + (slack >> s->is_wide_char), JS_STRING_LEN_MAX);
- s->size = new_size;
- s->str = new_str;
- return 0;
- }
- static no_inline int string_buffer_putc16_slow(StringBuffer *s, uint32_t c)
- {
- if (unlikely(s->len >= s->size)) {
- if (string_buffer_realloc(s, s->len + 1, c))
- return -1;
- }
- if (s->is_wide_char) {
- s->str->u.str16[s->len++] = c;
- } else if (c < 0x100) {
- s->str->u.str8[s->len++] = c;
- } else {
- if (string_buffer_widen(s, s->size))
- return -1;
- s->str->u.str16[s->len++] = c;
- }
- return 0;
- }
- /* 0 <= c <= 0xff */
- static int string_buffer_putc8(StringBuffer *s, uint32_t c)
- {
- if (unlikely(s->len >= s->size)) {
- if (string_buffer_realloc(s, s->len + 1, c))
- return -1;
- }
- if (s->is_wide_char) {
- s->str->u.str16[s->len++] = c;
- } else {
- s->str->u.str8[s->len++] = c;
- }
- return 0;
- }
- /* 0 <= c <= 0xffff */
- static int string_buffer_putc16(StringBuffer *s, uint32_t c)
- {
- if (likely(s->len < s->size)) {
- if (s->is_wide_char) {
- s->str->u.str16[s->len++] = c;
- return 0;
- } else if (c < 0x100) {
- s->str->u.str8[s->len++] = c;
- return 0;
- }
- }
- return string_buffer_putc16_slow(s, c);
- }
- static int string_buffer_putc_slow(StringBuffer *s, uint32_t c)
- {
- if (unlikely(c >= 0x10000)) {
- /* surrogate pair */
- if (string_buffer_putc16(s, get_hi_surrogate(c)))
- return -1;
- c = get_lo_surrogate(c);
- }
- return string_buffer_putc16(s, c);
- }
- /* 0 <= c <= 0x10ffff */
- static inline int string_buffer_putc(StringBuffer *s, uint32_t c)
- {
- if (likely(s->len < s->size)) {
- if (s->is_wide_char) {
- if (c < 0x10000) {
- s->str->u.str16[s->len++] = c;
- return 0;
- } else if (likely((s->len + 1) < s->size)) {
- s->str->u.str16[s->len++] = get_hi_surrogate(c);
- s->str->u.str16[s->len++] = get_lo_surrogate(c);
- return 0;
- }
- } else if (c < 0x100) {
- s->str->u.str8[s->len++] = c;
- return 0;
- }
- }
- return string_buffer_putc_slow(s, c);
- }
- static int string_getc(const JSString *p, int *pidx)
- {
- int idx, c, c1;
- idx = *pidx;
- if (p->is_wide_char) {
- c = p->u.str16[idx++];
- if (is_hi_surrogate(c) && idx < p->len) {
- c1 = p->u.str16[idx];
- if (is_lo_surrogate(c1)) {
- c = from_surrogate(c, c1);
- idx++;
- }
- }
- } else {
- c = p->u.str8[idx++];
- }
- *pidx = idx;
- return c;
- }
- static int string_buffer_write8(StringBuffer *s, const uint8_t *p, int len)
- {
- int i;
- if (s->len + len > s->size) {
- if (string_buffer_realloc(s, s->len + len, 0))
- return -1;
- }
- if (s->is_wide_char) {
- for (i = 0; i < len; i++) {
- s->str->u.str16[s->len + i] = p[i];
- }
- s->len += len;
- } else {
- memcpy(&s->str->u.str8[s->len], p, len);
- s->len += len;
- }
- return 0;
- }
- static int string_buffer_write16(StringBuffer *s, const uint16_t *p, int len)
- {
- int c = 0, i;
- for (i = 0; i < len; i++) {
- c |= p[i];
- }
- if (s->len + len > s->size) {
- if (string_buffer_realloc(s, s->len + len, c))
- return -1;
- } else if (!s->is_wide_char && c >= 0x100) {
- if (string_buffer_widen(s, s->size))
- return -1;
- }
- if (s->is_wide_char) {
- memcpy(&s->str->u.str16[s->len], p, len << 1);
- s->len += len;
- } else {
- for (i = 0; i < len; i++) {
- s->str->u.str8[s->len + i] = p[i];
- }
- s->len += len;
- }
- return 0;
- }
- /* appending an ASCII string */
- static int string_buffer_puts8(StringBuffer *s, const char *str)
- {
- return string_buffer_write8(s, (const uint8_t *)str, strlen(str));
- }
- static int string_buffer_concat(StringBuffer *s, const JSString *p,
- uint32_t from, uint32_t to)
- {
- if (to <= from)
- return 0;
- if (p->is_wide_char)
- return string_buffer_write16(s, p->u.str16 + from, to - from);
- else
- return string_buffer_write8(s, p->u.str8 + from, to - from);
- }
- static int string_buffer_concat_value(StringBuffer *s, JSValueConst v)
- {
- JSString *p;
- JSValue v1;
- int res;
- if (s->error_status) {
- /* prevent exception overload */
- return -1;
- }
- if (unlikely(JS_VALUE_GET_TAG(v) != JS_TAG_STRING)) {
- if (JS_VALUE_GET_TAG(v) == JS_TAG_STRING_ROPE) {
- JSStringRope *r = JS_VALUE_GET_STRING_ROPE(v);
- /* recursion is acceptable because the rope depth is bounded */
- if (string_buffer_concat_value(s, r->left))
- return -1;
- return string_buffer_concat_value(s, r->right);
- } else {
- v1 = JS_ToString(s->ctx, v);
- if (JS_IsException(v1))
- return string_buffer_set_error(s);
- p = JS_VALUE_GET_STRING(v1);
- res = string_buffer_concat(s, p, 0, p->len);
- JS_FreeValue(s->ctx, v1);
- return res;
- }
- }
- p = JS_VALUE_GET_STRING(v);
- return string_buffer_concat(s, p, 0, p->len);
- }
- static int string_buffer_concat_value_free(StringBuffer *s, JSValue v)
- {
- JSString *p;
- int res;
- if (s->error_status) {
- /* prevent exception overload */
- JS_FreeValue(s->ctx, v);
- return -1;
- }
- if (unlikely(JS_VALUE_GET_TAG(v) != JS_TAG_STRING)) {
- v = JS_ToStringFree(s->ctx, v);
- if (JS_IsException(v))
- return string_buffer_set_error(s);
- }
- p = JS_VALUE_GET_STRING(v);
- res = string_buffer_concat(s, p, 0, p->len);
- JS_FreeValue(s->ctx, v);
- return res;
- }
- static int string_buffer_fill(StringBuffer *s, int c, int count)
- {
- /* XXX: optimize */
- if (s->len + count > s->size) {
- if (string_buffer_realloc(s, s->len + count, c))
- return -1;
- }
- while (count-- > 0) {
- if (string_buffer_putc16(s, c))
- return -1;
- }
- return 0;
- }
- static JSValue string_buffer_end(StringBuffer *s)
- {
- JSString *str;
- str = s->str;
- if (s->error_status)
- return JS_EXCEPTION;
- if (s->len == 0) {
- js_free(s->ctx, str);
- s->str = NULL;
- return JS_AtomToString(s->ctx, JS_ATOM_empty_string);
- }
- if (s->len < s->size) {
- /* smaller size so js_realloc should not fail, but OK if it does */
- /* XXX: should add some slack to avoid unnecessary calls */
- /* XXX: might need to use malloc+free to ensure smaller size */
- str = js_realloc_rt(s->ctx->rt, str, sizeof(JSString) +
- (s->len << s->is_wide_char) + 1 - s->is_wide_char);
- if (str == NULL)
- str = s->str;
- s->str = str;
- }
- if (!s->is_wide_char)
- str->u.str8[s->len] = 0;
- #ifdef DUMP_LEAKS
- list_add_tail(&str->link, &s->ctx->rt->string_list);
- #endif
- str->is_wide_char = s->is_wide_char;
- str->len = s->len;
- s->str = NULL;
- return JS_MKPTR(JS_TAG_STRING, str);
- }
- /* create a string from a UTF-8 buffer */
- JSValue JS_NewStringLen(JSContext *ctx, const char *buf, size_t buf_len)
- {
- const uint8_t *p, *p_end, *p_start, *p_next;
- uint32_t c;
- StringBuffer b_s, *b = &b_s;
- size_t len1;
- p_start = (const uint8_t *)buf;
- p_end = p_start + buf_len;
- len1 = count_ascii(p_start, buf_len);
- p = p_start + len1;
- if (len1 > JS_STRING_LEN_MAX)
- return JS_ThrowInternalError(ctx, "string too long");
- if (p == p_end) {
- /* ASCII string */
- return js_new_string8_len(ctx, buf, buf_len);
- } else {
- if (string_buffer_init(ctx, b, buf_len))
- goto fail;
- string_buffer_write8(b, p_start, len1);
- while (p < p_end) {
- if (*p < 128) {
- string_buffer_putc8(b, *p++);
- } else {
- /* parse utf-8 sequence, return 0xFFFFFFFF for error */
- c = unicode_from_utf8(p, p_end - p, &p_next);
- if (c < 0x10000) {
- p = p_next;
- } else if (c <= 0x10FFFF) {
- p = p_next;
- /* surrogate pair */
- string_buffer_putc16(b, get_hi_surrogate(c));
- c = get_lo_surrogate(c);
- } else {
- /* invalid char */
- c = 0xfffd;
- /* skip the invalid chars */
- /* XXX: seems incorrect. Why not just use c = *p++; ? */
- while (p < p_end && (*p >= 0x80 && *p < 0xc0))
- p++;
- if (p < p_end) {
- p++;
- while (p < p_end && (*p >= 0x80 && *p < 0xc0))
- p++;
- }
- }
- string_buffer_putc16(b, c);
- }
- }
- }
- return string_buffer_end(b);
- fail:
- string_buffer_free(b);
- return JS_EXCEPTION;
- }
- static JSValue JS_ConcatString3(JSContext *ctx, const char *str1,
- JSValue str2, const char *str3)
- {
- StringBuffer b_s, *b = &b_s;
- int len1, len3;
- JSString *p;
- if (unlikely(JS_VALUE_GET_TAG(str2) != JS_TAG_STRING)) {
- str2 = JS_ToStringFree(ctx, str2);
- if (JS_IsException(str2))
- goto fail;
- }
- p = JS_VALUE_GET_STRING(str2);
- len1 = strlen(str1);
- len3 = strlen(str3);
- if (string_buffer_init2(ctx, b, len1 + p->len + len3, p->is_wide_char))
- goto fail;
- string_buffer_write8(b, (const uint8_t *)str1, len1);
- string_buffer_concat(b, p, 0, p->len);
- string_buffer_write8(b, (const uint8_t *)str3, len3);
- JS_FreeValue(ctx, str2);
- return string_buffer_end(b);
- fail:
- JS_FreeValue(ctx, str2);
- return JS_EXCEPTION;
- }
- JSValue JS_NewAtomString(JSContext *ctx, const char *str)
- {
- JSAtom atom = JS_NewAtom(ctx, str);
- if (atom == JS_ATOM_NULL)
- return JS_EXCEPTION;
- JSValue val = JS_AtomToString(ctx, atom);
- JS_FreeAtom(ctx, atom);
- return val;
- }
- /* return (NULL, 0) if exception. */
- /* return pointer into a JSString with a live ref_count */
- /* cesu8 determines if non-BMP1 codepoints are encoded as 1 or 2 utf-8 sequences */
- const char *JS_ToCStringLen2(JSContext *ctx, size_t *plen, JSValueConst val1, BOOL cesu8)
- {
- JSValue val;
- JSString *str, *str_new;
- int pos, len, c, c1;
- uint8_t *q;
- if (JS_VALUE_GET_TAG(val1) != JS_TAG_STRING) {
- val = JS_ToString(ctx, val1);
- if (JS_IsException(val))
- goto fail;
- } else {
- val = JS_DupValue(ctx, val1);
- }
- str = JS_VALUE_GET_STRING(val);
- len = str->len;
- if (!str->is_wide_char) {
- const uint8_t *src = str->u.str8;
- int count;
- /* count the number of non-ASCII characters */
- /* Scanning the whole string is required for ASCII strings,
- and computing the number of non-ASCII bytes is less expensive
- than testing each byte, hence this method is faster for ASCII
- strings, which is the most common case.
- */
- count = 0;
- for (pos = 0; pos < len; pos++) {
- count += src[pos] >> 7;
- }
- if (count == 0) {
- if (plen)
- *plen = len;
- return (const char *)src;
- }
- str_new = js_alloc_string(ctx, len + count, 0);
- if (!str_new)
- goto fail;
- q = str_new->u.str8;
- for (pos = 0; pos < len; pos++) {
- c = src[pos];
- if (c < 0x80) {
- *q++ = c;
- } else {
- *q++ = (c >> 6) | 0xc0;
- *q++ = (c & 0x3f) | 0x80;
- }
- }
- } else {
- const uint16_t *src = str->u.str16;
- /* Allocate 3 bytes per 16 bit code point. Surrogate pairs may
- produce 4 bytes but use 2 code points.
- */
- str_new = js_alloc_string(ctx, len * 3, 0);
- if (!str_new)
- goto fail;
- q = str_new->u.str8;
- pos = 0;
- while (pos < len) {
- c = src[pos++];
- if (c < 0x80) {
- *q++ = c;
- } else {
- if (is_hi_surrogate(c)) {
- if (pos < len && !cesu8) {
- c1 = src[pos];
- if (is_lo_surrogate(c1)) {
- pos++;
- c = from_surrogate(c, c1);
- } else {
- /* Keep unmatched surrogate code points */
- /* c = 0xfffd; */ /* error */
- }
- } else {
- /* Keep unmatched surrogate code points */
- /* c = 0xfffd; */ /* error */
- }
- }
- q += unicode_to_utf8(q, c);
- }
- }
- }
- *q = '\0';
- str_new->len = q - str_new->u.str8;
- JS_FreeValue(ctx, val);
- if (plen)
- *plen = str_new->len;
- return (const char *)str_new->u.str8;
- fail:
- if (plen)
- *plen = 0;
- return NULL;
- }
- void JS_FreeCString(JSContext *ctx, const char *ptr)
- {
- JSString *p;
- if (!ptr)
- return;
- /* purposely removing constness */
- p = container_of(ptr, JSString, u);
- JS_FreeValue(ctx, JS_MKPTR(JS_TAG_STRING, p));
- }
- static int memcmp16_8(const uint16_t *src1, const uint8_t *src2, int len)
- {
- int c, i;
- for(i = 0; i < len; i++) {
- c = src1[i] - src2[i];
- if (c != 0)
- return c;
- }
- return 0;
- }
- static int memcmp16(const uint16_t *src1, const uint16_t *src2, int len)
- {
- int c, i;
- for(i = 0; i < len; i++) {
- c = src1[i] - src2[i];
- if (c != 0)
- return c;
- }
- return 0;
- }
- static int js_string_memcmp(const JSString *p1, int pos1, const JSString *p2,
- int pos2, int len)
- {
- int res;
- if (likely(!p1->is_wide_char)) {
- if (likely(!p2->is_wide_char))
- res = memcmp(p1->u.str8 + pos1, p2->u.str8 + pos2, len);
- else
- res = -memcmp16_8(p2->u.str16 + pos2, p1->u.str8 + pos1, len);
- } else {
- if (!p2->is_wide_char)
- res = memcmp16_8(p1->u.str16 + pos1, p2->u.str8 + pos2, len);
- else
- res = memcmp16(p1->u.str16 + pos1, p2->u.str16 + pos2, len);
- }
- return res;
- }
- static BOOL js_string_eq(JSContext *ctx,
- const JSString *p1, const JSString *p2)
- {
- if (p1->len != p2->len)
- return FALSE;
- if (p1 == p2)
- return TRUE;
- return js_string_memcmp(p1, 0, p2, 0, p1->len) == 0;
- }
- /* return < 0, 0 or > 0 */
- static int js_string_compare(JSContext *ctx,
- const JSString *p1, const JSString *p2)
- {
- int res, len;
- len = min_int(p1->len, p2->len);
- res = js_string_memcmp(p1, 0, p2, 0, len);
- if (res == 0) {
- if (p1->len == p2->len)
- res = 0;
- else if (p1->len < p2->len)
- res = -1;
- else
- res = 1;
- }
- return res;
- }
- static void copy_str16(uint16_t *dst, const JSString *p, int offset, int len)
- {
- if (p->is_wide_char) {
- memcpy(dst, p->u.str16 + offset, len * 2);
- } else {
- const uint8_t *src1 = p->u.str8 + offset;
- int i;
- for(i = 0; i < len; i++)
- dst[i] = src1[i];
- }
- }
- static JSValue JS_ConcatString1(JSContext *ctx,
- const JSString *p1, const JSString *p2)
- {
- JSString *p;
- uint32_t len;
- int is_wide_char;
- len = p1->len + p2->len;
- if (len > JS_STRING_LEN_MAX)
- return JS_ThrowInternalError(ctx, "string too long");
- is_wide_char = p1->is_wide_char | p2->is_wide_char;
- p = js_alloc_string(ctx, len, is_wide_char);
- if (!p)
- return JS_EXCEPTION;
- if (!is_wide_char) {
- memcpy(p->u.str8, p1->u.str8, p1->len);
- memcpy(p->u.str8 + p1->len, p2->u.str8, p2->len);
- p->u.str8[len] = '\0';
- } else {
- copy_str16(p->u.str16, p1, 0, p1->len);
- copy_str16(p->u.str16 + p1->len, p2, 0, p2->len);
- }
- return JS_MKPTR(JS_TAG_STRING, p);
- }
- static BOOL JS_ConcatStringInPlace(JSContext *ctx, JSString *p1, JSValueConst op2) {
- if (JS_VALUE_GET_TAG(op2) == JS_TAG_STRING) {
- JSString *p2 = JS_VALUE_GET_STRING(op2);
- size_t size1;
- if (p2->len == 0)
- return TRUE;
- if (js_rc(p1)->ref_count != 1)
- return FALSE;
- size1 = js_malloc_usable_size(ctx, p1);
- if (p1->is_wide_char) {
- if (size1 >= sizeof(*p1) + ((p1->len + p2->len) << 1)) {
- if (p2->is_wide_char) {
- memcpy(p1->u.str16 + p1->len, p2->u.str16, p2->len << 1);
- p1->len += p2->len;
- return TRUE;
- } else {
- size_t i;
- for (i = 0; i < p2->len; i++) {
- p1->u.str16[p1->len++] = p2->u.str8[i];
- }
- return TRUE;
- }
- }
- } else if (!p2->is_wide_char) {
- if (size1 >= sizeof(*p1) + p1->len + p2->len + 1) {
- memcpy(p1->u.str8 + p1->len, p2->u.str8, p2->len);
- p1->len += p2->len;
- p1->u.str8[p1->len] = '\0';
- return TRUE;
- }
- }
- }
- return FALSE;
- }
- static JSValue JS_ConcatString2(JSContext *ctx, JSValue op1, JSValue op2)
- {
- JSValue ret;
- JSString *p1, *p2;
- p1 = JS_VALUE_GET_STRING(op1);
- if (JS_ConcatStringInPlace(ctx, p1, op2)) {
- JS_FreeValue(ctx, op2);
- return op1;
- }
- p2 = JS_VALUE_GET_STRING(op2);
- ret = JS_ConcatString1(ctx, p1, p2);
- JS_FreeValue(ctx, op1);
- JS_FreeValue(ctx, op2);
- return ret;
- }
- /* Return the character at position 'idx'. 'val' must be a string or rope */
- static int string_rope_get(JSValueConst val, uint32_t idx)
- {
- if (JS_VALUE_GET_TAG(val) == JS_TAG_STRING) {
- return string_get(JS_VALUE_GET_STRING(val), idx);
- } else {
- JSStringRope *r = JS_VALUE_GET_STRING_ROPE(val);
- uint32_t len;
- if (JS_VALUE_GET_TAG(r->left) == JS_TAG_STRING)
- len = JS_VALUE_GET_STRING(r->left)->len;
- else
- len = JS_VALUE_GET_STRING_ROPE(r->left)->len;
- if (idx < len)
- return string_rope_get(r->left, idx);
- else
- return string_rope_get(r->right, idx - len);
- }
- }
- typedef struct {
- JSValueConst stack[JS_STRING_ROPE_MAX_DEPTH];
- int stack_len;
- } JSStringRopeIter;
- static void string_rope_iter_init(JSStringRopeIter *s, JSValueConst val)
- {
- s->stack_len = 0;
- s->stack[s->stack_len++] = val;
- }
- /* iterate thru a rope and return the strings in order */
- static JSString *string_rope_iter_next(JSStringRopeIter *s)
- {
- JSValueConst val;
- JSStringRope *r;
- if (s->stack_len == 0)
- return NULL;
- val = s->stack[--s->stack_len];
- for(;;) {
- if (JS_VALUE_GET_TAG(val) == JS_TAG_STRING)
- return JS_VALUE_GET_STRING(val);
- r = JS_VALUE_GET_STRING_ROPE(val);
- assert(s->stack_len < JS_STRING_ROPE_MAX_DEPTH);
- s->stack[s->stack_len++] = r->right;
- val = r->left;
- }
- }
- static uint32_t string_rope_get_len(JSValueConst val)
- {
- if (JS_VALUE_GET_TAG(val) == JS_TAG_STRING)
- return JS_VALUE_GET_STRING(val)->len;
- else
- return JS_VALUE_GET_STRING_ROPE(val)->len;
- }
- static int js_string_rope_compare(JSContext *ctx, JSValueConst op1,
- JSValueConst op2, BOOL eq_only)
- {
- uint32_t len1, len2, len, pos1, pos2, l;
- int res;
- JSStringRopeIter it1, it2;
- JSString *p1, *p2;
-
- len1 = string_rope_get_len(op1);
- len2 = string_rope_get_len(op2);
- /* no need to go further for equality test if
- different length */
- if (eq_only && len1 != len2)
- return 1;
- len = min_uint32(len1, len2);
- string_rope_iter_init(&it1, op1);
- string_rope_iter_init(&it2, op2);
- p1 = string_rope_iter_next(&it1);
- p2 = string_rope_iter_next(&it2);
- pos1 = 0;
- pos2 = 0;
- while (len != 0) {
- l = min_uint32(p1->len - pos1, p2->len - pos2);
- l = min_uint32(l, len);
- res = js_string_memcmp(p1, pos1, p2, pos2, l);
- if (res != 0)
- return res;
- len -= l;
- pos1 += l;
- if (pos1 >= p1->len) {
- p1 = string_rope_iter_next(&it1);
- pos1 = 0;
- }
- pos2 += l;
- if (pos2 >= p2->len) {
- p2 = string_rope_iter_next(&it2);
- pos2 = 0;
- }
- }
- if (len1 == len2)
- res = 0;
- else if (len1 < len2)
- res = -1;
- else
- res = 1;
- return res;
- }
- /* 'rope' must be a rope. return a string and modify the rope so that
- it won't need to be linearized again. */
- static JSValue js_linearize_string_rope(JSContext *ctx, JSValue rope)
- {
- StringBuffer b_s, *b = &b_s;
- JSStringRope *r;
- JSValue ret;
-
- r = JS_VALUE_GET_STRING_ROPE(rope);
- /* check whether it is already linearized */
- if (JS_VALUE_GET_TAG(r->right) == JS_TAG_STRING &&
- JS_VALUE_GET_STRING(r->right)->len == 0) {
- ret = JS_DupValue(ctx, r->left);
- JS_FreeValue(ctx, rope);
- return ret;
- }
- if (string_buffer_init2(ctx, b, r->len, r->is_wide_char))
- goto fail;
- if (string_buffer_concat_value(b, rope))
- goto fail;
- ret = string_buffer_end(b);
- if (js_rc(r)->ref_count > 1) {
- /* update the rope so that it won't need to be linearized again */
- JS_FreeValue(ctx, r->left);
- JS_FreeValue(ctx, r->right);
- r->left = JS_DupValue(ctx, ret);
- r->right = JS_AtomToString(ctx, JS_ATOM_empty_string);
- }
- JS_FreeValue(ctx, rope);
- return ret;
- fail:
- JS_FreeValue(ctx, rope);
- return JS_EXCEPTION;
- }
- static JSValue js_rebalancee_string_rope(JSContext *ctx, JSValueConst rope);
- /* op1 and op2 must be strings or string ropes */
- static JSValue js_new_string_rope(JSContext *ctx, JSValue op1, JSValue op2)
- {
- uint32_t len;
- int is_wide_char, depth;
- JSStringRope *r;
- JSValue res;
-
- if (JS_VALUE_GET_TAG(op1) == JS_TAG_STRING) {
- JSString *p1 = JS_VALUE_GET_STRING(op1);
- len = p1->len;
- is_wide_char = p1->is_wide_char;
- depth = 0;
- } else {
- JSStringRope *r1 = JS_VALUE_GET_STRING_ROPE(op1);
- len = r1->len;
- is_wide_char = r1->is_wide_char;
- depth = r1->depth;
- }
- if (JS_VALUE_GET_TAG(op2) == JS_TAG_STRING) {
- JSString *p2 = JS_VALUE_GET_STRING(op2);
- len += p2->len;
- is_wide_char |= p2->is_wide_char;
- } else {
- JSStringRope *r2 = JS_VALUE_GET_STRING_ROPE(op2);
- len += r2->len;
- is_wide_char |= r2->is_wide_char;
- depth = max_int(depth, r2->depth);
- }
- if (len > JS_STRING_LEN_MAX) {
- JS_ThrowInternalError(ctx, "string too long");
- goto fail;
- }
- r = js_malloc(ctx, sizeof(*r));
- if (!r)
- goto fail;
- js_rc(r)->ref_count = 1;
- r->len = len;
- r->is_wide_char = is_wide_char;
- r->depth = depth + 1;
- r->left = op1;
- r->right = op2;
- res = JS_MKPTR(JS_TAG_STRING_ROPE, r);
- if (r->depth > JS_STRING_ROPE_MAX_DEPTH) {
- JSValue res2;
- #ifdef DUMP_ROPE_REBALANCE
- printf("rebalance: initial depth=%d\n", r->depth);
- #endif
- res2 = js_rebalancee_string_rope(ctx, res);
- #ifdef DUMP_ROPE_REBALANCE
- if (JS_VALUE_GET_TAG(res2) == JS_TAG_STRING_ROPE)
- printf("rebalance: final depth=%d\n", JS_VALUE_GET_STRING_ROPE(res2)->depth);
- #endif
- JS_FreeValue(ctx, res);
- return res2;
- } else {
- return res;
- }
- fail:
- JS_FreeValue(ctx, op1);
- JS_FreeValue(ctx, op2);
- return JS_EXCEPTION;
- }
- #define ROPE_N_BUCKETS 44
- /* Fibonacii numbers starting from F_2 */
- static const uint32_t rope_bucket_len[ROPE_N_BUCKETS] = {
- 1, 2, 3, 5,
- 8, 13, 21, 34,
- 55, 89, 144, 233,
- 377, 610, 987, 1597,
- 2584, 4181, 6765, 10946,
- 17711, 28657, 46368, 75025,
- 121393, 196418, 317811, 514229,
- 832040, 1346269, 2178309, 3524578,
- 5702887, 9227465, 14930352, 24157817,
- 39088169, 63245986, 102334155, 165580141,
- 267914296, 433494437, 701408733, 1134903170, /* > JS_STRING_LEN_MAX */
- };
- static int js_rebalancee_string_rope_rec(JSContext *ctx, JSValue *buckets,
- JSValueConst val)
- {
- if (JS_VALUE_GET_TAG(val) == JS_TAG_STRING) {
- JSString *p = JS_VALUE_GET_STRING(val);
- uint32_t len, i;
- JSValue a, b;
-
- len = p->len;
- if (len == 0)
- return 0; /* nothing to do */
- /* find the bucket i so that rope_bucket_len[i] <= len <
- rope_bucket_len[i + 1] and concatenate the ropes in the
- buckets before */
- a = JS_NULL;
- i = 0;
- while (len >= rope_bucket_len[i + 1]) {
- b = buckets[i];
- if (!JS_IsNull(b)) {
- buckets[i] = JS_NULL;
- if (JS_IsNull(a)) {
- a = b;
- } else {
- a = js_new_string_rope(ctx, b, a);
- if (JS_IsException(a))
- return -1;
- }
- }
- i++;
- }
- if (!JS_IsNull(a)) {
- a = js_new_string_rope(ctx, a, JS_DupValue(ctx, val));
- if (JS_IsException(a))
- return -1;
- } else {
- a = JS_DupValue(ctx, val);
- }
- while (!JS_IsNull(buckets[i])) {
- a = js_new_string_rope(ctx, buckets[i], a);
- buckets[i] = JS_NULL;
- if (JS_IsException(a))
- return -1;
- i++;
- }
- buckets[i] = a;
- } else {
- JSStringRope *r = JS_VALUE_GET_STRING_ROPE(val);
- js_rebalancee_string_rope_rec(ctx, buckets, r->left);
- js_rebalancee_string_rope_rec(ctx, buckets, r->right);
- }
- return 0;
- }
- /* Return a new rope which is balanced. Algorithm from "Ropes: an
- Alternative to Strings", Hans-J. Boehm, Russ Atkinson and Michael
- Plass. */
- static JSValue js_rebalancee_string_rope(JSContext *ctx, JSValueConst rope)
- {
- JSValue buckets[ROPE_N_BUCKETS], a, b;
- int i;
-
- for(i = 0; i < ROPE_N_BUCKETS; i++)
- buckets[i] = JS_NULL;
- if (js_rebalancee_string_rope_rec(ctx, buckets, rope))
- goto fail;
- a = JS_NULL;
- for(i = 0; i < ROPE_N_BUCKETS; i++) {
- b = buckets[i];
- if (!JS_IsNull(b)) {
- buckets[i] = JS_NULL;
- if (JS_IsNull(a)) {
- a = b;
- } else {
- a = js_new_string_rope(ctx, b, a);
- if (JS_IsException(a))
- goto fail;
- }
- }
- }
- /* fail safe */
- if (JS_IsNull(a))
- return JS_AtomToString(ctx, JS_ATOM_empty_string);
- else
- return a;
- fail:
- for(i = 0; i < ROPE_N_BUCKETS; i++) {
- JS_FreeValue(ctx, buckets[i]);
- }
- return JS_EXCEPTION;
- }
- /* op1 and op2 are converted to strings. For convenience, op1 or op2 =
- JS_EXCEPTION are accepted and return JS_EXCEPTION. */
- static JSValue JS_ConcatString(JSContext *ctx, JSValue op1, JSValue op2)
- {
- JSString *p1, *p2;
- if (unlikely(JS_VALUE_GET_TAG(op1) != JS_TAG_STRING &&
- JS_VALUE_GET_TAG(op1) != JS_TAG_STRING_ROPE)) {
- op1 = JS_ToStringFree(ctx, op1);
- if (JS_IsException(op1)) {
- JS_FreeValue(ctx, op2);
- return JS_EXCEPTION;
- }
- }
- if (unlikely(JS_VALUE_GET_TAG(op2) != JS_TAG_STRING &&
- JS_VALUE_GET_TAG(op2) != JS_TAG_STRING_ROPE)) {
- op2 = JS_ToStringFree(ctx, op2);
- if (JS_IsException(op2)) {
- JS_FreeValue(ctx, op1);
- return JS_EXCEPTION;
- }
- }
- /* normal concatenation for short strings */
- if (JS_VALUE_GET_TAG(op2) == JS_TAG_STRING) {
- p2 = JS_VALUE_GET_STRING(op2);
- if (p2->len == 0) {
- JS_FreeValue(ctx, op2);
- return op1;
- }
- if (p2->len <= JS_STRING_ROPE_SHORT_LEN) {
- if (JS_VALUE_GET_TAG(op1) == JS_TAG_STRING) {
- p1 = JS_VALUE_GET_STRING(op1);
- if (p1->len <= JS_STRING_ROPE_SHORT2_LEN) {
- return JS_ConcatString2(ctx, op1, op2);
- } else {
- return js_new_string_rope(ctx, op1, op2);
- }
- } else {
- JSStringRope *r1;
- r1 = JS_VALUE_GET_STRING_ROPE(op1);
- if (JS_VALUE_GET_TAG(r1->right) == JS_TAG_STRING &&
- JS_VALUE_GET_STRING(r1->right)->len <= JS_STRING_ROPE_SHORT_LEN) {
- JSValue val, ret;
- val = JS_ConcatString2(ctx, JS_DupValue(ctx, r1->right), op2);
- if (JS_IsException(val)) {
- JS_FreeValue(ctx, op1);
- return JS_EXCEPTION;
- }
- ret = js_new_string_rope(ctx, JS_DupValue(ctx, r1->left), val);
- JS_FreeValue(ctx, op1);
- return ret;
- }
- }
- }
- } else if (JS_VALUE_GET_TAG(op1) == JS_TAG_STRING) {
- JSStringRope *r2;
- p1 = JS_VALUE_GET_STRING(op1);
- if (p1->len == 0) {
- JS_FreeValue(ctx, op1);
- return op2;
- }
- r2 = JS_VALUE_GET_STRING_ROPE(op2);
- if (JS_VALUE_GET_TAG(r2->left) == JS_TAG_STRING &&
- JS_VALUE_GET_STRING(r2->left)->len <= JS_STRING_ROPE_SHORT_LEN) {
- JSValue val, ret;
- val = JS_ConcatString2(ctx, op1, JS_DupValue(ctx, r2->left));
- if (JS_IsException(val)) {
- JS_FreeValue(ctx, op2);
- return JS_EXCEPTION;
- }
- ret = js_new_string_rope(ctx, val, JS_DupValue(ctx, r2->right));
- JS_FreeValue(ctx, op2);
- return ret;
- }
- }
- return js_new_string_rope(ctx, op1, op2);
- }
- /* Shape support */
- static inline size_t get_shape_size(size_t hash_size, size_t prop_size)
- {
- return sizeof(JSShape) + hash_size * sizeof(uint32_t) +
- prop_size * sizeof(JSShapeProperty);
- }
- static inline JSShapeProperty *get_shape_prop(JSShape *sh)
- {
- return (JSShapeProperty *)((uint32_t *)(sh + 1) + sh->prop_hash_mask + 1);
- }
- static int init_shape_hash(JSRuntime *rt)
- {
- rt->shape_hash_bits = 4; /* 16 shapes */
- rt->shape_hash_size = 1 << rt->shape_hash_bits;
- rt->shape_hash_count = 0;
- rt->shape_hash = js_mallocz_rt(rt, sizeof(rt->shape_hash[0]) *
- rt->shape_hash_size);
- if (!rt->shape_hash)
- return -1;
- return 0;
- }
- /* same magic hash multiplier as the Linux kernel */
- static uint32_t shape_hash(uint32_t h, uint32_t val)
- {
- return (h + val) * 0x9e370001;
- }
- /* truncate the shape hash to 'hash_bits' bits */
- static uint32_t get_shape_hash(uint32_t h, int hash_bits)
- {
- return h >> (32 - hash_bits);
- }
- static uint32_t shape_initial_hash(JSObject *proto)
- {
- uint32_t h;
- h = shape_hash(1, (uintptr_t)proto);
- if (sizeof(proto) > 4)
- h = shape_hash(h, (uint64_t)(uintptr_t)proto >> 32);
- return h;
- }
- static int resize_shape_hash(JSRuntime *rt, int new_shape_hash_bits)
- {
- int new_shape_hash_size, i;
- uint32_t h;
- JSShape **new_shape_hash, *sh, *sh_next;
- new_shape_hash_size = 1 << new_shape_hash_bits;
- new_shape_hash = js_mallocz_rt(rt, sizeof(rt->shape_hash[0]) *
- new_shape_hash_size);
- if (!new_shape_hash)
- return -1;
- for(i = 0; i < rt->shape_hash_size; i++) {
- for(sh = rt->shape_hash[i]; sh != NULL; sh = sh_next) {
- sh_next = sh->shape_hash_next;
- h = get_shape_hash(sh->hash, new_shape_hash_bits);
- sh->shape_hash_next = new_shape_hash[h];
- new_shape_hash[h] = sh;
- }
- }
- js_free_rt(rt, rt->shape_hash);
- rt->shape_hash_bits = new_shape_hash_bits;
- rt->shape_hash_size = new_shape_hash_size;
- rt->shape_hash = new_shape_hash;
- return 0;
- }
- static void js_shape_hash_link(JSRuntime *rt, JSShape *sh)
- {
- uint32_t h;
- h = get_shape_hash(sh->hash, rt->shape_hash_bits);
- sh->shape_hash_next = rt->shape_hash[h];
- rt->shape_hash[h] = sh;
- rt->shape_hash_count++;
- }
- static void js_shape_hash_unlink(JSRuntime *rt, JSShape *sh)
- {
- uint32_t h;
- JSShape **psh;
- h = get_shape_hash(sh->hash, rt->shape_hash_bits);
- psh = &rt->shape_hash[h];
- while (*psh != sh)
- psh = &(*psh)->shape_hash_next;
- *psh = sh->shape_hash_next;
- rt->shape_hash_count--;
- }
- /* create a new empty shape with prototype 'proto'. It is not hashed */
- static inline JSShape *js_new_shape_nohash(JSContext *ctx, JSObject *proto,
- int hash_size, int prop_size)
- {
- JSRuntime *rt = ctx->rt;
- JSShape *sh;
- sh = js_malloc(ctx, get_shape_size(hash_size, prop_size));
- if (!sh)
- return NULL;
- js_rc(sh)->ref_count = 1;
- add_gc_object(rt, &sh->header, JS_GC_OBJ_TYPE_SHAPE);
- if (proto)
- JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, proto));
- sh->proto = proto;
- memset(sh->hash_table, 0, sizeof(sh->hash_table[0]) * hash_size);
- sh->prop_hash_mask = hash_size - 1;
- sh->prop_size = prop_size;
- sh->prop_count = 0;
- sh->deleted_prop_count = 0;
- sh->is_hashed = FALSE;
- return sh;
- }
- /* create a new empty shape with prototype 'proto' */
- static no_inline JSShape *js_new_shape2(JSContext *ctx, JSObject *proto,
- int hash_size, int prop_size)
- {
- JSRuntime *rt = ctx->rt;
- JSShape *sh;
- /* resize the shape hash table if necessary */
- if (2 * (rt->shape_hash_count + 1) > rt->shape_hash_size) {
- resize_shape_hash(rt, rt->shape_hash_bits + 1);
- }
- sh = js_new_shape_nohash(ctx, proto, hash_size, prop_size);
- if (!sh)
- return NULL;
-
- /* insert in the hash table */
- sh->hash = shape_initial_hash(proto);
- sh->is_hashed = TRUE;
- js_shape_hash_link(ctx->rt, sh);
- return sh;
- }
- static JSShape *js_new_shape(JSContext *ctx, JSObject *proto)
- {
- return js_new_shape2(ctx, proto, JS_PROP_INITIAL_HASH_SIZE,
- JS_PROP_INITIAL_SIZE);
- }
- /* The shape is cloned. The new shape is not inserted in the shape
- hash table */
- static JSShape *js_clone_shape(JSContext *ctx, JSShape *sh1)
- {
- JSShape *sh;
- size_t size;
- JSShapeProperty *pr;
- uint32_t i, hash_size;
- hash_size = sh1->prop_hash_mask + 1;
- size = get_shape_size(hash_size, sh1->prop_size);
- sh = js_malloc(ctx, size);
- if (!sh)
- return NULL;
- memcpy(&sh->header + 1, &sh1->header + 1,
- size - sizeof(JSGCObjectHeader));
- js_rc(sh)->ref_count = 1;
- add_gc_object(ctx->rt, &sh->header, JS_GC_OBJ_TYPE_SHAPE);
- sh->is_hashed = FALSE;
- if (sh->proto) {
- JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, sh->proto));
- }
- for(i = 0, pr = get_shape_prop(sh); i < sh->prop_count; i++, pr++) {
- JS_DupAtom(ctx, pr->atom);
- }
- return sh;
- }
- static JSShape *js_dup_shape(JSShape *sh)
- {
- js_rc(sh)->ref_count++;
- return sh;
- }
- static void js_free_shape0(JSRuntime *rt, JSShape *sh)
- {
- uint32_t i;
- JSShapeProperty *pr;
- assert(js_rc(sh)->ref_count == 0);
- if (sh->is_hashed)
- js_shape_hash_unlink(rt, sh);
- if (sh->proto != NULL) {
- JS_FreeValueRT(rt, JS_MKPTR(JS_TAG_OBJECT, sh->proto));
- }
- pr = get_shape_prop(sh);
- for(i = 0; i < sh->prop_count; i++) {
- JS_FreeAtomRT(rt, pr->atom);
- pr++;
- }
- remove_gc_object(&sh->header);
- js_free_rt(rt, sh);
- }
- static void js_free_shape(JSRuntime *rt, JSShape *sh)
- {
- if (unlikely(--js_rc(sh)->ref_count <= 0)) {
- js_free_shape0(rt, sh);
- }
- }
- static void js_free_shape_null(JSRuntime *rt, JSShape *sh)
- {
- if (sh)
- js_free_shape(rt, sh);
- }
- /* make space to hold at least 'count' properties */
- static no_inline int resize_properties(JSContext *ctx, JSShape **psh,
- JSObject *p, uint32_t count)
- {
- JSShape *sh;
- uint32_t new_size, new_hash_size, new_hash_mask, i;
- JSShapeProperty *pr;
- intptr_t h;
- JSShape *old_sh;
- sh = *psh;
- new_size = max_int(count, sh->prop_size * 3 / 2);
- /* Reallocate prop array first to avoid crash or size inconsistency
- in case of memory allocation failure */
- if (p) {
- JSProperty *new_prop;
- new_prop = js_realloc(ctx, p->prop, sizeof(new_prop[0]) * new_size);
- if (unlikely(!new_prop))
- return -1;
- p->prop = new_prop;
- }
- new_hash_size = sh->prop_hash_mask + 1;
- while (new_hash_size < new_size)
- new_hash_size = 2 * new_hash_size;
- /* resize the property shapes. Using js_realloc() is not possible in
- case the GC runs during the allocation */
- old_sh = sh;
- sh = js_malloc(ctx, get_shape_size(new_hash_size, new_size));
- if (!sh)
- return -1;
- remove_gc_object(&old_sh->header);
- js_rc(sh)->ref_count = 1;
- add_gc_object(ctx->rt, &sh->header, JS_GC_OBJ_TYPE_SHAPE);
- memcpy(&sh->header + 1, &old_sh->header + 1,
- sizeof(JSShape) - sizeof(JSGCObjectHeader));
-
- if (new_hash_size != (sh->prop_hash_mask + 1)) {
- /* resize the hash table and the properties */
- new_hash_mask = new_hash_size - 1;
- sh->prop_hash_mask = new_hash_mask;
- memset(sh->hash_table, 0,
- sizeof(sh->hash_table[0]) * new_hash_size);
- memcpy(get_shape_prop(sh), get_shape_prop(old_sh),
- sizeof(JSShapeProperty) * old_sh->prop_count);
- for(i = 0, pr = get_shape_prop(sh); i < sh->prop_count; i++, pr++) {
- if (pr->atom != JS_ATOM_NULL) {
- h = ((uintptr_t)pr->atom & new_hash_mask);
- pr->hash_next = sh->hash_table[h];
- sh->hash_table[h] = i + 1;
- }
- }
- } else {
- /* just copy the previous hash table and the properties */
- memcpy(sh->hash_table, old_sh->hash_table,
- sizeof(sh->hash_table[0]) * new_hash_size);
- memcpy(get_shape_prop(sh), get_shape_prop(old_sh),
- sizeof(JSShapeProperty) * old_sh->prop_count);
- }
- js_free(ctx, old_sh);
- *psh = sh;
- sh->prop_size = new_size;
- return 0;
- }
- /* remove the deleted properties. */
- static int compact_properties(JSContext *ctx, JSObject *p)
- {
- JSShape *sh, *old_sh;
- intptr_t h;
- uint32_t new_hash_size, i, j, new_hash_mask, new_size;
- JSShapeProperty *old_pr, *pr;
- JSProperty *prop, *new_prop;
- sh = p->shape;
- assert(!sh->is_hashed);
- new_size = max_int(JS_PROP_INITIAL_SIZE,
- sh->prop_count - sh->deleted_prop_count);
- assert(new_size <= sh->prop_size);
- new_hash_size = sh->prop_hash_mask + 1;
- while ((new_hash_size / 2) >= new_size)
- new_hash_size = new_hash_size / 2;
- new_hash_mask = new_hash_size - 1;
- /* resize the hash table and the properties */
- old_sh = sh;
- sh = js_malloc(ctx, get_shape_size(new_hash_size, new_size));
- if (!sh)
- return -1;
- remove_gc_object(&old_sh->header);
- js_rc(sh)->ref_count = 1;
- add_gc_object(ctx->rt, &sh->header, JS_GC_OBJ_TYPE_SHAPE);
- memcpy(&sh->header + 1, &old_sh->header + 1,
- sizeof(JSShape) - sizeof(JSGCObjectHeader));
- memset(sh->hash_table, 0, sizeof(sh->hash_table[0]) * new_hash_size);
- sh->prop_hash_mask = new_hash_mask;
- j = 0;
- old_pr = get_shape_prop(old_sh);
- pr = get_shape_prop(sh);
- prop = p->prop;
- for(i = 0; i < sh->prop_count; i++) {
- if (old_pr->atom != JS_ATOM_NULL) {
- pr->atom = old_pr->atom;
- pr->flags = old_pr->flags;
- h = ((uintptr_t)old_pr->atom & new_hash_mask);
- pr->hash_next = sh->hash_table[h];
- sh->hash_table[h] = j + 1;
- prop[j] = prop[i];
- j++;
- pr++;
- }
- old_pr++;
- }
- assert(j == (sh->prop_count - sh->deleted_prop_count));
- sh->prop_size = new_size;
- sh->deleted_prop_count = 0;
- sh->prop_count = j;
- p->shape = sh;
- js_free(ctx, old_sh);
- /* reduce the size of the object properties */
- new_prop = js_realloc(ctx, p->prop, sizeof(new_prop[0]) * new_size);
- if (new_prop)
- p->prop = new_prop;
- return 0;
- }
- static int add_shape_property(JSContext *ctx, JSShape **psh,
- JSObject *p, JSAtom atom, int prop_flags)
- {
- JSRuntime *rt = ctx->rt;
- JSShape *sh = *psh;
- JSShapeProperty *pr, *prop;
- uint32_t hash_mask, new_shape_hash = 0;
- intptr_t h;
- /* update the shape hash */
- if (sh->is_hashed) {
- js_shape_hash_unlink(rt, sh);
- new_shape_hash = shape_hash(shape_hash(sh->hash, atom), prop_flags);
- }
- if (unlikely(sh->prop_count >= sh->prop_size)) {
- if (resize_properties(ctx, psh, p, sh->prop_count + 1)) {
- /* in case of error, reinsert in the hash table.
- sh is still valid if resize_properties() failed */
- if (sh->is_hashed)
- js_shape_hash_link(rt, sh);
- return -1;
- }
- sh = *psh;
- }
- if (sh->is_hashed) {
- sh->hash = new_shape_hash;
- js_shape_hash_link(rt, sh);
- }
- /* Initialize the new shape property.
- The object property at p->prop[sh->prop_count] is uninitialized */
- prop = get_shape_prop(sh);
- pr = &prop[sh->prop_count++];
- pr->atom = JS_DupAtom(ctx, atom);
- pr->flags = prop_flags;
- /* add in hash table */
- hash_mask = sh->prop_hash_mask;
- h = atom & hash_mask;
- pr->hash_next = sh->hash_table[h];
- sh->hash_table[h] = sh->prop_count;
- return 0;
- }
- /* find a hashed empty shape matching the prototype. Return NULL if
- not found */
- static JSShape *find_hashed_shape_proto(JSRuntime *rt, JSObject *proto)
- {
- JSShape *sh1;
- uint32_t h, h1;
- h = shape_initial_hash(proto);
- h1 = get_shape_hash(h, rt->shape_hash_bits);
- for(sh1 = rt->shape_hash[h1]; sh1 != NULL; sh1 = sh1->shape_hash_next) {
- if (sh1->hash == h &&
- sh1->proto == proto &&
- sh1->prop_count == 0) {
- return sh1;
- }
- }
- return NULL;
- }
- /* find a hashed shape matching sh + (prop, prop_flags). Return NULL if
- not found */
- static JSShape *find_hashed_shape_prop(JSRuntime *rt, JSShape *sh,
- JSAtom atom, int prop_flags)
- {
- JSShape *sh1;
- uint32_t h, h1, i, n;
- h = sh->hash;
- h = shape_hash(h, atom);
- h = shape_hash(h, prop_flags);
- h1 = get_shape_hash(h, rt->shape_hash_bits);
- for(sh1 = rt->shape_hash[h1]; sh1 != NULL; sh1 = sh1->shape_hash_next) {
- /* we test the hash first so that the rest is done only if the
- shapes really match */
- if (sh1->hash == h &&
- sh1->proto == sh->proto &&
- sh1->prop_count == ((n = sh->prop_count) + 1)) {
- JSShapeProperty *prop = get_shape_prop(sh);
- JSShapeProperty *prop1 = get_shape_prop(sh1);
- for(i = 0; i < n; i++) {
- if (unlikely(prop1[i].atom != prop[i].atom) ||
- unlikely(prop1[i].flags != prop[i].flags))
- goto next;
- }
- if (unlikely(prop1[n].atom != atom) ||
- unlikely(prop1[n].flags != prop_flags))
- goto next;
- return sh1;
- }
- next: ;
- }
- return NULL;
- }
- static __maybe_unused void JS_DumpShape(JSRuntime *rt, int i, JSShape *sh)
- {
- char atom_buf[ATOM_GET_STR_BUF_SIZE];
- int j;
- /* XXX: should output readable class prototype */
- printf("%5d %3d%c %14p %5d %5d", i,
- js_rc(sh)->ref_count, " *"[sh->is_hashed],
- (void *)sh->proto, sh->prop_size, sh->prop_count);
- for(j = 0; j < sh->prop_count; j++) {
- printf(" %s", JS_AtomGetStrRT(rt, atom_buf, sizeof(atom_buf),
- get_shape_prop(sh)[j].atom));
- }
- printf("\n");
- }
- static __maybe_unused void JS_DumpShapes(JSRuntime *rt)
- {
- int i;
- JSShape *sh;
- struct list_head *el;
- JSObject *p;
- JSGCObjectHeader *gp;
- printf("JSShapes: {\n");
- printf("%5s %4s %14s %5s %5s %s\n", "SLOT", "REFS", "PROTO", "SIZE", "COUNT", "PROPS");
- for(i = 0; i < rt->shape_hash_size; i++) {
- for(sh = rt->shape_hash[i]; sh != NULL; sh = sh->shape_hash_next) {
- JS_DumpShape(rt, i, sh);
- assert(sh->is_hashed);
- }
- }
- /* dump non-hashed shapes */
- list_for_each(el, &rt->gc_obj_list) {
- gp = list_entry(el, JSGCObjectHeader, link);
- if (js_rc(gp)->gc_obj_type == JS_GC_OBJ_TYPE_JS_OBJECT) {
- p = (JSObject *)gp;
- if (!p->shape->is_hashed) {
- JS_DumpShape(rt, -1, p->shape);
- }
- }
- }
- printf("}\n");
- }
- /* 'props[]' is used to initialized the object properties. The number
- of elements depends on the shape. */
- static JSValue JS_NewObjectFromShape(JSContext *ctx, JSShape *sh, JSClassID class_id,
- JSProperty *props)
- {
- JSObject *p;
- int i;
-
- js_trigger_gc(ctx->rt, sizeof(JSObject));
- p = js_malloc(ctx, sizeof(JSObject));
- if (unlikely(!p))
- goto fail;
- p->class_id = class_id;
- p->is_std_array_prototype = 0;
- p->extensible = TRUE;
- p->free_mark = 0;
- p->is_exotic = 0;
- p->fast_array = 0;
- p->is_constructor = 0;
- p->has_immutable_prototype = 0;
- p->tmp_mark = 0;
- p->is_HTMLDDA = 0;
- p->weakref_count = 0;
- p->u.opaque = NULL;
- p->shape = sh;
- p->prop = js_malloc(ctx, sizeof(JSProperty) * sh->prop_size);
- if (unlikely(!p->prop)) {
- js_free(ctx, p);
- fail:
- if (props) {
- JSShapeProperty *prs = get_shape_prop(sh);
- for(i = 0; i < sh->prop_count; i++) {
- free_property(ctx->rt, &props[i], prs->flags);
- prs++;
- }
- }
- js_free_shape(ctx->rt, sh);
- return JS_EXCEPTION;
- }
- switch(class_id) {
- case JS_CLASS_OBJECT:
- break;
- case JS_CLASS_ARRAY:
- {
- JSProperty *pr;
- p->is_exotic = 1;
- p->fast_array = 1;
- p->u.array.u.values = NULL;
- p->u.array.count = 0;
- p->u.array.u1.size = 0;
- if (!props) {
- /* XXX: remove */
- /* the length property is always the first one */
- if (likely(sh == ctx->array_shape)) {
- pr = &p->prop[0];
- } else {
- /* only used for the first array */
- /* cannot fail */
- pr = add_property(ctx, p, JS_ATOM_length,
- JS_PROP_WRITABLE | JS_PROP_LENGTH);
- }
- pr->u.value = JS_NewInt32(ctx, 0);
- }
- }
- break;
- case JS_CLASS_C_FUNCTION:
- p->prop[0].u.value = JS_UNDEFINED;
- break;
- case JS_CLASS_ARGUMENTS:
- case JS_CLASS_MAPPED_ARGUMENTS:
- case JS_CLASS_UINT8C_ARRAY:
- case JS_CLASS_INT8_ARRAY:
- case JS_CLASS_UINT8_ARRAY:
- case JS_CLASS_INT16_ARRAY:
- case JS_CLASS_UINT16_ARRAY:
- case JS_CLASS_INT32_ARRAY:
- case JS_CLASS_UINT32_ARRAY:
- case JS_CLASS_BIG_INT64_ARRAY:
- case JS_CLASS_BIG_UINT64_ARRAY:
- case JS_CLASS_FLOAT16_ARRAY:
- case JS_CLASS_FLOAT32_ARRAY:
- case JS_CLASS_FLOAT64_ARRAY:
- p->is_exotic = 1;
- p->fast_array = 1;
- p->u.array.u.ptr = NULL;
- p->u.array.count = 0;
- break;
- case JS_CLASS_DATAVIEW:
- p->u.array.u.ptr = NULL;
- p->u.array.count = 0;
- break;
- case JS_CLASS_NUMBER:
- case JS_CLASS_STRING:
- case JS_CLASS_BOOLEAN:
- case JS_CLASS_SYMBOL:
- case JS_CLASS_DATE:
- case JS_CLASS_BIG_INT:
- p->u.object_data = JS_UNDEFINED;
- goto set_exotic;
- case JS_CLASS_REGEXP:
- p->u.regexp.pattern = NULL;
- p->u.regexp.bytecode = NULL;
- break;
- case JS_CLASS_GLOBAL_OBJECT:
- p->u.global_object.uninitialized_vars = JS_UNDEFINED;
- break;
- default:
- set_exotic:
- if (ctx->rt->class_array[class_id].exotic) {
- p->is_exotic = 1;
- }
- break;
- }
- js_rc(p)->ref_count = 1;
- add_gc_object(ctx->rt, &p->header, JS_GC_OBJ_TYPE_JS_OBJECT);
- if (props) {
- for(i = 0; i < sh->prop_count; i++)
- p->prop[i] = props[i];
- }
- return JS_MKPTR(JS_TAG_OBJECT, p);
- }
- static JSObject *get_proto_obj(JSValueConst proto_val)
- {
- if (JS_VALUE_GET_TAG(proto_val) != JS_TAG_OBJECT)
- return NULL;
- else
- return JS_VALUE_GET_OBJ(proto_val);
- }
- /* WARNING: proto must be an object or JS_NULL */
- JSValue JS_NewObjectProtoClass(JSContext *ctx, JSValueConst proto_val,
- JSClassID class_id)
- {
- JSShape *sh;
- JSObject *proto;
- proto = get_proto_obj(proto_val);
- sh = find_hashed_shape_proto(ctx->rt, proto);
- if (likely(sh)) {
- sh = js_dup_shape(sh);
- } else {
- sh = js_new_shape(ctx, proto);
- if (!sh)
- return JS_EXCEPTION;
- }
- return JS_NewObjectFromShape(ctx, sh, class_id, NULL);
- }
- /* WARNING: the shape is not hashed. It is used for objects where
- factorizing the shape is not relevant (prototypes, constructors) */
- static JSValue JS_NewObjectProtoClassAlloc(JSContext *ctx, JSValueConst proto_val,
- JSClassID class_id, int n_alloc_props)
- {
- JSShape *sh;
- JSObject *proto;
- int hash_size, hash_bits;
-
- if (n_alloc_props <= JS_PROP_INITIAL_SIZE) {
- n_alloc_props = JS_PROP_INITIAL_SIZE;
- hash_size = JS_PROP_INITIAL_HASH_SIZE;
- } else {
- hash_bits = 32 - clz32(n_alloc_props - 1); /* ceil(log2(radix)) */
- hash_size = 1 << hash_bits;
- }
- proto = get_proto_obj(proto_val);
- sh = js_new_shape_nohash(ctx, proto, hash_size, n_alloc_props);
- if (!sh)
- return JS_EXCEPTION;
- return JS_NewObjectFromShape(ctx, sh, class_id, NULL);
- }
- #if 0
- static JSValue JS_GetObjectData(JSContext *ctx, JSValueConst obj)
- {
- JSObject *p;
- if (JS_VALUE_GET_TAG(obj) == JS_TAG_OBJECT) {
- p = JS_VALUE_GET_OBJ(obj);
- switch(p->class_id) {
- case JS_CLASS_NUMBER:
- case JS_CLASS_STRING:
- case JS_CLASS_BOOLEAN:
- case JS_CLASS_SYMBOL:
- case JS_CLASS_DATE:
- case JS_CLASS_BIG_INT:
- return JS_DupValue(ctx, p->u.object_data);
- }
- }
- return JS_UNDEFINED;
- }
- #endif
- static int JS_SetObjectData(JSContext *ctx, JSValueConst obj, JSValue val)
- {
- JSObject *p;
- if (JS_VALUE_GET_TAG(obj) == JS_TAG_OBJECT) {
- p = JS_VALUE_GET_OBJ(obj);
- switch(p->class_id) {
- case JS_CLASS_NUMBER:
- case JS_CLASS_STRING:
- case JS_CLASS_BOOLEAN:
- case JS_CLASS_SYMBOL:
- case JS_CLASS_DATE:
- case JS_CLASS_BIG_INT:
- JS_FreeValue(ctx, p->u.object_data);
- p->u.object_data = val; /* for JS_CLASS_STRING, 'val' must
- be JS_TAG_STRING (and not a
- rope) */
- return 0;
- }
- }
- JS_FreeValue(ctx, val);
- if (!JS_IsException(obj))
- JS_ThrowTypeError(ctx, "invalid object type");
- return -1;
- }
- JSValue JS_NewObjectClass(JSContext *ctx, int class_id)
- {
- return JS_NewObjectProtoClass(ctx, ctx->class_proto[class_id], class_id);
- }
- JSValue JS_NewObjectProto(JSContext *ctx, JSValueConst proto)
- {
- return JS_NewObjectProtoClass(ctx, proto, JS_CLASS_OBJECT);
- }
- JSValue JS_NewArray(JSContext *ctx)
- {
- return JS_NewObjectFromShape(ctx, js_dup_shape(ctx->array_shape),
- JS_CLASS_ARRAY, NULL);
- }
- JSValue JS_NewObject(JSContext *ctx)
- {
- /* inline JS_NewObjectClass(ctx, JS_CLASS_OBJECT); */
- return JS_NewObjectProtoClass(ctx, ctx->class_proto[JS_CLASS_OBJECT], JS_CLASS_OBJECT);
- }
- static void js_function_set_properties(JSContext *ctx, JSValueConst func_obj,
- JSAtom name, int len)
- {
- /* ES6 feature non compatible with ES5.1: length is configurable */
- JS_DefinePropertyValue(ctx, func_obj, JS_ATOM_length, JS_NewInt32(ctx, len),
- JS_PROP_CONFIGURABLE);
- JS_DefinePropertyValue(ctx, func_obj, JS_ATOM_name,
- JS_AtomToString(ctx, name), JS_PROP_CONFIGURABLE);
- }
- static BOOL js_class_has_bytecode(JSClassID class_id)
- {
- return (class_id == JS_CLASS_BYTECODE_FUNCTION ||
- class_id == JS_CLASS_GENERATOR_FUNCTION ||
- class_id == JS_CLASS_ASYNC_FUNCTION ||
- class_id == JS_CLASS_ASYNC_GENERATOR_FUNCTION);
- }
- /* return NULL without exception if not a function or no bytecode */
- static JSFunctionBytecode *JS_GetFunctionBytecode(JSValueConst val)
- {
- JSObject *p;
- if (JS_VALUE_GET_TAG(val) != JS_TAG_OBJECT)
- return NULL;
- p = JS_VALUE_GET_OBJ(val);
- if (!js_class_has_bytecode(p->class_id))
- return NULL;
- return p->u.func.function_bytecode;
- }
- static void js_method_set_home_object(JSContext *ctx, JSValueConst func_obj,
- JSValueConst home_obj)
- {
- JSObject *p, *p1;
- JSFunctionBytecode *b;
- if (JS_VALUE_GET_TAG(func_obj) != JS_TAG_OBJECT)
- return;
- p = JS_VALUE_GET_OBJ(func_obj);
- if (!js_class_has_bytecode(p->class_id))
- return;
- b = p->u.func.function_bytecode;
- if (b->need_home_object) {
- p1 = p->u.func.home_object;
- if (p1) {
- JS_FreeValue(ctx, JS_MKPTR(JS_TAG_OBJECT, p1));
- }
- if (JS_VALUE_GET_TAG(home_obj) == JS_TAG_OBJECT)
- p1 = JS_VALUE_GET_OBJ(JS_DupValue(ctx, home_obj));
- else
- p1 = NULL;
- p->u.func.home_object = p1;
- }
- }
- static JSValue js_get_function_name(JSContext *ctx, JSAtom name)
- {
- JSValue name_str;
- name_str = JS_AtomToString(ctx, name);
- if (JS_AtomSymbolHasDescription(ctx, name)) {
- name_str = JS_ConcatString3(ctx, "[", name_str, "]");
- }
- return name_str;
- }
- /* Modify the name of a method according to the atom and
- 'flags'. 'flags' is a bitmask of JS_PROP_HAS_GET and
- JS_PROP_HAS_SET. Also set the home object of the method.
- Return < 0 if exception. */
- static int js_method_set_properties(JSContext *ctx, JSValueConst func_obj,
- JSAtom name, int flags, JSValueConst home_obj)
- {
- JSValue name_str;
- name_str = js_get_function_name(ctx, name);
- if (flags & JS_PROP_HAS_GET) {
- name_str = JS_ConcatString3(ctx, "get ", name_str, "");
- } else if (flags & JS_PROP_HAS_SET) {
- name_str = JS_ConcatString3(ctx, "set ", name_str, "");
- }
- if (JS_IsException(name_str))
- return -1;
- if (JS_DefinePropertyValue(ctx, func_obj, JS_ATOM_name, name_str,
- JS_PROP_CONFIGURABLE) < 0)
- return -1;
- js_method_set_home_object(ctx, func_obj, home_obj);
- return 0;
- }
- /* Note: at least 'length' arguments will be readable in 'argv' */
- static JSValue JS_NewCFunction3(JSContext *ctx, JSCFunction *func,
- const char *name,
- int length, JSCFunctionEnum cproto, int magic,
- JSValueConst proto_val, int n_fields)
- {
- JSValue func_obj;
- JSObject *p;
- JSAtom name_atom;
- if (n_fields > 0) {
- func_obj = JS_NewObjectProtoClassAlloc(ctx, proto_val, JS_CLASS_C_FUNCTION, n_fields);
- } else {
- func_obj = JS_NewObjectProtoClass(ctx, proto_val, JS_CLASS_C_FUNCTION);
- }
- if (JS_IsException(func_obj))
- return func_obj;
- p = JS_VALUE_GET_OBJ(func_obj);
- p->u.cfunc.realm = JS_DupContext(ctx);
- p->u.cfunc.c_function.generic = func;
- p->u.cfunc.length = length;
- p->u.cfunc.cproto = cproto;
- p->u.cfunc.magic = magic;
- p->is_constructor = (cproto == JS_CFUNC_constructor ||
- cproto == JS_CFUNC_constructor_magic ||
- cproto == JS_CFUNC_constructor_or_func ||
- cproto == JS_CFUNC_constructor_or_func_magic);
- if (!name)
- name = "";
- name_atom = JS_NewAtom(ctx, name);
- if (name_atom == JS_ATOM_NULL) {
- JS_FreeValue(ctx, func_obj);
- return JS_EXCEPTION;
- }
- js_function_set_properties(ctx, func_obj, name_atom, length);
- JS_FreeAtom(ctx, name_atom);
- return func_obj;
- }
- /* Note: at least 'length' arguments will be readable in 'argv' */
- JSValue JS_NewCFunction2(JSContext *ctx, JSCFunction *func,
- const char *name,
- int length, JSCFunctionEnum cproto, int magic)
- {
- return JS_NewCFunction3(ctx, func, name, length, cproto, magic,
- ctx->function_proto, 0);
- }
- typedef struct JSCFunctionDataRecord {
- JSCFunctionData *func;
- uint8_t length;
- uint8_t data_len;
- uint16_t magic;
- JSValue data[0];
- } JSCFunctionDataRecord;
- static void js_c_function_data_finalizer(JSRuntime *rt, JSValue val)
- {
- JSCFunctionDataRecord *s = JS_GetOpaque(val, JS_CLASS_C_FUNCTION_DATA);
- int i;
- if (s) {
- for(i = 0; i < s->data_len; i++) {
- JS_FreeValueRT(rt, s->data[i]);
- }
- js_free_rt(rt, s);
- }
- }
- static void js_c_function_data_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func)
- {
- JSCFunctionDataRecord *s = JS_GetOpaque(val, JS_CLASS_C_FUNCTION_DATA);
- int i;
- if (s) {
- for(i = 0; i < s->data_len; i++) {
- JS_MarkValue(rt, s->data[i], mark_func);
- }
- }
- }
- static JSValue js_c_function_data_call(JSContext *ctx, JSValueConst func_obj,
- JSValueConst this_val,
- int argc, JSValueConst *argv, int flags)
- {
- JSCFunctionDataRecord *s = JS_GetOpaque(func_obj, JS_CLASS_C_FUNCTION_DATA);
- JSValueConst *arg_buf;
- int i;
- /* XXX: could add the function on the stack for debug */
- if (unlikely(argc < s->length)) {
- arg_buf = alloca(sizeof(arg_buf[0]) * s->length);
- for(i = 0; i < argc; i++)
- arg_buf[i] = argv[i];
- for(i = argc; i < s->length; i++)
- arg_buf[i] = JS_UNDEFINED;
- } else {
- arg_buf = argv;
- }
- return s->func(ctx, this_val, argc, arg_buf, s->magic, s->data);
- }
- JSValue JS_NewCFunctionData(JSContext *ctx, JSCFunctionData *func,
- int length, int magic, int data_len,
- JSValueConst *data)
- {
- JSCFunctionDataRecord *s;
- JSValue func_obj;
- int i;
- func_obj = JS_NewObjectProtoClass(ctx, ctx->function_proto,
- JS_CLASS_C_FUNCTION_DATA);
- if (JS_IsException(func_obj))
- return func_obj;
- s = js_malloc(ctx, sizeof(*s) + data_len * sizeof(JSValue));
- if (!s) {
- JS_FreeValue(ctx, func_obj);
- return JS_EXCEPTION;
- }
- s->func = func;
- s->length = length;
- s->data_len = data_len;
- s->magic = magic;
- for(i = 0; i < data_len; i++)
- s->data[i] = JS_DupValue(ctx, data[i]);
- JS_SetOpaque(func_obj, s);
- js_function_set_properties(ctx, func_obj,
- JS_ATOM_empty_string, length);
- return func_obj;
- }
- static JSContext *js_autoinit_get_realm(JSProperty *pr)
- {
- return (JSContext *)(pr->u.init.realm_and_id & ~3);
- }
- static JSAutoInitIDEnum js_autoinit_get_id(JSProperty *pr)
- {
- return pr->u.init.realm_and_id & 3;
- }
- static void js_autoinit_free(JSRuntime *rt, JSProperty *pr)
- {
- JS_FreeContext(js_autoinit_get_realm(pr));
- }
- static void js_autoinit_mark(JSRuntime *rt, JSProperty *pr,
- JS_MarkFunc *mark_func)
- {
- mark_func(rt, &js_autoinit_get_realm(pr)->header);
- }
- static void free_property(JSRuntime *rt, JSProperty *pr, int prop_flags)
- {
- if (unlikely(prop_flags & JS_PROP_TMASK)) {
- if ((prop_flags & JS_PROP_TMASK) == JS_PROP_GETSET) {
- if (pr->u.getset.getter)
- JS_FreeValueRT(rt, JS_MKPTR(JS_TAG_OBJECT, pr->u.getset.getter));
- if (pr->u.getset.setter)
- JS_FreeValueRT(rt, JS_MKPTR(JS_TAG_OBJECT, pr->u.getset.setter));
- } else if ((prop_flags & JS_PROP_TMASK) == JS_PROP_VARREF) {
- free_var_ref(rt, pr->u.var_ref);
- } else if ((prop_flags & JS_PROP_TMASK) == JS_PROP_AUTOINIT) {
- js_autoinit_free(rt, pr);
- }
- } else {
- JS_FreeValueRT(rt, pr->u.value);
- }
- }
- static force_inline JSShapeProperty *find_own_property1(JSObject *p,
- JSAtom atom)
- {
- JSShape *sh;
- JSShapeProperty *pr, *prop;
- intptr_t h;
- sh = p->shape;
- h = (uintptr_t)atom & sh->prop_hash_mask;
- h = sh->hash_table[h];
- prop = get_shape_prop(sh);
- while (h) {
- pr = &prop[h - 1];
- if (likely(pr->atom == atom)) {
- return pr;
- }
- h = pr->hash_next;
- }
- return NULL;
- }
- static force_inline JSShapeProperty *find_own_property(JSProperty **ppr,
- JSObject *p,
- JSAtom atom)
- {
- JSShape *sh;
- JSShapeProperty *pr, *prop;
- intptr_t h;
- sh = p->shape;
- h = (uintptr_t)atom & sh->prop_hash_mask;
- h = sh->hash_table[h];
- prop = get_shape_prop(sh);
- while (h) {
- pr = &prop[h - 1];
- if (likely(pr->atom == atom)) {
- *ppr = &p->prop[h - 1];
- /* the compiler should be able to assume that pr != NULL here */
- return pr;
- }
- h = pr->hash_next;
- }
- *ppr = NULL;
- return NULL;
- }
- /* indicate that the object may be part of a function prototype cycle */
- static void set_cycle_flag(JSContext *ctx, JSValueConst obj)
- {
- }
- static void free_var_ref(JSRuntime *rt, JSVarRef *var_ref)
- {
- if (var_ref) {
- assert(js_rc(var_ref)->ref_count > 0);
- if (--js_rc(var_ref)->ref_count == 0) {
- if (var_ref->is_detached) {
- JS_FreeValueRT(rt, var_ref->value);
- } else {
- JSStackFrame *sf = var_ref->stack_frame;
- assert(sf->var_refs[var_ref->var_ref_idx] == var_ref);
- sf->var_refs[var_ref->var_ref_idx] = NULL;
- if (sf->js_mode & JS_MODE_ASYNC) {
- JSAsyncFunctionState *async_func = container_of(sf, JSAsyncFunctionState, frame);
- async_func_free(rt, async_func);
- }
- }
- remove_gc_object(&var_ref->header);
- js_free_rt(rt, var_ref);
- }
- }
- }
- static void js_array_finalizer(JSRuntime *rt, JSValue val)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- int i;
- for(i = 0; i < p->u.array.count; i++) {
- JS_FreeValueRT(rt, p->u.array.u.values[i]);
- }
- js_free_rt(rt, p->u.array.u.values);
- }
- static void js_array_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- int i;
- for(i = 0; i < p->u.array.count; i++) {
- JS_MarkValue(rt, p->u.array.u.values[i], mark_func);
- }
- }
- static void js_object_data_finalizer(JSRuntime *rt, JSValue val)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JS_FreeValueRT(rt, p->u.object_data);
- p->u.object_data = JS_UNDEFINED;
- }
- static void js_object_data_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JS_MarkValue(rt, p->u.object_data, mark_func);
- }
- static void js_c_function_finalizer(JSRuntime *rt, JSValue val)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- if (p->u.cfunc.realm)
- JS_FreeContext(p->u.cfunc.realm);
- }
- static void js_c_function_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- if (p->u.cfunc.realm)
- mark_func(rt, &p->u.cfunc.realm->header);
- }
- static void js_bytecode_function_finalizer(JSRuntime *rt, JSValue val)
- {
- JSObject *p1, *p = JS_VALUE_GET_OBJ(val);
- JSFunctionBytecode *b;
- JSVarRef **var_refs;
- int i;
- p1 = p->u.func.home_object;
- if (p1) {
- JS_FreeValueRT(rt, JS_MKPTR(JS_TAG_OBJECT, p1));
- }
- b = p->u.func.function_bytecode;
- if (b) {
- var_refs = p->u.func.var_refs;
- if (var_refs) {
- for(i = 0; i < b->closure_var_count; i++)
- free_var_ref(rt, var_refs[i]);
- js_free_rt(rt, var_refs);
- }
- JS_FreeValueRT(rt, JS_MKPTR(JS_TAG_FUNCTION_BYTECODE, b));
- }
- }
- static void js_bytecode_function_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JSVarRef **var_refs = p->u.func.var_refs;
- JSFunctionBytecode *b = p->u.func.function_bytecode;
- int i;
- if (p->u.func.home_object) {
- JS_MarkValue(rt, JS_MKPTR(JS_TAG_OBJECT, p->u.func.home_object),
- mark_func);
- }
- if (b) {
- if (var_refs) {
- for(i = 0; i < b->closure_var_count; i++) {
- JSVarRef *var_ref = var_refs[i];
- if (var_ref) {
- mark_func(rt, &var_ref->header);
- }
- }
- }
- /* must mark the function bytecode because template objects may be
- part of a cycle */
- JS_MarkValue(rt, JS_MKPTR(JS_TAG_FUNCTION_BYTECODE, b), mark_func);
- }
- }
- static void js_bound_function_finalizer(JSRuntime *rt, JSValue val)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JSBoundFunction *bf = p->u.bound_function;
- int i;
- JS_FreeValueRT(rt, bf->func_obj);
- JS_FreeValueRT(rt, bf->this_val);
- for(i = 0; i < bf->argc; i++) {
- JS_FreeValueRT(rt, bf->argv[i]);
- }
- js_free_rt(rt, bf);
- }
- static void js_bound_function_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JSBoundFunction *bf = p->u.bound_function;
- int i;
- JS_MarkValue(rt, bf->func_obj, mark_func);
- JS_MarkValue(rt, bf->this_val, mark_func);
- for(i = 0; i < bf->argc; i++)
- JS_MarkValue(rt, bf->argv[i], mark_func);
- }
- static void js_for_in_iterator_finalizer(JSRuntime *rt, JSValue val)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JSForInIterator *it = p->u.for_in_iterator;
- int i;
- JS_FreeValueRT(rt, it->obj);
- if (!it->is_array) {
- for(i = 0; i < it->atom_count; i++) {
- JS_FreeAtomRT(rt, it->tab_atom[i].atom);
- }
- js_free_rt(rt, it->tab_atom);
- }
- js_free_rt(rt, it);
- }
- static void js_for_in_iterator_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JSForInIterator *it = p->u.for_in_iterator;
- JS_MarkValue(rt, it->obj, mark_func);
- }
- static void free_object(JSRuntime *rt, JSObject *p)
- {
- int i;
- JSClassFinalizer *finalizer;
- JSShape *sh;
- JSShapeProperty *pr;
- p->free_mark = 1; /* used to tell the object is invalid when
- freeing cycles */
- /* free all the fields */
- sh = p->shape;
- pr = get_shape_prop(sh);
- for(i = 0; i < sh->prop_count; i++) {
- free_property(rt, &p->prop[i], pr->flags);
- pr++;
- }
- js_free_rt(rt, p->prop);
- /* as an optimization we destroy the shape immediately without
- putting it in gc_zero_ref_count_list */
- js_free_shape(rt, sh);
- /* fail safe */
- p->shape = NULL;
- p->prop = NULL;
- finalizer = rt->class_array[p->class_id].finalizer;
- if (finalizer)
- (*finalizer)(rt, JS_MKPTR(JS_TAG_OBJECT, p));
- /* fail safe */
- p->class_id = 0;
- p->u.opaque = NULL;
- p->u.func.var_refs = NULL;
- p->u.func.home_object = NULL;
- remove_gc_object(&p->header);
- if (rt->gc_phase == JS_GC_PHASE_REMOVE_CYCLES) {
- if (js_rc(p)->ref_count == 0 && p->weakref_count == 0) {
- js_free_rt(rt, p);
- } else {
- /* keep the object structure because there are may be
- references to it */
- list_add_tail(&p->header.link, &rt->gc_zero_ref_count_list);
- }
- } else {
- /* keep the object structure in case there are weak references to it */
- if (p->weakref_count == 0) {
- js_free_rt(rt, p);
- } else {
- js_rc(p)->mark = 0; /* reset the mark so that the weakref can be freed */
- }
- }
- }
- static void free_gc_object(JSRuntime *rt, JSGCObjectHeader *gp)
- {
- switch(js_rc(gp)->gc_obj_type) {
- case JS_GC_OBJ_TYPE_JS_OBJECT:
- free_object(rt, (JSObject *)gp);
- break;
- case JS_GC_OBJ_TYPE_FUNCTION_BYTECODE:
- free_function_bytecode(rt, (JSFunctionBytecode *)gp);
- break;
- case JS_GC_OBJ_TYPE_ASYNC_FUNCTION:
- __async_func_free(rt, (JSAsyncFunctionState *)gp);
- break;
- case JS_GC_OBJ_TYPE_MODULE:
- js_free_module_def(rt, (JSModuleDef *)gp);
- break;
- default:
- abort();
- }
- }
- static void free_zero_refcount(JSRuntime *rt)
- {
- struct list_head *el;
- JSGCObjectHeader *p;
- rt->gc_phase = JS_GC_PHASE_DECREF;
- for(;;) {
- el = rt->gc_zero_ref_count_list.next;
- if (el == &rt->gc_zero_ref_count_list)
- break;
- p = list_entry(el, JSGCObjectHeader, link);
- assert(js_rc(p)->ref_count == 0);
- free_gc_object(rt, p);
- }
- rt->gc_phase = JS_GC_PHASE_NONE;
- }
- /* called with the ref_count of 'v' reaches zero. */
- void __JS_FreeValueRT(JSRuntime *rt, JSValue v)
- {
- uint32_t tag = JS_VALUE_GET_TAG(v);
- #ifdef DUMP_FREE
- {
- printf("Freeing ");
- if (tag == JS_TAG_OBJECT) {
- JS_DumpObject(rt, JS_VALUE_GET_OBJ(v));
- } else {
- JS_DumpValueShort(rt, v);
- printf("\n");
- }
- }
- #endif
- switch(tag) {
- case JS_TAG_STRING:
- {
- JSString *p = JS_VALUE_GET_STRING(v);
- if (p->atom_type) {
- JS_FreeAtomStruct(rt, p);
- } else {
- #ifdef DUMP_LEAKS
- list_del(&p->link);
- #endif
- js_free_rt(rt, p);
- }
- }
- break;
- case JS_TAG_STRING_ROPE:
- /* Note: recursion is acceptable because the rope depth is bounded */
- {
- JSStringRope *p = JS_VALUE_GET_STRING_ROPE(v);
- JS_FreeValueRT(rt, p->left);
- JS_FreeValueRT(rt, p->right);
- js_free_rt(rt, p);
- }
- break;
- case JS_TAG_OBJECT:
- case JS_TAG_FUNCTION_BYTECODE:
- case JS_TAG_MODULE:
- {
- JSGCObjectHeader *p = JS_VALUE_GET_PTR(v);
- if (rt->gc_phase != JS_GC_PHASE_REMOVE_CYCLES) {
- list_del(&p->link);
- list_add(&p->link, &rt->gc_zero_ref_count_list);
- js_rc(p)->mark = 1; /* indicate that the object is about to be freed */
- if (rt->gc_phase == JS_GC_PHASE_NONE) {
- free_zero_refcount(rt);
- }
- }
- }
- break;
- case JS_TAG_BIG_INT:
- {
- JSBigInt *p = JS_VALUE_GET_PTR(v);
- js_free_rt(rt, p);
- }
- break;
- case JS_TAG_SYMBOL:
- {
- JSAtomStruct *p = JS_VALUE_GET_PTR(v);
- JS_FreeAtomStruct(rt, p);
- }
- break;
- default:
- abort();
- }
- }
- void __JS_FreeValue(JSContext *ctx, JSValue v)
- {
- __JS_FreeValueRT(ctx->rt, v);
- }
- /* garbage collection */
- static void gc_remove_weak_objects(JSRuntime *rt)
- {
- struct list_head *el;
- /* add the freed objects to rt->gc_zero_ref_count_list so that
- rt->weakref_list is not modified while we traverse it */
- rt->gc_phase = JS_GC_PHASE_DECREF;
-
- list_for_each(el, &rt->weakref_list) {
- JSWeakRefHeader *wh = list_entry(el, JSWeakRefHeader, link);
- switch(wh->weakref_type) {
- case JS_WEAKREF_TYPE_MAP:
- map_delete_weakrefs(rt, wh);
- break;
- case JS_WEAKREF_TYPE_WEAKREF:
- weakref_delete_weakref(rt, wh);
- break;
- case JS_WEAKREF_TYPE_FINREC:
- finrec_delete_weakref(rt, wh);
- break;
- default:
- abort();
- }
- }
- rt->gc_phase = JS_GC_PHASE_NONE;
- /* free the freed objects here. */
- free_zero_refcount(rt);
- }
- static void add_gc_object(JSRuntime *rt, JSGCObjectHeader *h,
- JSGCObjectTypeEnum type)
- {
- js_rc(h)->mark = 0;
- js_rc(h)->gc_obj_type = type;
- list_add_tail(&h->link, &rt->gc_obj_list);
- }
- static void remove_gc_object(JSGCObjectHeader *h)
- {
- list_del(&h->link);
- }
- void JS_MarkValue(JSRuntime *rt, JSValueConst val, JS_MarkFunc *mark_func)
- {
- if (JS_VALUE_HAS_REF_COUNT(val)) {
- switch(JS_VALUE_GET_TAG(val)) {
- case JS_TAG_OBJECT:
- case JS_TAG_FUNCTION_BYTECODE:
- case JS_TAG_MODULE:
- mark_func(rt, JS_VALUE_GET_PTR(val));
- break;
- default:
- break;
- }
- }
- }
- static void mark_children(JSRuntime *rt, JSGCObjectHeader *gp,
- JS_MarkFunc *mark_func)
- {
- switch(js_rc(gp)->gc_obj_type) {
- case JS_GC_OBJ_TYPE_JS_OBJECT:
- {
- JSObject *p = (JSObject *)gp;
- JSShapeProperty *prs;
- JSShape *sh;
- int i;
- sh = p->shape;
- mark_func(rt, &sh->header);
- /* mark all the fields */
- prs = get_shape_prop(sh);
- for(i = 0; i < sh->prop_count; i++) {
- JSProperty *pr = &p->prop[i];
- if (prs->atom != JS_ATOM_NULL) {
- if (prs->flags & JS_PROP_TMASK) {
- if ((prs->flags & JS_PROP_TMASK) == JS_PROP_GETSET) {
- if (pr->u.getset.getter)
- mark_func(rt, &pr->u.getset.getter->header);
- if (pr->u.getset.setter)
- mark_func(rt, &pr->u.getset.setter->header);
- } else if ((prs->flags & JS_PROP_TMASK) == JS_PROP_VARREF) {
- /* Note: the tag does not matter
- provided it is a GC object */
- mark_func(rt, &pr->u.var_ref->header);
- } else if ((prs->flags & JS_PROP_TMASK) == JS_PROP_AUTOINIT) {
- js_autoinit_mark(rt, pr, mark_func);
- }
- } else {
- JS_MarkValue(rt, pr->u.value, mark_func);
- }
- }
- prs++;
- }
- if (p->class_id != JS_CLASS_OBJECT) {
- JSClassGCMark *gc_mark;
- gc_mark = rt->class_array[p->class_id].gc_mark;
- if (gc_mark)
- gc_mark(rt, JS_MKPTR(JS_TAG_OBJECT, p), mark_func);
- }
- }
- break;
- case JS_GC_OBJ_TYPE_FUNCTION_BYTECODE:
- /* the template objects can be part of a cycle */
- {
- JSFunctionBytecode *b = (JSFunctionBytecode *)gp;
- int i;
- for(i = 0; i < b->cpool_count; i++) {
- JS_MarkValue(rt, b->cpool[i], mark_func);
- }
- if (b->realm)
- mark_func(rt, &b->realm->header);
- }
- break;
- case JS_GC_OBJ_TYPE_VAR_REF:
- {
- JSVarRef *var_ref = (JSVarRef *)gp;
- if (var_ref->is_detached) {
- JS_MarkValue(rt, *var_ref->pvalue, mark_func);
- } else {
- JSStackFrame *sf = var_ref->stack_frame;
- if (sf->js_mode & JS_MODE_ASYNC) {
- JSAsyncFunctionState *async_func = container_of(sf, JSAsyncFunctionState, frame);
- mark_func(rt, &async_func->header);
- }
- }
- }
- break;
- case JS_GC_OBJ_TYPE_ASYNC_FUNCTION:
- {
- JSAsyncFunctionState *s = (JSAsyncFunctionState *)gp;
- JSStackFrame *sf = &s->frame;
- JSValue *sp;
- if (!s->is_completed) {
- JS_MarkValue(rt, sf->cur_func, mark_func);
- JS_MarkValue(rt, s->this_val, mark_func);
- /* sf->cur_sp = NULL if the function is running */
- if (sf->cur_sp) {
- /* if the function is running, cur_sp is not known so we
- cannot mark the stack. Marking the variables is not needed
- because a running function cannot be part of a removable
- cycle */
- for(sp = sf->arg_buf; sp < sf->cur_sp; sp++)
- JS_MarkValue(rt, *sp, mark_func);
- }
- }
- JS_MarkValue(rt, s->resolving_funcs[0], mark_func);
- JS_MarkValue(rt, s->resolving_funcs[1], mark_func);
- }
- break;
- case JS_GC_OBJ_TYPE_SHAPE:
- {
- JSShape *sh = (JSShape *)gp;
- if (sh->proto != NULL) {
- mark_func(rt, &sh->proto->header);
- }
- }
- break;
- case JS_GC_OBJ_TYPE_JS_CONTEXT:
- {
- JSContext *ctx = (JSContext *)gp;
- JS_MarkContext(rt, ctx, mark_func);
- }
- break;
- case JS_GC_OBJ_TYPE_MODULE:
- {
- JSModuleDef *m = (JSModuleDef *)gp;
- js_mark_module_def(rt, m, mark_func);
- }
- break;
- default:
- abort();
- }
- }
- static void gc_decref_child(JSRuntime *rt, JSGCObjectHeader *p)
- {
- assert(js_rc(p)->ref_count > 0);
- js_rc(p)->ref_count--;
- if (js_rc(p)->ref_count == 0 && js_rc(p)->mark == 1) {
- list_del(&p->link);
- list_add_tail(&p->link, &rt->tmp_obj_list);
- }
- }
- static void gc_decref(JSRuntime *rt)
- {
- struct list_head *el, *el1;
- JSGCObjectHeader *p;
- init_list_head(&rt->tmp_obj_list);
- /* decrement the refcount of all the children of all the GC
- objects and move the GC objects with zero refcount to
- tmp_obj_list */
- list_for_each_safe(el, el1, &rt->gc_obj_list) {
- p = list_entry(el, JSGCObjectHeader, link);
- assert(js_rc(p)->mark == 0);
- mark_children(rt, p, gc_decref_child);
- js_rc(p)->mark = 1;
- if (js_rc(p)->ref_count == 0) {
- list_del(&p->link);
- list_add_tail(&p->link, &rt->tmp_obj_list);
- }
- }
- }
- static void gc_scan_incref_child(JSRuntime *rt, JSGCObjectHeader *p)
- {
- js_rc(p)->ref_count++;
- if (js_rc(p)->ref_count == 1) {
- /* ref_count was 0: remove from tmp_obj_list and add at the
- end of gc_obj_list */
- list_del(&p->link);
- list_add_tail(&p->link, &rt->gc_obj_list);
- js_rc(p)->mark = 0; /* reset the mark for the next GC call */
- }
- }
- static void gc_scan_incref_child2(JSRuntime *rt, JSGCObjectHeader *p)
- {
- js_rc(p)->ref_count++;
- }
- static void gc_scan(JSRuntime *rt)
- {
- struct list_head *el;
- JSGCObjectHeader *p;
- /* keep the objects with a refcount > 0 and their children. */
- list_for_each(el, &rt->gc_obj_list) {
- p = list_entry(el, JSGCObjectHeader, link);
- assert(js_rc(p)->ref_count > 0);
- js_rc(p)->mark = 0; /* reset the mark for the next GC call */
- mark_children(rt, p, gc_scan_incref_child);
- }
- /* restore the refcount of the objects to be deleted. */
- list_for_each(el, &rt->tmp_obj_list) {
- p = list_entry(el, JSGCObjectHeader, link);
- mark_children(rt, p, gc_scan_incref_child2);
- }
- }
- static void gc_free_cycles(JSRuntime *rt)
- {
- struct list_head *el, *el1;
- JSGCObjectHeader *p;
- #ifdef DUMP_GC_FREE
- BOOL header_done = FALSE;
- #endif
- rt->gc_phase = JS_GC_PHASE_REMOVE_CYCLES;
- for(;;) {
- el = rt->tmp_obj_list.next;
- if (el == &rt->tmp_obj_list)
- break;
- p = list_entry(el, JSGCObjectHeader, link);
- /* Only need to free the GC object associated with JS values
- or async functions. The rest will be automatically removed
- because they must be referenced by them. */
- switch(js_rc(p)->gc_obj_type) {
- case JS_GC_OBJ_TYPE_JS_OBJECT:
- case JS_GC_OBJ_TYPE_FUNCTION_BYTECODE:
- case JS_GC_OBJ_TYPE_ASYNC_FUNCTION:
- case JS_GC_OBJ_TYPE_MODULE:
- #ifdef DUMP_GC_FREE
- if (!header_done) {
- printf("Freeing cycles:\n");
- JS_DumpObjectHeader(rt);
- header_done = TRUE;
- }
- JS_DumpGCObject(rt, p);
- #endif
- free_gc_object(rt, p);
- break;
- default:
- list_del(&p->link);
- list_add_tail(&p->link, &rt->gc_zero_ref_count_list);
- break;
- }
- }
- rt->gc_phase = JS_GC_PHASE_NONE;
- list_for_each_safe(el, el1, &rt->gc_zero_ref_count_list) {
- p = list_entry(el, JSGCObjectHeader, link);
- assert(js_rc(p)->gc_obj_type == JS_GC_OBJ_TYPE_JS_OBJECT ||
- js_rc(p)->gc_obj_type == JS_GC_OBJ_TYPE_FUNCTION_BYTECODE ||
- js_rc(p)->gc_obj_type == JS_GC_OBJ_TYPE_ASYNC_FUNCTION ||
- js_rc(p)->gc_obj_type == JS_GC_OBJ_TYPE_MODULE);
- if (js_rc(p)->gc_obj_type == JS_GC_OBJ_TYPE_JS_OBJECT &&
- ((JSObject *)p)->weakref_count != 0) {
- /* keep the object because there are weak references to it */
- js_rc(p)->mark = 0;
- } else {
- js_free_rt(rt, p);
- }
- }
- init_list_head(&rt->gc_zero_ref_count_list);
- }
- static void JS_RunGCInternal(JSRuntime *rt, BOOL remove_weak_objects)
- {
- if (remove_weak_objects) {
- /* free the weakly referenced object or symbol structures, delete
- the associated Map/Set entries and queue the finalization
- registry callbacks. */
- gc_remove_weak_objects(rt);
- }
-
- /* decrement the reference of the children of each object. mark =
- 1 after this pass. */
- gc_decref(rt);
- /* keep the GC objects with a non zero refcount and their childs */
- gc_scan(rt);
- /* free the GC objects in a cycle */
- gc_free_cycles(rt);
- }
- void JS_RunGC(JSRuntime *rt)
- {
- JS_RunGCInternal(rt, TRUE);
- }
- /* Return false if not an object or if the object has already been
- freed (zombie objects are visible in finalizers when freeing
- cycles). */
- BOOL JS_IsLiveObject(JSRuntime *rt, JSValueConst obj)
- {
- JSObject *p;
- if (!JS_IsObject(obj))
- return FALSE;
- p = JS_VALUE_GET_OBJ(obj);
- return !p->free_mark;
- }
- /* Compute memory used by various object types */
- /* XXX: poor man's approach to handling multiply referenced objects */
- typedef struct JSMemoryUsage_helper {
- double memory_used_count;
- double str_count;
- double str_size;
- int64_t js_func_count;
- double js_func_size;
- int64_t js_func_code_size;
- int64_t js_func_pc2line_count;
- int64_t js_func_pc2line_size;
- } JSMemoryUsage_helper;
- static void compute_value_size(JSValueConst val, JSMemoryUsage_helper *hp);
- static void compute_jsstring_size(JSString *str, JSMemoryUsage_helper *hp)
- {
- if (!str->atom_type) { /* atoms are handled separately */
- double s_ref_count = js_rc(str)->ref_count;
- hp->str_count += 1 / s_ref_count;
- hp->str_size += ((sizeof(*str) + (str->len << str->is_wide_char) +
- 1 - str->is_wide_char) / s_ref_count);
- }
- }
- static void compute_bytecode_size(JSFunctionBytecode *b, JSMemoryUsage_helper *hp)
- {
- int memory_used_count, js_func_size, i;
- memory_used_count = 0;
- js_func_size = offsetof(JSFunctionBytecode, debug);
- if (b->vardefs) {
- js_func_size += (b->arg_count + b->var_count) * sizeof(*b->vardefs);
- }
- if (b->cpool) {
- js_func_size += b->cpool_count * sizeof(*b->cpool);
- for (i = 0; i < b->cpool_count; i++) {
- JSValueConst val = b->cpool[i];
- compute_value_size(val, hp);
- }
- }
- if (b->closure_var) {
- js_func_size += b->closure_var_count * sizeof(*b->closure_var);
- }
- if (!b->read_only_bytecode && b->byte_code_buf) {
- hp->js_func_code_size += b->byte_code_len;
- }
- if (b->has_debug) {
- js_func_size += sizeof(*b) - offsetof(JSFunctionBytecode, debug);
- if (b->debug.source) {
- memory_used_count++;
- js_func_size += b->debug.source_len + 1;
- }
- if (b->debug.pc2line_len) {
- memory_used_count++;
- hp->js_func_pc2line_count += 1;
- hp->js_func_pc2line_size += b->debug.pc2line_len;
- }
- }
- hp->js_func_size += js_func_size;
- hp->js_func_count += 1;
- hp->memory_used_count += memory_used_count;
- }
- static void compute_value_size(JSValueConst val, JSMemoryUsage_helper *hp)
- {
- switch(JS_VALUE_GET_TAG(val)) {
- case JS_TAG_STRING:
- compute_jsstring_size(JS_VALUE_GET_STRING(val), hp);
- break;
- case JS_TAG_BIG_INT:
- /* should track JSBigInt usage */
- break;
- }
- }
- void JS_ComputeMemoryUsage(JSRuntime *rt, JSMemoryUsage *s)
- {
- struct list_head *el, *el1;
- int i;
- JSMemoryUsage_helper mem = { 0 }, *hp = &mem;
- memset(s, 0, sizeof(*s));
- s->malloc_count = rt->malloc_ctx.malloc_state.malloc_count;
- s->malloc_size = rt->malloc_ctx.malloc_state.malloc_size;
- s->malloc_limit = rt->malloc_ctx.malloc_state.malloc_limit;
- s->memory_used_count = 2; /* rt + rt->class_array */
- s->memory_used_size = sizeof(JSRuntime) + sizeof(JSValue) * rt->class_count;
- list_for_each(el, &rt->context_list) {
- JSContext *ctx = list_entry(el, JSContext, link);
- JSShape *sh = ctx->array_shape;
- s->memory_used_count += 2; /* ctx + ctx->class_proto */
- s->memory_used_size += sizeof(JSContext) +
- sizeof(JSValue) * rt->class_count;
- s->binary_object_count += ctx->binary_object_count;
- s->binary_object_size += ctx->binary_object_size;
- /* the hashed shapes are counted separately */
- if (sh && !sh->is_hashed) {
- int hash_size = sh->prop_hash_mask + 1;
- s->shape_count++;
- s->shape_size += get_shape_size(hash_size, sh->prop_size);
- }
- list_for_each(el1, &ctx->loaded_modules) {
- JSModuleDef *m = list_entry(el1, JSModuleDef, link);
- s->memory_used_count += 1;
- s->memory_used_size += sizeof(*m);
- if (m->req_module_entries) {
- s->memory_used_count += 1;
- s->memory_used_size += m->req_module_entries_count * sizeof(*m->req_module_entries);
- }
- if (m->export_entries) {
- s->memory_used_count += 1;
- s->memory_used_size += m->export_entries_count * sizeof(*m->export_entries);
- for (i = 0; i < m->export_entries_count; i++) {
- JSExportEntry *me = &m->export_entries[i];
- if (me->export_type == JS_EXPORT_TYPE_LOCAL && me->u.local.var_ref) {
- /* potential multiple count */
- s->memory_used_count += 1;
- compute_value_size(me->u.local.var_ref->value, hp);
- }
- }
- }
- if (m->star_export_entries) {
- s->memory_used_count += 1;
- s->memory_used_size += m->star_export_entries_count * sizeof(*m->star_export_entries);
- }
- if (m->import_entries) {
- s->memory_used_count += 1;
- s->memory_used_size += m->import_entries_count * sizeof(*m->import_entries);
- }
- compute_value_size(m->module_ns, hp);
- compute_value_size(m->func_obj, hp);
- }
- }
- list_for_each(el, &rt->gc_obj_list) {
- JSGCObjectHeader *gp = list_entry(el, JSGCObjectHeader, link);
- JSObject *p;
- JSShape *sh;
- JSShapeProperty *prs;
- /* XXX: could count the other GC object types too */
- if (js_rc(gp)->gc_obj_type == JS_GC_OBJ_TYPE_FUNCTION_BYTECODE) {
- compute_bytecode_size((JSFunctionBytecode *)gp, hp);
- continue;
- } else if (js_rc(gp)->gc_obj_type != JS_GC_OBJ_TYPE_JS_OBJECT) {
- continue;
- }
- p = (JSObject *)gp;
- sh = p->shape;
- s->obj_count++;
- if (p->prop) {
- s->memory_used_count++;
- s->prop_size += sh->prop_size * sizeof(*p->prop);
- s->prop_count += sh->prop_count;
- prs = get_shape_prop(sh);
- for(i = 0; i < sh->prop_count; i++) {
- JSProperty *pr = &p->prop[i];
- if (prs->atom != JS_ATOM_NULL && !(prs->flags & JS_PROP_TMASK)) {
- compute_value_size(pr->u.value, hp);
- }
- prs++;
- }
- }
- /* the hashed shapes are counted separately */
- if (!sh->is_hashed) {
- int hash_size = sh->prop_hash_mask + 1;
- s->shape_count++;
- s->shape_size += get_shape_size(hash_size, sh->prop_size);
- }
- switch(p->class_id) {
- case JS_CLASS_ARRAY: /* u.array | length */
- case JS_CLASS_ARGUMENTS: /* u.array | length */
- s->array_count++;
- if (p->fast_array) {
- s->fast_array_count++;
- if (p->u.array.u.values) {
- s->memory_used_count++;
- s->memory_used_size += p->u.array.count *
- sizeof(*p->u.array.u.values);
- s->fast_array_elements += p->u.array.count;
- for (i = 0; i < p->u.array.count; i++) {
- compute_value_size(p->u.array.u.values[i], hp);
- }
- }
- }
- break;
- case JS_CLASS_MAPPED_ARGUMENTS: /* u.array | length */
- if (p->fast_array) {
- s->fast_array_count++;
- if (p->u.array.u.values) {
- s->memory_used_count++;
- s->memory_used_size += p->u.array.count *
- sizeof(*p->u.array.u.var_refs);
- s->fast_array_elements += p->u.array.count;
- for (i = 0; i < p->u.array.count; i++) {
- compute_value_size(*p->u.array.u.var_refs[i]->pvalue, hp);
- }
- }
- }
- break;
- case JS_CLASS_NUMBER: /* u.object_data */
- case JS_CLASS_STRING: /* u.object_data */
- case JS_CLASS_BOOLEAN: /* u.object_data */
- case JS_CLASS_SYMBOL: /* u.object_data */
- case JS_CLASS_DATE: /* u.object_data */
- case JS_CLASS_BIG_INT: /* u.object_data */
- compute_value_size(p->u.object_data, hp);
- break;
- case JS_CLASS_C_FUNCTION: /* u.cfunc */
- s->c_func_count++;
- break;
- case JS_CLASS_BYTECODE_FUNCTION: /* u.func */
- {
- JSFunctionBytecode *b = p->u.func.function_bytecode;
- JSVarRef **var_refs = p->u.func.var_refs;
- /* home_object: object will be accounted for in list scan */
- if (var_refs) {
- s->memory_used_count++;
- s->js_func_size += b->closure_var_count * sizeof(*var_refs);
- for (i = 0; i < b->closure_var_count; i++) {
- if (var_refs[i]) {
- double ref_count = js_rc(var_refs[i])->ref_count;
- s->memory_used_count += 1 / ref_count;
- s->js_func_size += sizeof(*var_refs[i]) / ref_count;
- /* handle non object closed values */
- if (var_refs[i]->pvalue == &var_refs[i]->value) {
- /* potential multiple count */
- compute_value_size(var_refs[i]->value, hp);
- }
- }
- }
- }
- }
- break;
- case JS_CLASS_BOUND_FUNCTION: /* u.bound_function */
- {
- JSBoundFunction *bf = p->u.bound_function;
- /* func_obj and this_val are objects */
- for (i = 0; i < bf->argc; i++) {
- compute_value_size(bf->argv[i], hp);
- }
- s->memory_used_count += 1;
- s->memory_used_size += sizeof(*bf) + bf->argc * sizeof(*bf->argv);
- }
- break;
- case JS_CLASS_C_FUNCTION_DATA: /* u.c_function_data_record */
- {
- JSCFunctionDataRecord *fd = p->u.c_function_data_record;
- if (fd) {
- for (i = 0; i < fd->data_len; i++) {
- compute_value_size(fd->data[i], hp);
- }
- s->memory_used_count += 1;
- s->memory_used_size += sizeof(*fd) + fd->data_len * sizeof(*fd->data);
- }
- }
- break;
- case JS_CLASS_REGEXP: /* u.regexp */
- compute_jsstring_size(p->u.regexp.pattern, hp);
- compute_jsstring_size(p->u.regexp.bytecode, hp);
- break;
- case JS_CLASS_FOR_IN_ITERATOR: /* u.for_in_iterator */
- {
- JSForInIterator *it = p->u.for_in_iterator;
- if (it) {
- compute_value_size(it->obj, hp);
- s->memory_used_count += 1;
- s->memory_used_size += sizeof(*it);
- }
- }
- break;
- case JS_CLASS_ARRAY_BUFFER: /* u.array_buffer */
- case JS_CLASS_SHARED_ARRAY_BUFFER: /* u.array_buffer */
- {
- JSArrayBuffer *abuf = p->u.array_buffer;
- if (abuf) {
- s->memory_used_count += 1;
- s->memory_used_size += sizeof(*abuf);
- if (abuf->data) {
- s->memory_used_count += 1;
- s->memory_used_size += abuf->byte_length;
- }
- }
- }
- break;
- case JS_CLASS_GENERATOR: /* u.generator_data */
- case JS_CLASS_UINT8C_ARRAY: /* u.typed_array / u.array */
- case JS_CLASS_INT8_ARRAY: /* u.typed_array / u.array */
- case JS_CLASS_UINT8_ARRAY: /* u.typed_array / u.array */
- case JS_CLASS_INT16_ARRAY: /* u.typed_array / u.array */
- case JS_CLASS_UINT16_ARRAY: /* u.typed_array / u.array */
- case JS_CLASS_INT32_ARRAY: /* u.typed_array / u.array */
- case JS_CLASS_UINT32_ARRAY: /* u.typed_array / u.array */
- case JS_CLASS_BIG_INT64_ARRAY: /* u.typed_array / u.array */
- case JS_CLASS_BIG_UINT64_ARRAY: /* u.typed_array / u.array */
- case JS_CLASS_FLOAT16_ARRAY: /* u.typed_array / u.array */
- case JS_CLASS_FLOAT32_ARRAY: /* u.typed_array / u.array */
- case JS_CLASS_FLOAT64_ARRAY: /* u.typed_array / u.array */
- case JS_CLASS_DATAVIEW: /* u.typed_array */
- case JS_CLASS_MAP: /* u.map_state */
- case JS_CLASS_SET: /* u.map_state */
- case JS_CLASS_WEAKMAP: /* u.map_state */
- case JS_CLASS_WEAKSET: /* u.map_state */
- case JS_CLASS_MAP_ITERATOR: /* u.map_iterator_data */
- case JS_CLASS_SET_ITERATOR: /* u.map_iterator_data */
- case JS_CLASS_ARRAY_ITERATOR: /* u.array_iterator_data */
- case JS_CLASS_STRING_ITERATOR: /* u.array_iterator_data */
- case JS_CLASS_PROXY: /* u.proxy_data */
- case JS_CLASS_PROMISE: /* u.promise_data */
- case JS_CLASS_PROMISE_RESOLVE_FUNCTION: /* u.promise_function_data */
- case JS_CLASS_PROMISE_REJECT_FUNCTION: /* u.promise_function_data */
- case JS_CLASS_ASYNC_FUNCTION_RESOLVE: /* u.async_function_data */
- case JS_CLASS_ASYNC_FUNCTION_REJECT: /* u.async_function_data */
- case JS_CLASS_ASYNC_FROM_SYNC_ITERATOR: /* u.async_from_sync_iterator_data */
- case JS_CLASS_ASYNC_GENERATOR: /* u.async_generator_data */
- /* TODO */
- default:
- /* XXX: class definition should have an opaque block size */
- if (p->u.opaque) {
- s->memory_used_count += 1;
- }
- break;
- }
- }
- s->obj_size += s->obj_count * sizeof(JSObject);
- /* hashed shapes */
- s->memory_used_count++; /* rt->shape_hash */
- s->memory_used_size += sizeof(rt->shape_hash[0]) * rt->shape_hash_size;
- for(i = 0; i < rt->shape_hash_size; i++) {
- JSShape *sh;
- for(sh = rt->shape_hash[i]; sh != NULL; sh = sh->shape_hash_next) {
- int hash_size = sh->prop_hash_mask + 1;
- s->shape_count++;
- s->shape_size += get_shape_size(hash_size, sh->prop_size);
- }
- }
- /* atoms */
- s->memory_used_count += 2; /* rt->atom_array, rt->atom_hash */
- s->atom_count = rt->atom_count;
- s->atom_size = sizeof(rt->atom_array[0]) * rt->atom_size +
- sizeof(rt->atom_hash[0]) * rt->atom_hash_size;
- for(i = 0; i < rt->atom_size; i++) {
- JSAtomStruct *p = rt->atom_array[i];
- if (!atom_is_free(p)) {
- s->atom_size += (sizeof(*p) + (p->len << p->is_wide_char) +
- 1 - p->is_wide_char);
- }
- }
- s->str_count = round(mem.str_count);
- s->str_size = round(mem.str_size);
- s->js_func_count = mem.js_func_count;
- s->js_func_size = round(mem.js_func_size);
- s->js_func_code_size = mem.js_func_code_size;
- s->js_func_pc2line_count = mem.js_func_pc2line_count;
- s->js_func_pc2line_size = mem.js_func_pc2line_size;
- s->memory_used_count += round(mem.memory_used_count) +
- s->atom_count + s->str_count +
- s->obj_count + s->shape_count +
- s->js_func_count + s->js_func_pc2line_count;
- s->memory_used_size += s->atom_size + s->str_size +
- s->obj_size + s->prop_size + s->shape_size +
- s->js_func_size + s->js_func_code_size + s->js_func_pc2line_size;
- }
- void JS_DumpMemoryUsage(FILE *fp, const JSMemoryUsage *s, JSRuntime *rt)
- {
- fprintf(fp, "QuickJS memory usage -- " CONFIG_VERSION " version, %d-bit, malloc limit: %"PRId64"\n\n",
- (int)sizeof(void *) * 8, s->malloc_limit);
- #if 1
- if (rt) {
- static const struct {
- const char *name;
- size_t size;
- } object_types[] = {
- { "JSRuntime", sizeof(JSRuntime) },
- { "JSContext", sizeof(JSContext) },
- { "JSObject", sizeof(JSObject) },
- { "JSString", sizeof(JSString) },
- { "JSFunctionBytecode", sizeof(JSFunctionBytecode) },
- };
- int i, usage_size_ok = 0;
- for(i = 0; i < countof(object_types); i++) {
- unsigned int size = object_types[i].size;
- void *p = js_malloc_rt(rt, size);
- if (p) {
- unsigned int size1 = js_malloc_usable_size_rt(rt, p);
- if (size1 >= size) {
- usage_size_ok = 1;
- fprintf(fp, " %3u + %-2u %s\n",
- size, size1 - size, object_types[i].name);
- }
- js_free_rt(rt, p);
- }
- }
- if (!usage_size_ok) {
- fprintf(fp, " malloc_usable_size unavailable\n");
- }
- {
- int obj_classes[JS_CLASS_INIT_COUNT + 1] = { 0 };
- int class_id;
- struct list_head *el;
- list_for_each(el, &rt->gc_obj_list) {
- JSGCObjectHeader *gp = list_entry(el, JSGCObjectHeader, link);
- JSObject *p;
- if (js_rc(gp)->gc_obj_type == JS_GC_OBJ_TYPE_JS_OBJECT) {
- p = (JSObject *)gp;
- obj_classes[min_uint32(p->class_id, JS_CLASS_INIT_COUNT)]++;
- }
- }
- fprintf(fp, "\n" "JSObject classes\n");
- if (obj_classes[0])
- fprintf(fp, " %5d %2.0d %s\n", obj_classes[0], 0, "none");
- for (class_id = 1; class_id < JS_CLASS_INIT_COUNT; class_id++) {
- if (obj_classes[class_id] && class_id < rt->class_count) {
- char buf[ATOM_GET_STR_BUF_SIZE];
- fprintf(fp, " %5d %2.0d %s\n", obj_classes[class_id], class_id,
- JS_AtomGetStrRT(rt, buf, sizeof(buf), rt->class_array[class_id].class_name));
- }
- }
- if (obj_classes[JS_CLASS_INIT_COUNT])
- fprintf(fp, " %5d %2.0d %s\n", obj_classes[JS_CLASS_INIT_COUNT], 0, "other");
- }
- fprintf(fp, "\n");
- }
- #endif
- fprintf(fp, "%-20s %8s %8s\n", "NAME", "COUNT", "SIZE");
- if (s->malloc_count) {
- fprintf(fp, "%-20s %8"PRId64" %8"PRId64" (%0.1f per block)\n",
- "memory allocated", s->malloc_count, s->malloc_size,
- (double)s->malloc_size / s->malloc_count);
- fprintf(fp, "%-20s %8"PRId64" %8"PRId64" (%d overhead, %0.1f average slack)\n",
- "memory used", s->memory_used_count, s->memory_used_size,
- MALLOC_OVERHEAD, ((double)(s->malloc_size - s->memory_used_size) /
- s->memory_used_count));
- }
- if (s->atom_count) {
- fprintf(fp, "%-20s %8"PRId64" %8"PRId64" (%0.1f per atom)\n",
- "atoms", s->atom_count, s->atom_size,
- (double)s->atom_size / s->atom_count);
- }
- if (s->str_count) {
- fprintf(fp, "%-20s %8"PRId64" %8"PRId64" (%0.1f per string)\n",
- "strings", s->str_count, s->str_size,
- (double)s->str_size / s->str_count);
- }
- if (s->obj_count) {
- fprintf(fp, "%-20s %8"PRId64" %8"PRId64" (%0.1f per object)\n",
- "objects", s->obj_count, s->obj_size,
- (double)s->obj_size / s->obj_count);
- fprintf(fp, "%-20s %8"PRId64" %8"PRId64" (%0.1f per object)\n",
- " properties", s->prop_count, s->prop_size,
- (double)s->prop_count / s->obj_count);
- fprintf(fp, "%-20s %8"PRId64" %8"PRId64" (%0.1f per shape)\n",
- " shapes", s->shape_count, s->shape_size,
- (double)s->shape_size / s->shape_count);
- }
- if (s->js_func_count) {
- fprintf(fp, "%-20s %8"PRId64" %8"PRId64"\n",
- "bytecode functions", s->js_func_count, s->js_func_size);
- fprintf(fp, "%-20s %8"PRId64" %8"PRId64" (%0.1f per function)\n",
- " bytecode", s->js_func_count, s->js_func_code_size,
- (double)s->js_func_code_size / s->js_func_count);
- if (s->js_func_pc2line_count) {
- fprintf(fp, "%-20s %8"PRId64" %8"PRId64" (%0.1f per function)\n",
- " pc2line", s->js_func_pc2line_count,
- s->js_func_pc2line_size,
- (double)s->js_func_pc2line_size / s->js_func_pc2line_count);
- }
- }
- if (s->c_func_count) {
- fprintf(fp, "%-20s %8"PRId64"\n", "C functions", s->c_func_count);
- }
- if (s->array_count) {
- fprintf(fp, "%-20s %8"PRId64"\n", "arrays", s->array_count);
- if (s->fast_array_count) {
- fprintf(fp, "%-20s %8"PRId64"\n", " fast arrays", s->fast_array_count);
- fprintf(fp, "%-20s %8"PRId64" %8"PRId64" (%0.1f per fast array)\n",
- " elements", s->fast_array_elements,
- s->fast_array_elements * (int)sizeof(JSValue),
- (double)s->fast_array_elements / s->fast_array_count);
- }
- }
- if (s->binary_object_count) {
- fprintf(fp, "%-20s %8"PRId64" %8"PRId64"\n",
- "binary objects", s->binary_object_count, s->binary_object_size);
- }
- }
- JSValue JS_GetGlobalObject(JSContext *ctx)
- {
- return JS_DupValue(ctx, ctx->global_obj);
- }
- /* WARNING: obj is freed */
- JSValue JS_Throw(JSContext *ctx, JSValue obj)
- {
- JSRuntime *rt = ctx->rt;
- JS_FreeValue(ctx, rt->current_exception);
- rt->current_exception = obj;
- rt->current_exception_is_uncatchable = FALSE;
- return JS_EXCEPTION;
- }
- /* return the pending exception (cannot be called twice). */
- JSValue JS_GetException(JSContext *ctx)
- {
- JSValue val;
- JSRuntime *rt = ctx->rt;
- val = rt->current_exception;
- rt->current_exception = JS_UNINITIALIZED;
- return val;
- }
- JS_BOOL JS_HasException(JSContext *ctx)
- {
- return !JS_IsUninitialized(ctx->rt->current_exception);
- }
- static void dbuf_put_leb128(DynBuf *s, uint32_t v)
- {
- uint32_t a;
- for(;;) {
- a = v & 0x7f;
- v >>= 7;
- if (v != 0) {
- dbuf_putc(s, a | 0x80);
- } else {
- dbuf_putc(s, a);
- break;
- }
- }
- }
- static void dbuf_put_sleb128(DynBuf *s, int32_t v1)
- {
- uint32_t v = v1;
- dbuf_put_leb128(s, (2 * v) ^ -(v >> 31));
- }
- static int get_leb128(uint32_t *pval, const uint8_t *buf,
- const uint8_t *buf_end)
- {
- const uint8_t *ptr = buf;
- uint32_t v, a, i;
- v = 0;
- for(i = 0; i < 5; i++) {
- if (unlikely(ptr >= buf_end))
- break;
- a = *ptr++;
- v |= (a & 0x7f) << (i * 7);
- if (!(a & 0x80)) {
- *pval = v;
- return ptr - buf;
- }
- }
- *pval = 0;
- return -1;
- }
- static int get_sleb128(int32_t *pval, const uint8_t *buf,
- const uint8_t *buf_end)
- {
- int ret;
- uint32_t val;
- ret = get_leb128(&val, buf, buf_end);
- if (ret < 0) {
- *pval = 0;
- return -1;
- }
- *pval = (val >> 1) ^ -(val & 1);
- return ret;
- }
- /* use pc_value = -1 to get the position of the function definition */
- static int find_line_num(JSContext *ctx, JSFunctionBytecode *b,
- uint32_t pc_value, int *pcol_num)
- {
- const uint8_t *p_end, *p;
- int new_line_num, line_num, pc, v, ret, new_col_num, col_num;
- uint32_t val;
- unsigned int op;
- if (!b->has_debug || !b->debug.pc2line_buf)
- goto fail; /* function was stripped */
- p = b->debug.pc2line_buf;
- p_end = p + b->debug.pc2line_len;
- /* get the function line and column numbers */
- ret = get_leb128(&val, p, p_end);
- if (ret < 0)
- goto fail;
- p += ret;
- line_num = val + 1;
- ret = get_leb128(&val, p, p_end);
- if (ret < 0)
- goto fail;
- p += ret;
- col_num = val + 1;
- if (pc_value != -1) {
- pc = 0;
- while (p < p_end) {
- op = *p++;
- if (op == 0) {
- ret = get_leb128(&val, p, p_end);
- if (ret < 0)
- goto fail;
- pc += val;
- p += ret;
- ret = get_sleb128(&v, p, p_end);
- if (ret < 0)
- goto fail;
- p += ret;
- new_line_num = line_num + v;
- } else {
- op -= PC2LINE_OP_FIRST;
- pc += (op / PC2LINE_RANGE);
- new_line_num = line_num + (op % PC2LINE_RANGE) + PC2LINE_BASE;
- }
- ret = get_sleb128(&v, p, p_end);
- if (ret < 0)
- goto fail;
- p += ret;
- new_col_num = col_num + v;
-
- if (pc_value < pc)
- goto done;
- line_num = new_line_num;
- col_num = new_col_num;
- }
- }
- done:
- *pcol_num = col_num;
- return line_num;
- fail:
- *pcol_num = 0;
- return 0;
- }
- /* return a string property without executing arbitrary JS code (used
- when dumping the stack trace or in debug print). */
- static const char *get_prop_string(JSContext *ctx, JSValueConst obj, JSAtom prop)
- {
- JSObject *p;
- JSProperty *pr;
- JSShapeProperty *prs;
- JSValueConst val;
- if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)
- return NULL;
- p = JS_VALUE_GET_OBJ(obj);
- prs = find_own_property(&pr, p, prop);
- if (!prs) {
- /* we look at one level in the prototype to handle the 'name'
- field of the Error objects */
- p = p->shape->proto;
- if (!p)
- return NULL;
- prs = find_own_property(&pr, p, prop);
- if (!prs)
- return NULL;
- }
-
- if ((prs->flags & JS_PROP_TMASK) != JS_PROP_NORMAL)
- return NULL;
- val = pr->u.value;
- if (JS_VALUE_GET_TAG(val) != JS_TAG_STRING)
- return NULL;
- return JS_ToCString(ctx, val);
- }
- #define JS_BACKTRACE_FLAG_SKIP_FIRST_LEVEL (1 << 0)
- /* if filename != NULL, an additional level is added with the filename
- and line number information (used for parse error). */
- static void build_backtrace(JSContext *ctx, JSValueConst error_obj,
- const char *filename, int line_num, int col_num,
- int backtrace_flags)
- {
- JSStackFrame *sf;
- JSValue str;
- DynBuf dbuf;
- const char *func_name_str;
- const char *str1;
- JSObject *p;
- if (!JS_IsObject(error_obj))
- return; /* protection in the out of memory case */
-
- js_dbuf_init(ctx, &dbuf);
- if (filename) {
- dbuf_printf(&dbuf, " at %s", filename);
- if (line_num != -1)
- dbuf_printf(&dbuf, ":%d:%d", line_num, col_num);
- dbuf_putc(&dbuf, '\n');
- str = JS_NewString(ctx, filename);
- if (JS_IsException(str))
- return;
- /* Note: SpiderMonkey does that, could update once there is a standard */
- if (JS_DefinePropertyValue(ctx, error_obj, JS_ATOM_fileName, str,
- JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE) < 0 ||
- JS_DefinePropertyValue(ctx, error_obj, JS_ATOM_lineNumber, JS_NewInt32(ctx, line_num),
- JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE) < 0 ||
- JS_DefinePropertyValue(ctx, error_obj, JS_ATOM_columnNumber, JS_NewInt32(ctx, col_num),
- JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE) < 0) {
- return;
- }
- }
- for(sf = ctx->rt->current_stack_frame; sf != NULL; sf = sf->prev_frame) {
- if (sf->js_mode & JS_MODE_BACKTRACE_BARRIER)
- break;
- if (backtrace_flags & JS_BACKTRACE_FLAG_SKIP_FIRST_LEVEL) {
- backtrace_flags &= ~JS_BACKTRACE_FLAG_SKIP_FIRST_LEVEL;
- continue;
- }
- func_name_str = get_prop_string(ctx, sf->cur_func, JS_ATOM_name);
- if (!func_name_str || func_name_str[0] == '\0')
- str1 = "<anonymous>";
- else
- str1 = func_name_str;
- dbuf_printf(&dbuf, " at %s", str1);
- JS_FreeCString(ctx, func_name_str);
- p = JS_VALUE_GET_OBJ(sf->cur_func);
- if (js_class_has_bytecode(p->class_id)) {
- JSFunctionBytecode *b;
- const char *atom_str;
- int line_num1, col_num1;
- b = p->u.func.function_bytecode;
- if (b->has_debug) {
- line_num1 = find_line_num(ctx, b,
- sf->cur_pc - b->byte_code_buf - 1, &col_num1);
- atom_str = JS_AtomToCString(ctx, b->debug.filename);
- dbuf_printf(&dbuf, " (%s",
- atom_str ? atom_str : "<null>");
- JS_FreeCString(ctx, atom_str);
- if (line_num1 != 0)
- dbuf_printf(&dbuf, ":%d:%d", line_num1, col_num1);
- dbuf_putc(&dbuf, ')');
- }
- } else {
- dbuf_printf(&dbuf, " (native)");
- }
- dbuf_putc(&dbuf, '\n');
- }
- dbuf_putc(&dbuf, '\0');
- if (dbuf_error(&dbuf))
- str = JS_NULL;
- else
- str = JS_NewString(ctx, (char *)dbuf.buf);
- dbuf_free(&dbuf);
- JS_DefinePropertyValue(ctx, error_obj, JS_ATOM_stack, str,
- JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
- }
- /* Note: it is important that no exception is returned by this function */
- static BOOL is_backtrace_needed(JSContext *ctx, JSValueConst obj)
- {
- JSObject *p;
- if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)
- return FALSE;
- p = JS_VALUE_GET_OBJ(obj);
- if (p->class_id != JS_CLASS_ERROR)
- return FALSE;
- if (find_own_property1(p, JS_ATOM_stack))
- return FALSE;
- return TRUE;
- }
- JSValue JS_NewError(JSContext *ctx)
- {
- return JS_NewObjectClass(ctx, JS_CLASS_ERROR);
- }
- static JSValue JS_ThrowError2(JSContext *ctx, JSErrorEnum error_num,
- const char *fmt, va_list ap, BOOL add_backtrace)
- {
- char buf[256];
- JSValue obj, ret;
- vsnprintf(buf, sizeof(buf), fmt, ap);
- obj = JS_NewObjectProtoClass(ctx, ctx->native_error_proto[error_num],
- JS_CLASS_ERROR);
- if (unlikely(JS_IsException(obj))) {
- /* out of memory: throw JS_NULL to avoid recursing */
- obj = JS_NULL;
- } else {
- JS_DefinePropertyValue(ctx, obj, JS_ATOM_message,
- JS_NewString(ctx, buf),
- JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
- if (add_backtrace) {
- build_backtrace(ctx, obj, NULL, 0, 0, 0);
- }
- }
- ret = JS_Throw(ctx, obj);
- return ret;
- }
- static JSValue JS_ThrowError(JSContext *ctx, JSErrorEnum error_num,
- const char *fmt, va_list ap)
- {
- JSRuntime *rt = ctx->rt;
- JSStackFrame *sf;
- BOOL add_backtrace;
- /* the backtrace is added later if called from a bytecode function */
- sf = rt->current_stack_frame;
- add_backtrace = !rt->in_out_of_memory &&
- (!sf || (JS_GetFunctionBytecode(sf->cur_func) == NULL));
- return JS_ThrowError2(ctx, error_num, fmt, ap, add_backtrace);
- }
- JSValue __attribute__((format(printf, 2, 3))) JS_ThrowSyntaxError(JSContext *ctx, const char *fmt, ...)
- {
- JSValue val;
- va_list ap;
- va_start(ap, fmt);
- val = JS_ThrowError(ctx, JS_SYNTAX_ERROR, fmt, ap);
- va_end(ap);
- return val;
- }
- JSValue __attribute__((format(printf, 2, 3))) JS_ThrowTypeError(JSContext *ctx, const char *fmt, ...)
- {
- JSValue val;
- va_list ap;
- va_start(ap, fmt);
- val = JS_ThrowError(ctx, JS_TYPE_ERROR, fmt, ap);
- va_end(ap);
- return val;
- }
- static int __attribute__((format(printf, 3, 4))) JS_ThrowTypeErrorOrFalse(JSContext *ctx, int flags, const char *fmt, ...)
- {
- va_list ap;
- if ((flags & JS_PROP_THROW) ||
- ((flags & JS_PROP_THROW_STRICT) && is_strict_mode(ctx))) {
- va_start(ap, fmt);
- JS_ThrowError(ctx, JS_TYPE_ERROR, fmt, ap);
- va_end(ap);
- return -1;
- } else {
- return FALSE;
- }
- }
- /* never use it directly */
- static JSValue __attribute__((format(printf, 3, 4))) __JS_ThrowTypeErrorAtom(JSContext *ctx, JSAtom atom, const char *fmt, ...)
- {
- char buf[ATOM_GET_STR_BUF_SIZE];
- return JS_ThrowTypeError(ctx, fmt,
- JS_AtomGetStr(ctx, buf, sizeof(buf), atom));
- }
- /* never use it directly */
- static JSValue __attribute__((format(printf, 3, 4))) __JS_ThrowSyntaxErrorAtom(JSContext *ctx, JSAtom atom, const char *fmt, ...)
- {
- char buf[ATOM_GET_STR_BUF_SIZE];
- return JS_ThrowSyntaxError(ctx, fmt,
- JS_AtomGetStr(ctx, buf, sizeof(buf), atom));
- }
- /* %s is replaced by 'atom'. The macro is used so that gcc can check
- the format string. */
- #define JS_ThrowTypeErrorAtom(ctx, fmt, atom) __JS_ThrowTypeErrorAtom(ctx, atom, fmt, "")
- #define JS_ThrowSyntaxErrorAtom(ctx, fmt, atom) __JS_ThrowSyntaxErrorAtom(ctx, atom, fmt, "")
- static int JS_ThrowTypeErrorReadOnly(JSContext *ctx, int flags, JSAtom atom)
- {
- if ((flags & JS_PROP_THROW) ||
- ((flags & JS_PROP_THROW_STRICT) && is_strict_mode(ctx))) {
- JS_ThrowTypeErrorAtom(ctx, "'%s' is read-only", atom);
- return -1;
- } else {
- return FALSE;
- }
- }
- JSValue __attribute__((format(printf, 2, 3))) JS_ThrowReferenceError(JSContext *ctx, const char *fmt, ...)
- {
- JSValue val;
- va_list ap;
- va_start(ap, fmt);
- val = JS_ThrowError(ctx, JS_REFERENCE_ERROR, fmt, ap);
- va_end(ap);
- return val;
- }
- JSValue __attribute__((format(printf, 2, 3))) JS_ThrowRangeError(JSContext *ctx, const char *fmt, ...)
- {
- JSValue val;
- va_list ap;
- va_start(ap, fmt);
- val = JS_ThrowError(ctx, JS_RANGE_ERROR, fmt, ap);
- va_end(ap);
- return val;
- }
- JSValue __attribute__((format(printf, 2, 3))) JS_ThrowInternalError(JSContext *ctx, const char *fmt, ...)
- {
- JSValue val;
- va_list ap;
- va_start(ap, fmt);
- val = JS_ThrowError(ctx, JS_INTERNAL_ERROR, fmt, ap);
- va_end(ap);
- return val;
- }
- JSValue JS_ThrowOutOfMemory(JSContext *ctx)
- {
- JSRuntime *rt = ctx->rt;
- if (!rt->in_out_of_memory) {
- rt->in_out_of_memory = TRUE;
- JS_ThrowInternalError(ctx, "out of memory");
- rt->in_out_of_memory = FALSE;
- }
- return JS_EXCEPTION;
- }
- static JSValue JS_ThrowStackOverflow(JSContext *ctx)
- {
- return JS_ThrowInternalError(ctx, "stack overflow");
- }
- static JSValue JS_ThrowTypeErrorNotAnObject(JSContext *ctx)
- {
- return JS_ThrowTypeError(ctx, "not an object");
- }
- static JSValue JS_ThrowTypeErrorNotAConstructor(JSContext *ctx,
- JSValueConst func_obj)
- {
- const char *name;
- if (!JS_IsFunction(ctx, func_obj))
- goto fail;
- name = get_prop_string(ctx, func_obj, JS_ATOM_name);
- if (!name) {
- fail:
- return JS_ThrowTypeError(ctx, "not a constructor");
- }
- JS_ThrowTypeError(ctx, "%s is not a constructor", name);
- JS_FreeCString(ctx, name);
- return JS_EXCEPTION;
- }
- static JSValue JS_ThrowTypeErrorNotASymbol(JSContext *ctx)
- {
- return JS_ThrowTypeError(ctx, "not a symbol");
- }
- static JSValue JS_ThrowReferenceErrorNotDefined(JSContext *ctx, JSAtom name)
- {
- char buf[ATOM_GET_STR_BUF_SIZE];
- return JS_ThrowReferenceError(ctx, "'%s' is not defined",
- JS_AtomGetStr(ctx, buf, sizeof(buf), name));
- }
- static JSValue JS_ThrowReferenceErrorUninitialized(JSContext *ctx, JSAtom name)
- {
- char buf[ATOM_GET_STR_BUF_SIZE];
- return JS_ThrowReferenceError(ctx, "%s is not initialized",
- name == JS_ATOM_NULL ? "lexical variable" :
- JS_AtomGetStr(ctx, buf, sizeof(buf), name));
- }
- static JSValue JS_ThrowReferenceErrorUninitialized2(JSContext *ctx,
- JSFunctionBytecode *b,
- int idx, BOOL is_ref)
- {
- JSAtom atom = JS_ATOM_NULL;
- if (is_ref) {
- atom = b->closure_var[idx].var_name;
- } else {
- /* not present if the function is stripped and contains no eval() */
- if (b->vardefs)
- atom = b->vardefs[b->arg_count + idx].var_name;
- }
- return JS_ThrowReferenceErrorUninitialized(ctx, atom);
- }
- static JSValue JS_ThrowTypeErrorInvalidClass(JSContext *ctx, int class_id)
- {
- JSRuntime *rt = ctx->rt;
- JSAtom name;
- name = rt->class_array[class_id].class_name;
- return JS_ThrowTypeErrorAtom(ctx, "%s object expected", name);
- }
- static void JS_ThrowInterrupted(JSContext *ctx)
- {
- JS_ThrowInternalError(ctx, "interrupted");
- JS_SetUncatchableException(ctx, TRUE);
- }
- static no_inline __exception int __js_poll_interrupts(JSContext *ctx)
- {
- JSRuntime *rt = ctx->rt;
- ctx->interrupt_counter = JS_INTERRUPT_COUNTER_INIT;
- if (rt->interrupt_handler) {
- if (rt->interrupt_handler(rt, rt->interrupt_opaque)) {
- JS_ThrowInterrupted(ctx);
- return -1;
- }
- }
- return 0;
- }
- static inline __exception int js_poll_interrupts(JSContext *ctx)
- {
- if (unlikely(--ctx->interrupt_counter <= 0)) {
- return __js_poll_interrupts(ctx);
- } else {
- return 0;
- }
- }
- static void JS_SetImmutablePrototype(JSContext *ctx, JSValueConst obj)
- {
- JSObject *p;
- if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)
- return;
- p = JS_VALUE_GET_OBJ(obj);
- p->has_immutable_prototype = TRUE;
- }
- /* Return -1 (exception) or TRUE/FALSE. 'throw_flag' = FALSE indicates
- that it is called from Reflect.setPrototypeOf(). */
- static int JS_SetPrototypeInternal(JSContext *ctx, JSValueConst obj,
- JSValueConst proto_val,
- BOOL throw_flag)
- {
- JSObject *proto, *p, *p1;
- JSShape *sh;
- if (throw_flag) {
- if (JS_VALUE_GET_TAG(obj) == JS_TAG_NULL ||
- JS_VALUE_GET_TAG(obj) == JS_TAG_UNDEFINED)
- goto not_obj;
- } else {
- if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)
- goto not_obj;
- }
- p = JS_VALUE_GET_OBJ(obj);
- if (JS_VALUE_GET_TAG(proto_val) != JS_TAG_OBJECT) {
- if (JS_VALUE_GET_TAG(proto_val) != JS_TAG_NULL) {
- not_obj:
- JS_ThrowTypeErrorNotAnObject(ctx);
- return -1;
- }
- proto = NULL;
- } else {
- proto = JS_VALUE_GET_OBJ(proto_val);
- }
- if (throw_flag && JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)
- return TRUE;
- if (unlikely(p->is_exotic)) {
- const JSClassExoticMethods *em = ctx->rt->class_array[p->class_id].exotic;
- int ret;
- if (em && em->set_prototype) {
- ret = em->set_prototype(ctx, obj, proto_val);
- if (ret == 0 && throw_flag) {
- JS_ThrowTypeError(ctx, "proxy: bad prototype");
- return -1;
- } else {
- return ret;
- }
- }
- }
- sh = p->shape;
- if (sh->proto == proto)
- return TRUE;
- if (unlikely(p->has_immutable_prototype)) {
- if (throw_flag) {
- JS_ThrowTypeError(ctx, "prototype is immutable");
- return -1;
- } else {
- return FALSE;
- }
- }
- if (unlikely(!p->extensible)) {
- if (throw_flag) {
- JS_ThrowTypeError(ctx, "object is not extensible");
- return -1;
- } else {
- return FALSE;
- }
- }
- if (proto) {
- /* check if there is a cycle */
- p1 = proto;
- do {
- if (p1 == p) {
- if (throw_flag) {
- JS_ThrowTypeError(ctx, "circular prototype chain");
- return -1;
- } else {
- return FALSE;
- }
- }
- /* Note: for Proxy objects, proto is NULL */
- p1 = p1->shape->proto;
- } while (p1 != NULL);
- JS_DupValue(ctx, proto_val);
- }
- if (js_shape_prepare_update(ctx, p, NULL))
- return -1;
- sh = p->shape;
- if (sh->proto)
- JS_FreeValue(ctx, JS_MKPTR(JS_TAG_OBJECT, sh->proto));
- sh->proto = proto;
- p->is_std_array_prototype = FALSE;
- return TRUE;
- }
- /* return -1 (exception) or TRUE/FALSE */
- int JS_SetPrototype(JSContext *ctx, JSValueConst obj, JSValueConst proto_val)
- {
- return JS_SetPrototypeInternal(ctx, obj, proto_val, TRUE);
- }
- /* Only works for primitive types, otherwise return JS_NULL. */
- static JSValueConst JS_GetPrototypePrimitive(JSContext *ctx, JSValueConst val)
- {
- switch(JS_VALUE_GET_NORM_TAG(val)) {
- case JS_TAG_SHORT_BIG_INT:
- case JS_TAG_BIG_INT:
- val = ctx->class_proto[JS_CLASS_BIG_INT];
- break;
- case JS_TAG_INT:
- case JS_TAG_FLOAT64:
- val = ctx->class_proto[JS_CLASS_NUMBER];
- break;
- case JS_TAG_BOOL:
- val = ctx->class_proto[JS_CLASS_BOOLEAN];
- break;
- case JS_TAG_STRING:
- case JS_TAG_STRING_ROPE:
- val = ctx->class_proto[JS_CLASS_STRING];
- break;
- case JS_TAG_SYMBOL:
- val = ctx->class_proto[JS_CLASS_SYMBOL];
- break;
- case JS_TAG_OBJECT:
- case JS_TAG_NULL:
- case JS_TAG_UNDEFINED:
- default:
- val = JS_NULL;
- break;
- }
- return val;
- }
- /* Return an Object, JS_NULL or JS_EXCEPTION in case of exotic object. */
- JSValue JS_GetPrototype(JSContext *ctx, JSValueConst obj)
- {
- JSValue val;
- if (JS_VALUE_GET_TAG(obj) == JS_TAG_OBJECT) {
- JSObject *p;
- p = JS_VALUE_GET_OBJ(obj);
- if (unlikely(p->is_exotic)) {
- const JSClassExoticMethods *em = ctx->rt->class_array[p->class_id].exotic;
- if (em && em->get_prototype) {
- return em->get_prototype(ctx, obj);
- }
- }
- p = p->shape->proto;
- if (!p)
- val = JS_NULL;
- else
- val = JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, p));
- } else {
- val = JS_DupValue(ctx, JS_GetPrototypePrimitive(ctx, obj));
- }
- return val;
- }
- static JSValue JS_GetPrototypeFree(JSContext *ctx, JSValue obj)
- {
- JSValue obj1;
- obj1 = JS_GetPrototype(ctx, obj);
- JS_FreeValue(ctx, obj);
- return obj1;
- }
- /* return TRUE, FALSE or (-1) in case of exception */
- static int JS_OrdinaryIsInstanceOf(JSContext *ctx, JSValueConst val,
- JSValueConst obj)
- {
- JSValue obj_proto;
- JSObject *proto;
- const JSObject *p, *proto1;
- BOOL ret;
- if (!JS_IsFunction(ctx, obj))
- return FALSE;
- p = JS_VALUE_GET_OBJ(obj);
- if (p->class_id == JS_CLASS_BOUND_FUNCTION) {
- JSBoundFunction *s = p->u.bound_function;
- return JS_IsInstanceOf(ctx, val, s->func_obj);
- }
- /* Only explicitly boxed values are instances of constructors */
- if (JS_VALUE_GET_TAG(val) != JS_TAG_OBJECT)
- return FALSE;
- obj_proto = JS_GetProperty(ctx, obj, JS_ATOM_prototype);
- if (JS_VALUE_GET_TAG(obj_proto) != JS_TAG_OBJECT) {
- if (!JS_IsException(obj_proto))
- JS_ThrowTypeError(ctx, "operand 'prototype' property is not an object");
- ret = -1;
- goto done;
- }
- proto = JS_VALUE_GET_OBJ(obj_proto);
- p = JS_VALUE_GET_OBJ(val);
- for(;;) {
- proto1 = p->shape->proto;
- if (!proto1) {
- /* slow case if exotic object in the prototype chain */
- if (unlikely(p->is_exotic && !p->fast_array)) {
- JSValue obj1;
- obj1 = JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, (JSObject *)p));
- for(;;) {
- obj1 = JS_GetPrototypeFree(ctx, obj1);
- if (JS_IsException(obj1)) {
- ret = -1;
- break;
- }
- if (JS_IsNull(obj1)) {
- ret = FALSE;
- break;
- }
- if (proto == JS_VALUE_GET_OBJ(obj1)) {
- JS_FreeValue(ctx, obj1);
- ret = TRUE;
- break;
- }
- /* must check for timeout to avoid infinite loop */
- if (js_poll_interrupts(ctx)) {
- JS_FreeValue(ctx, obj1);
- ret = -1;
- break;
- }
- }
- } else {
- ret = FALSE;
- }
- break;
- }
- p = proto1;
- if (proto == p) {
- ret = TRUE;
- break;
- }
- }
- done:
- JS_FreeValue(ctx, obj_proto);
- return ret;
- }
- /* return TRUE, FALSE or (-1) in case of exception */
- int JS_IsInstanceOf(JSContext *ctx, JSValueConst val, JSValueConst obj)
- {
- JSValue method;
- if (!JS_IsObject(obj))
- goto fail;
- method = JS_GetProperty(ctx, obj, JS_ATOM_Symbol_hasInstance);
- if (JS_IsException(method))
- return -1;
- if (!JS_IsNull(method) && !JS_IsUndefined(method)) {
- JSValue ret;
- ret = JS_CallFree(ctx, method, obj, 1, &val);
- return JS_ToBoolFree(ctx, ret);
- }
- /* legacy case */
- if (!JS_IsFunction(ctx, obj)) {
- fail:
- JS_ThrowTypeError(ctx, "invalid 'instanceof' right operand");
- return -1;
- }
- return JS_OrdinaryIsInstanceOf(ctx, val, obj);
- }
- /* return the value associated to the autoinit property or an exception */
- typedef JSValue JSAutoInitFunc(JSContext *ctx, JSObject *p, JSAtom atom, void *opaque);
- static JSAutoInitFunc *js_autoinit_func_table[] = {
- js_instantiate_prototype, /* JS_AUTOINIT_ID_PROTOTYPE */
- js_module_ns_autoinit, /* JS_AUTOINIT_ID_MODULE_NS */
- JS_InstantiateFunctionListItem2, /* JS_AUTOINIT_ID_PROP */
- };
- /* warning: 'prs' is reallocated after it */
- static int JS_AutoInitProperty(JSContext *ctx, JSObject *p, JSAtom prop,
- JSProperty *pr, JSShapeProperty *prs)
- {
- JSValue val;
- JSContext *realm;
- JSAutoInitFunc *func;
- JSAutoInitIDEnum id;
-
- if (js_shape_prepare_update(ctx, p, &prs))
- return -1;
- realm = js_autoinit_get_realm(pr);
- id = js_autoinit_get_id(pr);
- func = js_autoinit_func_table[id];
- /* 'func' shall not modify the object properties 'pr' */
- val = func(realm, p, prop, pr->u.init.opaque);
- js_autoinit_free(ctx->rt, pr);
- prs->flags &= ~JS_PROP_TMASK;
- pr->u.value = JS_UNDEFINED;
- if (JS_IsException(val))
- return -1;
- if (id == JS_AUTOINIT_ID_MODULE_NS &&
- JS_VALUE_GET_TAG(val) == JS_TAG_STRING) {
- /* WARNING: a varref is returned as a string ! */
- prs->flags |= JS_PROP_VARREF;
- pr->u.var_ref = JS_VALUE_GET_PTR(val);
- js_rc(pr->u.var_ref)->ref_count++;
- } else if (p->class_id == JS_CLASS_GLOBAL_OBJECT) {
- JSVarRef *var_ref;
- /* in the global object we use references */
- var_ref = js_create_var_ref(ctx, FALSE);
- if (!var_ref)
- return -1;
- prs->flags |= JS_PROP_VARREF;
- pr->u.var_ref = var_ref;
- var_ref->value = val;
- var_ref->is_const = !(prs->flags & JS_PROP_WRITABLE);
- } else {
- pr->u.value = val;
- }
- return 0;
- }
- JSValue JS_GetPropertyInternal(JSContext *ctx, JSValueConst obj,
- JSAtom prop, JSValueConst this_obj,
- BOOL throw_ref_error)
- {
- JSObject *p;
- JSProperty *pr;
- JSShapeProperty *prs;
- uint32_t tag;
- tag = JS_VALUE_GET_TAG(obj);
- if (unlikely(tag != JS_TAG_OBJECT)) {
- switch(tag) {
- case JS_TAG_NULL:
- return JS_ThrowTypeErrorAtom(ctx, "cannot read property '%s' of null", prop);
- case JS_TAG_UNDEFINED:
- return JS_ThrowTypeErrorAtom(ctx, "cannot read property '%s' of undefined", prop);
- case JS_TAG_EXCEPTION:
- return JS_EXCEPTION;
- case JS_TAG_STRING:
- {
- JSString *p1 = JS_VALUE_GET_STRING(obj);
- if (__JS_AtomIsTaggedInt(prop)) {
- uint32_t idx;
- idx = __JS_AtomToUInt32(prop);
- if (idx < p1->len) {
- return js_new_string_char(ctx, string_get(p1, idx));
- }
- } else if (prop == JS_ATOM_length) {
- return JS_NewInt32(ctx, p1->len);
- }
- }
- break;
- case JS_TAG_STRING_ROPE:
- {
- JSStringRope *p1 = JS_VALUE_GET_STRING_ROPE(obj);
- if (__JS_AtomIsTaggedInt(prop)) {
- uint32_t idx;
- idx = __JS_AtomToUInt32(prop);
- if (idx < p1->len) {
- return js_new_string_char(ctx, string_rope_get(obj, idx));
- }
- } else if (prop == JS_ATOM_length) {
- return JS_NewInt32(ctx, p1->len);
- }
- }
- break;
- default:
- break;
- }
- /* cannot raise an exception */
- p = JS_VALUE_GET_OBJ(JS_GetPrototypePrimitive(ctx, obj));
- if (!p)
- return JS_UNDEFINED;
- } else {
- p = JS_VALUE_GET_OBJ(obj);
- }
- for(;;) {
- prs = find_own_property(&pr, p, prop);
- if (prs) {
- /* found */
- if (unlikely(prs->flags & JS_PROP_TMASK)) {
- if ((prs->flags & JS_PROP_TMASK) == JS_PROP_GETSET) {
- if (unlikely(!pr->u.getset.getter)) {
- return JS_UNDEFINED;
- } else {
- JSValue func = JS_MKPTR(JS_TAG_OBJECT, pr->u.getset.getter);
- /* Note: the field could be removed in the getter */
- func = JS_DupValue(ctx, func);
- return JS_CallFree(ctx, func, this_obj, 0, NULL);
- }
- } else if ((prs->flags & JS_PROP_TMASK) == JS_PROP_VARREF) {
- JSValue val = *pr->u.var_ref->pvalue;
- if (unlikely(JS_IsUninitialized(val)))
- return JS_ThrowReferenceErrorUninitialized(ctx, prs->atom);
- return JS_DupValue(ctx, val);
- } else if ((prs->flags & JS_PROP_TMASK) == JS_PROP_AUTOINIT) {
- /* Instantiate property and retry */
- if (JS_AutoInitProperty(ctx, p, prop, pr, prs))
- return JS_EXCEPTION;
- continue;
- }
- } else {
- return JS_DupValue(ctx, pr->u.value);
- }
- }
- if (unlikely(p->is_exotic)) {
- /* exotic behaviors */
- if (p->fast_array) {
- if (__JS_AtomIsTaggedInt(prop)) {
- uint32_t idx = __JS_AtomToUInt32(prop);
- if (idx < p->u.array.count) {
- /* we avoid duplicating the code */
- return JS_GetPropertyUint32(ctx, JS_MKPTR(JS_TAG_OBJECT, p), idx);
- } else if (p->class_id >= JS_CLASS_UINT8C_ARRAY &&
- p->class_id <= JS_CLASS_FLOAT64_ARRAY) {
- return JS_UNDEFINED;
- }
- } else if (p->class_id >= JS_CLASS_UINT8C_ARRAY &&
- p->class_id <= JS_CLASS_FLOAT64_ARRAY) {
- int ret;
- ret = JS_AtomIsNumericIndex(ctx, prop);
- if (ret != 0) {
- if (ret < 0)
- return JS_EXCEPTION;
- return JS_UNDEFINED;
- }
- }
- } else {
- const JSClassExoticMethods *em = ctx->rt->class_array[p->class_id].exotic;
- if (em) {
- if (em->get_property) {
- JSValue obj1, retval;
- /* XXX: should pass throw_ref_error */
- /* Note: if 'p' is a prototype, it can be
- freed in the called function */
- obj1 = JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, p));
- retval = em->get_property(ctx, obj1, prop, this_obj);
- JS_FreeValue(ctx, obj1);
- return retval;
- }
- if (em->get_own_property) {
- JSPropertyDescriptor desc;
- int ret;
- JSValue obj1;
- /* Note: if 'p' is a prototype, it can be
- freed in the called function */
- obj1 = JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, p));
- ret = em->get_own_property(ctx, &desc, obj1, prop);
- JS_FreeValue(ctx, obj1);
- if (ret < 0)
- return JS_EXCEPTION;
- if (ret) {
- if (desc.flags & JS_PROP_GETSET) {
- JS_FreeValue(ctx, desc.setter);
- return JS_CallFree(ctx, desc.getter, this_obj, 0, NULL);
- } else {
- return desc.value;
- }
- }
- }
- }
- }
- }
- p = p->shape->proto;
- if (!p)
- break;
- }
- if (unlikely(throw_ref_error)) {
- return JS_ThrowReferenceErrorNotDefined(ctx, prop);
- } else {
- return JS_UNDEFINED;
- }
- }
- static JSValue JS_ThrowTypeErrorPrivateNotFound(JSContext *ctx, JSAtom atom)
- {
- return JS_ThrowTypeErrorAtom(ctx, "private class field '%s' does not exist",
- atom);
- }
- /* Private fields can be added even on non extensible objects or
- Proxies */
- static int JS_DefinePrivateField(JSContext *ctx, JSValueConst obj,
- JSValueConst name, JSValue val)
- {
- JSObject *p;
- JSShapeProperty *prs;
- JSProperty *pr;
- JSAtom prop;
- if (unlikely(JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)) {
- JS_ThrowTypeErrorNotAnObject(ctx);
- goto fail;
- }
- /* safety check */
- if (unlikely(JS_VALUE_GET_TAG(name) != JS_TAG_SYMBOL)) {
- JS_ThrowTypeErrorNotASymbol(ctx);
- goto fail;
- }
- prop = js_symbol_to_atom(ctx, (JSValue)name);
- p = JS_VALUE_GET_OBJ(obj);
- prs = find_own_property(&pr, p, prop);
- if (prs) {
- JS_ThrowTypeErrorAtom(ctx, "private class field '%s' already exists",
- prop);
- goto fail;
- }
- pr = add_property(ctx, p, prop, JS_PROP_C_W_E);
- if (unlikely(!pr)) {
- fail:
- JS_FreeValue(ctx, val);
- return -1;
- }
- pr->u.value = val;
- return 0;
- }
- static JSValue JS_GetPrivateField(JSContext *ctx, JSValueConst obj,
- JSValueConst name)
- {
- JSObject *p;
- JSShapeProperty *prs;
- JSProperty *pr;
- JSAtom prop;
- if (unlikely(JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT))
- return JS_ThrowTypeErrorNotAnObject(ctx);
- /* safety check */
- if (unlikely(JS_VALUE_GET_TAG(name) != JS_TAG_SYMBOL))
- return JS_ThrowTypeErrorNotASymbol(ctx);
- prop = js_symbol_to_atom(ctx, (JSValue)name);
- p = JS_VALUE_GET_OBJ(obj);
- prs = find_own_property(&pr, p, prop);
- if (!prs) {
- JS_ThrowTypeErrorPrivateNotFound(ctx, prop);
- return JS_EXCEPTION;
- }
- return JS_DupValue(ctx, pr->u.value);
- }
- static int JS_SetPrivateField(JSContext *ctx, JSValueConst obj,
- JSValueConst name, JSValue val)
- {
- JSObject *p;
- JSShapeProperty *prs;
- JSProperty *pr;
- JSAtom prop;
- if (unlikely(JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)) {
- JS_ThrowTypeErrorNotAnObject(ctx);
- goto fail;
- }
- /* safety check */
- if (unlikely(JS_VALUE_GET_TAG(name) != JS_TAG_SYMBOL)) {
- JS_ThrowTypeErrorNotASymbol(ctx);
- goto fail;
- }
- prop = js_symbol_to_atom(ctx, (JSValue)name);
- p = JS_VALUE_GET_OBJ(obj);
- prs = find_own_property(&pr, p, prop);
- if (!prs) {
- JS_ThrowTypeErrorPrivateNotFound(ctx, prop);
- fail:
- JS_FreeValue(ctx, val);
- return -1;
- }
- set_value(ctx, &pr->u.value, val);
- return 0;
- }
- /* add a private brand field to 'home_obj' if not already present and
- if obj is != null add a private brand to it */
- static int JS_AddBrand(JSContext *ctx, JSValueConst obj, JSValueConst home_obj)
- {
- JSObject *p, *p1;
- JSShapeProperty *prs;
- JSProperty *pr;
- JSValue brand;
- JSAtom brand_atom;
- if (unlikely(JS_VALUE_GET_TAG(home_obj) != JS_TAG_OBJECT)) {
- JS_ThrowTypeErrorNotAnObject(ctx);
- return -1;
- }
- p = JS_VALUE_GET_OBJ(home_obj);
- prs = find_own_property(&pr, p, JS_ATOM_Private_brand);
- if (!prs) {
- /* if the brand is not present, add it */
- brand = JS_NewSymbolFromAtom(ctx, JS_ATOM_brand, JS_ATOM_TYPE_PRIVATE);
- if (JS_IsException(brand))
- return -1;
- pr = add_property(ctx, p, JS_ATOM_Private_brand, JS_PROP_C_W_E);
- if (!pr) {
- JS_FreeValue(ctx, brand);
- return -1;
- }
- pr->u.value = JS_DupValue(ctx, brand);
- } else {
- brand = JS_DupValue(ctx, pr->u.value);
- }
- brand_atom = js_symbol_to_atom(ctx, brand);
- if (JS_IsObject(obj)) {
- p1 = JS_VALUE_GET_OBJ(obj);
- prs = find_own_property(&pr, p1, brand_atom);
- if (unlikely(prs)) {
- JS_FreeAtom(ctx, brand_atom);
- JS_ThrowTypeError(ctx, "private method is already present");
- return -1;
- }
- pr = add_property(ctx, p1, brand_atom, JS_PROP_C_W_E);
- JS_FreeAtom(ctx, brand_atom);
- if (!pr)
- return -1;
- pr->u.value = JS_UNDEFINED;
- } else {
- JS_FreeAtom(ctx, brand_atom);
- }
- return 0;
- }
- /* return a boolean telling if the brand of the home object of 'func'
- is present on 'obj' or -1 in case of exception */
- static int JS_CheckBrand(JSContext *ctx, JSValueConst obj, JSValueConst func)
- {
- JSObject *p, *p1, *home_obj;
- JSShapeProperty *prs;
- JSProperty *pr;
- JSValueConst brand;
- /* get the home object of 'func' */
- if (unlikely(JS_VALUE_GET_TAG(func) != JS_TAG_OBJECT))
- goto not_obj;
- p1 = JS_VALUE_GET_OBJ(func);
- if (!js_class_has_bytecode(p1->class_id))
- goto not_obj;
- home_obj = p1->u.func.home_object;
- if (!home_obj)
- goto not_obj;
- prs = find_own_property(&pr, home_obj, JS_ATOM_Private_brand);
- if (!prs) {
- JS_ThrowTypeError(ctx, "expecting <brand> private field");
- return -1;
- }
- brand = pr->u.value;
- /* safety check */
- if (unlikely(JS_VALUE_GET_TAG(brand) != JS_TAG_SYMBOL))
- goto not_obj;
- /* get the brand array of 'obj' */
- if (unlikely(JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)) {
- not_obj:
- JS_ThrowTypeErrorNotAnObject(ctx);
- return -1;
- }
- p = JS_VALUE_GET_OBJ(obj);
- prs = find_own_property(&pr, p, js_symbol_to_atom(ctx, (JSValue)brand));
- return (prs != NULL);
- }
- static uint32_t js_string_obj_get_length(JSContext *ctx,
- JSValueConst obj)
- {
- JSObject *p;
- uint32_t len = 0;
- /* This is a class exotic method: obj class_id is JS_CLASS_STRING */
- p = JS_VALUE_GET_OBJ(obj);
- if (JS_VALUE_GET_TAG(p->u.object_data) == JS_TAG_STRING) {
- JSString *p1 = JS_VALUE_GET_STRING(p->u.object_data);
- len = p1->len;
- }
- return len;
- }
- static int num_keys_cmp(const void *p1, const void *p2, void *opaque)
- {
- JSContext *ctx = opaque;
- JSAtom atom1 = ((const JSPropertyEnum *)p1)->atom;
- JSAtom atom2 = ((const JSPropertyEnum *)p2)->atom;
- uint32_t v1, v2;
- BOOL atom1_is_integer, atom2_is_integer;
- atom1_is_integer = JS_AtomIsArrayIndex(ctx, &v1, atom1);
- atom2_is_integer = JS_AtomIsArrayIndex(ctx, &v2, atom2);
- assert(atom1_is_integer && atom2_is_integer);
- if (v1 < v2)
- return -1;
- else if (v1 == v2)
- return 0;
- else
- return 1;
- }
- void JS_FreePropertyEnum(JSContext *ctx, JSPropertyEnum *tab, uint32_t len)
- {
- uint32_t i;
- if (tab) {
- for(i = 0; i < len; i++)
- JS_FreeAtom(ctx, tab[i].atom);
- js_free(ctx, tab);
- }
- }
- /* return < 0 in case if exception, 0 if OK. ptab and its atoms must
- be freed by the user. */
- static int __exception JS_GetOwnPropertyNamesInternal(JSContext *ctx,
- JSPropertyEnum **ptab,
- uint32_t *plen,
- JSObject *p, int flags)
- {
- int i, j;
- JSShape *sh;
- JSShapeProperty *prs;
- JSPropertyEnum *tab_atom, *tab_exotic;
- JSAtom atom;
- uint32_t num_keys_count, str_keys_count, sym_keys_count, atom_count;
- uint32_t num_index, str_index, sym_index, exotic_count, exotic_keys_count;
- BOOL is_enumerable, num_sorted;
- uint32_t num_key;
- JSAtomKindEnum kind;
- /* clear pointer for consistency in case of failure */
- *ptab = NULL;
- *plen = 0;
- /* compute the number of returned properties */
- num_keys_count = 0;
- str_keys_count = 0;
- sym_keys_count = 0;
- exotic_keys_count = 0;
- exotic_count = 0;
- tab_exotic = NULL;
- sh = p->shape;
- for(i = 0, prs = get_shape_prop(sh); i < sh->prop_count; i++, prs++) {
- atom = prs->atom;
- if (atom != JS_ATOM_NULL) {
- is_enumerable = ((prs->flags & JS_PROP_ENUMERABLE) != 0);
- kind = JS_AtomGetKind(ctx, atom);
- if ((!(flags & JS_GPN_ENUM_ONLY) || is_enumerable) &&
- ((flags >> kind) & 1) != 0) {
- /* need to raise an exception in case of the module
- name space (implicit GetOwnProperty) */
- if (unlikely((prs->flags & JS_PROP_TMASK) == JS_PROP_VARREF) &&
- (flags & (JS_GPN_SET_ENUM | JS_GPN_ENUM_ONLY))) {
- JSVarRef *var_ref = p->prop[i].u.var_ref;
- if (unlikely(JS_IsUninitialized(*var_ref->pvalue))) {
- JS_ThrowReferenceErrorUninitialized(ctx, prs->atom);
- return -1;
- }
- }
- if (JS_AtomIsArrayIndex(ctx, &num_key, atom)) {
- num_keys_count++;
- } else if (kind == JS_ATOM_KIND_STRING) {
- str_keys_count++;
- } else {
- sym_keys_count++;
- }
- }
- }
- }
- if (p->is_exotic) {
- if (p->fast_array) {
- if (flags & JS_GPN_STRING_MASK) {
- num_keys_count += p->u.array.count;
- }
- } else if (p->class_id == JS_CLASS_STRING) {
- if (flags & JS_GPN_STRING_MASK) {
- num_keys_count += js_string_obj_get_length(ctx, JS_MKPTR(JS_TAG_OBJECT, p));
- }
- } else {
- const JSClassExoticMethods *em = ctx->rt->class_array[p->class_id].exotic;
- if (em && em->get_own_property_names) {
- if (em->get_own_property_names(ctx, &tab_exotic, &exotic_count,
- JS_MKPTR(JS_TAG_OBJECT, p)))
- return -1;
- for(i = 0; i < exotic_count; i++) {
- atom = tab_exotic[i].atom;
- kind = JS_AtomGetKind(ctx, atom);
- if (((flags >> kind) & 1) != 0) {
- is_enumerable = FALSE;
- if (flags & (JS_GPN_SET_ENUM | JS_GPN_ENUM_ONLY)) {
- JSPropertyDescriptor desc;
- int res;
- /* set the "is_enumerable" field if necessary */
- res = JS_GetOwnPropertyInternal(ctx, &desc, p, atom);
- if (res < 0) {
- JS_FreePropertyEnum(ctx, tab_exotic, exotic_count);
- return -1;
- }
- if (res) {
- is_enumerable =
- ((desc.flags & JS_PROP_ENUMERABLE) != 0);
- js_free_desc(ctx, &desc);
- }
- tab_exotic[i].is_enumerable = is_enumerable;
- }
- if (!(flags & JS_GPN_ENUM_ONLY) || is_enumerable) {
- exotic_keys_count++;
- }
- }
- }
- }
- }
- }
- /* fill them */
- atom_count = num_keys_count + str_keys_count;
- if (atom_count < str_keys_count)
- goto add_overflow;
- atom_count += sym_keys_count;
- if (atom_count < sym_keys_count)
- goto add_overflow;
- atom_count += exotic_keys_count;
- if (atom_count < exotic_keys_count || atom_count > INT32_MAX) {
- add_overflow:
- JS_ThrowOutOfMemory(ctx);
- JS_FreePropertyEnum(ctx, tab_exotic, exotic_count);
- return -1;
- }
- /* XXX: need generic way to test for js_malloc(ctx, a * b) overflow */
-
- /* avoid allocating 0 bytes */
- tab_atom = js_malloc(ctx, sizeof(tab_atom[0]) * max_int(atom_count, 1));
- if (!tab_atom) {
- JS_FreePropertyEnum(ctx, tab_exotic, exotic_count);
- return -1;
- }
- num_index = 0;
- str_index = num_keys_count;
- sym_index = str_index + str_keys_count;
- num_sorted = TRUE;
- sh = p->shape;
- for(i = 0, prs = get_shape_prop(sh); i < sh->prop_count; i++, prs++) {
- atom = prs->atom;
- if (atom != JS_ATOM_NULL) {
- is_enumerable = ((prs->flags & JS_PROP_ENUMERABLE) != 0);
- kind = JS_AtomGetKind(ctx, atom);
- if ((!(flags & JS_GPN_ENUM_ONLY) || is_enumerable) &&
- ((flags >> kind) & 1) != 0) {
- if (JS_AtomIsArrayIndex(ctx, &num_key, atom)) {
- j = num_index++;
- num_sorted = FALSE;
- } else if (kind == JS_ATOM_KIND_STRING) {
- j = str_index++;
- } else {
- j = sym_index++;
- }
- tab_atom[j].atom = JS_DupAtom(ctx, atom);
- tab_atom[j].is_enumerable = is_enumerable;
- }
- }
- }
- if (p->is_exotic) {
- int len;
- if (p->fast_array) {
- if (flags & JS_GPN_STRING_MASK) {
- len = p->u.array.count;
- goto add_array_keys;
- }
- } else if (p->class_id == JS_CLASS_STRING) {
- if (flags & JS_GPN_STRING_MASK) {
- len = js_string_obj_get_length(ctx, JS_MKPTR(JS_TAG_OBJECT, p));
- add_array_keys:
- for(i = 0; i < len; i++) {
- tab_atom[num_index].atom = __JS_AtomFromUInt32(i);
- if (tab_atom[num_index].atom == JS_ATOM_NULL) {
- JS_FreePropertyEnum(ctx, tab_atom, num_index);
- return -1;
- }
- tab_atom[num_index].is_enumerable = TRUE;
- num_index++;
- }
- }
- } else {
- /* Note: exotic keys are not reordered and comes after the object own properties. */
- for(i = 0; i < exotic_count; i++) {
- atom = tab_exotic[i].atom;
- is_enumerable = tab_exotic[i].is_enumerable;
- kind = JS_AtomGetKind(ctx, atom);
- if ((!(flags & JS_GPN_ENUM_ONLY) || is_enumerable) &&
- ((flags >> kind) & 1) != 0) {
- tab_atom[sym_index].atom = atom;
- tab_atom[sym_index].is_enumerable = is_enumerable;
- sym_index++;
- } else {
- JS_FreeAtom(ctx, atom);
- }
- }
- js_free(ctx, tab_exotic);
- }
- }
- assert(num_index == num_keys_count);
- assert(str_index == num_keys_count + str_keys_count);
- assert(sym_index == atom_count);
- if (num_keys_count != 0 && !num_sorted) {
- rqsort(tab_atom, num_keys_count, sizeof(tab_atom[0]), num_keys_cmp,
- ctx);
- }
- *ptab = tab_atom;
- *plen = atom_count;
- return 0;
- }
- int JS_GetOwnPropertyNames(JSContext *ctx, JSPropertyEnum **ptab,
- uint32_t *plen, JSValueConst obj, int flags)
- {
- if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT) {
- JS_ThrowTypeErrorNotAnObject(ctx);
- return -1;
- }
- return JS_GetOwnPropertyNamesInternal(ctx, ptab, plen,
- JS_VALUE_GET_OBJ(obj), flags);
- }
- /* Return -1 if exception,
- FALSE if the property does not exist, TRUE if it exists. If TRUE is
- returned, the property descriptor 'desc' is filled present. */
- static int JS_GetOwnPropertyInternal(JSContext *ctx, JSPropertyDescriptor *desc,
- JSObject *p, JSAtom prop)
- {
- JSShapeProperty *prs;
- JSProperty *pr;
- retry:
- prs = find_own_property(&pr, p, prop);
- if (prs) {
- if (desc) {
- desc->flags = prs->flags & JS_PROP_C_W_E;
- desc->getter = JS_UNDEFINED;
- desc->setter = JS_UNDEFINED;
- desc->value = JS_UNDEFINED;
- if (unlikely(prs->flags & JS_PROP_TMASK)) {
- if ((prs->flags & JS_PROP_TMASK) == JS_PROP_GETSET) {
- desc->flags |= JS_PROP_GETSET;
- if (pr->u.getset.getter)
- desc->getter = JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, pr->u.getset.getter));
- if (pr->u.getset.setter)
- desc->setter = JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, pr->u.getset.setter));
- } else if ((prs->flags & JS_PROP_TMASK) == JS_PROP_VARREF) {
- JSValue val = *pr->u.var_ref->pvalue;
- if (unlikely(JS_IsUninitialized(val))) {
- JS_ThrowReferenceErrorUninitialized(ctx, prs->atom);
- return -1;
- }
- desc->value = JS_DupValue(ctx, val);
- } else if ((prs->flags & JS_PROP_TMASK) == JS_PROP_AUTOINIT) {
- /* Instantiate property and retry */
- if (JS_AutoInitProperty(ctx, p, prop, pr, prs))
- return -1;
- goto retry;
- }
- } else {
- desc->value = JS_DupValue(ctx, pr->u.value);
- }
- } else {
- /* for consistency, send the exception even if desc is NULL */
- if (unlikely((prs->flags & JS_PROP_TMASK) == JS_PROP_VARREF)) {
- if (unlikely(JS_IsUninitialized(*pr->u.var_ref->pvalue))) {
- JS_ThrowReferenceErrorUninitialized(ctx, prs->atom);
- return -1;
- }
- } else if ((prs->flags & JS_PROP_TMASK) == JS_PROP_AUTOINIT) {
- /* nothing to do: delay instantiation until actual value and/or attributes are read */
- }
- }
- return TRUE;
- }
- if (p->is_exotic) {
- if (p->fast_array) {
- /* specific case for fast arrays */
- if (__JS_AtomIsTaggedInt(prop)) {
- uint32_t idx;
- idx = __JS_AtomToUInt32(prop);
- if (idx < p->u.array.count) {
- if (desc) {
- desc->flags = JS_PROP_WRITABLE | JS_PROP_ENUMERABLE |
- JS_PROP_CONFIGURABLE;
- desc->getter = JS_UNDEFINED;
- desc->setter = JS_UNDEFINED;
- desc->value = JS_GetPropertyUint32(ctx, JS_MKPTR(JS_TAG_OBJECT, p), idx);
- }
- return TRUE;
- }
- }
- } else {
- const JSClassExoticMethods *em = ctx->rt->class_array[p->class_id].exotic;
- if (em && em->get_own_property) {
- return em->get_own_property(ctx, desc,
- JS_MKPTR(JS_TAG_OBJECT, p), prop);
- }
- }
- }
- return FALSE;
- }
- int JS_GetOwnProperty(JSContext *ctx, JSPropertyDescriptor *desc,
- JSValueConst obj, JSAtom prop)
- {
- if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT) {
- JS_ThrowTypeErrorNotAnObject(ctx);
- return -1;
- }
- return JS_GetOwnPropertyInternal(ctx, desc, JS_VALUE_GET_OBJ(obj), prop);
- }
- /* return -1 if exception (exotic object only) or TRUE/FALSE */
- int JS_IsExtensible(JSContext *ctx, JSValueConst obj)
- {
- JSObject *p;
- if (unlikely(JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT))
- return FALSE;
- p = JS_VALUE_GET_OBJ(obj);
- if (unlikely(p->is_exotic)) {
- const JSClassExoticMethods *em = ctx->rt->class_array[p->class_id].exotic;
- if (em && em->is_extensible) {
- return em->is_extensible(ctx, obj);
- }
- }
- return p->extensible;
- }
- /* return -1 if exception (exotic object only) or TRUE/FALSE */
- int JS_PreventExtensions(JSContext *ctx, JSValueConst obj)
- {
- JSObject *p;
- if (unlikely(JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT))
- return FALSE;
- p = JS_VALUE_GET_OBJ(obj);
- if (unlikely(p->is_exotic)) {
- if (p->class_id >= JS_CLASS_UINT8C_ARRAY &&
- p->class_id <= JS_CLASS_FLOAT64_ARRAY) {
- JSTypedArray *ta;
- JSArrayBuffer *abuf;
- /* resizable type arrays return FALSE */
- ta = p->u.typed_array;
- abuf = ta->buffer->u.array_buffer;
- if (ta->track_rab ||
- (array_buffer_is_resizable(abuf) && !abuf->shared))
- return FALSE;
- } else {
- const JSClassExoticMethods *em = ctx->rt->class_array[p->class_id].exotic;
- if (em && em->prevent_extensions) {
- return em->prevent_extensions(ctx, obj);
- }
- }
- }
- p->extensible = FALSE;
- return TRUE;
- }
- /* return -1 if exception otherwise TRUE or FALSE */
- int JS_HasProperty(JSContext *ctx, JSValueConst obj, JSAtom prop)
- {
- JSObject *p;
- int ret;
- JSValue obj1;
- if (unlikely(JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT))
- return FALSE;
- p = JS_VALUE_GET_OBJ(obj);
- for(;;) {
- if (p->is_exotic) {
- const JSClassExoticMethods *em = ctx->rt->class_array[p->class_id].exotic;
- if (em && em->has_property) {
- /* has_property can free the prototype */
- obj1 = JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, p));
- ret = em->has_property(ctx, obj1, prop);
- JS_FreeValue(ctx, obj1);
- return ret;
- }
- }
- /* JS_GetOwnPropertyInternal can free the prototype */
- JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, p));
- ret = JS_GetOwnPropertyInternal(ctx, NULL, p, prop);
- JS_FreeValue(ctx, JS_MKPTR(JS_TAG_OBJECT, p));
- if (ret != 0)
- return ret;
- if (p->class_id >= JS_CLASS_UINT8C_ARRAY &&
- p->class_id <= JS_CLASS_FLOAT64_ARRAY) {
- ret = JS_AtomIsNumericIndex(ctx, prop);
- if (ret != 0) {
- if (ret < 0)
- return -1;
- return FALSE;
- }
- }
- p = p->shape->proto;
- if (!p)
- break;
- }
- return FALSE;
- }
- /* val must be a symbol */
- static JSAtom js_symbol_to_atom(JSContext *ctx, JSValue val)
- {
- JSAtomStruct *p = JS_VALUE_GET_PTR(val);
- return js_get_atom_index(ctx->rt, p);
- }
- /* return JS_ATOM_NULL in case of exception */
- JSAtom JS_ValueToAtom(JSContext *ctx, JSValueConst val)
- {
- JSAtom atom;
- uint32_t tag;
- tag = JS_VALUE_GET_TAG(val);
- if (tag == JS_TAG_INT &&
- (uint32_t)JS_VALUE_GET_INT(val) <= JS_ATOM_MAX_INT) {
- /* fast path for integer values */
- atom = __JS_AtomFromUInt32(JS_VALUE_GET_INT(val));
- } else if (tag == JS_TAG_SYMBOL) {
- JSAtomStruct *p = JS_VALUE_GET_PTR(val);
- atom = JS_DupAtom(ctx, js_get_atom_index(ctx->rt, p));
- } else {
- JSValue str;
- str = JS_ToPropertyKey(ctx, val);
- if (JS_IsException(str))
- return JS_ATOM_NULL;
- if (JS_VALUE_GET_TAG(str) == JS_TAG_SYMBOL) {
- atom = js_symbol_to_atom(ctx, str);
- } else {
- atom = JS_NewAtomStr(ctx, JS_VALUE_GET_STRING(str));
- }
- }
- return atom;
- }
- static JSValue JS_GetPropertyValue(JSContext *ctx, JSValueConst this_obj,
- JSValue prop)
- {
- JSAtom atom;
- JSValue ret;
- if (likely(JS_VALUE_GET_TAG(this_obj) == JS_TAG_OBJECT &&
- JS_VALUE_GET_TAG(prop) == JS_TAG_INT)) {
- JSObject *p;
- uint32_t idx;
- /* fast path for array access */
- p = JS_VALUE_GET_OBJ(this_obj);
- idx = JS_VALUE_GET_INT(prop);
- switch(p->class_id) {
- case JS_CLASS_ARRAY:
- case JS_CLASS_ARGUMENTS:
- if (unlikely(idx >= p->u.array.count)) goto slow_path;
- return JS_DupValue(ctx, p->u.array.u.values[idx]);
- case JS_CLASS_MAPPED_ARGUMENTS:
- if (unlikely(idx >= p->u.array.count)) goto slow_path;
- return JS_DupValue(ctx, *p->u.array.u.var_refs[idx]->pvalue);
- case JS_CLASS_INT8_ARRAY:
- if (unlikely(idx >= p->u.array.count)) goto slow_path;
- return JS_NewInt32(ctx, p->u.array.u.int8_ptr[idx]);
- case JS_CLASS_UINT8C_ARRAY:
- case JS_CLASS_UINT8_ARRAY:
- if (unlikely(idx >= p->u.array.count)) goto slow_path;
- return JS_NewInt32(ctx, p->u.array.u.uint8_ptr[idx]);
- case JS_CLASS_INT16_ARRAY:
- if (unlikely(idx >= p->u.array.count)) goto slow_path;
- return JS_NewInt32(ctx, p->u.array.u.int16_ptr[idx]);
- case JS_CLASS_UINT16_ARRAY:
- if (unlikely(idx >= p->u.array.count)) goto slow_path;
- return JS_NewInt32(ctx, p->u.array.u.uint16_ptr[idx]);
- case JS_CLASS_INT32_ARRAY:
- if (unlikely(idx >= p->u.array.count)) goto slow_path;
- return JS_NewInt32(ctx, p->u.array.u.int32_ptr[idx]);
- case JS_CLASS_UINT32_ARRAY:
- if (unlikely(idx >= p->u.array.count)) goto slow_path;
- return JS_NewUint32(ctx, p->u.array.u.uint32_ptr[idx]);
- case JS_CLASS_BIG_INT64_ARRAY:
- if (unlikely(idx >= p->u.array.count)) goto slow_path;
- return JS_NewBigInt64(ctx, p->u.array.u.int64_ptr[idx]);
- case JS_CLASS_BIG_UINT64_ARRAY:
- if (unlikely(idx >= p->u.array.count)) goto slow_path;
- return JS_NewBigUint64(ctx, p->u.array.u.uint64_ptr[idx]);
- case JS_CLASS_FLOAT16_ARRAY:
- if (unlikely(idx >= p->u.array.count)) goto slow_path;
- return __JS_NewFloat64(ctx, fromfp16(p->u.array.u.fp16_ptr[idx]));
- case JS_CLASS_FLOAT32_ARRAY:
- if (unlikely(idx >= p->u.array.count)) goto slow_path;
- return __JS_NewFloat64(ctx, p->u.array.u.float_ptr[idx]);
- case JS_CLASS_FLOAT64_ARRAY:
- if (unlikely(idx >= p->u.array.count)) goto slow_path;
- return __JS_NewFloat64(ctx, p->u.array.u.double_ptr[idx]);
- default:
- goto slow_path;
- }
- } else {
- slow_path:
- /* ToObject() must be done before ToPropertyKey() */
- if (JS_IsNull(this_obj) || JS_IsUndefined(this_obj)) {
- JS_FreeValue(ctx, prop);
- return JS_ThrowTypeError(ctx, "cannot read property of %s", JS_IsNull(this_obj) ? "null" : "undefined");
- }
- atom = JS_ValueToAtom(ctx, prop);
- JS_FreeValue(ctx, prop);
- if (unlikely(atom == JS_ATOM_NULL))
- return JS_EXCEPTION;
- ret = JS_GetProperty(ctx, this_obj, atom);
- JS_FreeAtom(ctx, atom);
- return ret;
- }
- }
- JSValue JS_GetPropertyUint32(JSContext *ctx, JSValueConst this_obj,
- uint32_t idx)
- {
- return JS_GetPropertyValue(ctx, this_obj, JS_NewUint32(ctx, idx));
- }
- /* Check if an object has a generalized numeric property. Return value:
- -1 for exception,
- TRUE if property exists, stored into *pval,
- FALSE if proprty does not exist.
- */
- static int JS_TryGetPropertyInt64(JSContext *ctx, JSValueConst obj, int64_t idx, JSValue *pval)
- {
- JSValue val = JS_UNDEFINED;
- JSAtom prop;
- int present;
- if (likely((uint64_t)idx <= JS_ATOM_MAX_INT)) {
- /* fast path */
- present = JS_HasProperty(ctx, obj, __JS_AtomFromUInt32(idx));
- if (present > 0) {
- val = JS_GetPropertyValue(ctx, obj, JS_NewInt32(ctx, idx));
- if (unlikely(JS_IsException(val)))
- present = -1;
- }
- } else {
- prop = JS_NewAtomInt64(ctx, idx);
- present = -1;
- if (likely(prop != JS_ATOM_NULL)) {
- present = JS_HasProperty(ctx, obj, prop);
- if (present > 0) {
- val = JS_GetProperty(ctx, obj, prop);
- if (unlikely(JS_IsException(val)))
- present = -1;
- }
- JS_FreeAtom(ctx, prop);
- }
- }
- *pval = val;
- return present;
- }
- static JSValue JS_GetPropertyInt64(JSContext *ctx, JSValueConst obj, int64_t idx)
- {
- JSAtom prop;
- JSValue val;
- if ((uint64_t)idx <= INT32_MAX) {
- /* fast path for fast arrays */
- return JS_GetPropertyValue(ctx, obj, JS_NewInt32(ctx, idx));
- }
- prop = JS_NewAtomInt64(ctx, idx);
- if (prop == JS_ATOM_NULL)
- return JS_EXCEPTION;
- val = JS_GetProperty(ctx, obj, prop);
- JS_FreeAtom(ctx, prop);
- return val;
- }
- JSValue JS_GetPropertyStr(JSContext *ctx, JSValueConst this_obj,
- const char *prop)
- {
- JSAtom atom;
- JSValue ret;
- atom = JS_NewAtom(ctx, prop);
- if (atom == JS_ATOM_NULL)
- return JS_EXCEPTION;
- ret = JS_GetProperty(ctx, this_obj, atom);
- JS_FreeAtom(ctx, atom);
- return ret;
- }
- /* Note: the property value is not initialized. Return NULL if memory
- error. */
- static JSProperty *add_property(JSContext *ctx,
- JSObject *p, JSAtom prop, int prop_flags)
- {
- JSShape *sh, *new_sh;
- if (unlikely(__JS_AtomIsTaggedInt(prop))) {
- /* update is_std_array_prototype */
- if (unlikely(p->is_std_array_prototype)) {
- p->is_std_array_prototype = FALSE;
- } else if (unlikely(p->has_immutable_prototype)) {
- struct list_head *el;
-
- /* modifying Object.prototype : reset the corresponding is_std_array_prototype */
- list_for_each(el, &ctx->rt->context_list) {
- JSContext *ctx1 = list_entry(el, JSContext, link);
- if (JS_IsObject(ctx1->class_proto[JS_CLASS_OBJECT]) &&
- JS_VALUE_GET_OBJ(ctx1->class_proto[JS_CLASS_OBJECT]) == p) {
- if (JS_IsObject(ctx1->class_proto[JS_CLASS_ARRAY])) {
- JSObject *p1 = JS_VALUE_GET_OBJ(ctx1->class_proto[JS_CLASS_ARRAY]);
- p1->is_std_array_prototype = FALSE;
- }
- break;
- }
- }
- }
- }
- sh = p->shape;
- if (sh->is_hashed) {
- /* try to find an existing shape */
- new_sh = find_hashed_shape_prop(ctx->rt, sh, prop, prop_flags);
- if (new_sh) {
- /* matching shape found: use it */
- /* the property array may need to be resized */
- if (new_sh->prop_size != sh->prop_size) {
- JSProperty *new_prop;
- new_prop = js_realloc(ctx, p->prop, sizeof(p->prop[0]) *
- new_sh->prop_size);
- if (!new_prop)
- return NULL;
- p->prop = new_prop;
- }
- p->shape = js_dup_shape(new_sh);
- js_free_shape(ctx->rt, sh);
- return &p->prop[new_sh->prop_count - 1];
- } else if (js_rc(sh)->ref_count != 1) {
- /* if the shape is shared, clone it */
- new_sh = js_clone_shape(ctx, sh);
- if (!new_sh)
- return NULL;
- /* hash the cloned shape */
- new_sh->is_hashed = TRUE;
- js_shape_hash_link(ctx->rt, new_sh);
- js_free_shape(ctx->rt, p->shape);
- p->shape = new_sh;
- }
- }
- assert(js_rc(p->shape)->ref_count == 1);
- if (add_shape_property(ctx, &p->shape, p, prop, prop_flags))
- return NULL;
- return &p->prop[p->shape->prop_count - 1];
- }
- /* can be called on JS_CLASS_ARRAY, JS_CLASS_ARGUMENTS or
- JS_CLASS_MAPPED_ARGUMENTS objects. return < 0 if memory alloc
- error. */
- static no_inline __exception int convert_fast_array_to_array(JSContext *ctx,
- JSObject *p)
- {
- JSProperty *pr;
- JSShape *sh;
- uint32_t i, len, new_count;
- if (js_shape_prepare_update(ctx, p, NULL))
- return -1;
- len = p->u.array.count;
- /* resize the properties once to simplify the error handling */
- sh = p->shape;
- new_count = sh->prop_count + len;
- if (new_count > sh->prop_size) {
- if (resize_properties(ctx, &p->shape, p, new_count))
- return -1;
- }
- if (p->class_id == JS_CLASS_MAPPED_ARGUMENTS) {
- JSVarRef **tab = p->u.array.u.var_refs;
- for(i = 0; i < len; i++) {
- /* add_property cannot fail here but
- __JS_AtomFromUInt32(i) fails for i > INT32_MAX */
- pr = add_property(ctx, p, __JS_AtomFromUInt32(i), JS_PROP_C_W_E | JS_PROP_VARREF);
- pr->u.var_ref = *tab++;
- }
- } else {
- JSValue *tab = p->u.array.u.values;
- for(i = 0; i < len; i++) {
- /* add_property cannot fail here but
- __JS_AtomFromUInt32(i) fails for i > INT32_MAX */
- pr = add_property(ctx, p, __JS_AtomFromUInt32(i), JS_PROP_C_W_E);
- pr->u.value = *tab++;
- }
- }
- js_free(ctx, p->u.array.u.values);
- p->u.array.count = 0;
- p->u.array.u.values = NULL; /* fail safe */
- p->u.array.u1.size = 0;
- p->fast_array = 0;
- p->is_std_array_prototype = FALSE;
- return 0;
- }
- static int remove_global_object_property(JSContext *ctx, JSObject *p,
- JSShapeProperty *prs, JSProperty *pr)
- {
- JSVarRef *var_ref;
- JSObject *p1;
- JSProperty *pr1;
-
- var_ref = pr->u.var_ref;
- if (js_rc(var_ref)->ref_count == 1)
- return 0;
- p1 = JS_VALUE_GET_OBJ(p->u.global_object.uninitialized_vars);
- pr1 = add_property(ctx, p1, prs->atom, JS_PROP_C_W_E | JS_PROP_VARREF);
- if (!pr1)
- return -1;
- pr1->u.var_ref = var_ref;
- js_rc(var_ref)->ref_count++;
- JS_FreeValue(ctx, var_ref->value);
- var_ref->is_lexical = FALSE;
- var_ref->is_const = FALSE;
- var_ref->value = JS_UNINITIALIZED;
- return 0;
- }
- static int delete_property(JSContext *ctx, JSObject *p, JSAtom atom)
- {
- JSShape *sh;
- JSShapeProperty *pr, *lpr, *prop;
- JSProperty *pr1;
- uint32_t lpr_idx;
- intptr_t h, h1;
- redo:
- sh = p->shape;
- h1 = atom & sh->prop_hash_mask;
- h = sh->hash_table[h1];
- prop = get_shape_prop(sh);
- lpr = NULL;
- lpr_idx = 0; /* prevent warning */
- while (h != 0) {
- pr = &prop[h - 1];
- if (likely(pr->atom == atom)) {
- /* found ! */
- if (!(pr->flags & JS_PROP_CONFIGURABLE))
- return FALSE;
- /* realloc the shape if needed */
- if (lpr)
- lpr_idx = lpr - get_shape_prop(sh);
- if (js_shape_prepare_update(ctx, p, &pr))
- return -1;
- sh = p->shape;
- /* remove property */
- if (lpr) {
- lpr = get_shape_prop(sh) + lpr_idx;
- lpr->hash_next = pr->hash_next;
- } else {
- sh->hash_table[h1] = pr->hash_next;
- }
- sh->deleted_prop_count++;
- /* free the entry */
- pr1 = &p->prop[h - 1];
- if (unlikely(p->class_id == JS_CLASS_GLOBAL_OBJECT)) {
- if ((pr->flags & JS_PROP_TMASK) == JS_PROP_VARREF)
- if (remove_global_object_property(ctx, p, pr, pr1))
- return -1;
- }
- free_property(ctx->rt, pr1, pr->flags);
- JS_FreeAtom(ctx, pr->atom);
- /* put default values */
- pr->flags = 0;
- pr->atom = JS_ATOM_NULL;
- pr1->u.value = JS_UNDEFINED;
- /* compact the properties if too many deleted properties */
- if (sh->deleted_prop_count >= 8 &&
- sh->deleted_prop_count >= ((unsigned)sh->prop_count / 2)) {
- compact_properties(ctx, p);
- }
- return TRUE;
- }
- lpr = pr;
- h = pr->hash_next;
- }
- if (p->is_exotic) {
- if (p->fast_array) {
- uint32_t idx;
- if (JS_AtomIsArrayIndex(ctx, &idx, atom) &&
- idx < p->u.array.count) {
- if (p->class_id == JS_CLASS_ARRAY ||
- p->class_id == JS_CLASS_ARGUMENTS ||
- p->class_id == JS_CLASS_MAPPED_ARGUMENTS) {
- /* Special case deleting the last element of a fast Array */
- if (idx == p->u.array.count - 1) {
- if (p->class_id == JS_CLASS_MAPPED_ARGUMENTS) {
- free_var_ref(ctx->rt, p->u.array.u.var_refs[idx]);
- } else {
- JS_FreeValue(ctx, p->u.array.u.values[idx]);
- }
- p->u.array.count = idx;
- return TRUE;
- }
- if (convert_fast_array_to_array(ctx, p))
- return -1;
- goto redo;
- } else {
- return FALSE;
- }
- }
- } else {
- const JSClassExoticMethods *em = ctx->rt->class_array[p->class_id].exotic;
- if (em && em->delete_property) {
- return em->delete_property(ctx, JS_MKPTR(JS_TAG_OBJECT, p), atom);
- }
- }
- }
- /* not found */
- return TRUE;
- }
- static int call_setter(JSContext *ctx, JSObject *setter,
- JSValueConst this_obj, JSValue val, int flags)
- {
- JSValue ret, func;
- if (likely(setter)) {
- func = JS_MKPTR(JS_TAG_OBJECT, setter);
- /* Note: the field could be removed in the setter */
- func = JS_DupValue(ctx, func);
- ret = JS_CallFree(ctx, func, this_obj, 1, (JSValueConst *)&val);
- JS_FreeValue(ctx, val);
- if (JS_IsException(ret))
- return -1;
- JS_FreeValue(ctx, ret);
- return TRUE;
- } else {
- JS_FreeValue(ctx, val);
- if ((flags & JS_PROP_THROW) ||
- ((flags & JS_PROP_THROW_STRICT) && is_strict_mode(ctx))) {
- JS_ThrowTypeError(ctx, "no setter for property");
- return -1;
- }
- return FALSE;
- }
- }
- /* set the array length and remove the array elements if necessary. */
- static int set_array_length(JSContext *ctx, JSObject *p, JSValue val,
- int flags)
- {
- uint32_t len, idx, cur_len;
- int i, ret;
- /* Note: this call can reallocate the properties of 'p' */
- ret = JS_ToArrayLengthFree(ctx, &len, val, FALSE);
- if (ret)
- return -1;
- /* JS_ToArrayLengthFree() must be done before the read-only test */
- if (unlikely(!(get_shape_prop(p->shape)[0].flags & JS_PROP_WRITABLE)))
- return JS_ThrowTypeErrorReadOnly(ctx, flags, JS_ATOM_length);
- if (likely(p->fast_array)) {
- uint32_t old_len = p->u.array.count;
- if (len < old_len) {
- for(i = len; i < old_len; i++) {
- JS_FreeValue(ctx, p->u.array.u.values[i]);
- }
- p->u.array.count = len;
- }
- p->prop[0].u.value = JS_NewUint32(ctx, len);
- } else {
- /* Note: length is always a uint32 because the object is an
- array */
- JS_ToUint32(ctx, &cur_len, p->prop[0].u.value);
- if (len < cur_len) {
- uint32_t d;
- JSShape *sh;
- JSShapeProperty *pr;
- d = cur_len - len;
- sh = p->shape;
- if (d <= sh->prop_count) {
- JSAtom atom;
- /* faster to iterate */
- while (cur_len > len) {
- atom = JS_NewAtomUInt32(ctx, cur_len - 1);
- ret = delete_property(ctx, p, atom);
- JS_FreeAtom(ctx, atom);
- if (unlikely(!ret)) {
- /* unlikely case: property is not
- configurable */
- break;
- }
- cur_len--;
- }
- } else {
- /* faster to iterate thru all the properties. Need two
- passes in case one of the property is not
- configurable */
- cur_len = len;
- for(i = 0, pr = get_shape_prop(sh); i < sh->prop_count;
- i++, pr++) {
- if (pr->atom != JS_ATOM_NULL &&
- JS_AtomIsArrayIndex(ctx, &idx, pr->atom)) {
- if (idx >= cur_len &&
- !(pr->flags & JS_PROP_CONFIGURABLE)) {
- cur_len = idx + 1;
- }
- }
- }
- for(i = 0, pr = get_shape_prop(sh); i < sh->prop_count;
- i++, pr++) {
- if (pr->atom != JS_ATOM_NULL &&
- JS_AtomIsArrayIndex(ctx, &idx, pr->atom)) {
- if (idx >= cur_len) {
- /* remove the property */
- delete_property(ctx, p, pr->atom);
- /* WARNING: the shape may have been modified */
- sh = p->shape;
- pr = get_shape_prop(sh) + i;
- }
- }
- }
- }
- } else {
- cur_len = len;
- }
- set_value(ctx, &p->prop[0].u.value, JS_NewUint32(ctx, cur_len));
- if (unlikely(cur_len > len)) {
- return JS_ThrowTypeErrorOrFalse(ctx, flags, "not configurable");
- }
- }
- return TRUE;
- }
- /* return -1 if exception */
- static int expand_fast_array(JSContext *ctx, JSObject *p, uint32_t new_len)
- {
- uint32_t new_size;
- size_t slack;
- JSValue *new_array_prop;
- /* XXX: potential arithmetic overflow */
- new_size = max_int(new_len, p->u.array.u1.size * 3 / 2);
- new_array_prop = js_realloc2(ctx, p->u.array.u.values, sizeof(JSValue) * new_size, &slack);
- if (!new_array_prop)
- return -1;
- new_size += slack / sizeof(*new_array_prop);
- p->u.array.u.values = new_array_prop;
- p->u.array.u1.size = new_size;
- return 0;
- }
- /* Preconditions: 'p' must be of class JS_CLASS_ARRAY, p->fast_array =
- TRUE and p->extensible = TRUE */
- static inline int add_fast_array_element(JSContext *ctx, JSObject *p,
- JSValue val, int flags)
- {
- uint32_t new_len, array_len;
- /* extend the array by one */
- /* XXX: convert to slow array if new_len > 2^31-1 elements */
- new_len = p->u.array.count + 1;
- /* update the length if necessary. We assume that if the length is
- not an integer, then if it >= 2^31. */
- if (likely(JS_VALUE_GET_TAG(p->prop[0].u.value) == JS_TAG_INT)) {
- array_len = JS_VALUE_GET_INT(p->prop[0].u.value);
- if (new_len > array_len) {
- if (unlikely(!(get_shape_prop(p->shape)->flags & JS_PROP_WRITABLE))) {
- JS_FreeValue(ctx, val);
- return JS_ThrowTypeErrorReadOnly(ctx, flags, JS_ATOM_length);
- }
- p->prop[0].u.value = JS_NewInt32(ctx, new_len);
- }
- }
- if (unlikely(new_len > p->u.array.u1.size)) {
- if (expand_fast_array(ctx, p, new_len)) {
- JS_FreeValue(ctx, val);
- return -1;
- }
- }
- p->u.array.u.values[new_len - 1] = val;
- p->u.array.count = new_len;
- return TRUE;
- }
- /* Allocate a new fast array initialized to JS_UNDEFINED. Its maximum
- size is 2^31-1 elements. For convenience, 'len' is a 64 bit
- integer. */
- static JSValue js_allocate_fast_array(JSContext *ctx, int64_t len)
- {
- JSValue arr;
- JSObject *p;
- int i;
-
- if (len > INT32_MAX)
- return JS_ThrowRangeError(ctx, "invalid array length");
- arr = JS_NewArray(ctx);
- if (JS_IsException(arr))
- return arr;
- if (len > 0) {
- p = JS_VALUE_GET_OBJ(arr);
- if (expand_fast_array(ctx, p, len) < 0) {
- JS_FreeValue(ctx, arr);
- return JS_EXCEPTION;
- }
- p->u.array.count = len;
- for(i = 0; i < len; i++)
- p->u.array.u.values[i] = JS_UNDEFINED;
- /* update the 'length' field */
- set_value(ctx, &p->prop[0].u.value, JS_NewInt32(ctx, len));
- }
- return arr;
- }
- static JSValue js_create_array(JSContext *ctx, int len, JSValueConst *tab)
- {
- JSValue obj;
- JSObject *p;
- int i;
- obj = JS_NewArray(ctx);
- if (JS_IsException(obj))
- return JS_EXCEPTION;
- if (len > 0) {
- p = JS_VALUE_GET_OBJ(obj);
- if (expand_fast_array(ctx, p, len) < 0) {
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- p->u.array.count = len;
- for(i = 0; i < len; i++)
- p->u.array.u.values[i] = JS_DupValue(ctx, tab[i]);
- /* update the 'length' field */
- set_value(ctx, &p->prop[0].u.value, JS_NewInt32(ctx, len));
- }
- return obj;
- }
- static JSValue js_create_array_free(JSContext *ctx, int len, JSValue *tab)
- {
- JSValue obj;
- JSObject *p;
- int i;
- obj = JS_NewArray(ctx);
- if (JS_IsException(obj))
- goto fail;
- if (len > 0) {
- p = JS_VALUE_GET_OBJ(obj);
- if (expand_fast_array(ctx, p, len) < 0) {
- JS_FreeValue(ctx, obj);
- fail:
- for(i = 0; i < len; i++)
- JS_FreeValue(ctx, tab[i]);
- return JS_EXCEPTION;
- }
- p->u.array.count = len;
- for(i = 0; i < len; i++)
- p->u.array.u.values[i] = tab[i];
- /* update the 'length' field */
- set_value(ctx, &p->prop[0].u.value, JS_NewInt32(ctx, len));
- }
- return obj;
- }
- static void js_free_desc(JSContext *ctx, JSPropertyDescriptor *desc)
- {
- JS_FreeValue(ctx, desc->getter);
- JS_FreeValue(ctx, desc->setter);
- JS_FreeValue(ctx, desc->value);
- }
- /* return -1 in case of exception or TRUE or FALSE. Warning: 'val' is
- freed by the function. 'flags' is a bitmask of JS_PROP_THROW and
- JS_PROP_THROW_STRICT. 'this_obj' is the receiver. If obj !=
- this_obj, then obj must be an object (Reflect.set case). */
- int JS_SetPropertyInternal(JSContext *ctx, JSValueConst obj,
- JSAtom prop, JSValue val, JSValueConst this_obj, int flags)
- {
- JSObject *p, *p1;
- JSShapeProperty *prs;
- JSProperty *pr;
- uint32_t tag;
- JSPropertyDescriptor desc;
- int ret;
- #if 0
- printf("JS_SetPropertyInternal: "); print_atom(ctx, prop); printf("\n");
- #endif
- tag = JS_VALUE_GET_TAG(this_obj);
- if (unlikely(tag != JS_TAG_OBJECT)) {
- if (JS_VALUE_GET_TAG(obj) == JS_TAG_OBJECT) {
- p = NULL;
- p1 = JS_VALUE_GET_OBJ(obj);
- goto prototype_lookup;
- } else {
- switch(tag) {
- case JS_TAG_NULL:
- JS_FreeValue(ctx, val);
- JS_ThrowTypeErrorAtom(ctx, "cannot set property '%s' of null", prop);
- return -1;
- case JS_TAG_UNDEFINED:
- JS_FreeValue(ctx, val);
- JS_ThrowTypeErrorAtom(ctx, "cannot set property '%s' of undefined", prop);
- return -1;
- default:
- /* even on a primitive type we can have setters on the prototype */
- p = NULL;
- p1 = JS_VALUE_GET_OBJ(JS_GetPrototypePrimitive(ctx, obj));
- goto prototype_lookup;
- }
- }
- } else {
- p = JS_VALUE_GET_OBJ(this_obj);
- p1 = JS_VALUE_GET_OBJ(obj);
- if (unlikely(p != p1))
- goto retry2;
- }
- /* fast path if obj == this_obj */
- retry:
- prs = find_own_property(&pr, p1, prop);
- if (prs) {
- if (likely((prs->flags & (JS_PROP_TMASK | JS_PROP_WRITABLE |
- JS_PROP_LENGTH)) == JS_PROP_WRITABLE)) {
- /* fast case */
- set_value(ctx, &pr->u.value, val);
- return TRUE;
- } else if (prs->flags & JS_PROP_LENGTH) {
- assert(p->class_id == JS_CLASS_ARRAY);
- assert(prop == JS_ATOM_length);
- return set_array_length(ctx, p, val, flags);
- } else if ((prs->flags & JS_PROP_TMASK) == JS_PROP_GETSET) {
- return call_setter(ctx, pr->u.getset.setter, this_obj, val, flags);
- } else if ((prs->flags & JS_PROP_TMASK) == JS_PROP_VARREF) {
- /* XXX: already use var_ref->is_const. Cannot simplify use the
- writable flag for JS_CLASS_MODULE_NS. */
- if (p->class_id == JS_CLASS_MODULE_NS || pr->u.var_ref->is_const)
- goto read_only_prop;
- set_value(ctx, pr->u.var_ref->pvalue, val);
- return TRUE;
- } else if ((prs->flags & JS_PROP_TMASK) == JS_PROP_AUTOINIT) {
- /* Instantiate property and retry (potentially useless) */
- if (JS_AutoInitProperty(ctx, p, prop, pr, prs)) {
- JS_FreeValue(ctx, val);
- return -1;
- }
- goto retry;
- } else {
- goto read_only_prop;
- }
- }
- for(;;) {
- if (p1->is_exotic) {
- if (p1->fast_array) {
- if (__JS_AtomIsTaggedInt(prop)) {
- uint32_t idx = __JS_AtomToUInt32(prop);
- if (idx < p1->u.array.count) {
- if (unlikely(p == p1))
- return JS_SetPropertyValue(ctx, this_obj, JS_NewInt32(ctx, idx), val, flags);
- else
- break;
- } else if (p1->class_id >= JS_CLASS_UINT8C_ARRAY &&
- p1->class_id <= JS_CLASS_FLOAT64_ARRAY) {
- goto typed_array_oob;
- }
- } else if (p1->class_id >= JS_CLASS_UINT8C_ARRAY &&
- p1->class_id <= JS_CLASS_FLOAT64_ARRAY) {
- ret = JS_AtomIsNumericIndex(ctx, prop);
- if (ret != 0) {
- if (ret < 0) {
- JS_FreeValue(ctx, val);
- return -1;
- }
- typed_array_oob:
- if (p == p1) {
- /* must convert the argument even if out of bound access */
- if (p1->class_id == JS_CLASS_BIG_INT64_ARRAY ||
- p1->class_id == JS_CLASS_BIG_UINT64_ARRAY) {
- int64_t v;
- if (JS_ToBigInt64Free(ctx, &v, val))
- return -1;
- } else {
- val = JS_ToNumberFree(ctx, val);
- JS_FreeValue(ctx, val);
- if (JS_IsException(val))
- return -1;
- }
- } else {
- JS_FreeValue(ctx, val);
- }
- return TRUE;
- }
- }
- } else {
- const JSClassExoticMethods *em = ctx->rt->class_array[p1->class_id].exotic;
- if (em) {
- JSValue obj1;
- if (em->set_property) {
- /* set_property can free the prototype */
- obj1 = JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, p1));
- ret = em->set_property(ctx, obj1, prop,
- val, this_obj, flags);
- JS_FreeValue(ctx, obj1);
- JS_FreeValue(ctx, val);
- return ret;
- }
- if (em->get_own_property) {
- /* get_own_property can free the prototype */
- obj1 = JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, p1));
- ret = em->get_own_property(ctx, &desc,
- obj1, prop);
- JS_FreeValue(ctx, obj1);
- if (ret < 0) {
- JS_FreeValue(ctx, val);
- return ret;
- }
- if (ret) {
- if (desc.flags & JS_PROP_GETSET) {
- JSObject *setter;
- if (JS_IsUndefined(desc.setter))
- setter = NULL;
- else
- setter = JS_VALUE_GET_OBJ(desc.setter);
- ret = call_setter(ctx, setter, this_obj, val, flags);
- JS_FreeValue(ctx, desc.getter);
- JS_FreeValue(ctx, desc.setter);
- return ret;
- } else {
- JS_FreeValue(ctx, desc.value);
- if (!(desc.flags & JS_PROP_WRITABLE))
- goto read_only_prop;
- if (likely(p == p1)) {
- ret = JS_DefineProperty(ctx, this_obj, prop, val,
- JS_UNDEFINED, JS_UNDEFINED,
- JS_PROP_HAS_VALUE);
- JS_FreeValue(ctx, val);
- return ret;
- } else {
- break;
- }
- }
- }
- }
- }
- }
- }
- p1 = p1->shape->proto;
- prototype_lookup:
- if (!p1)
- break;
- retry2:
- prs = find_own_property(&pr, p1, prop);
- if (prs) {
- if ((prs->flags & JS_PROP_TMASK) == JS_PROP_GETSET) {
- return call_setter(ctx, pr->u.getset.setter, this_obj, val, flags);
- } else if ((prs->flags & JS_PROP_TMASK) == JS_PROP_AUTOINIT) {
- /* Instantiate property and retry (potentially useless) */
- if (JS_AutoInitProperty(ctx, p1, prop, pr, prs))
- return -1;
- goto retry2;
- } else if (!(prs->flags & JS_PROP_WRITABLE)) {
- goto read_only_prop;
- } else {
- break;
- }
- }
- }
- if (unlikely(!p)) {
- JS_FreeValue(ctx, val);
- return JS_ThrowTypeErrorOrFalse(ctx, flags, "not an object");
- }
- if (unlikely(!p->extensible)) {
- JS_FreeValue(ctx, val);
- return JS_ThrowTypeErrorOrFalse(ctx, flags, "object is not extensible");
- }
- if (likely(p == JS_VALUE_GET_OBJ(obj))) {
- if (p->is_exotic) {
- if (p->class_id == JS_CLASS_ARRAY && p->fast_array &&
- __JS_AtomIsTaggedInt(prop)) {
- uint32_t idx = __JS_AtomToUInt32(prop);
- if (idx == p->u.array.count) {
- /* fast case */
- return add_fast_array_element(ctx, p, val, flags);
- } else {
- goto generic_create_prop;
- }
- } else {
- goto generic_create_prop;
- }
- } else {
- if (unlikely(p->class_id == JS_CLASS_GLOBAL_OBJECT))
- goto generic_create_prop;
- pr = add_property(ctx, p, prop, JS_PROP_C_W_E);
- if (unlikely(!pr)) {
- JS_FreeValue(ctx, val);
- return -1;
- }
- pr->u.value = val;
- return TRUE;
- }
- } else {
- /* generic case: modify the property in this_obj if it already exists */
- ret = JS_GetOwnPropertyInternal(ctx, &desc, p, prop);
- if (ret < 0) {
- JS_FreeValue(ctx, val);
- return ret;
- }
- if (ret) {
- if (desc.flags & JS_PROP_GETSET) {
- JS_FreeValue(ctx, desc.getter);
- JS_FreeValue(ctx, desc.setter);
- JS_FreeValue(ctx, val);
- return JS_ThrowTypeErrorOrFalse(ctx, flags, "setter is forbidden");
- } else {
- JS_FreeValue(ctx, desc.value);
- if (!(desc.flags & JS_PROP_WRITABLE) ||
- p->class_id == JS_CLASS_MODULE_NS) {
- read_only_prop:
- JS_FreeValue(ctx, val);
- return JS_ThrowTypeErrorReadOnly(ctx, flags, prop);
- }
- }
- ret = JS_DefineProperty(ctx, this_obj, prop, val,
- JS_UNDEFINED, JS_UNDEFINED,
- JS_PROP_HAS_VALUE);
- JS_FreeValue(ctx, val);
- return ret;
- } else {
- generic_create_prop:
- ret = JS_CreateProperty(ctx, p, prop, val, JS_UNDEFINED, JS_UNDEFINED,
- flags |
- JS_PROP_HAS_VALUE |
- JS_PROP_HAS_ENUMERABLE |
- JS_PROP_HAS_WRITABLE |
- JS_PROP_HAS_CONFIGURABLE |
- JS_PROP_C_W_E);
- JS_FreeValue(ctx, val);
- return ret;
- }
- }
- }
- /* return true if an element can be added to a fast array without further tests */
- static force_inline BOOL can_extend_fast_array(JSObject *p)
- {
- JSObject *proto;
- if (!p->extensible)
- return FALSE;
- proto = p->shape->proto;
- if (!proto)
- return TRUE;
- return proto->is_std_array_prototype;
- }
- /* flags can be JS_PROP_THROW or JS_PROP_THROW_STRICT */
- static int JS_SetPropertyValue(JSContext *ctx, JSValueConst this_obj,
- JSValue prop, JSValue val, int flags)
- {
- if (likely(JS_VALUE_GET_TAG(this_obj) == JS_TAG_OBJECT &&
- JS_VALUE_GET_TAG(prop) == JS_TAG_INT)) {
- JSObject *p;
- uint32_t idx;
- double d;
- int32_t v;
- /* fast path for array access */
- p = JS_VALUE_GET_OBJ(this_obj);
- idx = JS_VALUE_GET_INT(prop);
- switch(p->class_id) {
- case JS_CLASS_ARRAY:
- if (unlikely(idx >= (uint32_t)p->u.array.count)) {
- /* fast path to add an element to the array */
- if (unlikely(idx != (uint32_t)p->u.array.count ||
- !p->fast_array ||
- !can_extend_fast_array(p))) {
- goto slow_path;
- }
- /* add element */
- return add_fast_array_element(ctx, p, val, flags);
- }
- set_value(ctx, &p->u.array.u.values[idx], val);
- break;
- case JS_CLASS_ARGUMENTS:
- if (unlikely(idx >= (uint32_t)p->u.array.count))
- goto slow_path;
- set_value(ctx, &p->u.array.u.values[idx], val);
- break;
- case JS_CLASS_MAPPED_ARGUMENTS:
- if (unlikely(idx >= (uint32_t)p->u.array.count))
- goto slow_path;
- set_value(ctx, p->u.array.u.var_refs[idx]->pvalue, val);
- break;
- case JS_CLASS_UINT8C_ARRAY:
- if (JS_ToUint8ClampFree(ctx, &v, val))
- return -1;
- /* Note: the conversion can detach the typed array, so the
- array bound check must be done after */
- if (unlikely(idx >= (uint32_t)p->u.array.count))
- goto ta_out_of_bound;
- p->u.array.u.uint8_ptr[idx] = v;
- break;
- case JS_CLASS_INT8_ARRAY:
- case JS_CLASS_UINT8_ARRAY:
- if (JS_ToInt32Free(ctx, &v, val))
- return -1;
- if (unlikely(idx >= (uint32_t)p->u.array.count))
- goto ta_out_of_bound;
- p->u.array.u.uint8_ptr[idx] = v;
- break;
- case JS_CLASS_INT16_ARRAY:
- case JS_CLASS_UINT16_ARRAY:
- if (JS_ToInt32Free(ctx, &v, val))
- return -1;
- if (unlikely(idx >= (uint32_t)p->u.array.count))
- goto ta_out_of_bound;
- p->u.array.u.uint16_ptr[idx] = v;
- break;
- case JS_CLASS_INT32_ARRAY:
- case JS_CLASS_UINT32_ARRAY:
- if (JS_ToInt32Free(ctx, &v, val))
- return -1;
- if (unlikely(idx >= (uint32_t)p->u.array.count))
- goto ta_out_of_bound;
- p->u.array.u.uint32_ptr[idx] = v;
- break;
- case JS_CLASS_BIG_INT64_ARRAY:
- case JS_CLASS_BIG_UINT64_ARRAY:
- /* XXX: need specific conversion function */
- {
- int64_t v;
- if (JS_ToBigInt64Free(ctx, &v, val))
- return -1;
- if (unlikely(idx >= (uint32_t)p->u.array.count))
- goto ta_out_of_bound;
- p->u.array.u.uint64_ptr[idx] = v;
- }
- break;
- case JS_CLASS_FLOAT16_ARRAY:
- if (JS_ToFloat64Free(ctx, &d, val))
- return -1;
- if (unlikely(idx >= (uint32_t)p->u.array.count))
- goto ta_out_of_bound;
- p->u.array.u.fp16_ptr[idx] = tofp16(d);
- break;
- case JS_CLASS_FLOAT32_ARRAY:
- if (JS_ToFloat64Free(ctx, &d, val))
- return -1;
- if (unlikely(idx >= (uint32_t)p->u.array.count))
- goto ta_out_of_bound;
- p->u.array.u.float_ptr[idx] = d;
- break;
- case JS_CLASS_FLOAT64_ARRAY:
- if (JS_ToFloat64Free(ctx, &d, val))
- return -1;
- if (unlikely(idx >= (uint32_t)p->u.array.count)) {
- ta_out_of_bound:
- return TRUE;
- }
- p->u.array.u.double_ptr[idx] = d;
- break;
- default:
- goto slow_path;
- }
- return TRUE;
- } else {
- JSAtom atom;
- int ret;
- slow_path:
- atom = JS_ValueToAtom(ctx, prop);
- JS_FreeValue(ctx, prop);
- if (unlikely(atom == JS_ATOM_NULL)) {
- JS_FreeValue(ctx, val);
- return -1;
- }
- ret = JS_SetPropertyInternal(ctx, this_obj, atom, val, this_obj, flags);
- JS_FreeAtom(ctx, atom);
- return ret;
- }
- }
- int JS_SetPropertyUint32(JSContext *ctx, JSValueConst this_obj,
- uint32_t idx, JSValue val)
- {
- return JS_SetPropertyValue(ctx, this_obj, JS_NewUint32(ctx, idx), val,
- JS_PROP_THROW);
- }
- int JS_SetPropertyInt64(JSContext *ctx, JSValueConst this_obj,
- int64_t idx, JSValue val)
- {
- JSAtom prop;
- int res;
- if ((uint64_t)idx <= INT32_MAX) {
- /* fast path for fast arrays */
- return JS_SetPropertyValue(ctx, this_obj, JS_NewInt32(ctx, idx), val,
- JS_PROP_THROW);
- }
- prop = JS_NewAtomInt64(ctx, idx);
- if (prop == JS_ATOM_NULL) {
- JS_FreeValue(ctx, val);
- return -1;
- }
- res = JS_SetProperty(ctx, this_obj, prop, val);
- JS_FreeAtom(ctx, prop);
- return res;
- }
- int JS_SetPropertyStr(JSContext *ctx, JSValueConst this_obj,
- const char *prop, JSValue val)
- {
- JSAtom atom;
- int ret;
- atom = JS_NewAtom(ctx, prop);
- if (atom == JS_ATOM_NULL) {
- JS_FreeValue(ctx, val);
- return -1;
- }
- ret = JS_SetPropertyInternal(ctx, this_obj, atom, val, this_obj, JS_PROP_THROW);
- JS_FreeAtom(ctx, atom);
- return ret;
- }
- /* compute the property flags. For each flag: (JS_PROP_HAS_x forces
- it, otherwise def_flags is used)
- Note: makes assumption about the bit pattern of the flags
- */
- static int get_prop_flags(int flags, int def_flags)
- {
- int mask;
- mask = (flags >> JS_PROP_HAS_SHIFT) & JS_PROP_C_W_E;
- return (flags & mask) | (def_flags & ~mask);
- }
- static int JS_CreateProperty(JSContext *ctx, JSObject *p,
- JSAtom prop, JSValueConst val,
- JSValueConst getter, JSValueConst setter,
- int flags)
- {
- JSProperty *pr;
- int ret, prop_flags;
- JSVarRef *var_ref;
- JSObject *delete_obj;
-
- /* add a new property or modify an existing exotic one */
- if (p->is_exotic) {
- if (p->class_id == JS_CLASS_ARRAY) {
- uint32_t idx, len;
- if (p->fast_array) {
- if (__JS_AtomIsTaggedInt(prop)) {
- idx = __JS_AtomToUInt32(prop);
- if (idx == p->u.array.count) {
- if (!p->extensible)
- goto not_extensible;
- if (flags & (JS_PROP_HAS_GET | JS_PROP_HAS_SET))
- goto convert_to_array;
- prop_flags = get_prop_flags(flags, 0);
- if (prop_flags != JS_PROP_C_W_E)
- goto convert_to_array;
- return add_fast_array_element(ctx, p,
- JS_DupValue(ctx, val), flags);
- } else {
- goto convert_to_array;
- }
- } else if (JS_AtomIsArrayIndex(ctx, &idx, prop)) {
- /* convert the fast array to normal array */
- convert_to_array:
- if (convert_fast_array_to_array(ctx, p))
- return -1;
- goto generic_array;
- }
- } else if (JS_AtomIsArrayIndex(ctx, &idx, prop)) {
- JSProperty *plen;
- JSShapeProperty *pslen;
- generic_array:
- /* update the length field */
- plen = &p->prop[0];
- JS_ToUint32(ctx, &len, plen->u.value);
- if ((idx + 1) > len) {
- pslen = get_shape_prop(p->shape);
- if (unlikely(!(pslen->flags & JS_PROP_WRITABLE)))
- return JS_ThrowTypeErrorReadOnly(ctx, flags, JS_ATOM_length);
- /* XXX: should update the length after defining
- the property */
- len = idx + 1;
- set_value(ctx, &plen->u.value, JS_NewUint32(ctx, len));
- }
- }
- } else if (p->class_id >= JS_CLASS_UINT8C_ARRAY &&
- p->class_id <= JS_CLASS_FLOAT64_ARRAY) {
- ret = JS_AtomIsNumericIndex(ctx, prop);
- if (ret != 0) {
- if (ret < 0)
- return -1;
- return JS_ThrowTypeErrorOrFalse(ctx, flags, "cannot create numeric index in typed array");
- }
- } else if (!(flags & JS_PROP_NO_EXOTIC)) {
- const JSClassExoticMethods *em = ctx->rt->class_array[p->class_id].exotic;
- if (em) {
- if (em->define_own_property) {
- return em->define_own_property(ctx, JS_MKPTR(JS_TAG_OBJECT, p),
- prop, val, getter, setter, flags);
- }
- ret = JS_IsExtensible(ctx, JS_MKPTR(JS_TAG_OBJECT, p));
- if (ret < 0)
- return -1;
- if (!ret)
- goto not_extensible;
- }
- }
- }
- if (!p->extensible) {
- not_extensible:
- return JS_ThrowTypeErrorOrFalse(ctx, flags, "object is not extensible");
- }
- var_ref = NULL;
- delete_obj = NULL;
- if (flags & (JS_PROP_HAS_GET | JS_PROP_HAS_SET)) {
- prop_flags = (flags & (JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE)) |
- JS_PROP_GETSET;
- } else {
- prop_flags = flags & JS_PROP_C_W_E;
- if (p->class_id == JS_CLASS_GLOBAL_OBJECT) {
- JSObject *p1 = JS_VALUE_GET_OBJ(p->u.global_object.uninitialized_vars);
- JSShapeProperty *prs1;
- JSProperty *pr1;
- prs1 = find_own_property(&pr1, p1, prop);
- if (prs1) {
- delete_obj = p1;
- var_ref = pr1->u.var_ref;
- js_rc(var_ref)->ref_count++;
- } else {
- var_ref = js_create_var_ref(ctx, FALSE);
- if (!var_ref)
- return -1;
- }
- var_ref->is_const = !(prop_flags & JS_PROP_WRITABLE);
- prop_flags |= JS_PROP_VARREF;
- }
- }
- pr = add_property(ctx, p, prop, prop_flags);
- if (unlikely(!pr)) {
- if (var_ref)
- free_var_ref(ctx->rt, var_ref);
- return -1;
- }
- if (flags & (JS_PROP_HAS_GET | JS_PROP_HAS_SET)) {
- pr->u.getset.getter = NULL;
- if ((flags & JS_PROP_HAS_GET) && JS_IsFunction(ctx, getter)) {
- pr->u.getset.getter =
- JS_VALUE_GET_OBJ(JS_DupValue(ctx, getter));
- }
- pr->u.getset.setter = NULL;
- if ((flags & JS_PROP_HAS_SET) && JS_IsFunction(ctx, setter)) {
- pr->u.getset.setter =
- JS_VALUE_GET_OBJ(JS_DupValue(ctx, setter));
- }
- } else if (p->class_id == JS_CLASS_GLOBAL_OBJECT) {
- if (delete_obj)
- delete_property(ctx, delete_obj, prop);
- pr->u.var_ref = var_ref;
- if (flags & JS_PROP_HAS_VALUE) {
- *var_ref->pvalue = JS_DupValue(ctx, val);
- } else {
- *var_ref->pvalue = JS_UNDEFINED;
- }
- } else {
- if (flags & JS_PROP_HAS_VALUE) {
- pr->u.value = JS_DupValue(ctx, val);
- } else {
- pr->u.value = JS_UNDEFINED;
- }
- }
- return TRUE;
- }
- /* return FALSE if not OK */
- static BOOL check_define_prop_flags(int prop_flags, int flags)
- {
- BOOL has_accessor, is_getset;
- if (!(prop_flags & JS_PROP_CONFIGURABLE)) {
- if ((flags & (JS_PROP_HAS_CONFIGURABLE | JS_PROP_CONFIGURABLE)) ==
- (JS_PROP_HAS_CONFIGURABLE | JS_PROP_CONFIGURABLE)) {
- return FALSE;
- }
- if ((flags & JS_PROP_HAS_ENUMERABLE) &&
- (flags & JS_PROP_ENUMERABLE) != (prop_flags & JS_PROP_ENUMERABLE))
- return FALSE;
- if (flags & (JS_PROP_HAS_VALUE | JS_PROP_HAS_WRITABLE |
- JS_PROP_HAS_GET | JS_PROP_HAS_SET)) {
- has_accessor = ((flags & (JS_PROP_HAS_GET | JS_PROP_HAS_SET)) != 0);
- is_getset = ((prop_flags & JS_PROP_TMASK) == JS_PROP_GETSET);
- if (has_accessor != is_getset)
- return FALSE;
- if (!is_getset && !(prop_flags & JS_PROP_WRITABLE)) {
- /* not writable: cannot set the writable bit */
- if ((flags & (JS_PROP_HAS_WRITABLE | JS_PROP_WRITABLE)) ==
- (JS_PROP_HAS_WRITABLE | JS_PROP_WRITABLE))
- return FALSE;
- }
- }
- }
- return TRUE;
- }
- /* ensure that the shape can be safely modified */
- static int js_shape_prepare_update(JSContext *ctx, JSObject *p,
- JSShapeProperty **pprs)
- {
- JSShape *sh;
- uint32_t idx = 0; /* prevent warning */
- sh = p->shape;
- if (sh->is_hashed) {
- if (js_rc(sh)->ref_count != 1) {
- if (pprs)
- idx = *pprs - get_shape_prop(sh);
- /* clone the shape (the resulting one is no longer hashed) */
- sh = js_clone_shape(ctx, sh);
- if (!sh)
- return -1;
- js_free_shape(ctx->rt, p->shape);
- p->shape = sh;
- if (pprs)
- *pprs = get_shape_prop(sh) + idx;
- } else {
- js_shape_hash_unlink(ctx->rt, sh);
- sh->is_hashed = FALSE;
- }
- }
- return 0;
- }
- static int js_update_property_flags(JSContext *ctx, JSObject *p,
- JSShapeProperty **pprs, int flags)
- {
- if (flags != (*pprs)->flags) {
- if (js_shape_prepare_update(ctx, p, pprs))
- return -1;
- (*pprs)->flags = flags;
- }
- return 0;
- }
- /* allowed flags:
- JS_PROP_CONFIGURABLE, JS_PROP_WRITABLE, JS_PROP_ENUMERABLE
- JS_PROP_HAS_GET, JS_PROP_HAS_SET, JS_PROP_HAS_VALUE,
- JS_PROP_HAS_CONFIGURABLE, JS_PROP_HAS_WRITABLE, JS_PROP_HAS_ENUMERABLE,
- JS_PROP_THROW, JS_PROP_NO_EXOTIC.
- If JS_PROP_THROW is set, return an exception instead of FALSE.
- if JS_PROP_NO_EXOTIC is set, do not call the exotic
- define_own_property callback.
- return -1 (exception), FALSE or TRUE.
- */
- int JS_DefineProperty(JSContext *ctx, JSValueConst this_obj,
- JSAtom prop, JSValueConst val,
- JSValueConst getter, JSValueConst setter, int flags)
- {
- JSObject *p;
- JSShapeProperty *prs;
- JSProperty *pr;
- int mask, res;
- if (JS_VALUE_GET_TAG(this_obj) != JS_TAG_OBJECT) {
- JS_ThrowTypeErrorNotAnObject(ctx);
- return -1;
- }
- p = JS_VALUE_GET_OBJ(this_obj);
- redo_prop_update:
- prs = find_own_property(&pr, p, prop);
- if (prs) {
- /* the range of the Array length property is always tested before */
- if ((prs->flags & JS_PROP_LENGTH) && (flags & JS_PROP_HAS_VALUE)) {
- uint32_t array_length;
- if (JS_ToArrayLengthFree(ctx, &array_length,
- JS_DupValue(ctx, val), FALSE)) {
- return -1;
- }
- /* this code relies on the fact that Uint32 are never allocated */
- val = (JSValueConst)JS_NewUint32(ctx, array_length);
- /* prs may have been modified */
- prs = find_own_property(&pr, p, prop);
- assert(prs != NULL);
- }
- /* property already exists */
- if (!check_define_prop_flags(prs->flags, flags)) {
- not_configurable:
- return JS_ThrowTypeErrorOrFalse(ctx, flags, "property is not configurable");
- }
- if ((prs->flags & JS_PROP_TMASK) == JS_PROP_AUTOINIT) {
- /* Instantiate property and retry */
- if (JS_AutoInitProperty(ctx, p, prop, pr, prs))
- return -1;
- goto redo_prop_update;
- }
- if (flags & (JS_PROP_HAS_VALUE | JS_PROP_HAS_WRITABLE |
- JS_PROP_HAS_GET | JS_PROP_HAS_SET)) {
- if (flags & (JS_PROP_HAS_GET | JS_PROP_HAS_SET)) {
- JSObject *new_getter, *new_setter;
- if (JS_IsFunction(ctx, getter)) {
- new_getter = JS_VALUE_GET_OBJ(getter);
- } else {
- new_getter = NULL;
- }
- if (JS_IsFunction(ctx, setter)) {
- new_setter = JS_VALUE_GET_OBJ(setter);
- } else {
- new_setter = NULL;
- }
- if ((prs->flags & JS_PROP_TMASK) != JS_PROP_GETSET) {
- if (js_shape_prepare_update(ctx, p, &prs))
- return -1;
- /* convert to getset */
- if ((prs->flags & JS_PROP_TMASK) == JS_PROP_VARREF) {
- if (unlikely(p->class_id == JS_CLASS_GLOBAL_OBJECT)) {
- if (remove_global_object_property(ctx, p, prs, pr))
- return -1;
- }
- free_var_ref(ctx->rt, pr->u.var_ref);
- } else {
- JS_FreeValue(ctx, pr->u.value);
- }
- prs->flags = (prs->flags &
- (JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE)) |
- JS_PROP_GETSET;
- pr->u.getset.getter = NULL;
- pr->u.getset.setter = NULL;
- } else {
- if (!(prs->flags & JS_PROP_CONFIGURABLE)) {
- if ((flags & JS_PROP_HAS_GET) &&
- new_getter != pr->u.getset.getter) {
- goto not_configurable;
- }
- if ((flags & JS_PROP_HAS_SET) &&
- new_setter != pr->u.getset.setter) {
- goto not_configurable;
- }
- }
- }
- if (flags & JS_PROP_HAS_GET) {
- if (pr->u.getset.getter)
- JS_FreeValue(ctx, JS_MKPTR(JS_TAG_OBJECT, pr->u.getset.getter));
- if (new_getter)
- JS_DupValue(ctx, getter);
- pr->u.getset.getter = new_getter;
- }
- if (flags & JS_PROP_HAS_SET) {
- if (pr->u.getset.setter)
- JS_FreeValue(ctx, JS_MKPTR(JS_TAG_OBJECT, pr->u.getset.setter));
- if (new_setter)
- JS_DupValue(ctx, setter);
- pr->u.getset.setter = new_setter;
- }
- } else {
- if ((prs->flags & JS_PROP_TMASK) == JS_PROP_GETSET) {
- /* convert to data descriptor */
- JSVarRef *var_ref;
- if (unlikely(p->class_id == JS_CLASS_GLOBAL_OBJECT)) {
- var_ref = js_global_object_find_uninitialized_var(ctx, p, prop, FALSE);
- if (!var_ref)
- return -1;
- } else {
- var_ref = NULL;
- }
- if (js_shape_prepare_update(ctx, p, &prs)) {
- if (var_ref)
- free_var_ref(ctx->rt, var_ref);
- return -1;
- }
- if (pr->u.getset.getter)
- JS_FreeValue(ctx, JS_MKPTR(JS_TAG_OBJECT, pr->u.getset.getter));
- if (pr->u.getset.setter)
- JS_FreeValue(ctx, JS_MKPTR(JS_TAG_OBJECT, pr->u.getset.setter));
- if (var_ref) {
- prs->flags = (prs->flags & ~JS_PROP_TMASK) |
- JS_PROP_VARREF | JS_PROP_WRITABLE;
- pr->u.var_ref = var_ref;
- } else {
- prs->flags &= ~(JS_PROP_TMASK | JS_PROP_WRITABLE);
- pr->u.value = JS_UNDEFINED;
- }
- } else if ((prs->flags & JS_PROP_TMASK) == JS_PROP_VARREF) {
- /* Note: JS_PROP_VARREF is always writable */
- } else {
- if ((prs->flags & (JS_PROP_CONFIGURABLE | JS_PROP_WRITABLE)) == 0 &&
- (flags & JS_PROP_HAS_VALUE)) {
- if (!js_same_value(ctx, val, pr->u.value)) {
- goto not_configurable;
- } else {
- return TRUE;
- }
- }
- }
- if ((prs->flags & JS_PROP_TMASK) == JS_PROP_VARREF) {
- if (flags & JS_PROP_HAS_VALUE) {
- if (p->class_id == JS_CLASS_MODULE_NS) {
- /* JS_PROP_WRITABLE is always true for variable
- references, but they are write protected in module name
- spaces. */
- if (!js_same_value(ctx, val, *pr->u.var_ref->pvalue))
- goto not_configurable;
- } else {
- /* update the reference */
- set_value(ctx, pr->u.var_ref->pvalue,
- JS_DupValue(ctx, val));
- }
- }
- if ((flags & (JS_PROP_HAS_WRITABLE | JS_PROP_WRITABLE)) == JS_PROP_HAS_WRITABLE) {
- JSValue val1;
- if (p->class_id == JS_CLASS_MODULE_NS) {
- return JS_ThrowTypeErrorOrFalse(ctx, flags, "module namespace properties have writable = false");
- }
- if (js_shape_prepare_update(ctx, p, &prs))
- return -1;
- if (p->class_id == JS_CLASS_GLOBAL_OBJECT) {
- pr->u.var_ref->is_const = TRUE; /* mark as read-only */
- prs->flags &= ~JS_PROP_WRITABLE;
- } else {
- /* if writable is set to false, no longer a
- reference (for mapped arguments) */
- val1 = JS_DupValue(ctx, *pr->u.var_ref->pvalue);
- free_var_ref(ctx->rt, pr->u.var_ref);
- pr->u.value = val1;
- prs->flags &= ~(JS_PROP_TMASK | JS_PROP_WRITABLE);
- }
- }
- } else if (prs->flags & JS_PROP_LENGTH) {
- if (flags & JS_PROP_HAS_VALUE) {
- /* Note: no JS code is executable because
- 'val' is guaranted to be a Uint32 */
- res = set_array_length(ctx, p, JS_DupValue(ctx, val),
- flags);
- } else {
- res = TRUE;
- }
- /* still need to reset the writable flag if
- needed. The JS_PROP_LENGTH is kept because the
- Uint32 test is still done if the length
- property is read-only. */
- if ((flags & (JS_PROP_HAS_WRITABLE | JS_PROP_WRITABLE)) ==
- JS_PROP_HAS_WRITABLE) {
- prs = get_shape_prop(p->shape);
- if (js_update_property_flags(ctx, p, &prs,
- prs->flags & ~JS_PROP_WRITABLE))
- return -1;
- }
- return res;
- } else {
- if (flags & JS_PROP_HAS_VALUE) {
- JS_FreeValue(ctx, pr->u.value);
- pr->u.value = JS_DupValue(ctx, val);
- }
- if (flags & JS_PROP_HAS_WRITABLE) {
- if (js_update_property_flags(ctx, p, &prs,
- (prs->flags & ~JS_PROP_WRITABLE) |
- (flags & JS_PROP_WRITABLE)))
- return -1;
- }
- }
- }
- }
- mask = 0;
- if (flags & JS_PROP_HAS_CONFIGURABLE)
- mask |= JS_PROP_CONFIGURABLE;
- if (flags & JS_PROP_HAS_ENUMERABLE)
- mask |= JS_PROP_ENUMERABLE;
- if (js_update_property_flags(ctx, p, &prs,
- (prs->flags & ~mask) | (flags & mask)))
- return -1;
- return TRUE;
- }
- /* handle modification of fast array elements */
- if (p->fast_array) {
- uint32_t idx;
- uint32_t prop_flags;
- if (p->class_id == JS_CLASS_ARRAY) {
- if (__JS_AtomIsTaggedInt(prop)) {
- idx = __JS_AtomToUInt32(prop);
- if (idx < p->u.array.count) {
- prop_flags = get_prop_flags(flags, JS_PROP_C_W_E);
- if (prop_flags != JS_PROP_C_W_E)
- goto convert_to_slow_array;
- if (flags & (JS_PROP_HAS_GET | JS_PROP_HAS_SET)) {
- convert_to_slow_array:
- if (convert_fast_array_to_array(ctx, p))
- return -1;
- else
- goto redo_prop_update;
- }
- if (flags & JS_PROP_HAS_VALUE) {
- set_value(ctx, &p->u.array.u.values[idx], JS_DupValue(ctx, val));
- }
- return TRUE;
- }
- }
- } else if (p->class_id >= JS_CLASS_UINT8C_ARRAY &&
- p->class_id <= JS_CLASS_FLOAT64_ARRAY) {
- JSValue num;
- int ret;
- if (!__JS_AtomIsTaggedInt(prop)) {
- /* slow path with to handle all numeric indexes */
- num = JS_AtomIsNumericIndex1(ctx, prop);
- if (JS_IsUndefined(num))
- goto typed_array_done;
- if (JS_IsException(num))
- return -1;
- ret = JS_NumberIsInteger(ctx, num);
- if (ret < 0) {
- JS_FreeValue(ctx, num);
- return -1;
- }
- if (!ret) {
- JS_FreeValue(ctx, num);
- return JS_ThrowTypeErrorOrFalse(ctx, flags, "non integer index in typed array");
- }
- ret = JS_NumberIsNegativeOrMinusZero(ctx, num);
- JS_FreeValue(ctx, num);
- if (ret) {
- return JS_ThrowTypeErrorOrFalse(ctx, flags, "negative index in typed array");
- }
- if (!__JS_AtomIsTaggedInt(prop))
- goto typed_array_oob;
- }
- idx = __JS_AtomToUInt32(prop);
- /* if the typed array is detached, p->u.array.count = 0 */
- if (idx >= p->u.array.count) {
- typed_array_oob:
- return JS_ThrowTypeErrorOrFalse(ctx, flags, "out-of-bound index in typed array");
- }
- prop_flags = get_prop_flags(flags, JS_PROP_ENUMERABLE | JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
- if (flags & (JS_PROP_HAS_GET | JS_PROP_HAS_SET) ||
- prop_flags != (JS_PROP_ENUMERABLE | JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE)) {
- return JS_ThrowTypeErrorOrFalse(ctx, flags, "invalid descriptor flags");
- }
- if (flags & JS_PROP_HAS_VALUE) {
- return JS_SetPropertyValue(ctx, this_obj, JS_NewInt32(ctx, idx), JS_DupValue(ctx, val), flags);
- }
- return TRUE;
- typed_array_done: ;
- }
- }
- return JS_CreateProperty(ctx, p, prop, val, getter, setter, flags);
- }
- static int JS_DefineAutoInitProperty(JSContext *ctx, JSValueConst this_obj,
- JSAtom prop, JSAutoInitIDEnum id,
- void *opaque, int flags)
- {
- JSObject *p;
- JSProperty *pr;
- if (JS_VALUE_GET_TAG(this_obj) != JS_TAG_OBJECT)
- return FALSE;
- p = JS_VALUE_GET_OBJ(this_obj);
- if (find_own_property(&pr, p, prop)) {
- /* property already exists */
- abort();
- return FALSE;
- }
- /* Specialized CreateProperty */
- pr = add_property(ctx, p, prop, (flags & JS_PROP_C_W_E) | JS_PROP_AUTOINIT);
- if (unlikely(!pr))
- return -1;
- pr->u.init.realm_and_id = (uintptr_t)JS_DupContext(ctx);
- assert((pr->u.init.realm_and_id & 3) == 0);
- assert(id <= 3);
- pr->u.init.realm_and_id |= id;
- pr->u.init.opaque = opaque;
- return TRUE;
- }
- /* shortcut to add or redefine a new property value */
- int JS_DefinePropertyValue(JSContext *ctx, JSValueConst this_obj,
- JSAtom prop, JSValue val, int flags)
- {
- int ret;
- ret = JS_DefineProperty(ctx, this_obj, prop, val, JS_UNDEFINED, JS_UNDEFINED,
- flags | JS_PROP_HAS_VALUE | JS_PROP_HAS_CONFIGURABLE | JS_PROP_HAS_WRITABLE | JS_PROP_HAS_ENUMERABLE);
- JS_FreeValue(ctx, val);
- return ret;
- }
- int JS_DefinePropertyValueValue(JSContext *ctx, JSValueConst this_obj,
- JSValue prop, JSValue val, int flags)
- {
- JSAtom atom;
- int ret;
- atom = JS_ValueToAtom(ctx, prop);
- JS_FreeValue(ctx, prop);
- if (unlikely(atom == JS_ATOM_NULL)) {
- JS_FreeValue(ctx, val);
- return -1;
- }
- ret = JS_DefinePropertyValue(ctx, this_obj, atom, val, flags);
- JS_FreeAtom(ctx, atom);
- return ret;
- }
- int JS_DefinePropertyValueUint32(JSContext *ctx, JSValueConst this_obj,
- uint32_t idx, JSValue val, int flags)
- {
- return JS_DefinePropertyValueValue(ctx, this_obj, JS_NewUint32(ctx, idx),
- val, flags);
- }
- int JS_DefinePropertyValueInt64(JSContext *ctx, JSValueConst this_obj,
- int64_t idx, JSValue val, int flags)
- {
- return JS_DefinePropertyValueValue(ctx, this_obj, JS_NewInt64(ctx, idx),
- val, flags);
- }
- int JS_DefinePropertyValueStr(JSContext *ctx, JSValueConst this_obj,
- const char *prop, JSValue val, int flags)
- {
- JSAtom atom;
- int ret;
- atom = JS_NewAtom(ctx, prop);
- if (atom == JS_ATOM_NULL) {
- JS_FreeValue(ctx, val);
- return -1;
- }
- ret = JS_DefinePropertyValue(ctx, this_obj, atom, val, flags);
- JS_FreeAtom(ctx, atom);
- return ret;
- }
- /* shortcut to add getter & setter */
- int JS_DefinePropertyGetSet(JSContext *ctx, JSValueConst this_obj,
- JSAtom prop, JSValue getter, JSValue setter,
- int flags)
- {
- int ret;
- ret = JS_DefineProperty(ctx, this_obj, prop, JS_UNDEFINED, getter, setter,
- flags | JS_PROP_HAS_GET | JS_PROP_HAS_SET |
- JS_PROP_HAS_CONFIGURABLE | JS_PROP_HAS_ENUMERABLE);
- JS_FreeValue(ctx, getter);
- JS_FreeValue(ctx, setter);
- return ret;
- }
- static int JS_CreateDataPropertyUint32(JSContext *ctx, JSValueConst this_obj,
- int64_t idx, JSValue val, int flags)
- {
- return JS_DefinePropertyValueValue(ctx, this_obj, JS_NewInt64(ctx, idx),
- val, flags | JS_PROP_CONFIGURABLE |
- JS_PROP_ENUMERABLE | JS_PROP_WRITABLE);
- }
- /* return TRUE if 'obj' has a non empty 'name' string */
- static BOOL js_object_has_name(JSContext *ctx, JSValueConst obj)
- {
- JSProperty *pr;
- JSShapeProperty *prs;
- JSValueConst val;
- JSString *p;
- prs = find_own_property(&pr, JS_VALUE_GET_OBJ(obj), JS_ATOM_name);
- if (!prs)
- return FALSE;
- if ((prs->flags & JS_PROP_TMASK) != JS_PROP_NORMAL)
- return TRUE;
- val = pr->u.value;
- if (JS_VALUE_GET_TAG(val) != JS_TAG_STRING)
- return TRUE;
- p = JS_VALUE_GET_STRING(val);
- return (p->len != 0);
- }
- static int JS_DefineObjectName(JSContext *ctx, JSValueConst obj,
- JSAtom name, int flags)
- {
- if (name != JS_ATOM_NULL
- && JS_IsObject(obj)
- && !js_object_has_name(ctx, obj)
- && JS_DefinePropertyValue(ctx, obj, JS_ATOM_name, JS_AtomToString(ctx, name), flags) < 0) {
- return -1;
- }
- return 0;
- }
- static int JS_DefineObjectNameComputed(JSContext *ctx, JSValueConst obj,
- JSValueConst str, int flags)
- {
- if (JS_IsObject(obj) &&
- !js_object_has_name(ctx, obj)) {
- JSAtom prop;
- JSValue name_str;
- prop = JS_ValueToAtom(ctx, str);
- if (prop == JS_ATOM_NULL)
- return -1;
- name_str = js_get_function_name(ctx, prop);
- JS_FreeAtom(ctx, prop);
- if (JS_IsException(name_str))
- return -1;
- if (JS_DefinePropertyValue(ctx, obj, JS_ATOM_name, name_str, flags) < 0)
- return -1;
- }
- return 0;
- }
- #define DEFINE_GLOBAL_LEX_VAR (1 << 7)
- #define DEFINE_GLOBAL_FUNC_VAR (1 << 6)
- static JSValue JS_ThrowSyntaxErrorVarRedeclaration(JSContext *ctx, JSAtom prop)
- {
- return JS_ThrowSyntaxErrorAtom(ctx, "redeclaration of '%s'", prop);
- }
- /* flags is 0, DEFINE_GLOBAL_LEX_VAR or DEFINE_GLOBAL_FUNC_VAR */
- /* XXX: could support exotic global object. */
- static int JS_CheckDefineGlobalVar(JSContext *ctx, JSAtom prop, int flags)
- {
- JSObject *p;
- JSShapeProperty *prs;
- p = JS_VALUE_GET_OBJ(ctx->global_obj);
- prs = find_own_property1(p, prop);
- /* XXX: should handle JS_PROP_AUTOINIT */
- if (flags & DEFINE_GLOBAL_LEX_VAR) {
- if (prs && !(prs->flags & JS_PROP_CONFIGURABLE))
- goto fail_redeclaration;
- } else {
- if (!prs && !p->extensible)
- goto define_error;
- if (flags & DEFINE_GLOBAL_FUNC_VAR) {
- if (prs) {
- if (!(prs->flags & JS_PROP_CONFIGURABLE) &&
- ((prs->flags & JS_PROP_TMASK) == JS_PROP_GETSET ||
- ((prs->flags & (JS_PROP_WRITABLE | JS_PROP_ENUMERABLE)) !=
- (JS_PROP_WRITABLE | JS_PROP_ENUMERABLE)))) {
- define_error:
- JS_ThrowTypeErrorAtom(ctx, "cannot define variable '%s'",
- prop);
- return -1;
- }
- }
- }
- }
- /* check if there already is a lexical declaration */
- p = JS_VALUE_GET_OBJ(ctx->global_var_obj);
- prs = find_own_property1(p, prop);
- if (prs) {
- fail_redeclaration:
- JS_ThrowSyntaxErrorVarRedeclaration(ctx, prop);
- return -1;
- }
- return 0;
- }
- /* construct a reference to a global variable */
- static int JS_GetGlobalVarRef(JSContext *ctx, JSAtom prop, JSValue *sp)
- {
- JSObject *p;
- JSShapeProperty *prs;
- JSProperty *pr;
- /* no exotic behavior is possible in global_var_obj */
- p = JS_VALUE_GET_OBJ(ctx->global_var_obj);
- prs = find_own_property(&pr, p, prop);
- if (prs) {
- /* XXX: conformance: do these tests in
- OP_put_var_ref/OP_get_var_ref ? */
- if (unlikely(JS_IsUninitialized(*pr->u.var_ref->pvalue))) {
- JS_ThrowReferenceErrorUninitialized(ctx, prs->atom);
- return -1;
- }
- if (unlikely(!(prs->flags & JS_PROP_WRITABLE))) {
- return JS_ThrowTypeErrorReadOnly(ctx, JS_PROP_THROW, prop);
- }
- sp[0] = JS_DupValue(ctx, ctx->global_var_obj);
- } else {
- int ret;
- ret = JS_HasProperty(ctx, ctx->global_obj, prop);
- if (ret < 0)
- return -1;
- if (ret) {
- sp[0] = JS_DupValue(ctx, ctx->global_obj);
- } else {
- sp[0] = JS_UNDEFINED;
- }
- }
- sp[1] = JS_AtomToValue(ctx, prop);
- return 0;
- }
- /* return -1, FALSE or TRUE */
- static int JS_DeleteGlobalVar(JSContext *ctx, JSAtom prop)
- {
- JSObject *p;
- JSShapeProperty *prs;
- JSProperty *pr;
- int ret;
- /* 9.1.1.4.7 DeleteBinding ( N ) */
- p = JS_VALUE_GET_OBJ(ctx->global_var_obj);
- prs = find_own_property(&pr, p, prop);
- if (prs)
- return FALSE; /* lexical variables cannot be deleted */
- ret = JS_HasProperty(ctx, ctx->global_obj, prop);
- if (ret < 0)
- return -1;
- if (ret) {
- return JS_DeleteProperty(ctx, ctx->global_obj, prop, 0);
- } else {
- return TRUE;
- }
- }
- /* return -1, FALSE or TRUE. return FALSE if not configurable or
- invalid object. return -1 in case of exception.
- flags can be 0, JS_PROP_THROW or JS_PROP_THROW_STRICT */
- int JS_DeleteProperty(JSContext *ctx, JSValueConst obj, JSAtom prop, int flags)
- {
- JSValue obj1;
- JSObject *p;
- int res;
- obj1 = JS_ToObject(ctx, obj);
- if (JS_IsException(obj1))
- return -1;
- p = JS_VALUE_GET_OBJ(obj1);
- res = delete_property(ctx, p, prop);
- JS_FreeValue(ctx, obj1);
- if (res != FALSE)
- return res;
- if ((flags & JS_PROP_THROW) ||
- ((flags & JS_PROP_THROW_STRICT) && is_strict_mode(ctx))) {
- JS_ThrowTypeError(ctx, "could not delete property");
- return -1;
- }
- return FALSE;
- }
- int JS_DeletePropertyInt64(JSContext *ctx, JSValueConst obj, int64_t idx, int flags)
- {
- JSAtom prop;
- int res;
- if ((uint64_t)idx <= JS_ATOM_MAX_INT) {
- /* fast path for fast arrays */
- return JS_DeleteProperty(ctx, obj, __JS_AtomFromUInt32(idx), flags);
- }
- prop = JS_NewAtomInt64(ctx, idx);
- if (prop == JS_ATOM_NULL)
- return -1;
- res = JS_DeleteProperty(ctx, obj, prop, flags);
- JS_FreeAtom(ctx, prop);
- return res;
- }
- BOOL JS_IsFunction(JSContext *ctx, JSValueConst val)
- {
- JSObject *p;
- if (JS_VALUE_GET_TAG(val) != JS_TAG_OBJECT)
- return FALSE;
- p = JS_VALUE_GET_OBJ(val);
- switch(p->class_id) {
- case JS_CLASS_BYTECODE_FUNCTION:
- return TRUE;
- case JS_CLASS_PROXY:
- return p->u.proxy_data->is_func;
- default:
- return (ctx->rt->class_array[p->class_id].call != NULL);
- }
- }
- BOOL JS_IsCFunction(JSContext *ctx, JSValueConst val, JSCFunction *func, int magic)
- {
- JSObject *p;
- if (JS_VALUE_GET_TAG(val) != JS_TAG_OBJECT)
- return FALSE;
- p = JS_VALUE_GET_OBJ(val);
- if (p->class_id == JS_CLASS_C_FUNCTION)
- return (p->u.cfunc.c_function.generic == func && p->u.cfunc.magic == magic);
- else
- return FALSE;
- }
- BOOL JS_IsConstructor(JSContext *ctx, JSValueConst val)
- {
- JSObject *p;
- if (JS_VALUE_GET_TAG(val) != JS_TAG_OBJECT)
- return FALSE;
- p = JS_VALUE_GET_OBJ(val);
- return p->is_constructor;
- }
- BOOL JS_SetConstructorBit(JSContext *ctx, JSValueConst func_obj, BOOL val)
- {
- JSObject *p;
- if (JS_VALUE_GET_TAG(func_obj) != JS_TAG_OBJECT)
- return FALSE;
- p = JS_VALUE_GET_OBJ(func_obj);
- p->is_constructor = val;
- return TRUE;
- }
- BOOL JS_IsError(JSContext *ctx, JSValueConst val)
- {
- JSObject *p;
- if (JS_VALUE_GET_TAG(val) != JS_TAG_OBJECT)
- return FALSE;
- p = JS_VALUE_GET_OBJ(val);
- return (p->class_id == JS_CLASS_ERROR);
- }
- /* must be called after JS_Throw() */
- void JS_SetUncatchableException(JSContext *ctx, BOOL flag)
- {
- ctx->rt->current_exception_is_uncatchable = flag;
- }
- void JS_SetOpaque(JSValue obj, void *opaque)
- {
- JSObject *p;
- if (JS_VALUE_GET_TAG(obj) == JS_TAG_OBJECT) {
- p = JS_VALUE_GET_OBJ(obj);
- p->u.opaque = opaque;
- }
- }
- /* return NULL if not an object of class class_id */
- void *JS_GetOpaque(JSValueConst obj, JSClassID class_id)
- {
- JSObject *p;
- if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)
- return NULL;
- p = JS_VALUE_GET_OBJ(obj);
- if (p->class_id != class_id)
- return NULL;
- return p->u.opaque;
- }
- void *JS_GetOpaque2(JSContext *ctx, JSValueConst obj, JSClassID class_id)
- {
- void *p = JS_GetOpaque(obj, class_id);
- if (unlikely(!p)) {
- JS_ThrowTypeErrorInvalidClass(ctx, class_id);
- }
- return p;
- }
- void *JS_GetAnyOpaque(JSValueConst obj, JSClassID *class_id)
- {
- JSObject *p;
- if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT) {
- *class_id = 0;
- return NULL;
- }
- p = JS_VALUE_GET_OBJ(obj);
- *class_id = p->class_id;
- return p->u.opaque;
- }
- static JSValue JS_ToPrimitiveFree(JSContext *ctx, JSValue val, int hint)
- {
- int i;
- BOOL force_ordinary;
- JSAtom method_name;
- JSValue method, ret;
- if (JS_VALUE_GET_TAG(val) != JS_TAG_OBJECT)
- return val;
- force_ordinary = hint & HINT_FORCE_ORDINARY;
- hint &= ~HINT_FORCE_ORDINARY;
- if (!force_ordinary) {
- method = JS_GetProperty(ctx, val, JS_ATOM_Symbol_toPrimitive);
- if (JS_IsException(method))
- goto exception;
- /* ECMA says *If exoticToPrim is not undefined* but tests in
- test262 use null as a non callable converter */
- if (!JS_IsUndefined(method) && !JS_IsNull(method)) {
- JSAtom atom;
- JSValue arg;
- switch(hint) {
- case HINT_STRING:
- atom = JS_ATOM_string;
- break;
- case HINT_NUMBER:
- atom = JS_ATOM_number;
- break;
- default:
- case HINT_NONE:
- atom = JS_ATOM_default;
- break;
- }
- arg = JS_AtomToString(ctx, atom);
- ret = JS_CallFree(ctx, method, val, 1, (JSValueConst *)&arg);
- JS_FreeValue(ctx, arg);
- if (JS_IsException(ret))
- goto exception;
- JS_FreeValue(ctx, val);
- if (JS_VALUE_GET_TAG(ret) != JS_TAG_OBJECT)
- return ret;
- JS_FreeValue(ctx, ret);
- return JS_ThrowTypeError(ctx, "toPrimitive");
- }
- }
- if (hint != HINT_STRING)
- hint = HINT_NUMBER;
- for(i = 0; i < 2; i++) {
- if ((i ^ hint) == 0) {
- method_name = JS_ATOM_toString;
- } else {
- method_name = JS_ATOM_valueOf;
- }
- method = JS_GetProperty(ctx, val, method_name);
- if (JS_IsException(method))
- goto exception;
- if (JS_IsFunction(ctx, method)) {
- ret = JS_CallFree(ctx, method, val, 0, NULL);
- if (JS_IsException(ret))
- goto exception;
- if (JS_VALUE_GET_TAG(ret) != JS_TAG_OBJECT) {
- JS_FreeValue(ctx, val);
- return ret;
- }
- JS_FreeValue(ctx, ret);
- } else {
- JS_FreeValue(ctx, method);
- }
- }
- JS_ThrowTypeError(ctx, "toPrimitive");
- exception:
- JS_FreeValue(ctx, val);
- return JS_EXCEPTION;
- }
- static JSValue JS_ToPrimitive(JSContext *ctx, JSValueConst val, int hint)
- {
- return JS_ToPrimitiveFree(ctx, JS_DupValue(ctx, val), hint);
- }
- void JS_SetIsHTMLDDA(JSContext *ctx, JSValueConst obj)
- {
- JSObject *p;
- if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)
- return;
- p = JS_VALUE_GET_OBJ(obj);
- p->is_HTMLDDA = TRUE;
- }
- static inline BOOL JS_IsHTMLDDA(JSContext *ctx, JSValueConst obj)
- {
- JSObject *p;
- if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)
- return FALSE;
- p = JS_VALUE_GET_OBJ(obj);
- return p->is_HTMLDDA;
- }
- static int JS_ToBoolFree(JSContext *ctx, JSValue val)
- {
- uint32_t tag = JS_VALUE_GET_TAG(val);
- switch(tag) {
- case JS_TAG_INT:
- return JS_VALUE_GET_INT(val) != 0;
- case JS_TAG_BOOL:
- case JS_TAG_NULL:
- case JS_TAG_UNDEFINED:
- return JS_VALUE_GET_INT(val);
- case JS_TAG_EXCEPTION:
- return -1;
- case JS_TAG_STRING:
- {
- BOOL ret = JS_VALUE_GET_STRING(val)->len != 0;
- JS_FreeValue(ctx, val);
- return ret;
- }
- case JS_TAG_STRING_ROPE:
- {
- BOOL ret = JS_VALUE_GET_STRING_ROPE(val)->len != 0;
- JS_FreeValue(ctx, val);
- return ret;
- }
- case JS_TAG_SHORT_BIG_INT:
- return JS_VALUE_GET_SHORT_BIG_INT(val) != 0;
- case JS_TAG_BIG_INT:
- {
- JSBigInt *p = JS_VALUE_GET_PTR(val);
- BOOL ret;
- int i;
-
- /* fail safe: we assume it is not necessarily
- normalized. Beginning from the MSB ensures that the
- test is fast. */
- ret = FALSE;
- for(i = p->len - 1; i >= 0; i--) {
- if (p->tab[i] != 0) {
- ret = TRUE;
- break;
- }
- }
- JS_FreeValue(ctx, val);
- return ret;
- }
- case JS_TAG_OBJECT:
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- BOOL ret;
- ret = !p->is_HTMLDDA;
- JS_FreeValue(ctx, val);
- return ret;
- }
- break;
- default:
- if (JS_TAG_IS_FLOAT64(tag)) {
- double d = JS_VALUE_GET_FLOAT64(val);
- return !isnan(d) && d != 0;
- } else {
- JS_FreeValue(ctx, val);
- return TRUE;
- }
- }
- }
- int JS_ToBool(JSContext *ctx, JSValueConst val)
- {
- return JS_ToBoolFree(ctx, JS_DupValue(ctx, val));
- }
- static int skip_spaces(const char *pc)
- {
- const uint8_t *p, *p_next, *p_start;
- uint32_t c;
- p = p_start = (const uint8_t *)pc;
- for (;;) {
- c = *p;
- if (c < 128) {
- if (!((c >= 0x09 && c <= 0x0d) || (c == 0x20)))
- break;
- p++;
- } else {
- c = unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p_next);
- if (!lre_is_space(c))
- break;
- p = p_next;
- }
- }
- return p - p_start;
- }
- static inline int to_digit(int c)
- {
- if (c >= '0' && c <= '9')
- return c - '0';
- else if (c >= 'A' && c <= 'Z')
- return c - 'A' + 10;
- else if (c >= 'a' && c <= 'z')
- return c - 'a' + 10;
- else
- return 36;
- }
- /* bigint support */
- #define JS_BIGINT_MAX_SIZE ((1024 * 1024) / JS_LIMB_BITS) /* in limbs */
- /* it is currently assumed that JS_SHORT_BIG_INT_BITS = JS_LIMB_BITS */
- #if JS_SHORT_BIG_INT_BITS == 32
- #define JS_SHORT_BIG_INT_MIN INT32_MIN
- #define JS_SHORT_BIG_INT_MAX INT32_MAX
- #elif JS_SHORT_BIG_INT_BITS == 64
- #define JS_SHORT_BIG_INT_MIN INT64_MIN
- #define JS_SHORT_BIG_INT_MAX INT64_MAX
- #else
- #error unsupported
- #endif
- #define ADDC(res, carry_out, op1, op2, carry_in) \
- do { \
- js_limb_t __v, __a, __k, __k1; \
- __v = (op1); \
- __a = __v + (op2); \
- __k1 = __a < __v; \
- __k = (carry_in); \
- __a = __a + __k; \
- carry_out = (__a < __k) | __k1; \
- res = __a; \
- } while (0)
- #if JS_LIMB_BITS == 32
- /* a != 0 */
- static inline js_limb_t js_limb_clz(js_limb_t a)
- {
- return clz32(a);
- }
- #else
- static inline js_limb_t js_limb_clz(js_limb_t a)
- {
- return clz64(a);
- }
- #endif
- /* handle a = 0 too */
- static inline js_limb_t js_limb_safe_clz(js_limb_t a)
- {
- if (a == 0)
- return JS_LIMB_BITS;
- else
- return js_limb_clz(a);
- }
- static js_limb_t mp_add(js_limb_t *res, const js_limb_t *op1, const js_limb_t *op2,
- js_limb_t n, js_limb_t carry)
- {
- int i;
- for(i = 0;i < n; i++) {
- ADDC(res[i], carry, op1[i], op2[i], carry);
- }
- return carry;
- }
- static js_limb_t mp_sub(js_limb_t *res, const js_limb_t *op1, const js_limb_t *op2,
- int n, js_limb_t carry)
- {
- int i;
- js_limb_t k, a, v, k1;
- k = carry;
- for(i=0;i<n;i++) {
- v = op1[i];
- a = v - op2[i];
- k1 = a > v;
- v = a - k;
- k = (v > a) | k1;
- res[i] = v;
- }
- return k;
- }
- /* compute 0 - op2. carry = 0 or 1. */
- static js_limb_t mp_neg(js_limb_t *res, const js_limb_t *op2, int n)
- {
- int i;
- js_limb_t v, carry;
- carry = 1;
- for(i=0;i<n;i++) {
- v = ~op2[i] + carry;
- carry = v < carry;
- res[i] = v;
- }
- return carry;
- }
- /* tabr[] = taba[] * b + l. Return the high carry */
- static js_limb_t mp_mul1(js_limb_t *tabr, const js_limb_t *taba, js_limb_t n,
- js_limb_t b, js_limb_t l)
- {
- js_limb_t i;
- js_dlimb_t t;
- for(i = 0; i < n; i++) {
- t = (js_dlimb_t)taba[i] * (js_dlimb_t)b + l;
- tabr[i] = t;
- l = t >> JS_LIMB_BITS;
- }
- return l;
- }
- static js_limb_t mp_div1(js_limb_t *tabr, const js_limb_t *taba, js_limb_t n,
- js_limb_t b, js_limb_t r)
- {
- js_slimb_t i;
- js_dlimb_t a1;
- for(i = n - 1; i >= 0; i--) {
- a1 = ((js_dlimb_t)r << JS_LIMB_BITS) | taba[i];
- tabr[i] = a1 / b;
- r = a1 % b;
- }
- return r;
- }
- /* tabr[] += taba[] * b, return the high word. */
- static js_limb_t mp_add_mul1(js_limb_t *tabr, const js_limb_t *taba, js_limb_t n,
- js_limb_t b)
- {
- js_limb_t i, l;
- js_dlimb_t t;
- l = 0;
- for(i = 0; i < n; i++) {
- t = (js_dlimb_t)taba[i] * (js_dlimb_t)b + l + tabr[i];
- tabr[i] = t;
- l = t >> JS_LIMB_BITS;
- }
- return l;
- }
- /* size of the result : op1_size + op2_size. */
- static void mp_mul_basecase(js_limb_t *result,
- const js_limb_t *op1, js_limb_t op1_size,
- const js_limb_t *op2, js_limb_t op2_size)
- {
- int i;
- js_limb_t r;
-
- result[op1_size] = mp_mul1(result, op1, op1_size, op2[0], 0);
- for(i=1;i<op2_size;i++) {
- r = mp_add_mul1(result + i, op1, op1_size, op2[i]);
- result[i + op1_size] = r;
- }
- }
- /* tabr[] -= taba[] * b. Return the value to substract to the high
- word. */
- static js_limb_t mp_sub_mul1(js_limb_t *tabr, const js_limb_t *taba, js_limb_t n,
- js_limb_t b)
- {
- js_limb_t i, l;
- js_dlimb_t t;
- l = 0;
- for(i = 0; i < n; i++) {
- t = tabr[i] - (js_dlimb_t)taba[i] * (js_dlimb_t)b - l;
- tabr[i] = t;
- l = -(t >> JS_LIMB_BITS);
- }
- return l;
- }
- /* WARNING: d must be >= 2^(JS_LIMB_BITS-1) */
- static inline js_limb_t udiv1norm_init(js_limb_t d)
- {
- js_limb_t a0, a1;
- a1 = -d - 1;
- a0 = -1;
- return (((js_dlimb_t)a1 << JS_LIMB_BITS) | a0) / d;
- }
- /* return the quotient and the remainder in '*pr'of 'a1*2^JS_LIMB_BITS+a0
- / d' with 0 <= a1 < d. */
- static inline js_limb_t udiv1norm(js_limb_t *pr, js_limb_t a1, js_limb_t a0,
- js_limb_t d, js_limb_t d_inv)
- {
- js_limb_t n1m, n_adj, q, r, ah;
- js_dlimb_t a;
- n1m = ((js_slimb_t)a0 >> (JS_LIMB_BITS - 1));
- n_adj = a0 + (n1m & d);
- a = (js_dlimb_t)d_inv * (a1 - n1m) + n_adj;
- q = (a >> JS_LIMB_BITS) + a1;
- /* compute a - q * r and update q so that the remainder is\
- between 0 and d - 1 */
- a = ((js_dlimb_t)a1 << JS_LIMB_BITS) | a0;
- a = a - (js_dlimb_t)q * d - d;
- ah = a >> JS_LIMB_BITS;
- q += 1 + ah;
- r = (js_limb_t)a + (ah & d);
- *pr = r;
- return q;
- }
- #define UDIV1NORM_THRESHOLD 3
- /* b must be >= 1 << (JS_LIMB_BITS - 1) */
- static js_limb_t mp_div1norm(js_limb_t *tabr, const js_limb_t *taba, js_limb_t n,
- js_limb_t b, js_limb_t r)
- {
- js_slimb_t i;
- if (n >= UDIV1NORM_THRESHOLD) {
- js_limb_t b_inv;
- b_inv = udiv1norm_init(b);
- for(i = n - 1; i >= 0; i--) {
- tabr[i] = udiv1norm(&r, r, taba[i], b, b_inv);
- }
- } else {
- js_dlimb_t a1;
- for(i = n - 1; i >= 0; i--) {
- a1 = ((js_dlimb_t)r << JS_LIMB_BITS) | taba[i];
- tabr[i] = a1 / b;
- r = a1 % b;
- }
- }
- return r;
- }
- /* base case division: divides taba[0..na-1] by tabb[0..nb-1]. tabb[nb
- - 1] must be >= 1 << (JS_LIMB_BITS - 1). na - nb must be >= 0. 'taba'
- is modified and contains the remainder (nb limbs). tabq[0..na-nb]
- contains the quotient with tabq[na - nb] <= 1. */
- static void mp_divnorm(js_limb_t *tabq, js_limb_t *taba, js_limb_t na,
- const js_limb_t *tabb, js_limb_t nb)
- {
- js_limb_t r, a, c, q, v, b1, b1_inv, n, dummy_r;
- int i, j;
- b1 = tabb[nb - 1];
- if (nb == 1) {
- taba[0] = mp_div1norm(tabq, taba, na, b1, 0);
- return;
- }
- n = na - nb;
- if (n >= UDIV1NORM_THRESHOLD)
- b1_inv = udiv1norm_init(b1);
- else
- b1_inv = 0;
- /* first iteration: the quotient is only 0 or 1 */
- q = 1;
- for(j = nb - 1; j >= 0; j--) {
- if (taba[n + j] != tabb[j]) {
- if (taba[n + j] < tabb[j])
- q = 0;
- break;
- }
- }
- tabq[n] = q;
- if (q) {
- mp_sub(taba + n, taba + n, tabb, nb, 0);
- }
- for(i = n - 1; i >= 0; i--) {
- if (unlikely(taba[i + nb] >= b1)) {
- q = -1;
- } else if (b1_inv) {
- q = udiv1norm(&dummy_r, taba[i + nb], taba[i + nb - 1], b1, b1_inv);
- } else {
- js_dlimb_t al;
- al = ((js_dlimb_t)taba[i + nb] << JS_LIMB_BITS) | taba[i + nb - 1];
- q = al / b1;
- r = al % b1;
- }
- r = mp_sub_mul1(taba + i, tabb, nb, q);
- v = taba[i + nb];
- a = v - r;
- c = (a > v);
- taba[i + nb] = a;
- if (c != 0) {
- /* negative result */
- for(;;) {
- q--;
- c = mp_add(taba + i, taba + i, tabb, nb, 0);
- /* propagate carry and test if positive result */
- if (c != 0) {
- if (++taba[i + nb] == 0) {
- break;
- }
- }
- }
- }
- tabq[i] = q;
- }
- }
- /* 1 <= shift <= JS_LIMB_BITS - 1 */
- static js_limb_t mp_shl(js_limb_t *tabr, const js_limb_t *taba, int n,
- int shift)
- {
- int i;
- js_limb_t l, v;
- l = 0;
- for(i = 0; i < n; i++) {
- v = taba[i];
- tabr[i] = (v << shift) | l;
- l = v >> (JS_LIMB_BITS - shift);
- }
- return l;
- }
- /* r = (a + high*B^n) >> shift. Return the remainder r (0 <= r < 2^shift).
- 1 <= shift <= LIMB_BITS - 1 */
- static js_limb_t mp_shr(js_limb_t *tab_r, const js_limb_t *tab, int n,
- int shift, js_limb_t high)
- {
- int i;
- js_limb_t l, a;
- l = high;
- for(i = n - 1; i >= 0; i--) {
- a = tab[i];
- tab_r[i] = (a >> shift) | (l << (JS_LIMB_BITS - shift));
- l = a;
- }
- return l & (((js_limb_t)1 << shift) - 1);
- }
- static JSBigInt *js_bigint_new(JSContext *ctx, int len)
- {
- JSBigInt *r;
- if (len > JS_BIGINT_MAX_SIZE) {
- JS_ThrowRangeError(ctx, "BigInt is too large to allocate");
- return NULL;
- }
- r = js_malloc(ctx, sizeof(JSBigInt) + len * sizeof(js_limb_t));
- if (!r)
- return NULL;
- js_rc(r)->ref_count = 1;
- r->len = len;
- return r;
- }
- static JSBigInt *js_bigint_set_si(JSBigIntBuf *buf, js_slimb_t a)
- {
- JSBigInt *r = (JSBigInt *)buf->big_int_buf;
- r->len = 1;
- r->tab[0] = a;
- return r;
- }
- static JSBigInt *js_bigint_set_si64(JSBigIntBuf *buf, int64_t a)
- {
- #if JS_LIMB_BITS == 64
- return js_bigint_set_si(buf, a);
- #else
- JSBigInt *r = (JSBigInt *)buf->big_int_buf;
- if (a >= INT32_MIN && a <= INT32_MAX) {
- r->len = 1;
- r->tab[0] = a;
- } else {
- r->len = 2;
- r->tab[0] = a;
- r->tab[1] = a >> JS_LIMB_BITS;
- }
- return r;
- #endif
- }
- /* val must be a short big int */
- static JSBigInt *js_bigint_set_short(JSBigIntBuf *buf, JSValueConst val)
- {
- return js_bigint_set_si(buf, JS_VALUE_GET_SHORT_BIG_INT(val));
- }
- static __maybe_unused void js_bigint_dump1(JSContext *ctx, const char *str,
- const js_limb_t *tab, int len)
- {
- int i;
- printf("%s: ", str);
- for(i = len - 1; i >= 0; i--) {
- #if JS_LIMB_BITS == 32
- printf(" %08x", tab[i]);
- #else
- printf(" %016" PRIx64, tab[i]);
- #endif
- }
- printf("\n");
- }
- static __maybe_unused void js_bigint_dump(JSContext *ctx, const char *str,
- const JSBigInt *p)
- {
- js_bigint_dump1(ctx, str, p->tab, p->len);
- }
- static JSBigInt *js_bigint_new_si(JSContext *ctx, js_slimb_t a)
- {
- JSBigInt *r;
- r = js_bigint_new(ctx, 1);
- if (!r)
- return NULL;
- r->tab[0] = a;
- return r;
- }
- static JSBigInt *js_bigint_new_si64(JSContext *ctx, int64_t a)
- {
- #if JS_LIMB_BITS == 64
- return js_bigint_new_si(ctx, a);
- #else
- if (a >= INT32_MIN && a <= INT32_MAX) {
- return js_bigint_new_si(ctx, a);
- } else {
- JSBigInt *r;
- r = js_bigint_new(ctx, 2);
- if (!r)
- return NULL;
- r->tab[0] = a;
- r->tab[1] = a >> 32;
- return r;
- }
- #endif
- }
- static JSBigInt *js_bigint_new_ui64(JSContext *ctx, uint64_t a)
- {
- if (a <= INT64_MAX) {
- return js_bigint_new_si64(ctx, a);
- } else {
- JSBigInt *r;
- r = js_bigint_new(ctx, (65 + JS_LIMB_BITS - 1) / JS_LIMB_BITS);
- if (!r)
- return NULL;
- #if JS_LIMB_BITS == 64
- r->tab[0] = a;
- r->tab[1] = 0;
- #else
- r->tab[0] = a;
- r->tab[1] = a >> 32;
- r->tab[2] = 0;
- #endif
- return r;
- }
- }
- static JSBigInt *js_bigint_new_di(JSContext *ctx, js_sdlimb_t a)
- {
- JSBigInt *r;
- if (a == (js_slimb_t)a) {
- r = js_bigint_new(ctx, 1);
- if (!r)
- return NULL;
- r->tab[0] = a;
- } else {
- r = js_bigint_new(ctx, 2);
- if (!r)
- return NULL;
- r->tab[0] = a;
- r->tab[1] = a >> JS_LIMB_BITS;
- }
- return r;
- }
- /* Remove redundant high order limbs. Warning: 'a' may be
- reallocated. Can never fail.
- */
- static JSBigInt *js_bigint_normalize1(JSContext *ctx, JSBigInt *a, int l)
- {
- js_limb_t v;
- assert(js_rc(a)->ref_count == 1);
- while (l > 1) {
- v = a->tab[l - 1];
- if ((v != 0 && v != -1) ||
- (v & 1) != (a->tab[l - 2] >> (JS_LIMB_BITS - 1))) {
- break;
- }
- l--;
- }
- if (l != a->len) {
- JSBigInt *a1;
- /* realloc to reduce the size */
- a->len = l;
- a1 = js_realloc(ctx, a, sizeof(JSBigInt) + l * sizeof(js_limb_t));
- if (a1)
- a = a1;
- }
- return a;
- }
- static JSBigInt *js_bigint_normalize(JSContext *ctx, JSBigInt *a)
- {
- return js_bigint_normalize1(ctx, a, a->len);
- }
- /* return 0 or 1 depending on the sign */
- static inline int js_bigint_sign(const JSBigInt *a)
- {
- return a->tab[a->len - 1] >> (JS_LIMB_BITS - 1);
- }
- static js_slimb_t js_bigint_get_si_sat(const JSBigInt *a)
- {
- if (a->len == 1) {
- return a->tab[0];
- } else {
- #if JS_LIMB_BITS == 32
- if (js_bigint_sign(a))
- return INT32_MIN;
- else
- return INT32_MAX;
- #else
- if (js_bigint_sign(a))
- return INT64_MIN;
- else
- return INT64_MAX;
- #endif
- }
- }
- /* add the op1 limb */
- static JSBigInt *js_bigint_extend(JSContext *ctx, JSBigInt *r,
- js_limb_t op1)
- {
- int n2 = r->len;
- if ((op1 != 0 && op1 != -1) ||
- (op1 & 1) != r->tab[n2 - 1] >> (JS_LIMB_BITS - 1)) {
- JSBigInt *r1;
- r1 = js_realloc(ctx, r,
- sizeof(JSBigInt) + (n2 + 1) * sizeof(js_limb_t));
- if (!r1) {
- js_free(ctx, r);
- return NULL;
- }
- r = r1;
- r->len = n2 + 1;
- r->tab[n2] = op1;
- } else {
- /* otherwise still need to normalize the result */
- r = js_bigint_normalize(ctx, r);
- }
- return r;
- }
- /* return NULL in case of error. Compute a + b (b_neg = 0) or a - b
- (b_neg = 1) */
- /* XXX: optimize */
- static JSBigInt *js_bigint_add(JSContext *ctx, const JSBigInt *a,
- const JSBigInt *b, int b_neg)
- {
- JSBigInt *r;
- int n1, n2, i;
- js_limb_t carry, op1, op2, a_sign, b_sign;
-
- n2 = max_int(a->len, b->len);
- n1 = min_int(a->len, b->len);
- r = js_bigint_new(ctx, n2);
- if (!r)
- return NULL;
- /* XXX: optimize */
- /* common part */
- carry = b_neg;
- for(i = 0; i < n1; i++) {
- op1 = a->tab[i];
- op2 = b->tab[i] ^ (-b_neg);
- ADDC(r->tab[i], carry, op1, op2, carry);
- }
- a_sign = -js_bigint_sign(a);
- b_sign = (-js_bigint_sign(b)) ^ (-b_neg);
- /* part with sign extension of one operand */
- if (a->len > b->len) {
- for(i = n1; i < n2; i++) {
- op1 = a->tab[i];
- ADDC(r->tab[i], carry, op1, b_sign, carry);
- }
- } else if (a->len < b->len) {
- for(i = n1; i < n2; i++) {
- op2 = b->tab[i] ^ (-b_neg);
- ADDC(r->tab[i], carry, a_sign, op2, carry);
- }
- }
- /* part with sign extension for both operands. Extend the result
- if necessary */
- return js_bigint_extend(ctx, r, a_sign + b_sign + carry);
- }
- /* XXX: optimize */
- static JSBigInt *js_bigint_neg(JSContext *ctx, const JSBigInt *a)
- {
- JSBigIntBuf buf;
- JSBigInt *b;
- b = js_bigint_set_si(&buf, 0);
- return js_bigint_add(ctx, b, a, 1);
- }
- static JSBigInt *js_bigint_mul(JSContext *ctx, const JSBigInt *a,
- const JSBigInt *b)
- {
- JSBigInt *r;
-
- r = js_bigint_new(ctx, a->len + b->len);
- if (!r)
- return NULL;
- mp_mul_basecase(r->tab, a->tab, a->len, b->tab, b->len);
- /* correct the result if negative operands (no overflow is
- possible) */
- if (js_bigint_sign(a))
- mp_sub(r->tab + a->len, r->tab + a->len, b->tab, b->len, 0);
- if (js_bigint_sign(b))
- mp_sub(r->tab + b->len, r->tab + b->len, a->tab, a->len, 0);
- return js_bigint_normalize(ctx, r);
- }
- /* return the division or the remainder. 'b' must be != 0. return NULL
- in case of exception (division by zero or memory error) */
- static JSBigInt *js_bigint_divrem(JSContext *ctx, const JSBigInt *a,
- const JSBigInt *b, BOOL is_rem)
- {
- JSBigInt *r, *q;
- js_limb_t *tabb, h;
- int na, nb, a_sign, b_sign, shift;
-
- if (b->len == 1 && b->tab[0] == 0) {
- JS_ThrowRangeError(ctx, "BigInt division by zero");
- return NULL;
- }
-
- a_sign = js_bigint_sign(a);
- b_sign = js_bigint_sign(b);
- na = a->len;
- nb = b->len;
- r = js_bigint_new(ctx, na + 2);
- if (!r)
- return NULL;
- if (a_sign) {
- mp_neg(r->tab, a->tab, na);
- } else {
- memcpy(r->tab, a->tab, na * sizeof(a->tab[0]));
- }
- /* normalize */
- while (na > 1 && r->tab[na - 1] == 0)
- na--;
- tabb = js_malloc(ctx, nb * sizeof(tabb[0]));
- if (!tabb) {
- js_free(ctx, r);
- return NULL;
- }
- if (b_sign) {
- mp_neg(tabb, b->tab, nb);
- } else {
- memcpy(tabb, b->tab, nb * sizeof(tabb[0]));
- }
- /* normalize */
- while (nb > 1 && tabb[nb - 1] == 0)
- nb--;
- /* trivial case if 'a' is small */
- if (na < nb) {
- js_free(ctx, r);
- js_free(ctx, tabb);
- if (is_rem) {
- /* r = a */
- r = js_bigint_new(ctx, a->len);
- if (!r)
- return NULL;
- memcpy(r->tab, a->tab, a->len * sizeof(a->tab[0]));
- return r;
- } else {
- /* q = 0 */
- return js_bigint_new_si(ctx, 0);
- }
- }
- /* normalize 'b' */
- shift = js_limb_clz(tabb[nb - 1]);
- if (shift != 0) {
- mp_shl(tabb, tabb, nb, shift);
- h = mp_shl(r->tab, r->tab, na, shift);
- if (h != 0)
- r->tab[na++] = h;
- }
- q = js_bigint_new(ctx, na - nb + 2); /* one more limb for the sign */
- if (!q) {
- js_free(ctx, r);
- js_free(ctx, tabb);
- return NULL;
- }
- // js_bigint_dump1(ctx, "a", r->tab, na);
- // js_bigint_dump1(ctx, "b", tabb, nb);
- mp_divnorm(q->tab, r->tab, na, tabb, nb);
- js_free(ctx, tabb);
- if (is_rem) {
- js_free(ctx, q);
- if (shift != 0)
- mp_shr(r->tab, r->tab, nb, shift, 0);
- r->tab[nb++] = 0;
- if (a_sign)
- mp_neg(r->tab, r->tab, nb);
- r = js_bigint_normalize1(ctx, r, nb);
- return r;
- } else {
- js_free(ctx, r);
- q->tab[na - nb + 1] = 0;
- if (a_sign ^ b_sign) {
- mp_neg(q->tab, q->tab, q->len);
- }
- q = js_bigint_normalize(ctx, q);
- return q;
- }
- }
- /* and, or, xor */
- static JSBigInt *js_bigint_logic(JSContext *ctx, const JSBigInt *a,
- const JSBigInt *b, OPCodeEnum op)
- {
- JSBigInt *r;
- js_limb_t b_sign;
- int a_len, b_len, i;
- if (a->len < b->len) {
- const JSBigInt *tmp;
- tmp = a;
- a = b;
- b = tmp;
- }
- /* a_len >= b_len */
- a_len = a->len;
- b_len = b->len;
- b_sign = -js_bigint_sign(b);
- r = js_bigint_new(ctx, a_len);
- if (!r)
- return NULL;
- switch(op) {
- case OP_or:
- for(i = 0; i < b_len; i++) {
- r->tab[i] = a->tab[i] | b->tab[i];
- }
- for(i = b_len; i < a_len; i++) {
- r->tab[i] = a->tab[i] | b_sign;
- }
- break;
- case OP_and:
- for(i = 0; i < b_len; i++) {
- r->tab[i] = a->tab[i] & b->tab[i];
- }
- for(i = b_len; i < a_len; i++) {
- r->tab[i] = a->tab[i] & b_sign;
- }
- break;
- case OP_xor:
- for(i = 0; i < b_len; i++) {
- r->tab[i] = a->tab[i] ^ b->tab[i];
- }
- for(i = b_len; i < a_len; i++) {
- r->tab[i] = a->tab[i] ^ b_sign;
- }
- break;
- default:
- abort();
- }
- return js_bigint_normalize(ctx, r);
- }
- static JSBigInt *js_bigint_not(JSContext *ctx, const JSBigInt *a)
- {
- JSBigInt *r;
- int i;
-
- r = js_bigint_new(ctx, a->len);
- if (!r)
- return NULL;
- for(i = 0; i < a->len; i++) {
- r->tab[i] = ~a->tab[i];
- }
- /* no normalization is needed */
- return r;
- }
- static JSBigInt *js_bigint_shl(JSContext *ctx, const JSBigInt *a,
- unsigned int shift1)
- {
- int d, i, shift;
- JSBigInt *r;
- js_limb_t l;
- if (a->len == 1 && a->tab[0] == 0)
- return js_bigint_new_si(ctx, 0); /* zero case */
- d = shift1 / JS_LIMB_BITS;
- shift = shift1 % JS_LIMB_BITS;
- r = js_bigint_new(ctx, a->len + d);
- if (!r)
- return NULL;
- for(i = 0; i < d; i++)
- r->tab[i] = 0;
- if (shift == 0) {
- for(i = 0; i < a->len; i++) {
- r->tab[i + d] = a->tab[i];
- }
- } else {
- l = mp_shl(r->tab + d, a->tab, a->len, shift);
- if (js_bigint_sign(a))
- l |= (js_limb_t)(-1) << shift;
- r = js_bigint_extend(ctx, r, l);
- }
- return r;
- }
- static JSBigInt *js_bigint_shr(JSContext *ctx, const JSBigInt *a,
- unsigned int shift1)
- {
- int d, i, shift, a_sign, n1;
- JSBigInt *r;
- d = shift1 / JS_LIMB_BITS;
- shift = shift1 % JS_LIMB_BITS;
- a_sign = js_bigint_sign(a);
- if (d >= a->len)
- return js_bigint_new_si(ctx, -a_sign);
- n1 = a->len - d;
- r = js_bigint_new(ctx, n1);
- if (!r)
- return NULL;
- if (shift == 0) {
- for(i = 0; i < n1; i++) {
- r->tab[i] = a->tab[i + d];
- }
- /* no normalization is needed */
- } else {
- mp_shr(r->tab, a->tab + d, n1, shift, -a_sign);
- r = js_bigint_normalize(ctx, r);
- }
- return r;
- }
- static JSBigInt *js_bigint_pow(JSContext *ctx, const JSBigInt *a, JSBigInt *b)
- {
- uint32_t e;
- int n_bits, i;
- JSBigInt *r, *r1;
-
- /* b must be >= 0 */
- if (js_bigint_sign(b)) {
- JS_ThrowRangeError(ctx, "BigInt negative exponent");
- return NULL;
- }
- if (b->len == 1 && b->tab[0] == 0) {
- /* a^0 = 1 */
- return js_bigint_new_si(ctx, 1);
- } else if (a->len == 1) {
- js_limb_t v;
- BOOL is_neg;
- v = a->tab[0];
- if (v <= 1)
- return js_bigint_new_si(ctx, v);
- else if (v == -1)
- return js_bigint_new_si(ctx, 1 - 2 * (b->tab[0] & 1));
- is_neg = (js_slimb_t)v < 0;
- if (is_neg)
- v = -v;
- if ((v & (v - 1)) == 0) {
- uint64_t e1;
- int n;
- /* v = 2^n */
- n = JS_LIMB_BITS - 1 - js_limb_clz(v);
- if (b->len > 1)
- goto overflow;
- if (b->tab[0] > INT32_MAX)
- goto overflow;
- e = b->tab[0];
- e1 = (uint64_t)e * n;
- if (e1 > JS_BIGINT_MAX_SIZE * JS_LIMB_BITS)
- goto overflow;
- e = e1;
- if (is_neg)
- is_neg = b->tab[0] & 1;
- r = js_bigint_new(ctx,
- (e + JS_LIMB_BITS + 1 - is_neg) / JS_LIMB_BITS);
- if (!r)
- return NULL;
- memset(r->tab, 0, sizeof(r->tab[0]) * r->len);
- r->tab[e / JS_LIMB_BITS] =
- (js_limb_t)(1 - 2 * is_neg) << (e % JS_LIMB_BITS);
- return r;
- }
- }
- if (b->len > 1)
- goto overflow;
- if (b->tab[0] > INT32_MAX)
- goto overflow;
- e = b->tab[0];
- n_bits = 32 - clz32(e);
- r = js_bigint_new(ctx, a->len);
- if (!r)
- return NULL;
- memcpy(r->tab, a->tab, a->len * sizeof(a->tab[0]));
- for(i = n_bits - 2; i >= 0; i--) {
- r1 = js_bigint_mul(ctx, r, r);
- if (!r1)
- return NULL;
- js_free(ctx, r);
- r = r1;
- if ((e >> i) & 1) {
- r1 = js_bigint_mul(ctx, r, a);
- if (!r1)
- return NULL;
- js_free(ctx, r);
- r = r1;
- }
- }
- return r;
- overflow:
- JS_ThrowRangeError(ctx, "BigInt is too large");
- return NULL;
- }
- /* return (mant, exp) so that abs(a) ~ mant*2^(exp - (limb_bits -
- 1). a must be != 0. */
- static uint64_t js_bigint_get_mant_exp(JSContext *ctx,
- int *pexp, const JSBigInt *a)
- {
- js_limb_t t[4 - JS_LIMB_BITS / 32], carry, v, low_bits;
- int n1, n2, sgn, shift, i, j, e;
- uint64_t a1, a0;
- n2 = 4 - JS_LIMB_BITS / 32;
- n1 = a->len - n2;
- sgn = js_bigint_sign(a);
- /* low_bits != 0 if there are a non zero low bit in abs(a) */
- low_bits = 0;
- carry = sgn;
- for(i = 0; i < n1; i++) {
- v = (a->tab[i] ^ (-sgn)) + carry;
- carry = v < carry;
- low_bits |= v;
- }
- /* get the n2 high limbs of abs(a) */
- for(j = 0; j < n2; j++) {
- i = j + n1;
- if (i < 0) {
- v = 0;
- } else {
- v = (a->tab[i] ^ (-sgn)) + carry;
- carry = v < carry;
- }
- t[j] = v;
- }
-
- #if JS_LIMB_BITS == 32
- a1 = ((uint64_t)t[2] << 32) | t[1];
- a0 = (uint64_t)t[0] << 32;
- #else
- a1 = t[1];
- a0 = t[0];
- #endif
- a0 |= (low_bits != 0);
- /* normalize */
- if (a1 == 0) {
- /* JS_LIMB_BITS = 64 bit only */
- shift = 64;
- a1 = a0;
- a0 = 0;
- } else {
- shift = clz64(a1);
- if (shift != 0) {
- a1 = (a1 << shift) | (a0 >> (64 - shift));
- a0 <<= shift;
- }
- }
- a1 |= (a0 != 0); /* keep the bits for the final rounding */
- /* compute the exponent */
- e = a->len * JS_LIMB_BITS - shift - 1;
- *pexp = e;
- return a1;
- }
- /* shift left with round to nearest, ties to even. n >= 1 */
- static uint64_t shr_rndn(uint64_t a, int n)
- {
- uint64_t addend = ((a >> n) & 1) + ((1 << (n - 1)) - 1);
- return (a + addend) >> n;
- }
- /* convert to float64 with round to nearest, ties to even. Return
- +/-infinity if too large. */
- static double js_bigint_to_float64(JSContext *ctx, const JSBigInt *a)
- {
- int sgn, e;
- uint64_t mant;
- if (a->len == 1) {
- /* fast case, including zero */
- return (double)(js_slimb_t)a->tab[0];
- }
- sgn = js_bigint_sign(a);
- mant = js_bigint_get_mant_exp(ctx, &e, a);
- if (e > 1023) {
- /* overflow: return infinity */
- mant = 0;
- e = 1024;
- } else {
- mant = (mant >> 1) | (mant & 1); /* avoid overflow in rounding */
- mant = shr_rndn(mant, 10);
- /* rounding can cause an overflow */
- if (mant >= ((uint64_t)1 << 53)) {
- mant >>= 1;
- e++;
- }
- mant &= (((uint64_t)1 << 52) - 1);
- }
- return uint64_as_float64(((uint64_t)sgn << 63) |
- ((uint64_t)(e + 1023) << 52) |
- mant);
- }
- /* return (1, NULL) if not an integer, (2, NULL) if NaN or Infinity,
- (0, n) if an integer, (0, NULL) in case of memory error */
- static JSBigInt *js_bigint_from_float64(JSContext *ctx, int *pres, double a1)
- {
- uint64_t a = float64_as_uint64(a1);
- int sgn, e, shift;
- uint64_t mant;
- JSBigIntBuf buf;
- JSBigInt *r;
-
- sgn = a >> 63;
- e = (a >> 52) & ((1 << 11) - 1);
- mant = a & (((uint64_t)1 << 52) - 1);
- if (e == 2047) {
- /* NaN, Infinity */
- *pres = 2;
- return NULL;
- }
- if (e == 0 && mant == 0) {
- /* zero */
- *pres = 0;
- return js_bigint_new_si(ctx, 0);
- }
- e -= 1023;
- /* 0 < a < 1 : not an integer */
- if (e < 0)
- goto not_an_integer;
- mant |= (uint64_t)1 << 52;
- if (e < 52) {
- shift = 52 - e;
- /* check that there is no fractional part */
- if (mant & (((uint64_t)1 << shift) - 1)) {
- not_an_integer:
- *pres = 1;
- return NULL;
- }
- mant >>= shift;
- e = 0;
- } else {
- e -= 52;
- }
- if (sgn)
- mant = -mant;
- /* the integer is mant*2^e */
- r = js_bigint_set_si64(&buf, (int64_t)mant);
- *pres = 0;
- return js_bigint_shl(ctx, r, e);
- }
- /* return -1, 0, 1 or (2) (unordered) */
- static int js_bigint_float64_cmp(JSContext *ctx, const JSBigInt *a,
- double b)
- {
- int b_sign, a_sign, e, f;
- uint64_t mant, b1, a_mant;
-
- b1 = float64_as_uint64(b);
- b_sign = b1 >> 63;
- e = (b1 >> 52) & ((1 << 11) - 1);
- mant = b1 & (((uint64_t)1 << 52) - 1);
- a_sign = js_bigint_sign(a);
- if (e == 2047) {
- if (mant != 0) {
- /* NaN */
- return 2;
- } else {
- /* +/- infinity */
- return 2 * b_sign - 1;
- }
- } else if (e == 0 && mant == 0) {
- /* b = +/-0 */
- if (a->len == 1 && a->tab[0] == 0)
- return 0;
- else
- return 1 - 2 * a_sign;
- } else if (a->len == 1 && a->tab[0] == 0) {
- /* a = 0, b != 0 */
- return 2 * b_sign - 1;
- } else if (a_sign != b_sign) {
- return 1 - 2 * a_sign;
- } else {
- e -= 1023;
- /* Note: handling denormals is not necessary because we
- compare to integers hence f >= 0 */
- /* compute f so that 2^f <= abs(a) < 2^(f+1) */
- a_mant = js_bigint_get_mant_exp(ctx, &f, a);
- if (f != e) {
- if (f < e)
- return -1;
- else
- return 1;
- } else {
- mant = (mant | ((uint64_t)1 << 52)) << 11; /* align to a_mant */
- if (a_mant < mant)
- return 2 * a_sign - 1;
- else if (a_mant > mant)
- return 1 - 2 * a_sign;
- else
- return 0;
- }
- }
- }
- /* return -1, 0 or 1 */
- static int js_bigint_cmp(JSContext *ctx, const JSBigInt *a,
- const JSBigInt *b)
- {
- int a_sign, b_sign, res, i;
- a_sign = js_bigint_sign(a);
- b_sign = js_bigint_sign(b);
- if (a_sign != b_sign) {
- res = 1 - 2 * a_sign;
- } else {
- /* we assume the numbers are normalized */
- if (a->len != b->len) {
- if (a->len < b->len)
- res = 2 * a_sign - 1;
- else
- res = 1 - 2 * a_sign;
- } else {
- res = 0;
- for(i = a->len -1; i >= 0; i--) {
- if (a->tab[i] != b->tab[i]) {
- if (a->tab[i] < b->tab[i])
- res = -1;
- else
- res = 1;
- break;
- }
- }
- }
- }
- return res;
- }
- /* contains 10^i */
- static const js_limb_t js_pow_dec[JS_LIMB_DIGITS + 1] = {
- 1U,
- 10U,
- 100U,
- 1000U,
- 10000U,
- 100000U,
- 1000000U,
- 10000000U,
- 100000000U,
- 1000000000U,
- #if JS_LIMB_BITS == 64
- 10000000000U,
- 100000000000U,
- 1000000000000U,
- 10000000000000U,
- 100000000000000U,
- 1000000000000000U,
- 10000000000000000U,
- 100000000000000000U,
- 1000000000000000000U,
- 10000000000000000000U,
- #endif
- };
- /* syntax: [-]digits in base radix. Return NULL if memory error. radix
- = 10, 2, 8 or 16. */
- static JSBigInt *js_bigint_from_string(JSContext *ctx,
- const char *str, int radix)
- {
- const char *p = str;
- size_t n_digits1;
- int is_neg, n_digits, n_limbs, len, log2_radix, n_bits, i;
- JSBigInt *r;
- js_limb_t v, c, h;
-
- is_neg = 0;
- if (*p == '-') {
- is_neg = 1;
- p++;
- }
- while (*p == '0')
- p++;
- n_digits1 = strlen(p);
- /* the real check for overflox is done js_bigint_new(). Here
- we just avoid integer overflow */
- if (n_digits1 > JS_BIGINT_MAX_SIZE * JS_LIMB_BITS) {
- JS_ThrowRangeError(ctx, "BigInt is too large to allocate");
- return NULL;
- }
- n_digits = n_digits1;
- log2_radix = 32 - clz32(radix - 1); /* ceil(log2(radix)) */
- /* compute the maximum number of limbs */
- if (radix == 10) {
- n_bits = (n_digits * 27 + 7) / 8; /* >= ceil(n_digits * log2(10)) */
- } else {
- n_bits = n_digits * log2_radix;
- }
- /* we add one extra bit for the sign */
- n_limbs = max_int(1, n_bits / JS_LIMB_BITS + 1);
- r = js_bigint_new(ctx, n_limbs);
- if (!r)
- return NULL;
- if (radix == 10) {
- int digits_per_limb = JS_LIMB_DIGITS;
- len = 1;
- r->tab[0] = 0;
- for(;;) {
- /* XXX: slow */
- v = 0;
- for(i = 0; i < digits_per_limb; i++) {
- c = to_digit(*p);
- if (c >= radix)
- break;
- p++;
- v = v * 10 + c;
- }
- if (i == 0)
- break;
- if (len == 1 && r->tab[0] == 0) {
- r->tab[0] = v;
- } else {
- h = mp_mul1(r->tab, r->tab, len, js_pow_dec[i], v);
- if (h != 0) {
- r->tab[len++] = h;
- }
- }
- }
- /* add one extra limb to have the correct sign*/
- if ((r->tab[len - 1] >> (JS_LIMB_BITS - 1)) != 0)
- r->tab[len++] = 0;
- r->len = len;
- } else {
- unsigned int bit_pos, shift, pos;
-
- /* power of two base: no multiplication is needed */
- r->len = n_limbs;
- memset(r->tab, 0, sizeof(r->tab[0]) * n_limbs);
- for(i = 0; i < n_digits; i++) {
- c = to_digit(p[n_digits - 1 - i]);
- assert(c < radix);
- bit_pos = i * log2_radix;
- shift = bit_pos & (JS_LIMB_BITS - 1);
- pos = bit_pos / JS_LIMB_BITS;
- r->tab[pos] |= c << shift;
- /* if log2_radix does not divide JS_LIMB_BITS, needed an
- additional op */
- if (shift + log2_radix > JS_LIMB_BITS) {
- r->tab[pos + 1] |= c >> (JS_LIMB_BITS - shift);
- }
- }
- }
- r = js_bigint_normalize(ctx, r);
- /* XXX: could do it in place */
- if (is_neg) {
- JSBigInt *r1;
- r1 = js_bigint_neg(ctx, r);
- js_free(ctx, r);
- r = r1;
- }
- return r;
- }
- /* 2 <= base <= 36 */
- static char const digits[36] = {
- '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b',
- 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
- 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
- };
- /* special version going backwards */
- /* XXX: use dtoa.c */
- static char *js_u64toa(char *q, int64_t n, unsigned int base)
- {
- int digit;
- if (base == 10) {
- /* division by known base uses multiplication */
- do {
- digit = (uint64_t)n % 10;
- n = (uint64_t)n / 10;
- *--q = '0' + digit;
- } while (n != 0);
- } else {
- do {
- digit = (uint64_t)n % base;
- n = (uint64_t)n / base;
- *--q = digits[digit];
- } while (n != 0);
- }
- return q;
- }
- /* len >= 1. 2 <= radix <= 36 */
- static char *limb_to_a(char *q, js_limb_t n, unsigned int radix, int len)
- {
- int digit, i;
- if (radix == 10) {
- /* specific case with constant divisor */
- /* XXX: optimize */
- for(i = 0; i < len; i++) {
- digit = (js_limb_t)n % 10;
- n = (js_limb_t)n / 10;
- *--q = digit + '0';
- }
- } else {
- for(i = 0; i < len; i++) {
- digit = (js_limb_t)n % radix;
- n = (js_limb_t)n / radix;
- *--q = digits[digit];
- }
- }
- return q;
- }
- #define JS_RADIX_MAX 36
- static const uint8_t digits_per_limb_table[JS_RADIX_MAX - 1] = {
- #if JS_LIMB_BITS == 32
- 32,20,16,13,12,11,10,10, 9, 9, 8, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
- #else
- 64,40,32,27,24,22,21,20,19,18,17,17,16,16,16,15,15,15,14,14,14,14,13,13,13,13,13,13,13,12,12,12,12,12,12,
- #endif
- };
- static const js_limb_t radix_base_table[JS_RADIX_MAX - 1] = {
- #if JS_LIMB_BITS == 32
- 0x00000000, 0xcfd41b91, 0x00000000, 0x48c27395,
- 0x81bf1000, 0x75db9c97, 0x40000000, 0xcfd41b91,
- 0x3b9aca00, 0x8c8b6d2b, 0x19a10000, 0x309f1021,
- 0x57f6c100, 0x98c29b81, 0x00000000, 0x18754571,
- 0x247dbc80, 0x3547667b, 0x4c4b4000, 0x6b5a6e1d,
- 0x94ace180, 0xcaf18367, 0x0b640000, 0x0e8d4a51,
- 0x1269ae40, 0x17179149, 0x1cb91000, 0x23744899,
- 0x2b73a840, 0x34e63b41, 0x40000000, 0x4cfa3cc1,
- 0x5c13d840, 0x6d91b519, 0x81bf1000,
- #else
- 0x0000000000000000, 0xa8b8b452291fe821, 0x0000000000000000, 0x6765c793fa10079d,
- 0x41c21cb8e1000000, 0x3642798750226111, 0x8000000000000000, 0xa8b8b452291fe821,
- 0x8ac7230489e80000, 0x4d28cb56c33fa539, 0x1eca170c00000000, 0x780c7372621bd74d,
- 0x1e39a5057d810000, 0x5b27ac993df97701, 0x0000000000000000, 0x27b95e997e21d9f1,
- 0x5da0e1e53c5c8000, 0xd2ae3299c1c4aedb, 0x16bcc41e90000000, 0x2d04b7fdd9c0ef49,
- 0x5658597bcaa24000, 0xa0e2073737609371, 0x0c29e98000000000, 0x14adf4b7320334b9,
- 0x226ed36478bfa000, 0x383d9170b85ff80b, 0x5a3c23e39c000000, 0x8e65137388122bcd,
- 0xdd41bb36d259e000, 0x0aee5720ee830681, 0x1000000000000000, 0x172588ad4f5f0981,
- 0x211e44f7d02c1000, 0x2ee56725f06e5c71, 0x41c21cb8e1000000,
- #endif
- };
- static JSValue js_bigint_to_string1(JSContext *ctx, JSValueConst val, int radix)
- {
- if (JS_VALUE_GET_TAG(val) == JS_TAG_SHORT_BIG_INT) {
- char buf[66];
- int len;
- len = i64toa_radix(buf, JS_VALUE_GET_SHORT_BIG_INT(val), radix);
- return js_new_string8_len(ctx, buf, len);
- } else {
- JSBigInt *r, *tmp = NULL;
- char *buf, *q, *buf_end;
- int is_neg, n_bits, log2_radix, n_digits;
- BOOL is_binary_radix;
- JSValue res;
-
- assert(JS_VALUE_GET_TAG(val) == JS_TAG_BIG_INT);
- r = JS_VALUE_GET_PTR(val);
- if (r->len == 1 && r->tab[0] == 0) {
- /* '0' case */
- return js_new_string8_len(ctx, "0", 1);
- }
- is_binary_radix = ((radix & (radix - 1)) == 0);
- is_neg = js_bigint_sign(r);
- if (is_neg) {
- tmp = js_bigint_neg(ctx, r);
- if (!tmp)
- return JS_EXCEPTION;
- r = tmp;
- } else if (!is_binary_radix) {
- /* need to modify 'r' */
- tmp = js_bigint_new(ctx, r->len);
- if (!tmp)
- return JS_EXCEPTION;
- memcpy(tmp->tab, r->tab, r->len * sizeof(r->tab[0]));
- r = tmp;
- }
- log2_radix = 31 - clz32(radix); /* floor(log2(radix)) */
- n_bits = r->len * JS_LIMB_BITS - js_limb_safe_clz(r->tab[r->len - 1]);
- /* n_digits is exact only if radix is a power of
- two. Otherwise it is >= the exact number of digits */
- n_digits = (n_bits + log2_radix - 1) / log2_radix;
- /* XXX: could directly build the JSString */
- buf = js_malloc(ctx, n_digits + is_neg + 1);
- if (!buf) {
- js_free(ctx, tmp);
- return JS_EXCEPTION;
- }
- q = buf + n_digits + is_neg + 1;
- *--q = '\0';
- buf_end = q;
- if (!is_binary_radix) {
- int len;
- js_limb_t radix_base, v;
- radix_base = radix_base_table[radix - 2];
- len = r->len;
- for(;;) {
- /* remove leading zero limbs */
- while (len > 1 && r->tab[len - 1] == 0)
- len--;
- if (len == 1 && r->tab[0] < radix_base) {
- v = r->tab[0];
- if (v != 0) {
- q = js_u64toa(q, v, radix);
- }
- break;
- } else {
- v = mp_div1(r->tab, r->tab, len, radix_base, 0);
- q = limb_to_a(q, v, radix, digits_per_limb_table[radix - 2]);
- }
- }
- } else {
- int i, shift;
- unsigned int bit_pos, pos, c;
- /* radix is a power of two */
- for(i = 0; i < n_digits; i++) {
- bit_pos = i * log2_radix;
- pos = bit_pos / JS_LIMB_BITS;
- shift = bit_pos % JS_LIMB_BITS;
- c = r->tab[pos] >> shift;
- if ((shift + log2_radix) > JS_LIMB_BITS &&
- (pos + 1) < r->len) {
- c |= r->tab[pos + 1] << (JS_LIMB_BITS - shift);
- }
- c &= (radix - 1);
- *--q = digits[c];
- }
- }
- if (is_neg)
- *--q = '-';
- js_free(ctx, tmp);
- res = js_new_string8_len(ctx, q, buf_end - q);
- js_free(ctx, buf);
- return res;
- }
- }
- /* if possible transform a BigInt to short big and free it, otherwise
- return a normal bigint */
- static JSValue JS_CompactBigInt(JSContext *ctx, JSBigInt *p)
- {
- JSValue res;
- if (p->len == 1) {
- res = __JS_NewShortBigInt(ctx, (js_slimb_t)p->tab[0]);
- js_free(ctx, p);
- return res;
- } else {
- return JS_MKPTR(JS_TAG_BIG_INT, p);
- }
- }
- #define ATOD_INT_ONLY (1 << 0)
- /* accept Oo and Ob prefixes in addition to 0x prefix if radix = 0 */
- #define ATOD_ACCEPT_BIN_OCT (1 << 2)
- /* accept O prefix as octal if radix == 0 and properly formed (Annex B) */
- #define ATOD_ACCEPT_LEGACY_OCTAL (1 << 4)
- /* accept _ between digits as a digit separator */
- #define ATOD_ACCEPT_UNDERSCORES (1 << 5)
- /* allow a suffix to override the type */
- #define ATOD_ACCEPT_SUFFIX (1 << 6)
- /* default type */
- #define ATOD_TYPE_MASK (3 << 7)
- #define ATOD_TYPE_FLOAT64 (0 << 7)
- #define ATOD_TYPE_BIG_INT (1 << 7)
- /* accept -0x1 */
- #define ATOD_ACCEPT_PREFIX_AFTER_SIGN (1 << 10)
- /* return an exception in case of memory error. Return JS_NAN if
- invalid syntax */
- /* XXX: directly use js_atod() */
- static JSValue js_atof(JSContext *ctx, const char *str, const char **pp,
- int radix, int flags)
- {
- const char *p, *p_start;
- int sep, is_neg;
- BOOL is_float, has_legacy_octal;
- int atod_type = flags & ATOD_TYPE_MASK;
- char buf1[64], *buf;
- int i, j, len;
- BOOL buf_allocated = FALSE;
- JSValue val;
- JSATODTempMem atod_mem;
-
- /* optional separator between digits */
- sep = (flags & ATOD_ACCEPT_UNDERSCORES) ? '_' : 256;
- has_legacy_octal = FALSE;
- p = str;
- p_start = p;
- is_neg = 0;
- if (p[0] == '+') {
- p++;
- p_start++;
- if (!(flags & ATOD_ACCEPT_PREFIX_AFTER_SIGN))
- goto no_radix_prefix;
- } else if (p[0] == '-') {
- p++;
- p_start++;
- is_neg = 1;
- if (!(flags & ATOD_ACCEPT_PREFIX_AFTER_SIGN))
- goto no_radix_prefix;
- }
- if (p[0] == '0') {
- if ((p[1] == 'x' || p[1] == 'X') &&
- (radix == 0 || radix == 16)) {
- p += 2;
- radix = 16;
- } else if ((p[1] == 'o' || p[1] == 'O') &&
- radix == 0 && (flags & ATOD_ACCEPT_BIN_OCT)) {
- p += 2;
- radix = 8;
- } else if ((p[1] == 'b' || p[1] == 'B') &&
- radix == 0 && (flags & ATOD_ACCEPT_BIN_OCT)) {
- p += 2;
- radix = 2;
- } else if ((p[1] >= '0' && p[1] <= '9') &&
- radix == 0 && (flags & ATOD_ACCEPT_LEGACY_OCTAL)) {
- int i;
- has_legacy_octal = TRUE;
- sep = 256;
- for (i = 1; (p[i] >= '0' && p[i] <= '7'); i++)
- continue;
- if (p[i] == '8' || p[i] == '9')
- goto no_prefix;
- p += 1;
- radix = 8;
- } else {
- goto no_prefix;
- }
- /* there must be a digit after the prefix */
- if (to_digit((uint8_t)*p) >= radix)
- goto fail;
- no_prefix: ;
- } else {
- no_radix_prefix:
- if (!(flags & ATOD_INT_ONLY) &&
- (atod_type == ATOD_TYPE_FLOAT64) &&
- strstart(p, "Infinity", &p)) {
- double d = 1.0 / 0.0;
- if (is_neg)
- d = -d;
- val = JS_NewFloat64(ctx, d);
- goto done;
- }
- }
- if (radix == 0)
- radix = 10;
- is_float = FALSE;
- p_start = p;
- while (to_digit((uint8_t)*p) < radix
- || (*p == sep && (radix != 10 ||
- p != p_start + 1 || p[-1] != '0') &&
- to_digit((uint8_t)p[1]) < radix)) {
- p++;
- }
- if (!(flags & ATOD_INT_ONLY) && radix == 10) {
- if (*p == '.' && (p > p_start || to_digit((uint8_t)p[1]) < radix)) {
- is_float = TRUE;
- p++;
- if (*p == sep)
- goto fail;
- while (to_digit((uint8_t)*p) < radix ||
- (*p == sep && to_digit((uint8_t)p[1]) < radix))
- p++;
- }
- if (p > p_start && (*p == 'e' || *p == 'E')) {
- const char *p1 = p + 1;
- is_float = TRUE;
- if (*p1 == '+') {
- p1++;
- } else if (*p1 == '-') {
- p1++;
- }
- if (is_digit((uint8_t)*p1)) {
- p = p1 + 1;
- while (is_digit((uint8_t)*p) || (*p == sep && is_digit((uint8_t)p[1])))
- p++;
- }
- }
- }
- if (p == p_start)
- goto fail;
- buf = buf1;
- buf_allocated = FALSE;
- len = p - p_start;
- if (unlikely((len + 2) > sizeof(buf1))) {
- buf = js_malloc_rt(ctx->rt, len + 2); /* no exception raised */
- if (!buf)
- goto mem_error;
- buf_allocated = TRUE;
- }
- /* remove the separators and the radix prefixes */
- j = 0;
- if (is_neg)
- buf[j++] = '-';
- for (i = 0; i < len; i++) {
- if (p_start[i] != '_')
- buf[j++] = p_start[i];
- }
- buf[j] = '\0';
- if ((flags & ATOD_ACCEPT_SUFFIX) && *p == 'n') {
- p++;
- atod_type = ATOD_TYPE_BIG_INT;
- }
- switch(atod_type) {
- case ATOD_TYPE_FLOAT64:
- {
- double d;
- d = js_atod(buf, NULL, radix, is_float ? 0 : JS_ATOD_INT_ONLY,
- &atod_mem);
- /* return int or float64 */
- val = JS_NewFloat64(ctx, d);
- }
- break;
- case ATOD_TYPE_BIG_INT:
- {
- JSBigInt *r;
- if (has_legacy_octal || is_float)
- goto fail;
- r = js_bigint_from_string(ctx, buf, radix);
- if (!r) {
- val = JS_EXCEPTION;
- goto done;
- }
- val = JS_CompactBigInt(ctx, r);
- }
- break;
- default:
- abort();
- }
- done:
- if (buf_allocated)
- js_free_rt(ctx->rt, buf);
- if (pp)
- *pp = p;
- return val;
- fail:
- val = JS_NAN;
- goto done;
- mem_error:
- val = JS_ThrowOutOfMemory(ctx);
- goto done;
- }
- typedef enum JSToNumberHintEnum {
- TON_FLAG_NUMBER,
- TON_FLAG_NUMERIC,
- } JSToNumberHintEnum;
- static JSValue JS_ToNumberHintFree(JSContext *ctx, JSValue val,
- JSToNumberHintEnum flag)
- {
- uint32_t tag;
- JSValue ret;
- redo:
- tag = JS_VALUE_GET_NORM_TAG(val);
- switch(tag) {
- case JS_TAG_BIG_INT:
- case JS_TAG_SHORT_BIG_INT:
- if (flag != TON_FLAG_NUMERIC) {
- JS_FreeValue(ctx, val);
- return JS_ThrowTypeError(ctx, "cannot convert bigint to number");
- }
- ret = val;
- break;
- case JS_TAG_FLOAT64:
- case JS_TAG_INT:
- case JS_TAG_EXCEPTION:
- ret = val;
- break;
- case JS_TAG_BOOL:
- case JS_TAG_NULL:
- ret = JS_NewInt32(ctx, JS_VALUE_GET_INT(val));
- break;
- case JS_TAG_UNDEFINED:
- ret = JS_NAN;
- break;
- case JS_TAG_OBJECT:
- val = JS_ToPrimitiveFree(ctx, val, HINT_NUMBER);
- if (JS_IsException(val))
- return JS_EXCEPTION;
- goto redo;
- case JS_TAG_STRING:
- case JS_TAG_STRING_ROPE:
- {
- const char *str;
- const char *p;
- size_t len;
- str = JS_ToCStringLen(ctx, &len, val);
- JS_FreeValue(ctx, val);
- if (!str)
- return JS_EXCEPTION;
- p = str;
- p += skip_spaces(p);
- if ((p - str) == len) {
- ret = JS_NewInt32(ctx, 0);
- } else {
- int flags = ATOD_ACCEPT_BIN_OCT;
- ret = js_atof(ctx, p, &p, 0, flags);
- if (!JS_IsException(ret)) {
- p += skip_spaces(p);
- if ((p - str) != len) {
- JS_FreeValue(ctx, ret);
- ret = JS_NAN;
- }
- }
- }
- JS_FreeCString(ctx, str);
- }
- break;
- case JS_TAG_SYMBOL:
- JS_FreeValue(ctx, val);
- return JS_ThrowTypeError(ctx, "cannot convert symbol to number");
- default:
- JS_FreeValue(ctx, val);
- ret = JS_NAN;
- break;
- }
- return ret;
- }
- static JSValue JS_ToNumberFree(JSContext *ctx, JSValue val)
- {
- return JS_ToNumberHintFree(ctx, val, TON_FLAG_NUMBER);
- }
- static JSValue JS_ToNumericFree(JSContext *ctx, JSValue val)
- {
- return JS_ToNumberHintFree(ctx, val, TON_FLAG_NUMERIC);
- }
- static JSValue JS_ToNumeric(JSContext *ctx, JSValueConst val)
- {
- return JS_ToNumericFree(ctx, JS_DupValue(ctx, val));
- }
- static __exception int __JS_ToFloat64Free(JSContext *ctx, double *pres,
- JSValue val)
- {
- double d;
- uint32_t tag;
-
- val = JS_ToNumberFree(ctx, val);
- if (JS_IsException(val))
- goto fail;
- tag = JS_VALUE_GET_NORM_TAG(val);
- switch(tag) {
- case JS_TAG_INT:
- d = JS_VALUE_GET_INT(val);
- break;
- case JS_TAG_FLOAT64:
- d = JS_VALUE_GET_FLOAT64(val);
- break;
- default:
- abort();
- }
- *pres = d;
- return 0;
- fail:
- *pres = JS_FLOAT64_NAN;
- return -1;
- }
- static inline int JS_ToFloat64Free(JSContext *ctx, double *pres, JSValue val)
- {
- uint32_t tag;
- tag = JS_VALUE_GET_TAG(val);
- if (tag <= JS_TAG_NULL) {
- *pres = JS_VALUE_GET_INT(val);
- return 0;
- } else if (JS_TAG_IS_FLOAT64(tag)) {
- *pres = JS_VALUE_GET_FLOAT64(val);
- return 0;
- } else {
- return __JS_ToFloat64Free(ctx, pres, val);
- }
- }
- int JS_ToFloat64(JSContext *ctx, double *pres, JSValueConst val)
- {
- return JS_ToFloat64Free(ctx, pres, JS_DupValue(ctx, val));
- }
- static JSValue JS_ToNumber(JSContext *ctx, JSValueConst val)
- {
- return JS_ToNumberFree(ctx, JS_DupValue(ctx, val));
- }
- /* same as JS_ToNumber() but return 0 in case of NaN/Undefined */
- static __maybe_unused JSValue JS_ToIntegerFree(JSContext *ctx, JSValue val)
- {
- uint32_t tag;
- JSValue ret;
- redo:
- tag = JS_VALUE_GET_NORM_TAG(val);
- switch(tag) {
- case JS_TAG_INT:
- case JS_TAG_BOOL:
- case JS_TAG_NULL:
- case JS_TAG_UNDEFINED:
- ret = JS_NewInt32(ctx, JS_VALUE_GET_INT(val));
- break;
- case JS_TAG_FLOAT64:
- {
- double d = JS_VALUE_GET_FLOAT64(val);
- if (isnan(d)) {
- ret = JS_NewInt32(ctx, 0);
- } else {
- /* convert -0 to +0 */
- d = trunc(d) + 0.0;
- ret = JS_NewFloat64(ctx, d);
- }
- }
- break;
- default:
- val = JS_ToNumberFree(ctx, val);
- if (JS_IsException(val))
- return val;
- goto redo;
- }
- return ret;
- }
- /* Note: the integer value is satured to 32 bits */
- static int JS_ToInt32SatFree(JSContext *ctx, int *pres, JSValue val)
- {
- uint32_t tag;
- int ret;
- redo:
- tag = JS_VALUE_GET_NORM_TAG(val);
- switch(tag) {
- case JS_TAG_INT:
- case JS_TAG_BOOL:
- case JS_TAG_NULL:
- case JS_TAG_UNDEFINED:
- ret = JS_VALUE_GET_INT(val);
- break;
- case JS_TAG_EXCEPTION:
- *pres = 0;
- return -1;
- case JS_TAG_FLOAT64:
- {
- double d = JS_VALUE_GET_FLOAT64(val);
- if (isnan(d)) {
- ret = 0;
- } else {
- if (d < INT32_MIN)
- ret = INT32_MIN;
- else if (d > INT32_MAX)
- ret = INT32_MAX;
- else
- ret = (int)d;
- }
- }
- break;
- default:
- val = JS_ToNumberFree(ctx, val);
- if (JS_IsException(val)) {
- *pres = 0;
- return -1;
- }
- goto redo;
- }
- *pres = ret;
- return 0;
- }
- int JS_ToInt32Sat(JSContext *ctx, int *pres, JSValueConst val)
- {
- return JS_ToInt32SatFree(ctx, pres, JS_DupValue(ctx, val));
- }
- int JS_ToInt32Clamp(JSContext *ctx, int *pres, JSValueConst val,
- int min, int max, int min_offset)
- {
- int res = JS_ToInt32SatFree(ctx, pres, JS_DupValue(ctx, val));
- if (res == 0) {
- if (*pres < min) {
- *pres += min_offset;
- if (*pres < min)
- *pres = min;
- } else {
- if (*pres > max)
- *pres = max;
- }
- }
- return res;
- }
- static int JS_ToInt64SatFree(JSContext *ctx, int64_t *pres, JSValue val)
- {
- uint32_t tag;
- redo:
- tag = JS_VALUE_GET_NORM_TAG(val);
- switch(tag) {
- case JS_TAG_INT:
- case JS_TAG_BOOL:
- case JS_TAG_NULL:
- case JS_TAG_UNDEFINED:
- *pres = JS_VALUE_GET_INT(val);
- return 0;
- case JS_TAG_EXCEPTION:
- *pres = 0;
- return -1;
- case JS_TAG_FLOAT64:
- {
- double d = JS_VALUE_GET_FLOAT64(val);
- if (isnan(d)) {
- *pres = 0;
- } else {
- if (d < INT64_MIN)
- *pres = INT64_MIN;
- else if (d >= 0x1p63) /* must use INT64_MAX + 1 because INT64_MAX cannot be exactly represented as a double */
- *pres = INT64_MAX;
- else
- *pres = (int64_t)d;
- }
- }
- return 0;
- default:
- val = JS_ToNumberFree(ctx, val);
- if (JS_IsException(val)) {
- *pres = 0;
- return -1;
- }
- goto redo;
- }
- }
- int JS_ToInt64Sat(JSContext *ctx, int64_t *pres, JSValueConst val)
- {
- return JS_ToInt64SatFree(ctx, pres, JS_DupValue(ctx, val));
- }
- int JS_ToInt64Clamp(JSContext *ctx, int64_t *pres, JSValueConst val,
- int64_t min, int64_t max, int64_t neg_offset)
- {
- int res = JS_ToInt64SatFree(ctx, pres, JS_DupValue(ctx, val));
- if (res == 0) {
- if (*pres < 0)
- *pres += neg_offset;
- if (*pres < min)
- *pres = min;
- else if (*pres > max)
- *pres = max;
- }
- return res;
- }
- /* Same as JS_ToInt32Free() but with a 64 bit result. Return (<0, 0)
- in case of exception */
- static int JS_ToInt64Free(JSContext *ctx, int64_t *pres, JSValue val)
- {
- uint32_t tag;
- int64_t ret;
- redo:
- tag = JS_VALUE_GET_NORM_TAG(val);
- switch(tag) {
- case JS_TAG_INT:
- case JS_TAG_BOOL:
- case JS_TAG_NULL:
- case JS_TAG_UNDEFINED:
- ret = JS_VALUE_GET_INT(val);
- break;
- case JS_TAG_FLOAT64:
- {
- JSFloat64Union u;
- double d;
- int e;
- d = JS_VALUE_GET_FLOAT64(val);
- u.d = d;
- /* we avoid doing fmod(x, 2^64) */
- e = (u.u64 >> 52) & 0x7ff;
- if (likely(e <= (1023 + 62))) {
- /* fast case */
- ret = (int64_t)d;
- } else if (e <= (1023 + 62 + 53)) {
- uint64_t v;
- /* remainder modulo 2^64 */
- v = (u.u64 & (((uint64_t)1 << 52) - 1)) | ((uint64_t)1 << 52);
- ret = v << ((e - 1023) - 52);
- /* take the sign into account */
- if (u.u64 >> 63)
- ret = -ret;
- } else {
- ret = 0; /* also handles NaN and +inf */
- }
- }
- break;
- default:
- val = JS_ToNumberFree(ctx, val);
- if (JS_IsException(val)) {
- *pres = 0;
- return -1;
- }
- goto redo;
- }
- *pres = ret;
- return 0;
- }
- int JS_ToInt64(JSContext *ctx, int64_t *pres, JSValueConst val)
- {
- return JS_ToInt64Free(ctx, pres, JS_DupValue(ctx, val));
- }
- int JS_ToInt64Ext(JSContext *ctx, int64_t *pres, JSValueConst val)
- {
- if (JS_IsBigInt(ctx, val))
- return JS_ToBigInt64(ctx, pres, val);
- else
- return JS_ToInt64(ctx, pres, val);
- }
- /* return (<0, 0) in case of exception */
- static int JS_ToInt32Free(JSContext *ctx, int32_t *pres, JSValue val)
- {
- uint32_t tag;
- int32_t ret;
- redo:
- tag = JS_VALUE_GET_NORM_TAG(val);
- switch(tag) {
- case JS_TAG_INT:
- case JS_TAG_BOOL:
- case JS_TAG_NULL:
- case JS_TAG_UNDEFINED:
- ret = JS_VALUE_GET_INT(val);
- break;
- case JS_TAG_FLOAT64:
- {
- JSFloat64Union u;
- double d;
- int e;
- d = JS_VALUE_GET_FLOAT64(val);
- u.d = d;
- /* we avoid doing fmod(x, 2^32) */
- e = (u.u64 >> 52) & 0x7ff;
- if (likely(e <= (1023 + 30))) {
- /* fast case */
- ret = (int32_t)d;
- } else if (e <= (1023 + 30 + 53)) {
- uint64_t v;
- /* remainder modulo 2^32 */
- v = (u.u64 & (((uint64_t)1 << 52) - 1)) | ((uint64_t)1 << 52);
- v = v << ((e - 1023) - 52 + 32);
- ret = v >> 32;
- /* take the sign into account */
- if (u.u64 >> 63)
- ret = -ret;
- } else {
- ret = 0; /* also handles NaN and +inf */
- }
- }
- break;
- default:
- val = JS_ToNumberFree(ctx, val);
- if (JS_IsException(val)) {
- *pres = 0;
- return -1;
- }
- goto redo;
- }
- *pres = ret;
- return 0;
- }
- int JS_ToInt32(JSContext *ctx, int32_t *pres, JSValueConst val)
- {
- return JS_ToInt32Free(ctx, pres, JS_DupValue(ctx, val));
- }
- static inline int JS_ToUint32Free(JSContext *ctx, uint32_t *pres, JSValue val)
- {
- return JS_ToInt32Free(ctx, (int32_t *)pres, val);
- }
- static int JS_ToUint8ClampFree(JSContext *ctx, int32_t *pres, JSValue val)
- {
- uint32_t tag;
- int res;
- redo:
- tag = JS_VALUE_GET_NORM_TAG(val);
- switch(tag) {
- case JS_TAG_INT:
- case JS_TAG_BOOL:
- case JS_TAG_NULL:
- case JS_TAG_UNDEFINED:
- res = JS_VALUE_GET_INT(val);
- res = max_int(0, min_int(255, res));
- break;
- case JS_TAG_FLOAT64:
- {
- double d = JS_VALUE_GET_FLOAT64(val);
- if (isnan(d)) {
- res = 0;
- } else {
- if (d < 0)
- res = 0;
- else if (d > 255)
- res = 255;
- else
- res = lrint(d);
- }
- }
- break;
- default:
- val = JS_ToNumberFree(ctx, val);
- if (JS_IsException(val)) {
- *pres = 0;
- return -1;
- }
- goto redo;
- }
- *pres = res;
- return 0;
- }
- static __exception int JS_ToArrayLengthFree(JSContext *ctx, uint32_t *plen,
- JSValue val, BOOL is_array_ctor)
- {
- uint32_t tag, len;
- tag = JS_VALUE_GET_TAG(val);
- switch(tag) {
- case JS_TAG_INT:
- case JS_TAG_BOOL:
- case JS_TAG_NULL:
- {
- int v;
- v = JS_VALUE_GET_INT(val);
- if (v < 0)
- goto fail;
- len = v;
- }
- break;
- default:
- if (JS_TAG_IS_FLOAT64(tag)) {
- double d;
- d = JS_VALUE_GET_FLOAT64(val);
- if (!(d >= 0 && d <= UINT32_MAX))
- goto fail;
- len = (uint32_t)d;
- if (len != d)
- goto fail;
- } else {
- uint32_t len1;
- if (is_array_ctor) {
- val = JS_ToNumberFree(ctx, val);
- if (JS_IsException(val))
- return -1;
- /* cannot recurse because val is a number */
- if (JS_ToArrayLengthFree(ctx, &len, val, TRUE))
- return -1;
- } else {
- /* legacy behavior: must do the conversion twice and compare */
- if (JS_ToUint32(ctx, &len, val)) {
- JS_FreeValue(ctx, val);
- return -1;
- }
- val = JS_ToNumberFree(ctx, val);
- if (JS_IsException(val))
- return -1;
- /* cannot recurse because val is a number */
- if (JS_ToArrayLengthFree(ctx, &len1, val, FALSE))
- return -1;
- if (len1 != len) {
- fail:
- JS_ThrowRangeError(ctx, "invalid array length");
- return -1;
- }
- }
- }
- break;
- }
- *plen = len;
- return 0;
- }
- #define MAX_SAFE_INTEGER (((int64_t)1 << 53) - 1)
- static BOOL is_safe_integer(double d)
- {
- return isfinite(d) && floor(d) == d &&
- fabs(d) <= (double)MAX_SAFE_INTEGER;
- }
- int JS_ToIndex(JSContext *ctx, uint64_t *plen, JSValueConst val)
- {
- int64_t v;
- if (JS_ToInt64Sat(ctx, &v, val))
- return -1;
- if (v < 0 || v > MAX_SAFE_INTEGER) {
- JS_ThrowRangeError(ctx, "invalid array index");
- *plen = 0;
- return -1;
- }
- *plen = v;
- return 0;
- }
- /* convert a value to a length between 0 and MAX_SAFE_INTEGER.
- return -1 for exception */
- static __exception int JS_ToLengthFree(JSContext *ctx, int64_t *plen,
- JSValue val)
- {
- int res = JS_ToInt64Clamp(ctx, plen, val, 0, MAX_SAFE_INTEGER, 0);
- JS_FreeValue(ctx, val);
- return res;
- }
- /* Note: can return an exception */
- static int JS_NumberIsInteger(JSContext *ctx, JSValueConst val)
- {
- double d;
- if (!JS_IsNumber(val))
- return FALSE;
- if (unlikely(JS_ToFloat64(ctx, &d, val)))
- return -1;
- return isfinite(d) && floor(d) == d;
- }
- static BOOL JS_NumberIsNegativeOrMinusZero(JSContext *ctx, JSValueConst val)
- {
- uint32_t tag;
- tag = JS_VALUE_GET_NORM_TAG(val);
- switch(tag) {
- case JS_TAG_INT:
- {
- int v;
- v = JS_VALUE_GET_INT(val);
- return (v < 0);
- }
- case JS_TAG_FLOAT64:
- {
- JSFloat64Union u;
- u.d = JS_VALUE_GET_FLOAT64(val);
- return (u.u64 >> 63);
- }
- case JS_TAG_SHORT_BIG_INT:
- return (JS_VALUE_GET_SHORT_BIG_INT(val) < 0);
- case JS_TAG_BIG_INT:
- {
- JSBigInt *p = JS_VALUE_GET_PTR(val);
- return js_bigint_sign(p);
- }
- default:
- return FALSE;
- }
- }
- static JSValue js_bigint_to_string(JSContext *ctx, JSValueConst val)
- {
- return js_bigint_to_string1(ctx, val, 10);
- }
- static JSValue js_dtoa2(JSContext *ctx,
- double d, int radix, int n_digits, int flags)
- {
- char static_buf[128], *buf, *tmp_buf;
- int len, len_max;
- JSValue res;
- JSDTOATempMem dtoa_mem;
- len_max = js_dtoa_max_len(d, radix, n_digits, flags);
-
- /* longer buffer may be used if radix != 10 */
- if (len_max > sizeof(static_buf) - 1) {
- tmp_buf = js_malloc(ctx, len_max + 1);
- if (!tmp_buf)
- return JS_EXCEPTION;
- buf = tmp_buf;
- } else {
- tmp_buf = NULL;
- buf = static_buf;
- }
- len = js_dtoa(buf, d, radix, n_digits, flags, &dtoa_mem);
- res = js_new_string8_len(ctx, buf, len);
- js_free(ctx, tmp_buf);
- return res;
- }
- static JSValue JS_ToStringInternal(JSContext *ctx, JSValueConst val, BOOL is_ToPropertyKey)
- {
- uint32_t tag;
- char buf[32];
- tag = JS_VALUE_GET_NORM_TAG(val);
- switch(tag) {
- case JS_TAG_STRING:
- return JS_DupValue(ctx, val);
- case JS_TAG_STRING_ROPE:
- return js_linearize_string_rope(ctx, JS_DupValue(ctx, val));
- case JS_TAG_INT:
- {
- size_t len;
- len = i32toa(buf, JS_VALUE_GET_INT(val));
- return js_new_string8_len(ctx, buf, len);
- }
- break;
- case JS_TAG_BOOL:
- return JS_AtomToString(ctx, JS_VALUE_GET_BOOL(val) ?
- JS_ATOM_true : JS_ATOM_false);
- case JS_TAG_NULL:
- return JS_AtomToString(ctx, JS_ATOM_null);
- case JS_TAG_UNDEFINED:
- return JS_AtomToString(ctx, JS_ATOM_undefined);
- case JS_TAG_EXCEPTION:
- return JS_EXCEPTION;
- case JS_TAG_OBJECT:
- {
- JSValue val1, ret;
- val1 = JS_ToPrimitive(ctx, val, HINT_STRING);
- if (JS_IsException(val1))
- return val1;
- ret = JS_ToStringInternal(ctx, val1, is_ToPropertyKey);
- JS_FreeValue(ctx, val1);
- return ret;
- }
- break;
- case JS_TAG_FUNCTION_BYTECODE:
- return js_new_string8(ctx, "[function bytecode]");
- case JS_TAG_SYMBOL:
- if (is_ToPropertyKey) {
- return JS_DupValue(ctx, val);
- } else {
- return JS_ThrowTypeError(ctx, "cannot convert symbol to string");
- }
- case JS_TAG_FLOAT64:
- return js_dtoa2(ctx, JS_VALUE_GET_FLOAT64(val), 10, 0,
- JS_DTOA_FORMAT_FREE);
- case JS_TAG_SHORT_BIG_INT:
- case JS_TAG_BIG_INT:
- return js_bigint_to_string(ctx, val);
- default:
- return js_new_string8(ctx, "[unsupported type]");
- }
- }
- JSValue JS_ToString(JSContext *ctx, JSValueConst val)
- {
- return JS_ToStringInternal(ctx, val, FALSE);
- }
- static JSValue JS_ToStringFree(JSContext *ctx, JSValue val)
- {
- JSValue ret;
- ret = JS_ToString(ctx, val);
- JS_FreeValue(ctx, val);
- return ret;
- }
- static JSValue JS_ToLocaleStringFree(JSContext *ctx, JSValue val)
- {
- if (JS_IsUndefined(val) || JS_IsNull(val))
- return JS_ToStringFree(ctx, val);
- return JS_InvokeFree(ctx, val, JS_ATOM_toLocaleString, 0, NULL);
- }
- JSValue JS_ToPropertyKey(JSContext *ctx, JSValueConst val)
- {
- return JS_ToStringInternal(ctx, val, TRUE);
- }
- static JSValue JS_ToStringCheckObject(JSContext *ctx, JSValueConst val)
- {
- uint32_t tag = JS_VALUE_GET_TAG(val);
- if (tag == JS_TAG_NULL || tag == JS_TAG_UNDEFINED)
- return JS_ThrowTypeError(ctx, "null or undefined are forbidden");
- return JS_ToString(ctx, val);
- }
- #define JS_PRINT_MAX_DEPTH 8
- typedef struct {
- JSRuntime *rt;
- JSContext *ctx; /* may be NULL */
- JSPrintValueOptions options;
- JSPrintValueWrite *write_func;
- void *write_opaque;
- int level;
- JSObject *print_stack[JS_PRINT_MAX_DEPTH]; /* level values */
- } JSPrintValueState;
- static void js_print_value(JSPrintValueState *s, JSValueConst val);
- static void js_putc(JSPrintValueState *s, char c)
- {
- s->write_func(s->write_opaque, &c, 1);
- }
- static void js_puts(JSPrintValueState *s, const char *str)
- {
- s->write_func(s->write_opaque, str, strlen(str));
- }
- static void __attribute__((format(printf, 2, 3))) js_printf(JSPrintValueState *s, const char *fmt, ...)
- {
- va_list ap;
- char buf[256];
-
- va_start(ap, fmt);
- vsnprintf(buf, sizeof(buf), fmt, ap);
- va_end(ap);
- s->write_func(s->write_opaque, buf, strlen(buf));
- }
- static void js_print_float64(JSPrintValueState *s, double d)
- {
- JSDTOATempMem dtoa_mem;
- char buf[32];
- int len;
- len = js_dtoa(buf, d, 10, 0, JS_DTOA_FORMAT_FREE | JS_DTOA_MINUS_ZERO, &dtoa_mem);
- s->write_func(s->write_opaque, buf, len);
- }
- static uint32_t js_string_get_length(JSValueConst val)
- {
- if (JS_VALUE_GET_TAG(val) == JS_TAG_STRING) {
- JSString *p = JS_VALUE_GET_STRING(val);
- return p->len;
- } else if (JS_VALUE_GET_TAG(val) == JS_TAG_STRING_ROPE) {
- JSStringRope *r = JS_VALUE_GET_PTR(val);
- return r->len;
- } else {
- return 0;
- }
- }
- /* pretty print the first 'len' characters of 'p' */
- static void js_print_string1(JSPrintValueState *s, JSString *p, int len, int sep)
- {
- uint8_t buf[UTF8_CHAR_LEN_MAX];
- int l, i, c, c1;
- for(i = 0; i < len; i++) {
- c = string_get(p, i);
- switch(c) {
- case '\t':
- c = 't';
- goto quote;
- case '\r':
- c = 'r';
- goto quote;
- case '\n':
- c = 'n';
- goto quote;
- case '\b':
- c = 'b';
- goto quote;
- case '\f':
- c = 'f';
- goto quote;
- case '\\':
- quote:
- js_putc(s, '\\');
- js_putc(s, c);
- break;
- default:
- if (c == sep)
- goto quote;
- if (c >= 32 && c <= 126) {
- js_putc(s, c);
- } else if (c < 32 ||
- (c >= 0x7f && c <= 0x9f)) {
- escape:
- js_printf(s, "\\u%04x", c);
- } else {
- if (is_hi_surrogate(c)) {
- if ((i + 1) >= len)
- goto escape;
- c1 = string_get(p, i + 1);
- if (!is_lo_surrogate(c1))
- goto escape;
- i++;
- c = from_surrogate(c, c1);
- } else if (is_lo_surrogate(c)) {
- goto escape;
- }
- l = unicode_to_utf8(buf, c);
- s->write_func(s->write_opaque, (char *)buf, l);
- }
- break;
- }
- }
- }
- static void js_print_string_rec(JSPrintValueState *s, JSValueConst val,
- int sep, uint32_t pos)
- {
- if (JS_VALUE_GET_TAG(val) == JS_TAG_STRING) {
- JSString *p = JS_VALUE_GET_STRING(val);
- uint32_t len;
- if (pos < s->options.max_string_length) {
- len = min_uint32(p->len, s->options.max_string_length - pos);
- js_print_string1(s, p, len, sep);
- }
- } else if (JS_VALUE_GET_TAG(val) == JS_TAG_STRING_ROPE) {
- JSStringRope *r = JS_VALUE_GET_PTR(val);
- js_print_string_rec(s, r->left, sep, pos);
- js_print_string_rec(s, r->right, sep, pos + js_string_get_length(r->left));
- } else {
- js_printf(s, "<invalid string tag %d>", (int)JS_VALUE_GET_TAG(val));
- }
- }
- static void js_print_string(JSPrintValueState *s, JSValueConst val)
- {
- int sep;
- if (s->options.raw_dump && JS_VALUE_GET_TAG(val) == JS_TAG_STRING) {
- JSString *p = JS_VALUE_GET_STRING(val);
- js_printf(s, "%d", js_rc(p)->ref_count);
- sep = (js_rc(p)->ref_count == 1) ? '\"' : '\'';
- } else {
- sep = '\"';
- }
- js_putc(s, sep);
- js_print_string_rec(s, val, sep, 0);
- js_putc(s, sep);
- if (js_string_get_length(val) > s->options.max_string_length) {
- uint32_t n = js_string_get_length(val) - s->options.max_string_length;
- js_printf(s, "... %u more character%s", n, n > 1 ? "s" : "");
- }
- }
- static void js_print_raw_string(JSPrintValueState *s, JSValueConst val)
- {
- const char *cstr;
- size_t len;
- cstr = JS_ToCStringLen(s->ctx, &len, val);
- if (cstr) {
- s->write_func(s->write_opaque, cstr, len);
- JS_FreeCString(s->ctx, cstr);
- }
- }
- static BOOL is_ascii_ident(const JSString *p)
- {
- int i, c;
- if (p->len == 0)
- return FALSE;
- for(i = 0; i < p->len; i++) {
- c = string_get(p, i);
- if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
- (c == '_' || c == '$') || (c >= '0' && c <= '9' && i > 0)))
- return FALSE;
- }
- return TRUE;
- }
- static void js_print_atom(JSPrintValueState *s, JSAtom atom)
- {
- int i;
- if (__JS_AtomIsTaggedInt(atom)) {
- js_printf(s, "%u", __JS_AtomToUInt32(atom));
- } else if (atom == JS_ATOM_NULL) {
- js_puts(s, "<null>");
- } else {
- assert(atom < s->rt->atom_size);
- JSString *p;
- p = s->rt->atom_array[atom];
- if (is_ascii_ident(p)) {
- for(i = 0; i < p->len; i++) {
- js_putc(s, string_get(p, i));
- }
- } else {
- js_putc(s, '"');
- js_print_string1(s, p, p->len, '\"');
- js_putc(s, '"');
- }
- }
- }
- /* return 0 if invalid length */
- static uint32_t js_print_array_get_length(JSObject *p)
- {
- JSProperty *pr;
- JSShapeProperty *prs;
- JSValueConst val;
- prs = find_own_property(&pr, p, JS_ATOM_length);
- if (!prs)
- return 0;
- if ((prs->flags & JS_PROP_TMASK) != JS_PROP_NORMAL)
- return 0;
- val = pr->u.value;
- switch(JS_VALUE_GET_NORM_TAG(val)) {
- case JS_TAG_INT:
- return JS_VALUE_GET_INT(val);
- case JS_TAG_FLOAT64:
- return (uint32_t)JS_VALUE_GET_FLOAT64(val);
- default:
- return 0;
- }
- }
- static void js_print_comma(JSPrintValueState *s, int *pcomma_state)
- {
- switch(*pcomma_state) {
- case 0:
- break;
- case 1:
- js_printf(s, ", ");
- break;
- case 2:
- js_printf(s, " { ");
- break;
- }
- *pcomma_state = 1;
- }
- static void js_print_more_items(JSPrintValueState *s, int *pcomma_state,
- uint32_t n)
- {
- js_print_comma(s, pcomma_state);
- js_printf(s, "... %u more item%s", n, n > 1 ? "s" : "");
- }
- /* similar to js_regexp_toString() but without side effect */
- static void js_print_regexp(JSPrintValueState *s, JSObject *p1)
- {
- JSRegExp *re = &p1->u.regexp;
- JSString *p;
- int i, n, c, c2, bra, flags;
- static const char regexp_flags[] = { 'g', 'i', 'm', 's', 'u', 'y', 'd', 'v' };
- if (!re->pattern || !re->bytecode) {
- /* the regexp fields are zeroed at init */
- js_puts(s, "[uninitialized_regexp]");
- return;
- }
- p = re->pattern;
- js_putc(s, '/');
- if (p->len == 0) {
- js_puts(s, "(?:)");
- } else {
- bra = 0;
- for (i = 0, n = p->len; i < n;) {
- c2 = -1;
- switch (c = string_get(p, i++)) {
- case '\\':
- if (i < n)
- c2 = string_get(p, i++);
- break;
- case ']':
- bra = 0;
- break;
- case '[':
- if (!bra) {
- if (i < n && string_get(p, i) == ']')
- c2 = string_get(p, i++);
- bra = 1;
- }
- break;
- case '\n':
- c = '\\';
- c2 = 'n';
- break;
- case '\r':
- c = '\\';
- c2 = 'r';
- break;
- case '/':
- if (!bra) {
- c = '\\';
- c2 = '/';
- }
- break;
- }
- js_putc(s, c);
- if (c2 >= 0)
- js_putc(s, c2);
- }
- }
- js_putc(s, '/');
- flags = lre_get_flags(re->bytecode->u.str8);
- for(i = 0; i < countof(regexp_flags); i++) {
- if ((flags >> i) & 1) {
- js_putc(s, regexp_flags[i]);
- }
- }
- }
- /* similar to js_error_toString() but without side effect */
- static void js_print_error(JSPrintValueState *s, JSObject *p)
- {
- const char *str;
- size_t len;
- str = get_prop_string(s->ctx, JS_MKPTR(JS_TAG_OBJECT, p), JS_ATOM_name);
- if (!str) {
- js_puts(s, "Error");
- } else {
- js_puts(s, str);
- JS_FreeCString(s->ctx, str);
- }
-
- str = get_prop_string(s->ctx, JS_MKPTR(JS_TAG_OBJECT, p), JS_ATOM_message);
- if (str && str[0] != '\0') {
- js_puts(s, ": ");
- js_puts(s, str);
- }
- JS_FreeCString(s->ctx, str);
- /* dump the stack if present */
- str = get_prop_string(s->ctx, JS_MKPTR(JS_TAG_OBJECT, p), JS_ATOM_stack);
- if (str) {
- js_putc(s, '\n');
-
- /* XXX: should remove the last '\n' in stack as
- v8. SpiderMonkey does not do it */
- len = strlen(str);
- if (len > 0 && str[len - 1] == '\n')
- len--;
- s->write_func(s->write_opaque, str, len);
-
- JS_FreeCString(s->ctx, str);
- }
- }
- static void js_print_object(JSPrintValueState *s, JSObject *p)
- {
- JSRuntime *rt = s->rt;
- JSShape *sh;
- JSShapeProperty *prs;
- JSProperty *pr;
- int comma_state;
- BOOL is_array;
- uint32_t i;
-
- comma_state = 0;
- is_array = FALSE;
- if (p->class_id == JS_CLASS_ARRAY) {
- is_array = TRUE;
- js_printf(s, "[ ");
- /* XXX: print array like properties even if not fast array */
- if (p->fast_array) {
- uint32_t len, n, len1;
- len = js_print_array_get_length(p);
- len1 = min_uint32(p->u.array.count, s->options.max_item_count);
- for(i = 0; i < len1; i++) {
- js_print_comma(s, &comma_state);
- js_print_value(s, p->u.array.u.values[i]);
- }
- if (len1 < p->u.array.count)
- js_print_more_items(s, &comma_state, p->u.array.count - len1);
- if (p->u.array.count < len) {
- n = len - p->u.array.count;
- js_print_comma(s, &comma_state);
- js_printf(s, "<%u empty item%s>", n, n > 1 ? "s" : "");
- }
- }
- } else if (p->class_id >= JS_CLASS_UINT8C_ARRAY && p->class_id <= JS_CLASS_FLOAT64_ARRAY) {
- uint32_t size = 1 << typed_array_size_log2(p->class_id);
- uint32_t len1;
- int64_t v;
- js_print_atom(s, rt->class_array[p->class_id].class_name);
- js_printf(s, "(%u) [ ", p->u.array.count);
-
- is_array = TRUE;
- len1 = min_uint32(p->u.array.count, s->options.max_item_count);
- for(i = 0; i < len1; i++) {
- const uint8_t *ptr = p->u.array.u.uint8_ptr + i * size;
- js_print_comma(s, &comma_state);
- switch(p->class_id) {
- case JS_CLASS_UINT8C_ARRAY:
- case JS_CLASS_UINT8_ARRAY:
- v = *ptr;
- goto ta_int64;
- case JS_CLASS_INT8_ARRAY:
- v = *(int8_t *)ptr;
- goto ta_int64;
- case JS_CLASS_INT16_ARRAY:
- v = *(int16_t *)ptr;
- goto ta_int64;
- case JS_CLASS_UINT16_ARRAY:
- v = *(uint16_t *)ptr;
- goto ta_int64;
- case JS_CLASS_INT32_ARRAY:
- v = *(int32_t *)ptr;
- goto ta_int64;
- case JS_CLASS_UINT32_ARRAY:
- v = *(uint32_t *)ptr;
- goto ta_int64;
- case JS_CLASS_BIG_INT64_ARRAY:
- v = *(int64_t *)ptr;
- ta_int64:
- js_printf(s, "%" PRId64, v);
- break;
- case JS_CLASS_BIG_UINT64_ARRAY:
- js_printf(s, "%" PRIu64, *(uint64_t *)ptr);
- break;
- case JS_CLASS_FLOAT16_ARRAY:
- js_print_float64(s, fromfp16(*(uint16_t *)ptr));
- break;
- case JS_CLASS_FLOAT32_ARRAY:
- js_print_float64(s, *(float *)ptr);
- break;
- case JS_CLASS_FLOAT64_ARRAY:
- js_print_float64(s, *(double *)ptr);
- break;
- }
- }
- if (len1 < p->u.array.count)
- js_print_more_items(s, &comma_state, p->u.array.count - len1);
- } else if (p->class_id == JS_CLASS_BYTECODE_FUNCTION ||
- (rt->class_array[p->class_id].call != NULL &&
- p->class_id != JS_CLASS_PROXY)) {
- js_printf(s, "[Function");
- /* XXX: allow dump without ctx */
- if (!s->options.raw_dump && s->ctx) {
- const char *func_name_str;
- js_putc(s, ' ');
- func_name_str = get_prop_string(s->ctx, JS_MKPTR(JS_TAG_OBJECT, p), JS_ATOM_name);
- if (!func_name_str || func_name_str[0] == '\0')
- js_puts(s, "(anonymous)");
- else
- js_puts(s, func_name_str);
- JS_FreeCString(s->ctx, func_name_str);
- }
- js_printf(s, "]");
- comma_state = 2;
- } else if (p->class_id == JS_CLASS_MAP || p->class_id == JS_CLASS_SET) {
- JSMapState *ms = p->u.opaque;
- struct list_head *el;
-
- if (!ms)
- goto default_obj;
- js_print_atom(s, rt->class_array[p->class_id].class_name);
- js_printf(s, "(%u) { ", ms->record_count);
- i = 0;
- list_for_each(el, &ms->records) {
- JSMapRecord *mr = list_entry(el, JSMapRecord, link);
- js_print_comma(s, &comma_state);
- if (mr->empty)
- continue;
- js_print_value(s, mr->key);
- if (p->class_id == JS_CLASS_MAP) {
- js_printf(s, " => ");
- js_print_value(s, mr->value);
- }
- i++;
- if (i >= s->options.max_item_count)
- break;
- }
- if (i < ms->record_count)
- js_print_more_items(s, &comma_state, ms->record_count - i);
- } else if (p->class_id == JS_CLASS_REGEXP && s->ctx) {
- js_print_regexp(s, p);
- comma_state = 2;
- } else if (p->class_id == JS_CLASS_DATE && s->ctx) {
- /* get_date_string() has no side effect */
- JSValue str = get_date_string(s->ctx, JS_MKPTR(JS_TAG_OBJECT, p), 0, NULL, 0x23); /* toISOString() */
- if (JS_IsException(str))
- goto default_obj;
- js_print_raw_string(s, str);
- JS_FreeValueRT(s->rt, str);
- comma_state = 2;
- } else if (p->class_id == JS_CLASS_ERROR && s->ctx) {
- js_print_error(s, p);
- comma_state = 2;
- } else {
- default_obj:
- if (p->class_id != JS_CLASS_OBJECT) {
- js_print_atom(s, rt->class_array[p->class_id].class_name);
- js_printf(s, " ");
- }
- js_printf(s, "{ ");
- }
-
- sh = p->shape; /* the shape can be NULL while freeing an object */
- if (sh) {
- uint32_t j;
-
- j = 0;
- for(i = 0, prs = get_shape_prop(sh); i < sh->prop_count; i++, prs++) {
- if (prs->atom != JS_ATOM_NULL) {
- if (!(prs->flags & JS_PROP_ENUMERABLE) &&
- !s->options.show_hidden) {
- continue;
- }
- if (j < s->options.max_item_count) {
- pr = &p->prop[i];
- js_print_comma(s, &comma_state);
- js_print_atom(s, prs->atom);
- js_printf(s, ": ");
-
- /* XXX: autoinit property */
- if ((prs->flags & JS_PROP_TMASK) == JS_PROP_GETSET) {
- if (s->options.raw_dump) {
- js_printf(s, "[Getter %p Setter %p]",
- pr->u.getset.getter, pr->u.getset.setter);
- } else {
- if (pr->u.getset.getter && pr->u.getset.setter) {
- js_printf(s, "[Getter/Setter]");
- } else if (pr->u.getset.setter) {
- js_printf(s, "[Setter]");
- } else {
- js_printf(s, "[Getter]");
- }
- }
- } else if ((prs->flags & JS_PROP_TMASK) == JS_PROP_VARREF) {
- if (s->options.raw_dump) {
- js_printf(s, "[varref %p]", (void *)pr->u.var_ref);
- } else {
- js_print_value(s, *pr->u.var_ref->pvalue);
- }
- } else if ((prs->flags & JS_PROP_TMASK) == JS_PROP_AUTOINIT) {
- if (s->options.raw_dump) {
- js_printf(s, "[autoinit %p %d %p]",
- (void *)js_autoinit_get_realm(pr),
- js_autoinit_get_id(pr),
- (void *)pr->u.init.opaque);
- } else {
- /* XXX: could autoinit but need to restart
- the iteration */
- js_printf(s, "[autoinit]");
- }
- } else {
- js_print_value(s, pr->u.value);
- }
- }
- j++;
- }
- }
- if (j > s->options.max_item_count)
- js_print_more_items(s, &comma_state, j - s->options.max_item_count);
- }
- if (s->options.raw_dump && js_class_has_bytecode(p->class_id)) {
- JSFunctionBytecode *b = p->u.func.function_bytecode;
- if (b->closure_var_count) {
- JSVarRef **var_refs;
- var_refs = p->u.func.var_refs;
-
- js_print_comma(s, &comma_state);
- js_printf(s, "[[Closure]]: [");
- for(i = 0; i < b->closure_var_count; i++) {
- if (i != 0)
- js_printf(s, ", ");
- js_print_value(s, var_refs[i]->value);
- }
- js_printf(s, " ]");
- }
- if (p->u.func.home_object) {
- js_print_comma(s, &comma_state);
- js_printf(s, "[[HomeObject]]: ");
- js_print_value(s, JS_MKPTR(JS_TAG_OBJECT, p->u.func.home_object));
- }
- }
- if (!is_array) {
- if (comma_state != 2) {
- js_printf(s, " }");
- }
- } else {
- js_printf(s, " ]");
- }
- }
- static int js_print_stack_index(JSPrintValueState *s, JSObject *p)
- {
- int i;
- for(i = 0; i < s->level; i++)
- if (s->print_stack[i] == p)
- return i;
- return -1;
- }
- static void js_print_value(JSPrintValueState *s, JSValueConst val)
- {
- uint32_t tag = JS_VALUE_GET_NORM_TAG(val);
- const char *str;
- switch(tag) {
- case JS_TAG_INT:
- js_printf(s, "%d", JS_VALUE_GET_INT(val));
- break;
- case JS_TAG_BOOL:
- if (JS_VALUE_GET_BOOL(val))
- str = "true";
- else
- str = "false";
- goto print_str;
- case JS_TAG_NULL:
- str = "null";
- goto print_str;
- case JS_TAG_EXCEPTION:
- str = "exception";
- goto print_str;
- case JS_TAG_UNINITIALIZED:
- str = "uninitialized";
- goto print_str;
- case JS_TAG_UNDEFINED:
- str = "undefined";
- print_str:
- js_puts(s, str);
- break;
- case JS_TAG_FLOAT64:
- js_print_float64(s, JS_VALUE_GET_FLOAT64(val));
- break;
- case JS_TAG_SHORT_BIG_INT:
- js_printf(s, "%" PRId64 "n", (int64_t)JS_VALUE_GET_SHORT_BIG_INT(val));
- break;
- case JS_TAG_BIG_INT:
- if (!s->options.raw_dump && s->ctx) {
- JSValue str = js_bigint_to_string(s->ctx, val);
- if (JS_IsException(str))
- goto raw_bigint;
- js_print_raw_string(s, str);
- js_putc(s, 'n');
- JS_FreeValueRT(s->rt, str);
- } else {
- JSBigInt *p;
- int sgn, i;
- raw_bigint:
- p = JS_VALUE_GET_PTR(val);
- /* In order to avoid allocations we just dump the limbs */
- sgn = js_bigint_sign(p);
- if (sgn)
- js_printf(s, "BigInt.asIntN(%d,", p->len * JS_LIMB_BITS);
- js_printf(s, "0x");
- for(i = p->len - 1; i >= 0; i--) {
- if (i != p->len - 1)
- js_putc(s, '_');
- #if JS_LIMB_BITS == 32
- js_printf(s, "%08x", p->tab[i]);
- #else
- js_printf(s, "%016" PRIx64, p->tab[i]);
- #endif
- }
- js_putc(s, 'n');
- if (sgn)
- js_putc(s, ')');
- }
- break;
- case JS_TAG_STRING:
- case JS_TAG_STRING_ROPE:
- if (s->options.raw_dump && tag == JS_TAG_STRING_ROPE) {
- JSStringRope *r = JS_VALUE_GET_STRING_ROPE(val);
- js_printf(s, "[rope len=%d depth=%d]", r->len, r->depth);
- } else {
- js_print_string(s, val);
- }
- break;
- case JS_TAG_FUNCTION_BYTECODE:
- {
- JSFunctionBytecode *b = JS_VALUE_GET_PTR(val);
- js_puts(s, "[bytecode ");
- js_print_atom(s, b->func_name);
- js_putc(s, ']');
- }
- break;
- case JS_TAG_OBJECT:
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- int idx;
- idx = js_print_stack_index(s, p);
- if (idx >= 0) {
- js_printf(s, "[circular %d]", idx);
- } else if (s->level < s->options.max_depth) {
- s->print_stack[s->level++] = p;
- js_print_object(s, JS_VALUE_GET_OBJ(val));
- s->level--;
- } else {
- JSAtom atom = s->rt->class_array[p->class_id].class_name;
- js_putc(s, '[');
- js_print_atom(s, atom);
- if (s->options.raw_dump) {
- js_printf(s, " %p", (void *)p);
- }
- js_putc(s, ']');
- }
- }
- break;
- case JS_TAG_SYMBOL:
- {
- JSAtomStruct *p = JS_VALUE_GET_PTR(val);
- js_puts(s, "Symbol(");
- js_print_atom(s, js_get_atom_index(s->rt, p));
- js_putc(s, ')');
- }
- break;
- case JS_TAG_MODULE:
- js_puts(s, "[module]");
- break;
- default:
- js_printf(s, "[unknown tag %d]", tag);
- break;
- }
- }
- void JS_PrintValueSetDefaultOptions(JSPrintValueOptions *options)
- {
- memset(options, 0, sizeof(*options));
- options->max_depth = 2;
- options->max_string_length = 1000;
- options->max_item_count = 100;
- }
- static void JS_PrintValueInternal(JSRuntime *rt, JSContext *ctx,
- JSPrintValueWrite *write_func, void *write_opaque,
- JSValueConst val, const JSPrintValueOptions *options)
- {
- JSPrintValueState ss, *s = &ss;
- if (options)
- s->options = *options;
- else
- JS_PrintValueSetDefaultOptions(&s->options);
- if (s->options.max_depth <= 0)
- s->options.max_depth = JS_PRINT_MAX_DEPTH;
- else
- s->options.max_depth = min_int(s->options.max_depth, JS_PRINT_MAX_DEPTH);
- if (s->options.max_string_length == 0)
- s->options.max_string_length = UINT32_MAX;
- if (s->options.max_item_count == 0)
- s->options.max_item_count = UINT32_MAX;
- s->rt = rt;
- s->ctx = ctx;
- s->write_func = write_func;
- s->write_opaque = write_opaque;
- s->level = 0;
- js_print_value(s, val);
- }
- void JS_PrintValueRT(JSRuntime *rt, JSPrintValueWrite *write_func, void *write_opaque,
- JSValueConst val, const JSPrintValueOptions *options)
- {
- JS_PrintValueInternal(rt, NULL, write_func, write_opaque, val, options);
- }
- void JS_PrintValue(JSContext *ctx, JSPrintValueWrite *write_func, void *write_opaque,
- JSValueConst val, const JSPrintValueOptions *options)
- {
- JS_PrintValueInternal(ctx->rt, ctx, write_func, write_opaque, val, options);
- }
- static void js_dump_value_write(void *opaque, const char *buf, size_t len)
- {
- FILE *fo = opaque;
- fwrite(buf, 1, len, fo);
- }
- static __maybe_unused void print_atom(JSContext *ctx, JSAtom atom)
- {
- JSPrintValueState ss, *s = &ss;
- memset(s, 0, sizeof(*s));
- s->rt = ctx->rt;
- s->ctx = ctx;
- s->write_func = js_dump_value_write;
- s->write_opaque = stdout;
- js_print_atom(s, atom);
- }
- static __maybe_unused void JS_DumpAtom(JSContext *ctx, const char *str, JSAtom atom)
- {
- printf("%s=", str);
- print_atom(ctx, atom);
- printf("\n");
- }
- static __maybe_unused void JS_DumpValue(JSContext *ctx, const char *str, JSValueConst val)
- {
- printf("%s=", str);
- JS_PrintValue(ctx, js_dump_value_write, stdout, val, NULL);
- printf("\n");
- }
- static __maybe_unused void JS_DumpValueRT(JSRuntime *rt, const char *str, JSValueConst val)
- {
- printf("%s=", str);
- JS_PrintValueRT(rt, js_dump_value_write, stdout, val, NULL);
- printf("\n");
- }
- static __maybe_unused void JS_DumpObjectHeader(JSRuntime *rt)
- {
- printf("%14s %4s %4s %14s %s\n",
- "ADDRESS", "REFS", "SHRF", "PROTO", "CONTENT");
- }
- /* for debug only: dump an object without side effect */
- static __maybe_unused void JS_DumpObject(JSRuntime *rt, JSObject *p)
- {
- JSShape *sh;
- JSPrintValueOptions options;
-
- /* XXX: should encode atoms with special characters */
- sh = p->shape; /* the shape can be NULL while freeing an object */
- printf("%14p %4d ",
- (void *)p,
- js_rc(p)->ref_count);
- if (sh) {
- printf("%3d%c %14p ",
- js_rc(sh)->ref_count,
- " *"[sh->is_hashed],
- (void *)sh->proto);
- } else {
- printf("%3s %14s ", "-", "-");
- }
- JS_PrintValueSetDefaultOptions(&options);
- options.max_depth = 1;
- options.show_hidden = TRUE;
- options.raw_dump = TRUE;
- JS_PrintValueRT(rt, js_dump_value_write, stdout, JS_MKPTR(JS_TAG_OBJECT, p), &options);
- printf("\n");
- }
- static __maybe_unused void JS_DumpGCObject(JSRuntime *rt, JSGCObjectHeader *p)
- {
- if (js_rc(p)->gc_obj_type == JS_GC_OBJ_TYPE_JS_OBJECT) {
- JS_DumpObject(rt, (JSObject *)p);
- } else {
- printf("%14p %4d ",
- (void *)p,
- js_rc(p)->ref_count);
- switch(js_rc(p)->gc_obj_type) {
- case JS_GC_OBJ_TYPE_FUNCTION_BYTECODE:
- printf("[function bytecode]");
- break;
- case JS_GC_OBJ_TYPE_SHAPE:
- printf("[shape]");
- break;
- case JS_GC_OBJ_TYPE_VAR_REF:
- printf("[var_ref]");
- break;
- case JS_GC_OBJ_TYPE_ASYNC_FUNCTION:
- printf("[async_function]");
- break;
- case JS_GC_OBJ_TYPE_JS_CONTEXT:
- printf("[js_context]");
- break;
- case JS_GC_OBJ_TYPE_MODULE:
- printf("[module]");
- break;
- default:
- printf("[unknown %d]", js_rc(p)->gc_obj_type);
- break;
- }
- printf("\n");
- }
- }
- /* return -1 if exception (proxy case) or TRUE/FALSE */
- // TODO: should take flags to make proxy resolution and exceptions optional
- int JS_IsArray(JSContext *ctx, JSValueConst val)
- {
- if (js_resolve_proxy(ctx, &val, TRUE))
- return -1;
- if (JS_VALUE_GET_TAG(val) == JS_TAG_OBJECT) {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- return p->class_id == JS_CLASS_ARRAY;
- } else {
- return FALSE;
- }
- }
- static double js_pow(double a, double b)
- {
- if (unlikely(!isfinite(b)) && fabs(a) == 1) {
- /* not compatible with IEEE 754 */
- return JS_FLOAT64_NAN;
- } else {
- return pow(a, b);
- }
- }
- JSValue JS_NewBigInt64(JSContext *ctx, int64_t v)
- {
- #if JS_SHORT_BIG_INT_BITS == 64
- return __JS_NewShortBigInt(ctx, v);
- #else
- if (v >= JS_SHORT_BIG_INT_MIN && v <= JS_SHORT_BIG_INT_MAX) {
- return __JS_NewShortBigInt(ctx, v);
- } else {
- JSBigInt *p;
- p = js_bigint_new_si64(ctx, v);
- if (!p)
- return JS_EXCEPTION;
- return JS_MKPTR(JS_TAG_BIG_INT, p);
- }
- #endif
- }
- JSValue JS_NewBigUint64(JSContext *ctx, uint64_t v)
- {
- if (v <= JS_SHORT_BIG_INT_MAX) {
- return __JS_NewShortBigInt(ctx, v);
- } else {
- JSBigInt *p;
- p = js_bigint_new_ui64(ctx, v);
- if (!p)
- return JS_EXCEPTION;
- return JS_MKPTR(JS_TAG_BIG_INT, p);
- }
- }
- /* return NaN if bad bigint literal */
- static JSValue JS_StringToBigInt(JSContext *ctx, JSValue val)
- {
- const char *str, *p;
- size_t len;
- int flags;
- str = JS_ToCStringLen(ctx, &len, val);
- JS_FreeValue(ctx, val);
- if (!str)
- return JS_EXCEPTION;
- p = str;
- p += skip_spaces(p);
- if ((p - str) == len) {
- val = JS_NewBigInt64(ctx, 0);
- } else {
- flags = ATOD_INT_ONLY | ATOD_ACCEPT_BIN_OCT | ATOD_TYPE_BIG_INT;
- val = js_atof(ctx, p, &p, 0, flags);
- p += skip_spaces(p);
- if (!JS_IsException(val)) {
- if ((p - str) != len) {
- JS_FreeValue(ctx, val);
- val = JS_NAN;
- }
- }
- }
- JS_FreeCString(ctx, str);
- return val;
- }
- static JSValue JS_StringToBigIntErr(JSContext *ctx, JSValue val)
- {
- val = JS_StringToBigInt(ctx, val);
- if (JS_VALUE_IS_NAN(val))
- return JS_ThrowSyntaxError(ctx, "invalid bigint literal");
- return val;
- }
- /* JS Numbers are not allowed */
- static JSValue JS_ToBigIntFree(JSContext *ctx, JSValue val)
- {
- uint32_t tag;
- redo:
- tag = JS_VALUE_GET_NORM_TAG(val);
- switch(tag) {
- case JS_TAG_SHORT_BIG_INT:
- case JS_TAG_BIG_INT:
- break;
- case JS_TAG_INT:
- case JS_TAG_NULL:
- case JS_TAG_UNDEFINED:
- case JS_TAG_FLOAT64:
- goto fail;
- case JS_TAG_BOOL:
- val = __JS_NewShortBigInt(ctx, JS_VALUE_GET_INT(val));
- break;
- case JS_TAG_STRING:
- case JS_TAG_STRING_ROPE:
- val = JS_StringToBigIntErr(ctx, val);
- if (JS_IsException(val))
- return val;
- goto redo;
- case JS_TAG_OBJECT:
- val = JS_ToPrimitiveFree(ctx, val, HINT_NUMBER);
- if (JS_IsException(val))
- return val;
- goto redo;
- default:
- fail:
- JS_FreeValue(ctx, val);
- return JS_ThrowTypeError(ctx, "cannot convert to bigint");
- }
- return val;
- }
- static JSValue JS_ToBigInt(JSContext *ctx, JSValueConst val)
- {
- return JS_ToBigIntFree(ctx, JS_DupValue(ctx, val));
- }
- /* XXX: merge with JS_ToInt64Free with a specific flag ? */
- static int JS_ToBigInt64Free(JSContext *ctx, int64_t *pres, JSValue val)
- {
- uint64_t res;
- val = JS_ToBigIntFree(ctx, val);
- if (JS_IsException(val)) {
- *pres = 0;
- return -1;
- }
- if (JS_VALUE_GET_TAG(val) == JS_TAG_SHORT_BIG_INT) {
- res = JS_VALUE_GET_SHORT_BIG_INT(val);
- } else {
- JSBigInt *p = JS_VALUE_GET_PTR(val);
- /* return the value mod 2^64 */
- res = p->tab[0];
- #if JS_LIMB_BITS == 32
- if (p->len >= 2)
- res |= (uint64_t)p->tab[1] << 32;
- #endif
- JS_FreeValue(ctx, val);
- }
- *pres = res;
- return 0;
- }
- int JS_ToBigInt64(JSContext *ctx, int64_t *pres, JSValueConst val)
- {
- return JS_ToBigInt64Free(ctx, pres, JS_DupValue(ctx, val));
- }
- static no_inline __exception int js_unary_arith_slow(JSContext *ctx,
- JSValue *sp,
- OPCodeEnum op)
- {
- JSValue op1;
- int v;
- uint32_t tag;
- JSBigIntBuf buf1;
- JSBigInt *p1;
- op1 = sp[-1];
- /* fast path for float64 */
- if (JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(op1)))
- goto handle_float64;
- op1 = JS_ToNumericFree(ctx, op1);
- if (JS_IsException(op1))
- goto exception;
- tag = JS_VALUE_GET_TAG(op1);
- switch(tag) {
- case JS_TAG_INT:
- {
- int64_t v64;
- v64 = JS_VALUE_GET_INT(op1);
- switch(op) {
- case OP_inc:
- case OP_dec:
- v = 2 * (op - OP_dec) - 1;
- v64 += v;
- break;
- case OP_plus:
- break;
- case OP_neg:
- if (v64 == 0) {
- sp[-1] = __JS_NewFloat64(ctx, -0.0);
- return 0;
- } else {
- v64 = -v64;
- }
- break;
- default:
- abort();
- }
- sp[-1] = JS_NewInt64(ctx, v64);
- }
- break;
- case JS_TAG_SHORT_BIG_INT:
- {
- int64_t v;
- v = JS_VALUE_GET_SHORT_BIG_INT(op1);
- switch(op) {
- case OP_plus:
- JS_ThrowTypeError(ctx, "bigint argument with unary +");
- goto exception;
- case OP_inc:
- if (v == JS_SHORT_BIG_INT_MAX)
- goto bigint_slow_case;
- sp[-1] = __JS_NewShortBigInt(ctx, v + 1);
- break;
- case OP_dec:
- if (v == JS_SHORT_BIG_INT_MIN)
- goto bigint_slow_case;
- sp[-1] = __JS_NewShortBigInt(ctx, v - 1);
- break;
- case OP_neg:
- v = JS_VALUE_GET_SHORT_BIG_INT(op1);
- if (v == JS_SHORT_BIG_INT_MIN) {
- bigint_slow_case:
- p1 = js_bigint_set_short(&buf1, op1);
- goto bigint_slow_case1;
- }
- sp[-1] = __JS_NewShortBigInt(ctx, -v);
- break;
- default:
- abort();
- }
- }
- break;
- case JS_TAG_BIG_INT:
- {
- JSBigInt *r;
- p1 = JS_VALUE_GET_PTR(op1);
- bigint_slow_case1:
- switch(op) {
- case OP_plus:
- JS_ThrowTypeError(ctx, "bigint argument with unary +");
- JS_FreeValue(ctx, op1);
- goto exception;
- case OP_inc:
- case OP_dec:
- {
- JSBigIntBuf buf2;
- JSBigInt *p2;
- p2 = js_bigint_set_si(&buf2, 2 * (op - OP_dec) - 1);
- r = js_bigint_add(ctx, p1, p2, 0);
- }
- break;
- case OP_neg:
- r = js_bigint_neg(ctx, p1);
- break;
- case OP_not:
- r = js_bigint_not(ctx, p1);
- break;
- default:
- abort();
- }
- JS_FreeValue(ctx, op1);
- if (!r)
- goto exception;
- sp[-1] = JS_CompactBigInt(ctx, r);
- }
- break;
- default:
- handle_float64:
- {
- double d;
- d = JS_VALUE_GET_FLOAT64(op1);
- switch(op) {
- case OP_inc:
- case OP_dec:
- v = 2 * (op - OP_dec) - 1;
- d += v;
- break;
- case OP_plus:
- break;
- case OP_neg:
- d = -d;
- break;
- default:
- abort();
- }
- sp[-1] = __JS_NewFloat64(ctx, d);
- }
- break;
- }
- return 0;
- exception:
- sp[-1] = JS_UNDEFINED;
- return -1;
- }
- static __exception int js_post_inc_slow(JSContext *ctx,
- JSValue *sp, OPCodeEnum op)
- {
- JSValue op1;
- /* XXX: allow custom operators */
- op1 = sp[-1];
- op1 = JS_ToNumericFree(ctx, op1);
- if (JS_IsException(op1)) {
- sp[-1] = JS_UNDEFINED;
- return -1;
- }
- sp[-1] = op1;
- sp[0] = JS_DupValue(ctx, op1);
- return js_unary_arith_slow(ctx, sp + 1, op - OP_post_dec + OP_dec);
- }
- static no_inline int js_not_slow(JSContext *ctx, JSValue *sp)
- {
- JSValue op1;
- op1 = sp[-1];
- op1 = JS_ToNumericFree(ctx, op1);
- if (JS_IsException(op1))
- goto exception;
- if (JS_VALUE_GET_TAG(op1) == JS_TAG_SHORT_BIG_INT) {
- sp[-1] = __JS_NewShortBigInt(ctx, ~JS_VALUE_GET_SHORT_BIG_INT(op1));
- } else if (JS_VALUE_GET_TAG(op1) == JS_TAG_BIG_INT) {
- JSBigInt *r;
- r = js_bigint_not(ctx, JS_VALUE_GET_PTR(op1));
- JS_FreeValue(ctx, op1);
- if (!r)
- goto exception;
- sp[-1] = JS_CompactBigInt(ctx, r);
- } else {
- int32_t v1;
- if (unlikely(JS_ToInt32Free(ctx, &v1, op1)))
- goto exception;
- sp[-1] = JS_NewInt32(ctx, ~v1);
- }
- return 0;
- exception:
- sp[-1] = JS_UNDEFINED;
- return -1;
- }
- static no_inline __exception int js_binary_arith_slow(JSContext *ctx, JSValue *sp,
- OPCodeEnum op)
- {
- JSValue op1, op2;
- uint32_t tag1, tag2;
- double d1, d2;
- op1 = sp[-2];
- op2 = sp[-1];
- tag1 = JS_VALUE_GET_NORM_TAG(op1);
- tag2 = JS_VALUE_GET_NORM_TAG(op2);
- /* fast path for float operations */
- if (tag1 == JS_TAG_FLOAT64 && tag2 == JS_TAG_FLOAT64) {
- d1 = JS_VALUE_GET_FLOAT64(op1);
- d2 = JS_VALUE_GET_FLOAT64(op2);
- goto handle_float64;
- }
- /* fast path for short big int operations */
- if (tag1 == JS_TAG_SHORT_BIG_INT && tag2 == JS_TAG_SHORT_BIG_INT) {
- js_slimb_t v1, v2;
- js_sdlimb_t v;
- v1 = JS_VALUE_GET_SHORT_BIG_INT(op1);
- v2 = JS_VALUE_GET_SHORT_BIG_INT(op2);
- switch(op) {
- case OP_sub:
- v = (js_sdlimb_t)v1 - (js_sdlimb_t)v2;
- break;
- case OP_mul:
- v = (js_sdlimb_t)v1 * (js_sdlimb_t)v2;
- break;
- case OP_div:
- if (v2 == 0 ||
- ((js_limb_t)v1 == (js_limb_t)1 << (JS_LIMB_BITS - 1) &&
- v2 == -1)) {
- goto slow_big_int;
- }
- sp[-2] = __JS_NewShortBigInt(ctx, v1 / v2);
- return 0;
- case OP_mod:
- if (v2 == 0 ||
- ((js_limb_t)v1 == (js_limb_t)1 << (JS_LIMB_BITS - 1) &&
- v2 == -1)) {
- goto slow_big_int;
- }
- sp[-2] = __JS_NewShortBigInt(ctx, v1 % v2);
- return 0;
- case OP_pow:
- goto slow_big_int;
- default:
- abort();
- }
- if (likely(v >= JS_SHORT_BIG_INT_MIN && v <= JS_SHORT_BIG_INT_MAX)) {
- sp[-2] = __JS_NewShortBigInt(ctx, v);
- } else {
- JSBigInt *r = js_bigint_new_di(ctx, v);
- if (!r)
- goto exception;
- sp[-2] = JS_MKPTR(JS_TAG_BIG_INT, r);
- }
- return 0;
- }
- op1 = JS_ToNumericFree(ctx, op1);
- if (JS_IsException(op1)) {
- JS_FreeValue(ctx, op2);
- goto exception;
- }
- op2 = JS_ToNumericFree(ctx, op2);
- if (JS_IsException(op2)) {
- JS_FreeValue(ctx, op1);
- goto exception;
- }
- tag1 = JS_VALUE_GET_NORM_TAG(op1);
- tag2 = JS_VALUE_GET_NORM_TAG(op2);
- if (tag1 == JS_TAG_INT && tag2 == JS_TAG_INT) {
- int32_t v1, v2;
- int64_t v;
- v1 = JS_VALUE_GET_INT(op1);
- v2 = JS_VALUE_GET_INT(op2);
- switch(op) {
- case OP_sub:
- v = (int64_t)v1 - (int64_t)v2;
- break;
- case OP_mul:
- v = (int64_t)v1 * (int64_t)v2;
- if (v == 0 && (v1 | v2) < 0) {
- sp[-2] = __JS_NewFloat64(ctx, -0.0);
- return 0;
- }
- break;
- case OP_div:
- sp[-2] = JS_NewFloat64(ctx, (double)v1 / (double)v2);
- return 0;
- case OP_mod:
- if (v1 < 0 || v2 <= 0) {
- sp[-2] = JS_NewFloat64(ctx, fmod(v1, v2));
- return 0;
- } else {
- v = (int64_t)v1 % (int64_t)v2;
- }
- break;
- case OP_pow:
- sp[-2] = JS_NewFloat64(ctx, js_pow(v1, v2));
- return 0;
- default:
- abort();
- }
- sp[-2] = JS_NewInt64(ctx, v);
- } else if ((tag1 == JS_TAG_SHORT_BIG_INT || tag1 == JS_TAG_BIG_INT) &&
- (tag2 == JS_TAG_SHORT_BIG_INT || tag2 == JS_TAG_BIG_INT)) {
- JSBigInt *p1, *p2, *r;
- JSBigIntBuf buf1, buf2;
- slow_big_int:
- /* bigint result */
- if (JS_VALUE_GET_TAG(op1) == JS_TAG_SHORT_BIG_INT)
- p1 = js_bigint_set_short(&buf1, op1);
- else
- p1 = JS_VALUE_GET_PTR(op1);
- if (JS_VALUE_GET_TAG(op2) == JS_TAG_SHORT_BIG_INT)
- p2 = js_bigint_set_short(&buf2, op2);
- else
- p2 = JS_VALUE_GET_PTR(op2);
- switch(op) {
- case OP_add:
- r = js_bigint_add(ctx, p1, p2, 0);
- break;
- case OP_sub:
- r = js_bigint_add(ctx, p1, p2, 1);
- break;
- case OP_mul:
- r = js_bigint_mul(ctx, p1, p2);
- break;
- case OP_div:
- r = js_bigint_divrem(ctx, p1, p2, FALSE);
- break;
- case OP_mod:
- r = js_bigint_divrem(ctx, p1, p2, TRUE);
- break;
- case OP_pow:
- r = js_bigint_pow(ctx, p1, p2);
- break;
- default:
- abort();
- }
- JS_FreeValue(ctx, op1);
- JS_FreeValue(ctx, op2);
- if (!r)
- goto exception;
- sp[-2] = JS_CompactBigInt(ctx, r);
- } else {
- double dr;
- /* float64 result */
- if (JS_ToFloat64Free(ctx, &d1, op1)) {
- JS_FreeValue(ctx, op2);
- goto exception;
- }
- if (JS_ToFloat64Free(ctx, &d2, op2))
- goto exception;
- handle_float64:
- switch(op) {
- case OP_sub:
- dr = d1 - d2;
- break;
- case OP_mul:
- dr = d1 * d2;
- break;
- case OP_div:
- dr = d1 / d2;
- break;
- case OP_mod:
- dr = fmod(d1, d2);
- break;
- case OP_pow:
- dr = js_pow(d1, d2);
- break;
- default:
- abort();
- }
- sp[-2] = __JS_NewFloat64(ctx, dr);
- }
- return 0;
- exception:
- sp[-2] = JS_UNDEFINED;
- sp[-1] = JS_UNDEFINED;
- return -1;
- }
- static inline BOOL tag_is_string(uint32_t tag)
- {
- return tag == JS_TAG_STRING || tag == JS_TAG_STRING_ROPE;
- }
- static no_inline __exception int js_add_slow(JSContext *ctx, JSValue *sp)
- {
- JSValue op1, op2;
- uint32_t tag1, tag2;
- op1 = sp[-2];
- op2 = sp[-1];
- tag1 = JS_VALUE_GET_NORM_TAG(op1);
- tag2 = JS_VALUE_GET_NORM_TAG(op2);
- /* fast path for float64 */
- if (tag1 == JS_TAG_FLOAT64 && tag2 == JS_TAG_FLOAT64) {
- double d1, d2;
- d1 = JS_VALUE_GET_FLOAT64(op1);
- d2 = JS_VALUE_GET_FLOAT64(op2);
- sp[-2] = __JS_NewFloat64(ctx, d1 + d2);
- return 0;
- }
- /* fast path for short bigint */
- if (tag1 == JS_TAG_SHORT_BIG_INT && tag2 == JS_TAG_SHORT_BIG_INT) {
- js_slimb_t v1, v2;
- js_sdlimb_t v;
- v1 = JS_VALUE_GET_SHORT_BIG_INT(op1);
- v2 = JS_VALUE_GET_SHORT_BIG_INT(op2);
- v = (js_sdlimb_t)v1 + (js_sdlimb_t)v2;
- if (likely(v >= JS_SHORT_BIG_INT_MIN && v <= JS_SHORT_BIG_INT_MAX)) {
- sp[-2] = __JS_NewShortBigInt(ctx, v);
- } else {
- JSBigInt *r = js_bigint_new_di(ctx, v);
- if (!r)
- goto exception;
- sp[-2] = JS_MKPTR(JS_TAG_BIG_INT, r);
- }
- return 0;
- }
-
- if (tag1 == JS_TAG_OBJECT || tag2 == JS_TAG_OBJECT) {
- op1 = JS_ToPrimitiveFree(ctx, op1, HINT_NONE);
- if (JS_IsException(op1)) {
- JS_FreeValue(ctx, op2);
- goto exception;
- }
- op2 = JS_ToPrimitiveFree(ctx, op2, HINT_NONE);
- if (JS_IsException(op2)) {
- JS_FreeValue(ctx, op1);
- goto exception;
- }
- tag1 = JS_VALUE_GET_NORM_TAG(op1);
- tag2 = JS_VALUE_GET_NORM_TAG(op2);
- }
- if (tag_is_string(tag1) || tag_is_string(tag2)) {
- sp[-2] = JS_ConcatString(ctx, op1, op2);
- if (JS_IsException(sp[-2]))
- goto exception;
- return 0;
- }
- op1 = JS_ToNumericFree(ctx, op1);
- if (JS_IsException(op1)) {
- JS_FreeValue(ctx, op2);
- goto exception;
- }
- op2 = JS_ToNumericFree(ctx, op2);
- if (JS_IsException(op2)) {
- JS_FreeValue(ctx, op1);
- goto exception;
- }
- tag1 = JS_VALUE_GET_NORM_TAG(op1);
- tag2 = JS_VALUE_GET_NORM_TAG(op2);
- if (tag1 == JS_TAG_INT && tag2 == JS_TAG_INT) {
- int32_t v1, v2;
- int64_t v;
- v1 = JS_VALUE_GET_INT(op1);
- v2 = JS_VALUE_GET_INT(op2);
- v = (int64_t)v1 + (int64_t)v2;
- sp[-2] = JS_NewInt64(ctx, v);
- } else if ((tag1 == JS_TAG_BIG_INT || tag1 == JS_TAG_SHORT_BIG_INT) &&
- (tag2 == JS_TAG_BIG_INT || tag2 == JS_TAG_SHORT_BIG_INT)) {
- JSBigInt *p1, *p2, *r;
- JSBigIntBuf buf1, buf2;
- /* bigint result */
- if (JS_VALUE_GET_TAG(op1) == JS_TAG_SHORT_BIG_INT)
- p1 = js_bigint_set_short(&buf1, op1);
- else
- p1 = JS_VALUE_GET_PTR(op1);
- if (JS_VALUE_GET_TAG(op2) == JS_TAG_SHORT_BIG_INT)
- p2 = js_bigint_set_short(&buf2, op2);
- else
- p2 = JS_VALUE_GET_PTR(op2);
- r = js_bigint_add(ctx, p1, p2, 0);
- JS_FreeValue(ctx, op1);
- JS_FreeValue(ctx, op2);
- if (!r)
- goto exception;
- sp[-2] = JS_CompactBigInt(ctx, r);
- } else {
- double d1, d2;
- /* float64 result */
- if (JS_ToFloat64Free(ctx, &d1, op1)) {
- JS_FreeValue(ctx, op2);
- goto exception;
- }
- if (JS_ToFloat64Free(ctx, &d2, op2))
- goto exception;
- sp[-2] = __JS_NewFloat64(ctx, d1 + d2);
- }
- return 0;
- exception:
- sp[-2] = JS_UNDEFINED;
- sp[-1] = JS_UNDEFINED;
- return -1;
- }
- static no_inline __exception int js_binary_logic_slow(JSContext *ctx,
- JSValue *sp,
- OPCodeEnum op)
- {
- JSValue op1, op2;
- uint32_t tag1, tag2;
- uint32_t v1, v2, r;
- op1 = sp[-2];
- op2 = sp[-1];
- tag1 = JS_VALUE_GET_NORM_TAG(op1);
- tag2 = JS_VALUE_GET_NORM_TAG(op2);
- if (tag1 == JS_TAG_SHORT_BIG_INT && tag2 == JS_TAG_SHORT_BIG_INT) {
- js_slimb_t v1, v2, v;
- js_sdlimb_t vd;
- v1 = JS_VALUE_GET_SHORT_BIG_INT(op1);
- v2 = JS_VALUE_GET_SHORT_BIG_INT(op2);
- /* bigint fast path */
- switch(op) {
- case OP_and:
- v = v1 & v2;
- break;
- case OP_or:
- v = v1 | v2;
- break;
- case OP_xor:
- v = v1 ^ v2;
- break;
- case OP_sar:
- if (v2 > (JS_LIMB_BITS - 1)) {
- goto slow_big_int;
- } else if (v2 < 0) {
- if (v2 < -(JS_LIMB_BITS - 1))
- goto slow_big_int;
- v2 = -v2;
- goto bigint_shl;
- }
- bigint_sar:
- v = v1 >> v2;
- break;
- case OP_shl:
- if (v2 > (JS_LIMB_BITS - 1)) {
- goto slow_big_int;
- } else if (v2 < 0) {
- if (v2 < -(JS_LIMB_BITS - 1))
- goto slow_big_int;
- v2 = -v2;
- goto bigint_sar;
- }
- bigint_shl:
- vd = (js_dlimb_t)v1 << v2;
- if (likely(vd >= JS_SHORT_BIG_INT_MIN &&
- vd <= JS_SHORT_BIG_INT_MAX)) {
- v = vd;
- } else {
- JSBigInt *r = js_bigint_new_di(ctx, vd);
- if (!r)
- goto exception;
- sp[-2] = JS_MKPTR(JS_TAG_BIG_INT, r);
- return 0;
- }
- break;
- default:
- abort();
- }
- sp[-2] = __JS_NewShortBigInt(ctx, v);
- return 0;
- }
- op1 = JS_ToNumericFree(ctx, op1);
- if (JS_IsException(op1)) {
- JS_FreeValue(ctx, op2);
- goto exception;
- }
- op2 = JS_ToNumericFree(ctx, op2);
- if (JS_IsException(op2)) {
- JS_FreeValue(ctx, op1);
- goto exception;
- }
- tag1 = JS_VALUE_GET_TAG(op1);
- tag2 = JS_VALUE_GET_TAG(op2);
- if ((tag1 == JS_TAG_BIG_INT || tag1 == JS_TAG_SHORT_BIG_INT) &&
- (tag2 == JS_TAG_BIG_INT || tag2 == JS_TAG_SHORT_BIG_INT)) {
- JSBigInt *p1, *p2, *r;
- JSBigIntBuf buf1, buf2;
- slow_big_int:
- if (JS_VALUE_GET_TAG(op1) == JS_TAG_SHORT_BIG_INT)
- p1 = js_bigint_set_short(&buf1, op1);
- else
- p1 = JS_VALUE_GET_PTR(op1);
- if (JS_VALUE_GET_TAG(op2) == JS_TAG_SHORT_BIG_INT)
- p2 = js_bigint_set_short(&buf2, op2);
- else
- p2 = JS_VALUE_GET_PTR(op2);
- switch(op) {
- case OP_and:
- case OP_or:
- case OP_xor:
- r = js_bigint_logic(ctx, p1, p2, op);
- break;
- case OP_shl:
- case OP_sar:
- {
- js_slimb_t shift;
- shift = js_bigint_get_si_sat(p2);
- if (shift > INT32_MAX)
- shift = INT32_MAX;
- else if (shift < -INT32_MAX)
- shift = -INT32_MAX;
- if (op == OP_sar)
- shift = -shift;
- if (shift >= 0)
- r = js_bigint_shl(ctx, p1, shift);
- else
- r = js_bigint_shr(ctx, p1, -shift);
- }
- break;
- default:
- abort();
- }
- JS_FreeValue(ctx, op1);
- JS_FreeValue(ctx, op2);
- if (!r)
- goto exception;
- sp[-2] = JS_CompactBigInt(ctx, r);
- } else {
- if (unlikely(JS_ToInt32Free(ctx, (int32_t *)&v1, op1))) {
- JS_FreeValue(ctx, op2);
- goto exception;
- }
- if (unlikely(JS_ToInt32Free(ctx, (int32_t *)&v2, op2)))
- goto exception;
- switch(op) {
- case OP_shl:
- r = v1 << (v2 & 0x1f);
- break;
- case OP_sar:
- r = (int)v1 >> (v2 & 0x1f);
- break;
- case OP_and:
- r = v1 & v2;
- break;
- case OP_or:
- r = v1 | v2;
- break;
- case OP_xor:
- r = v1 ^ v2;
- break;
- default:
- abort();
- }
- sp[-2] = JS_NewInt32(ctx, r);
- }
- return 0;
- exception:
- sp[-2] = JS_UNDEFINED;
- sp[-1] = JS_UNDEFINED;
- return -1;
- }
- /* op1 must be a bigint or int. */
- static JSBigInt *JS_ToBigIntBuf(JSContext *ctx, JSBigIntBuf *buf1,
- JSValue op1)
- {
- JSBigInt *p1;
-
- switch(JS_VALUE_GET_TAG(op1)) {
- case JS_TAG_INT:
- p1 = js_bigint_set_si(buf1, JS_VALUE_GET_INT(op1));
- break;
- case JS_TAG_SHORT_BIG_INT:
- p1 = js_bigint_set_short(buf1, op1);
- break;
- case JS_TAG_BIG_INT:
- p1 = JS_VALUE_GET_PTR(op1);
- break;
- default:
- abort();
- }
- return p1;
- }
- /* op1 and op2 must be numeric types and at least one must be a
- bigint. No exception is generated. */
- static int js_compare_bigint(JSContext *ctx, OPCodeEnum op,
- JSValue op1, JSValue op2)
- {
- int res, val, tag1, tag2;
- JSBigIntBuf buf1, buf2;
- JSBigInt *p1, *p2;
-
- tag1 = JS_VALUE_GET_NORM_TAG(op1);
- tag2 = JS_VALUE_GET_NORM_TAG(op2);
- if ((tag1 == JS_TAG_SHORT_BIG_INT || tag1 == JS_TAG_INT) &&
- (tag2 == JS_TAG_SHORT_BIG_INT || tag2 == JS_TAG_INT)) {
- /* fast path */
- js_slimb_t v1, v2;
- if (tag1 == JS_TAG_INT)
- v1 = JS_VALUE_GET_INT(op1);
- else
- v1 = JS_VALUE_GET_SHORT_BIG_INT(op1);
- if (tag2 == JS_TAG_INT)
- v2 = JS_VALUE_GET_INT(op2);
- else
- v2 = JS_VALUE_GET_SHORT_BIG_INT(op2);
- val = (v1 > v2) - (v1 < v2);
- } else {
- if (tag1 == JS_TAG_FLOAT64) {
- p2 = JS_ToBigIntBuf(ctx, &buf2, op2);
- val = js_bigint_float64_cmp(ctx, p2, JS_VALUE_GET_FLOAT64(op1));
- if (val == 2)
- goto unordered;
- val = -val;
- } else if (tag2 == JS_TAG_FLOAT64) {
- p1 = JS_ToBigIntBuf(ctx, &buf1, op1);
- val = js_bigint_float64_cmp(ctx, p1, JS_VALUE_GET_FLOAT64(op2));
- if (val == 2) {
- unordered:
- JS_FreeValue(ctx, op1);
- JS_FreeValue(ctx, op2);
- return FALSE;
- }
- } else {
- p1 = JS_ToBigIntBuf(ctx, &buf1, op1);
- p2 = JS_ToBigIntBuf(ctx, &buf2, op2);
- val = js_bigint_cmp(ctx, p1, p2);
- }
- JS_FreeValue(ctx, op1);
- JS_FreeValue(ctx, op2);
- }
- switch(op) {
- case OP_lt:
- res = val < 0;
- break;
- case OP_lte:
- res = val <= 0;
- break;
- case OP_gt:
- res = val > 0;
- break;
- case OP_gte:
- res = val >= 0;
- break;
- case OP_eq:
- res = val == 0;
- break;
- default:
- abort();
- }
- return res;
- }
- static no_inline int js_relational_slow(JSContext *ctx, JSValue *sp,
- OPCodeEnum op)
- {
- JSValue op1, op2;
- int res;
- uint32_t tag1, tag2;
- op1 = sp[-2];
- op2 = sp[-1];
- tag1 = JS_VALUE_GET_NORM_TAG(op1);
- tag2 = JS_VALUE_GET_NORM_TAG(op2);
- op1 = JS_ToPrimitiveFree(ctx, op1, HINT_NUMBER);
- if (JS_IsException(op1)) {
- JS_FreeValue(ctx, op2);
- goto exception;
- }
- op2 = JS_ToPrimitiveFree(ctx, op2, HINT_NUMBER);
- if (JS_IsException(op2)) {
- JS_FreeValue(ctx, op1);
- goto exception;
- }
- tag1 = JS_VALUE_GET_NORM_TAG(op1);
- tag2 = JS_VALUE_GET_NORM_TAG(op2);
- if (tag_is_string(tag1) && tag_is_string(tag2)) {
- if (tag1 == JS_TAG_STRING && tag2 == JS_TAG_STRING) {
- res = js_string_compare(ctx, JS_VALUE_GET_STRING(op1),
- JS_VALUE_GET_STRING(op2));
- } else {
- res = js_string_rope_compare(ctx, op1, op2, FALSE);
- }
- switch(op) {
- case OP_lt:
- res = (res < 0);
- break;
- case OP_lte:
- res = (res <= 0);
- break;
- case OP_gt:
- res = (res > 0);
- break;
- default:
- case OP_gte:
- res = (res >= 0);
- break;
- }
- JS_FreeValue(ctx, op1);
- JS_FreeValue(ctx, op2);
- } else if ((tag1 <= JS_TAG_NULL || tag1 == JS_TAG_FLOAT64) &&
- (tag2 <= JS_TAG_NULL || tag2 == JS_TAG_FLOAT64)) {
- /* fast path for float64/int */
- goto float64_compare;
- } else {
- if ((((tag1 == JS_TAG_BIG_INT || tag1 == JS_TAG_SHORT_BIG_INT) &&
- tag_is_string(tag2)) ||
- ((tag2 == JS_TAG_BIG_INT || tag2 == JS_TAG_SHORT_BIG_INT) &&
- tag_is_string(tag1)))) {
- if (tag_is_string(tag1)) {
- op1 = JS_StringToBigInt(ctx, op1);
- if (JS_VALUE_GET_TAG(op1) != JS_TAG_BIG_INT &&
- JS_VALUE_GET_TAG(op1) != JS_TAG_SHORT_BIG_INT)
- goto invalid_bigint_string;
- }
- if (tag_is_string(tag2)) {
- op2 = JS_StringToBigInt(ctx, op2);
- if (JS_VALUE_GET_TAG(op2) != JS_TAG_BIG_INT &&
- JS_VALUE_GET_TAG(op2) != JS_TAG_SHORT_BIG_INT) {
- invalid_bigint_string:
- JS_FreeValue(ctx, op1);
- JS_FreeValue(ctx, op2);
- res = FALSE;
- goto done;
- }
- }
- } else {
- op1 = JS_ToNumericFree(ctx, op1);
- if (JS_IsException(op1)) {
- JS_FreeValue(ctx, op2);
- goto exception;
- }
- op2 = JS_ToNumericFree(ctx, op2);
- if (JS_IsException(op2)) {
- JS_FreeValue(ctx, op1);
- goto exception;
- }
- }
- tag1 = JS_VALUE_GET_NORM_TAG(op1);
- tag2 = JS_VALUE_GET_NORM_TAG(op2);
- if (tag1 == JS_TAG_BIG_INT || tag1 == JS_TAG_SHORT_BIG_INT ||
- tag2 == JS_TAG_BIG_INT || tag2 == JS_TAG_SHORT_BIG_INT) {
- res = js_compare_bigint(ctx, op, op1, op2);
- } else {
- double d1, d2;
- float64_compare:
- /* can use floating point comparison */
- if (tag1 == JS_TAG_FLOAT64) {
- d1 = JS_VALUE_GET_FLOAT64(op1);
- } else {
- d1 = JS_VALUE_GET_INT(op1);
- }
- if (tag2 == JS_TAG_FLOAT64) {
- d2 = JS_VALUE_GET_FLOAT64(op2);
- } else {
- d2 = JS_VALUE_GET_INT(op2);
- }
- switch(op) {
- case OP_lt:
- res = (d1 < d2); /* if NaN return false */
- break;
- case OP_lte:
- res = (d1 <= d2); /* if NaN return false */
- break;
- case OP_gt:
- res = (d1 > d2); /* if NaN return false */
- break;
- default:
- case OP_gte:
- res = (d1 >= d2); /* if NaN return false */
- break;
- }
- }
- }
- done:
- sp[-2] = JS_NewBool(ctx, res);
- return 0;
- exception:
- sp[-2] = JS_UNDEFINED;
- sp[-1] = JS_UNDEFINED;
- return -1;
- }
- static BOOL tag_is_number(uint32_t tag)
- {
- return (tag == JS_TAG_INT ||
- tag == JS_TAG_FLOAT64 ||
- tag == JS_TAG_BIG_INT || tag == JS_TAG_SHORT_BIG_INT);
- }
- static no_inline __exception int js_eq_slow(JSContext *ctx, JSValue *sp,
- BOOL is_neq)
- {
- JSValue op1, op2;
- int res;
- uint32_t tag1, tag2;
- op1 = sp[-2];
- op2 = sp[-1];
- redo:
- tag1 = JS_VALUE_GET_NORM_TAG(op1);
- tag2 = JS_VALUE_GET_NORM_TAG(op2);
- if (tag_is_number(tag1) && tag_is_number(tag2)) {
- if (tag1 == JS_TAG_INT && tag2 == JS_TAG_INT) {
- res = JS_VALUE_GET_INT(op1) == JS_VALUE_GET_INT(op2);
- } else if ((tag1 == JS_TAG_FLOAT64 &&
- (tag2 == JS_TAG_INT || tag2 == JS_TAG_FLOAT64)) ||
- (tag2 == JS_TAG_FLOAT64 &&
- (tag1 == JS_TAG_INT || tag1 == JS_TAG_FLOAT64))) {
- double d1, d2;
- if (tag1 == JS_TAG_FLOAT64) {
- d1 = JS_VALUE_GET_FLOAT64(op1);
- } else {
- d1 = JS_VALUE_GET_INT(op1);
- }
- if (tag2 == JS_TAG_FLOAT64) {
- d2 = JS_VALUE_GET_FLOAT64(op2);
- } else {
- d2 = JS_VALUE_GET_INT(op2);
- }
- res = (d1 == d2);
- } else {
- res = js_compare_bigint(ctx, OP_eq, op1, op2);
- }
- } else if (tag1 == tag2) {
- res = js_strict_eq2(ctx, op1, op2, JS_EQ_STRICT);
- } else if ((tag1 == JS_TAG_NULL && tag2 == JS_TAG_UNDEFINED) ||
- (tag2 == JS_TAG_NULL && tag1 == JS_TAG_UNDEFINED)) {
- res = TRUE;
- } else if (tag_is_string(tag1) && tag_is_string(tag2)) {
- /* needed when comparing strings and ropes */
- res = js_strict_eq2(ctx, op1, op2, JS_EQ_STRICT);
- } else if ((tag_is_string(tag1) && tag_is_number(tag2)) ||
- (tag_is_string(tag2) && tag_is_number(tag1))) {
- if (tag1 == JS_TAG_BIG_INT || tag1 == JS_TAG_SHORT_BIG_INT ||
- tag2 == JS_TAG_BIG_INT || tag2 == JS_TAG_SHORT_BIG_INT) {
- if (tag_is_string(tag1)) {
- op1 = JS_StringToBigInt(ctx, op1);
- if (JS_VALUE_GET_TAG(op1) != JS_TAG_BIG_INT &&
- JS_VALUE_GET_TAG(op1) != JS_TAG_SHORT_BIG_INT)
- goto invalid_bigint_string;
- }
- if (tag_is_string(tag2)) {
- op2 = JS_StringToBigInt(ctx, op2);
- if (JS_VALUE_GET_TAG(op2) != JS_TAG_BIG_INT &&
- JS_VALUE_GET_TAG(op2) != JS_TAG_SHORT_BIG_INT ) {
- invalid_bigint_string:
- JS_FreeValue(ctx, op1);
- JS_FreeValue(ctx, op2);
- res = FALSE;
- goto done;
- }
- }
- } else {
- op1 = JS_ToNumericFree(ctx, op1);
- if (JS_IsException(op1)) {
- JS_FreeValue(ctx, op2);
- goto exception;
- }
- op2 = JS_ToNumericFree(ctx, op2);
- if (JS_IsException(op2)) {
- JS_FreeValue(ctx, op1);
- goto exception;
- }
- }
- res = js_strict_eq2(ctx, op1, op2, JS_EQ_STRICT);
- } else if (tag1 == JS_TAG_BOOL) {
- op1 = JS_NewInt32(ctx, JS_VALUE_GET_INT(op1));
- goto redo;
- } else if (tag2 == JS_TAG_BOOL) {
- op2 = JS_NewInt32(ctx, JS_VALUE_GET_INT(op2));
- goto redo;
- } else if ((tag1 == JS_TAG_OBJECT &&
- (tag_is_number(tag2) || tag_is_string(tag2) || tag2 == JS_TAG_SYMBOL)) ||
- (tag2 == JS_TAG_OBJECT &&
- (tag_is_number(tag1) || tag_is_string(tag1) || tag1 == JS_TAG_SYMBOL))) {
- op1 = JS_ToPrimitiveFree(ctx, op1, HINT_NONE);
- if (JS_IsException(op1)) {
- JS_FreeValue(ctx, op2);
- goto exception;
- }
- op2 = JS_ToPrimitiveFree(ctx, op2, HINT_NONE);
- if (JS_IsException(op2)) {
- JS_FreeValue(ctx, op1);
- goto exception;
- }
- goto redo;
- } else {
- /* IsHTMLDDA object is equivalent to undefined for '==' and '!=' */
- if ((JS_IsHTMLDDA(ctx, op1) &&
- (tag2 == JS_TAG_NULL || tag2 == JS_TAG_UNDEFINED)) ||
- (JS_IsHTMLDDA(ctx, op2) &&
- (tag1 == JS_TAG_NULL || tag1 == JS_TAG_UNDEFINED))) {
- res = TRUE;
- } else {
- res = FALSE;
- }
- JS_FreeValue(ctx, op1);
- JS_FreeValue(ctx, op2);
- }
- done:
- sp[-2] = JS_NewBool(ctx, res ^ is_neq);
- return 0;
- exception:
- sp[-2] = JS_UNDEFINED;
- sp[-1] = JS_UNDEFINED;
- return -1;
- }
- static no_inline int js_shr_slow(JSContext *ctx, JSValue *sp)
- {
- JSValue op1, op2;
- uint32_t v1, v2, r;
- op1 = sp[-2];
- op2 = sp[-1];
- op1 = JS_ToNumericFree(ctx, op1);
- if (JS_IsException(op1)) {
- JS_FreeValue(ctx, op2);
- goto exception;
- }
- op2 = JS_ToNumericFree(ctx, op2);
- if (JS_IsException(op2)) {
- JS_FreeValue(ctx, op1);
- goto exception;
- }
- if (JS_VALUE_GET_TAG(op1) == JS_TAG_BIG_INT ||
- JS_VALUE_GET_TAG(op1) == JS_TAG_SHORT_BIG_INT ||
- JS_VALUE_GET_TAG(op2) == JS_TAG_BIG_INT ||
- JS_VALUE_GET_TAG(op2) == JS_TAG_SHORT_BIG_INT) {
- JS_ThrowTypeError(ctx, "bigint operands are forbidden for >>>");
- JS_FreeValue(ctx, op1);
- JS_FreeValue(ctx, op2);
- goto exception;
- }
- /* cannot give an exception */
- JS_ToUint32Free(ctx, &v1, op1);
- JS_ToUint32Free(ctx, &v2, op2);
- r = v1 >> (v2 & 0x1f);
- sp[-2] = JS_NewUint32(ctx, r);
- return 0;
- exception:
- sp[-2] = JS_UNDEFINED;
- sp[-1] = JS_UNDEFINED;
- return -1;
- }
- /* XXX: Should take JSValueConst arguments */
- static BOOL js_strict_eq2(JSContext *ctx, JSValue op1, JSValue op2,
- JSStrictEqModeEnum eq_mode)
- {
- BOOL res;
- int tag1, tag2;
- double d1, d2;
- tag1 = JS_VALUE_GET_NORM_TAG(op1);
- tag2 = JS_VALUE_GET_NORM_TAG(op2);
- switch(tag1) {
- case JS_TAG_BOOL:
- if (tag1 != tag2) {
- res = FALSE;
- } else {
- res = JS_VALUE_GET_INT(op1) == JS_VALUE_GET_INT(op2);
- goto done_no_free;
- }
- break;
- case JS_TAG_NULL:
- case JS_TAG_UNDEFINED:
- res = (tag1 == tag2);
- break;
- case JS_TAG_STRING:
- case JS_TAG_STRING_ROPE:
- {
- if (!tag_is_string(tag2)) {
- res = FALSE;
- } else if (tag1 == JS_TAG_STRING && tag2 == JS_TAG_STRING) {
- res = js_string_eq(ctx, JS_VALUE_GET_STRING(op1),
- JS_VALUE_GET_STRING(op2));
- } else {
- res = (js_string_rope_compare(ctx, op1, op2, TRUE) == 0);
- }
- }
- break;
- case JS_TAG_SYMBOL:
- {
- JSAtomStruct *p1, *p2;
- if (tag1 != tag2) {
- res = FALSE;
- } else {
- p1 = JS_VALUE_GET_PTR(op1);
- p2 = JS_VALUE_GET_PTR(op2);
- res = (p1 == p2);
- }
- }
- break;
- case JS_TAG_OBJECT:
- if (tag1 != tag2)
- res = FALSE;
- else
- res = JS_VALUE_GET_OBJ(op1) == JS_VALUE_GET_OBJ(op2);
- break;
- case JS_TAG_INT:
- d1 = JS_VALUE_GET_INT(op1);
- if (tag2 == JS_TAG_INT) {
- d2 = JS_VALUE_GET_INT(op2);
- goto number_test;
- } else if (tag2 == JS_TAG_FLOAT64) {
- d2 = JS_VALUE_GET_FLOAT64(op2);
- goto number_test;
- } else {
- res = FALSE;
- }
- break;
- case JS_TAG_FLOAT64:
- d1 = JS_VALUE_GET_FLOAT64(op1);
- if (tag2 == JS_TAG_FLOAT64) {
- d2 = JS_VALUE_GET_FLOAT64(op2);
- } else if (tag2 == JS_TAG_INT) {
- d2 = JS_VALUE_GET_INT(op2);
- } else {
- res = FALSE;
- break;
- }
- number_test:
- if (unlikely(eq_mode >= JS_EQ_SAME_VALUE)) {
- JSFloat64Union u1, u2;
- /* NaN is not always normalized, so this test is necessary */
- if (isnan(d1) || isnan(d2)) {
- res = isnan(d1) == isnan(d2);
- } else if (eq_mode == JS_EQ_SAME_VALUE_ZERO) {
- res = (d1 == d2); /* +0 == -0 */
- } else {
- u1.d = d1;
- u2.d = d2;
- res = (u1.u64 == u2.u64); /* +0 != -0 */
- }
- } else {
- res = (d1 == d2); /* if NaN return false and +0 == -0 */
- }
- goto done_no_free;
- case JS_TAG_SHORT_BIG_INT:
- case JS_TAG_BIG_INT:
- {
- JSBigIntBuf buf1, buf2;
- JSBigInt *p1, *p2;
- if (tag2 != JS_TAG_SHORT_BIG_INT &&
- tag2 != JS_TAG_BIG_INT) {
- res = FALSE;
- break;
- }
-
- if (JS_VALUE_GET_TAG(op1) == JS_TAG_SHORT_BIG_INT)
- p1 = js_bigint_set_short(&buf1, op1);
- else
- p1 = JS_VALUE_GET_PTR(op1);
- if (JS_VALUE_GET_TAG(op2) == JS_TAG_SHORT_BIG_INT)
- p2 = js_bigint_set_short(&buf2, op2);
- else
- p2 = JS_VALUE_GET_PTR(op2);
- res = (js_bigint_cmp(ctx, p1, p2) == 0);
- }
- break;
- default:
- res = FALSE;
- break;
- }
- JS_FreeValue(ctx, op1);
- JS_FreeValue(ctx, op2);
- done_no_free:
- return res;
- }
- static BOOL js_strict_eq(JSContext *ctx, JSValueConst op1, JSValueConst op2)
- {
- return js_strict_eq2(ctx,
- JS_DupValue(ctx, op1), JS_DupValue(ctx, op2),
- JS_EQ_STRICT);
- }
- BOOL JS_StrictEq(JSContext *ctx, JSValueConst op1, JSValueConst op2)
- {
- return js_strict_eq(ctx, op1, op2);
- }
- static BOOL js_same_value(JSContext *ctx, JSValueConst op1, JSValueConst op2)
- {
- return js_strict_eq2(ctx,
- JS_DupValue(ctx, op1), JS_DupValue(ctx, op2),
- JS_EQ_SAME_VALUE);
- }
- BOOL JS_SameValue(JSContext *ctx, JSValueConst op1, JSValueConst op2)
- {
- return js_same_value(ctx, op1, op2);
- }
- static BOOL js_same_value_zero(JSContext *ctx, JSValueConst op1, JSValueConst op2)
- {
- return js_strict_eq2(ctx,
- JS_DupValue(ctx, op1), JS_DupValue(ctx, op2),
- JS_EQ_SAME_VALUE_ZERO);
- }
- BOOL JS_SameValueZero(JSContext *ctx, JSValueConst op1, JSValueConst op2)
- {
- return js_same_value_zero(ctx, op1, op2);
- }
- static no_inline int js_strict_eq_slow(JSContext *ctx, JSValue *sp,
- BOOL is_neq)
- {
- BOOL res;
- res = js_strict_eq2(ctx, sp[-2], sp[-1], JS_EQ_STRICT);
- sp[-2] = JS_NewBool(ctx, res ^ is_neq);
- return 0;
- }
- static __exception int js_operator_in(JSContext *ctx, JSValue *sp)
- {
- JSValue op1, op2;
- JSAtom atom;
- int ret;
- op1 = sp[-2];
- op2 = sp[-1];
- if (JS_VALUE_GET_TAG(op2) != JS_TAG_OBJECT) {
- JS_ThrowTypeError(ctx, "invalid 'in' operand");
- return -1;
- }
- atom = JS_ValueToAtom(ctx, op1);
- if (unlikely(atom == JS_ATOM_NULL))
- return -1;
- ret = JS_HasProperty(ctx, op2, atom);
- JS_FreeAtom(ctx, atom);
- if (ret < 0)
- return -1;
- JS_FreeValue(ctx, op1);
- JS_FreeValue(ctx, op2);
- sp[-2] = JS_NewBool(ctx, ret);
- return 0;
- }
- static __exception int js_operator_private_in(JSContext *ctx, JSValue *sp)
- {
- JSValue op1, op2;
- int ret;
- op1 = sp[-2]; /* object */
- op2 = sp[-1]; /* field name or method function */
- if (JS_VALUE_GET_TAG(op1) != JS_TAG_OBJECT) {
- JS_ThrowTypeError(ctx, "invalid 'in' operand");
- return -1;
- }
- if (JS_IsObject(op2)) {
- /* method: use the brand */
- ret = JS_CheckBrand(ctx, op1, op2);
- if (ret < 0)
- return -1;
- } else {
- JSAtom atom;
- JSObject *p;
- JSShapeProperty *prs;
- JSProperty *pr;
- /* field */
- atom = JS_ValueToAtom(ctx, op2);
- if (unlikely(atom == JS_ATOM_NULL))
- return -1;
- p = JS_VALUE_GET_OBJ(op1);
- prs = find_own_property(&pr, p, atom);
- JS_FreeAtom(ctx, atom);
- ret = (prs != NULL);
- }
- JS_FreeValue(ctx, op1);
- JS_FreeValue(ctx, op2);
- sp[-2] = JS_NewBool(ctx, ret);
- return 0;
- }
- static __exception int js_has_unscopable(JSContext *ctx, JSValueConst obj,
- JSAtom atom)
- {
- JSValue arr, val;
- int ret;
- arr = JS_GetProperty(ctx, obj, JS_ATOM_Symbol_unscopables);
- if (JS_IsException(arr))
- return -1;
- ret = 0;
- if (JS_IsObject(arr)) {
- val = JS_GetProperty(ctx, arr, atom);
- ret = JS_ToBoolFree(ctx, val);
- }
- JS_FreeValue(ctx, arr);
- return ret;
- }
- static __exception int js_operator_instanceof(JSContext *ctx, JSValue *sp)
- {
- JSValue op1, op2;
- BOOL ret;
- op1 = sp[-2];
- op2 = sp[-1];
- ret = JS_IsInstanceOf(ctx, op1, op2);
- if (ret < 0)
- return ret;
- JS_FreeValue(ctx, op1);
- JS_FreeValue(ctx, op2);
- sp[-2] = JS_NewBool(ctx, ret);
- return 0;
- }
- static __exception int js_operator_typeof(JSContext *ctx, JSValueConst op1)
- {
- JSAtom atom;
- uint32_t tag;
- tag = JS_VALUE_GET_NORM_TAG(op1);
- switch(tag) {
- case JS_TAG_SHORT_BIG_INT:
- case JS_TAG_BIG_INT:
- atom = JS_ATOM_bigint;
- break;
- case JS_TAG_INT:
- case JS_TAG_FLOAT64:
- atom = JS_ATOM_number;
- break;
- case JS_TAG_UNDEFINED:
- atom = JS_ATOM_undefined;
- break;
- case JS_TAG_BOOL:
- atom = JS_ATOM_boolean;
- break;
- case JS_TAG_STRING:
- case JS_TAG_STRING_ROPE:
- atom = JS_ATOM_string;
- break;
- case JS_TAG_OBJECT:
- {
- JSObject *p;
- p = JS_VALUE_GET_OBJ(op1);
- if (unlikely(p->is_HTMLDDA))
- atom = JS_ATOM_undefined;
- else if (JS_IsFunction(ctx, op1))
- atom = JS_ATOM_function;
- else
- goto obj_type;
- }
- break;
- case JS_TAG_NULL:
- obj_type:
- atom = JS_ATOM_object;
- break;
- case JS_TAG_SYMBOL:
- atom = JS_ATOM_symbol;
- break;
- default:
- atom = JS_ATOM_unknown;
- break;
- }
- return atom;
- }
- static __exception int js_operator_delete(JSContext *ctx, JSValue *sp)
- {
- JSValue op1, op2;
- JSAtom atom;
- int ret;
- op1 = sp[-2];
- op2 = sp[-1];
- atom = JS_ValueToAtom(ctx, op2);
- if (unlikely(atom == JS_ATOM_NULL))
- return -1;
- ret = JS_DeleteProperty(ctx, op1, atom, JS_PROP_THROW_STRICT);
- JS_FreeAtom(ctx, atom);
- if (unlikely(ret < 0))
- return -1;
- JS_FreeValue(ctx, op1);
- JS_FreeValue(ctx, op2);
- sp[-2] = JS_NewBool(ctx, ret);
- return 0;
- }
- /* XXX: not 100% compatible, but mozilla seems to use a similar
- implementation to ensure that caller in non strict mode does not
- throw (ES5 compatibility) */
- static JSValue js_throw_type_error(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSFunctionBytecode *b = JS_GetFunctionBytecode(this_val);
- if (!b || (b->js_mode & JS_MODE_STRICT) || !b->has_prototype || argc >= 1) {
- return JS_ThrowTypeError(ctx, "invalid property access");
- }
- return JS_UNDEFINED;
- }
- static JSValue js_function_proto_fileName(JSContext *ctx,
- JSValueConst this_val)
- {
- JSFunctionBytecode *b = JS_GetFunctionBytecode(this_val);
- if (b && b->has_debug) {
- return JS_AtomToString(ctx, b->debug.filename);
- }
- return JS_UNDEFINED;
- }
- static JSValue js_function_proto_lineNumber(JSContext *ctx,
- JSValueConst this_val, int is_col)
- {
- JSFunctionBytecode *b = JS_GetFunctionBytecode(this_val);
- if (b && b->has_debug) {
- int line_num, col_num;
- line_num = find_line_num(ctx, b, -1, &col_num);
- if (is_col)
- return JS_NewInt32(ctx, col_num);
- else
- return JS_NewInt32(ctx, line_num);
- }
- return JS_UNDEFINED;
- }
- static int js_arguments_define_own_property(JSContext *ctx,
- JSValueConst this_obj,
- JSAtom prop, JSValueConst val,
- JSValueConst getter, JSValueConst setter, int flags)
- {
- JSObject *p;
- uint32_t idx;
- p = JS_VALUE_GET_OBJ(this_obj);
- /* convert to normal array when redefining an existing numeric field */
- if (p->fast_array && JS_AtomIsArrayIndex(ctx, &idx, prop) &&
- idx < p->u.array.count) {
- if (convert_fast_array_to_array(ctx, p))
- return -1;
- }
- /* run the default define own property */
- return JS_DefineProperty(ctx, this_obj, prop, val, getter, setter,
- flags | JS_PROP_NO_EXOTIC);
- }
- static const JSClassExoticMethods js_arguments_exotic_methods = {
- .define_own_property = js_arguments_define_own_property,
- };
- static JSValue js_build_arguments(JSContext *ctx, int argc, JSValueConst *argv)
- {
- JSValue val, *tab;
- JSProperty props[3];
- JSObject *p;
- int i;
- props[0].u.value = JS_NewInt32(ctx, argc); /* length */
- props[1].u.value = JS_DupValue(ctx, ctx->array_proto_values); /* Symbol.iterator */
- props[2].u.getset.getter = JS_VALUE_GET_OBJ(JS_DupValue(ctx, ctx->throw_type_error)); /* callee */
- props[2].u.getset.setter = JS_VALUE_GET_OBJ(JS_DupValue(ctx, ctx->throw_type_error)); /* callee */
-
- val = JS_NewObjectFromShape(ctx, js_dup_shape(ctx->arguments_shape),
- JS_CLASS_ARGUMENTS, props);
- if (JS_IsException(val))
- return val;
- p = JS_VALUE_GET_OBJ(val);
- /* initialize the fast array part */
- tab = NULL;
- if (argc > 0) {
- tab = js_malloc(ctx, sizeof(tab[0]) * argc);
- if (!tab)
- goto fail;
- for(i = 0; i < argc; i++) {
- tab[i] = JS_DupValue(ctx, argv[i]);
- }
- }
- p->u.array.u.values = tab;
- p->u.array.count = argc;
- return val;
- fail:
- JS_FreeValue(ctx, val);
- return JS_EXCEPTION;
- }
- #define GLOBAL_VAR_OFFSET 0x40000000
- #define ARGUMENT_VAR_OFFSET 0x20000000
- static void js_mapped_arguments_finalizer(JSRuntime *rt, JSValue val)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JSVarRef **var_refs = p->u.array.u.var_refs;
- int i;
- for(i = 0; i < p->u.array.count; i++)
- free_var_ref(rt, var_refs[i]);
- js_free_rt(rt, var_refs);
- }
- static void js_mapped_arguments_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JSVarRef **var_refs = p->u.array.u.var_refs;
- int i;
-
- for(i = 0; i < p->u.array.count; i++)
- mark_func(rt, &var_refs[i]->header);
- }
- /* legacy arguments object: add references to the function arguments */
- static JSValue js_build_mapped_arguments(JSContext *ctx, int argc,
- JSValueConst *argv,
- JSStackFrame *sf, int arg_count)
- {
- JSValue val;
- JSProperty props[3];
- JSVarRef **tab, *var_ref;
- JSObject *p;
- int i, j;
- props[0].u.value = JS_NewInt32(ctx, argc); /* length */
- props[1].u.value = JS_DupValue(ctx, ctx->array_proto_values); /* Symbol.iterator */
- props[2].u.value = JS_DupValue(ctx, ctx->rt->current_stack_frame->cur_func); /* callee */
-
- val = JS_NewObjectFromShape(ctx, js_dup_shape(ctx->mapped_arguments_shape),
- JS_CLASS_MAPPED_ARGUMENTS, props);
- if (JS_IsException(val))
- return val;
- p = JS_VALUE_GET_OBJ(val);
- /* initialize the fast array part */
- tab = NULL;
- if (argc > 0) {
- tab = js_malloc(ctx, sizeof(tab[0]) * argc);
- if (!tab)
- goto fail;
- for(i = 0; i < arg_count; i++) {
- var_ref = get_var_ref(ctx, sf, i, TRUE);
- if (!var_ref)
- goto fail1;
- tab[i] = var_ref;
- }
- for(i = arg_count; i < argc; i++) {
- var_ref = js_create_var_ref(ctx, FALSE);
- if (!var_ref) {
- fail1:
- for(j = 0; j < i; j++)
- free_var_ref(ctx->rt, tab[j]);
- js_free(ctx, tab);
- goto fail;
- }
- var_ref->value = JS_DupValue(ctx, argv[i]);
- tab[i] = var_ref;
- }
- }
- p->u.array.u.var_refs = tab;
- p->u.array.count = argc;
- return val;
- fail:
- JS_FreeValue(ctx, val);
- return JS_EXCEPTION;
- }
- static JSValue build_for_in_iterator(JSContext *ctx, JSValue obj)
- {
- JSObject *p, *p1;
- JSPropertyEnum *tab_atom;
- int i;
- JSValue enum_obj;
- JSForInIterator *it;
- uint32_t tag, tab_atom_count;
- tag = JS_VALUE_GET_TAG(obj);
- if (tag != JS_TAG_OBJECT && tag != JS_TAG_NULL && tag != JS_TAG_UNDEFINED) {
- obj = JS_ToObjectFree(ctx, obj);
- }
- it = js_malloc(ctx, sizeof(*it));
- if (!it) {
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- enum_obj = JS_NewObjectProtoClass(ctx, JS_NULL, JS_CLASS_FOR_IN_ITERATOR);
- if (JS_IsException(enum_obj)) {
- js_free(ctx, it);
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- it->is_array = FALSE;
- it->obj = obj;
- it->idx = 0;
- it->tab_atom = NULL;
- it->atom_count = 0;
- it->in_prototype_chain = FALSE;
- p1 = JS_VALUE_GET_OBJ(enum_obj);
- p1->u.for_in_iterator = it;
- if (tag == JS_TAG_NULL || tag == JS_TAG_UNDEFINED)
- return enum_obj;
- p = JS_VALUE_GET_OBJ(obj);
- if (p->fast_array) {
- JSShape *sh;
- JSShapeProperty *prs;
- /* check that there are no enumerable normal fields */
- sh = p->shape;
- for(i = 0, prs = get_shape_prop(sh); i < sh->prop_count; i++, prs++) {
- if (prs->flags & JS_PROP_ENUMERABLE)
- goto normal_case;
- }
- /* for fast arrays, we only store the number of elements */
- it->is_array = TRUE;
- it->atom_count = p->u.array.count;
- } else {
- normal_case:
- if (JS_GetOwnPropertyNamesInternal(ctx, &tab_atom, &tab_atom_count, p,
- JS_GPN_STRING_MASK | JS_GPN_SET_ENUM)) {
- JS_FreeValue(ctx, enum_obj);
- return JS_EXCEPTION;
- }
- it->tab_atom = tab_atom;
- it->atom_count = tab_atom_count;
- }
- return enum_obj;
- }
- /* obj -> enum_obj */
- static __exception int js_for_in_start(JSContext *ctx, JSValue *sp)
- {
- sp[-1] = build_for_in_iterator(ctx, sp[-1]);
- if (JS_IsException(sp[-1]))
- return -1;
- return 0;
- }
- /* return -1 if exception, 0 if slow case, 1 if the enumeration is finished */
- static __exception int js_for_in_prepare_prototype_chain_enum(JSContext *ctx,
- JSValueConst enum_obj)
- {
- JSObject *p;
- JSForInIterator *it;
- JSPropertyEnum *tab_atom;
- uint32_t tab_atom_count, i;
- JSValue obj1;
- p = JS_VALUE_GET_OBJ(enum_obj);
- it = p->u.for_in_iterator;
- /* check if there are enumerable properties in the prototype chain (fast path) */
- obj1 = JS_DupValue(ctx, it->obj);
- for(;;) {
- obj1 = JS_GetPrototypeFree(ctx, obj1);
- if (JS_IsNull(obj1))
- break;
- if (JS_IsException(obj1))
- goto fail;
- if (JS_GetOwnPropertyNamesInternal(ctx, &tab_atom, &tab_atom_count,
- JS_VALUE_GET_OBJ(obj1),
- JS_GPN_STRING_MASK | JS_GPN_ENUM_ONLY)) {
- JS_FreeValue(ctx, obj1);
- goto fail;
- }
- JS_FreePropertyEnum(ctx, tab_atom, tab_atom_count);
- if (tab_atom_count != 0) {
- JS_FreeValue(ctx, obj1);
- goto slow_path;
- }
- /* must check for timeout to avoid infinite loop */
- if (js_poll_interrupts(ctx)) {
- JS_FreeValue(ctx, obj1);
- goto fail;
- }
- }
- JS_FreeValue(ctx, obj1);
- return 1;
- slow_path:
- /* add the visited properties, even if they are not enumerable */
- if (it->is_array) {
- if (JS_GetOwnPropertyNamesInternal(ctx, &tab_atom, &tab_atom_count,
- JS_VALUE_GET_OBJ(it->obj),
- JS_GPN_STRING_MASK | JS_GPN_SET_ENUM)) {
- goto fail;
- }
- it->is_array = FALSE;
- it->tab_atom = tab_atom;
- it->atom_count = tab_atom_count;
- }
- for(i = 0; i < it->atom_count; i++) {
- if (JS_DefinePropertyValue(ctx, enum_obj, it->tab_atom[i].atom, JS_NULL, JS_PROP_ENUMERABLE) < 0)
- goto fail;
- }
- return 0;
- fail:
- return -1;
- }
- /* enum_obj -> enum_obj value done */
- static __exception int js_for_in_next(JSContext *ctx, JSValue *sp)
- {
- JSValueConst enum_obj;
- JSObject *p;
- JSAtom prop;
- JSForInIterator *it;
- JSPropertyEnum *tab_atom;
- uint32_t tab_atom_count;
- int ret;
- enum_obj = sp[-1];
- /* fail safe */
- if (JS_VALUE_GET_TAG(enum_obj) != JS_TAG_OBJECT)
- goto done;
- p = JS_VALUE_GET_OBJ(enum_obj);
- if (p->class_id != JS_CLASS_FOR_IN_ITERATOR)
- goto done;
- it = p->u.for_in_iterator;
- for(;;) {
- if (it->idx >= it->atom_count) {
- if (JS_IsNull(it->obj) || JS_IsUndefined(it->obj))
- goto done; /* not an object */
- /* no more property in the current object: look in the prototype */
- if (!it->in_prototype_chain) {
- ret = js_for_in_prepare_prototype_chain_enum(ctx, enum_obj);
- if (ret < 0)
- return -1;
- if (ret)
- goto done;
- it->in_prototype_chain = TRUE;
- }
- it->obj = JS_GetPrototypeFree(ctx, it->obj);
- if (JS_IsException(it->obj))
- return -1;
- if (JS_IsNull(it->obj))
- goto done; /* no more prototype */
- /* must check for timeout to avoid infinite loop */
- if (js_poll_interrupts(ctx))
- return -1;
- if (JS_GetOwnPropertyNamesInternal(ctx, &tab_atom, &tab_atom_count,
- JS_VALUE_GET_OBJ(it->obj),
- JS_GPN_STRING_MASK | JS_GPN_SET_ENUM)) {
- return -1;
- }
- JS_FreePropertyEnum(ctx, it->tab_atom, it->atom_count);
- it->tab_atom = tab_atom;
- it->atom_count = tab_atom_count;
- it->idx = 0;
- } else {
- if (it->is_array) {
- prop = __JS_AtomFromUInt32(it->idx);
- it->idx++;
- } else {
- BOOL is_enumerable;
- prop = it->tab_atom[it->idx].atom;
- is_enumerable = it->tab_atom[it->idx].is_enumerable;
- it->idx++;
- if (it->in_prototype_chain) {
- /* slow case: we are in the prototype chain */
- ret = JS_GetOwnPropertyInternal(ctx, NULL, JS_VALUE_GET_OBJ(enum_obj), prop);
- if (ret < 0)
- return ret;
- if (ret)
- continue; /* already visited */
- /* add to the visited property list */
- if (JS_DefinePropertyValue(ctx, enum_obj, prop, JS_NULL,
- JS_PROP_ENUMERABLE) < 0)
- return -1;
- }
- if (!is_enumerable)
- continue;
- }
- /* check if the property was deleted */
- ret = JS_GetOwnPropertyInternal(ctx, NULL, JS_VALUE_GET_OBJ(it->obj), prop);
- if (ret < 0)
- return ret;
- if (ret)
- break;
- }
- }
- /* return the property */
- sp[0] = JS_AtomToValue(ctx, prop);
- sp[1] = JS_FALSE;
- return 0;
- done:
- /* return the end */
- sp[0] = JS_UNDEFINED;
- sp[1] = JS_TRUE;
- return 0;
- }
- static JSValue JS_GetIterator2(JSContext *ctx, JSValueConst obj,
- JSValueConst method)
- {
- JSValue enum_obj;
- enum_obj = JS_Call(ctx, method, obj, 0, NULL);
- if (JS_IsException(enum_obj))
- return enum_obj;
- if (!JS_IsObject(enum_obj)) {
- JS_FreeValue(ctx, enum_obj);
- return JS_ThrowTypeErrorNotAnObject(ctx);
- }
- return enum_obj;
- }
- static JSValue JS_GetIterator(JSContext *ctx, JSValueConst obj, BOOL is_async)
- {
- JSValue method, ret, sync_iter;
- if (is_async) {
- method = JS_GetProperty(ctx, obj, JS_ATOM_Symbol_asyncIterator);
- if (JS_IsException(method))
- return method;
- if (JS_IsUndefined(method) || JS_IsNull(method)) {
- method = JS_GetProperty(ctx, obj, JS_ATOM_Symbol_iterator);
- if (JS_IsException(method))
- return method;
- sync_iter = JS_GetIterator2(ctx, obj, method);
- JS_FreeValue(ctx, method);
- if (JS_IsException(sync_iter))
- return sync_iter;
- ret = JS_CreateAsyncFromSyncIterator(ctx, sync_iter);
- JS_FreeValue(ctx, sync_iter);
- return ret;
- }
- } else {
- method = JS_GetProperty(ctx, obj, JS_ATOM_Symbol_iterator);
- if (JS_IsException(method))
- return method;
- }
- if (!JS_IsFunction(ctx, method)) {
- JS_FreeValue(ctx, method);
- return JS_ThrowTypeError(ctx, "value is not iterable");
- }
- ret = JS_GetIterator2(ctx, obj, method);
- JS_FreeValue(ctx, method);
- return ret;
- }
- /* return *pdone = 2 if the iterator object is not parsed */
- static JSValue JS_IteratorNext2(JSContext *ctx, JSValueConst enum_obj,
- JSValueConst method,
- int argc, JSValueConst *argv, int *pdone)
- {
- JSValue obj;
- /* fast path for the built-in iterators (avoid creating the
- intermediate result object) */
- if (JS_IsObject(method)) {
- JSObject *p = JS_VALUE_GET_OBJ(method);
- if (p->class_id == JS_CLASS_C_FUNCTION &&
- p->u.cfunc.cproto == JS_CFUNC_iterator_next) {
- JSCFunctionType func;
- JSValueConst args[1];
- /* in case the function expects one argument */
- if (argc == 0) {
- args[0] = JS_UNDEFINED;
- argv = args;
- }
- func = p->u.cfunc.c_function;
- return func.iterator_next(ctx, enum_obj, argc, argv,
- pdone, p->u.cfunc.magic);
- }
- }
- obj = JS_Call(ctx, method, enum_obj, argc, argv);
- if (JS_IsException(obj))
- goto fail;
- if (!JS_IsObject(obj)) {
- JS_FreeValue(ctx, obj);
- JS_ThrowTypeError(ctx, "iterator must return an object");
- goto fail;
- }
- *pdone = 2;
- return obj;
- fail:
- *pdone = FALSE;
- return JS_EXCEPTION;
- }
- /* Note: always return JS_UNDEFINED when *pdone = TRUE. */
- static JSValue JS_IteratorNext(JSContext *ctx, JSValueConst enum_obj,
- JSValueConst method,
- int argc, JSValueConst *argv, BOOL *pdone)
- {
- JSValue obj, value, done_val;
- int done;
- obj = JS_IteratorNext2(ctx, enum_obj, method, argc, argv, &done);
- if (JS_IsException(obj))
- goto fail;
- if (likely(done == 0)) {
- *pdone = FALSE;
- return obj;
- } else if (done != 2) {
- JS_FreeValue(ctx, obj);
- *pdone = TRUE;
- return JS_UNDEFINED;
- } else {
- done_val = JS_GetProperty(ctx, obj, JS_ATOM_done);
- if (JS_IsException(done_val))
- goto fail;
- *pdone = JS_ToBoolFree(ctx, done_val);
- value = JS_UNDEFINED;
- if (!*pdone) {
- value = JS_GetProperty(ctx, obj, JS_ATOM_value);
- }
- JS_FreeValue(ctx, obj);
- return value;
- }
- fail:
- JS_FreeValue(ctx, obj);
- *pdone = FALSE;
- return JS_EXCEPTION;
- }
- /* return < 0 in case of exception */
- static int JS_IteratorClose(JSContext *ctx, JSValueConst enum_obj,
- BOOL is_exception_pending)
- {
- JSValue method, ret, ex_obj;
- int res;
- if (is_exception_pending) {
- ex_obj = ctx->rt->current_exception;
- ctx->rt->current_exception = JS_UNINITIALIZED;
- res = -1;
- } else {
- ex_obj = JS_UNDEFINED;
- res = 0;
- }
- method = JS_GetProperty(ctx, enum_obj, JS_ATOM_return);
- if (JS_IsException(method)) {
- res = -1;
- goto done;
- }
- if (JS_IsUndefined(method) || JS_IsNull(method)) {
- goto done;
- }
- ret = JS_CallFree(ctx, method, enum_obj, 0, NULL);
- if (!is_exception_pending) {
- if (JS_IsException(ret)) {
- res = -1;
- } else if (!JS_IsObject(ret)) {
- JS_ThrowTypeErrorNotAnObject(ctx);
- res = -1;
- }
- }
- JS_FreeValue(ctx, ret);
- done:
- if (is_exception_pending) {
- JS_Throw(ctx, ex_obj);
- }
- return res;
- }
- /* obj -> enum_rec (3 slots) */
- static __exception int js_for_of_start(JSContext *ctx, JSValue *sp,
- BOOL is_async)
- {
- JSValue op1, obj, method;
- op1 = sp[-1];
- obj = JS_GetIterator(ctx, op1, is_async);
- if (JS_IsException(obj))
- return -1;
- JS_FreeValue(ctx, op1);
- sp[-1] = obj;
- method = JS_GetProperty(ctx, obj, JS_ATOM_next);
- if (JS_IsException(method))
- return -1;
- sp[0] = method;
- return 0;
- }
- /* enum_rec [objs] -> enum_rec [objs] value done. There are 'offset'
- objs. If 'done' is true or in case of exception, 'enum_rec' is set
- to undefined. If 'done' is true, 'value' is always set to
- undefined. */
- static __exception int js_for_of_next(JSContext *ctx, JSValue *sp, int offset)
- {
- JSValue value = JS_UNDEFINED;
- int done = 1;
- if (likely(!JS_IsUndefined(sp[offset]))) {
- value = JS_IteratorNext(ctx, sp[offset], sp[offset + 1], 0, NULL, &done);
- if (JS_IsException(value))
- done = -1;
- if (done) {
- /* value is JS_UNDEFINED or JS_EXCEPTION */
- /* replace the iteration object with undefined */
- JS_FreeValue(ctx, sp[offset]);
- sp[offset] = JS_UNDEFINED;
- if (done < 0) {
- return -1;
- } else {
- JS_FreeValue(ctx, value);
- value = JS_UNDEFINED;
- }
- }
- }
- sp[0] = value;
- sp[1] = JS_NewBool(ctx, done);
- return 0;
- }
- static __exception int js_for_await_of_next(JSContext *ctx, JSValue *sp)
- {
- JSValue obj, iter, next;
- sp[-1] = JS_UNDEFINED; /* disable the catch offset so that
- exceptions do not close the iterator */
- iter = sp[-3];
- next = sp[-2];
- obj = JS_Call(ctx, next, iter, 0, NULL);
- if (JS_IsException(obj))
- return -1;
- sp[0] = obj;
- return 0;
- }
- static JSValue JS_IteratorGetCompleteValue(JSContext *ctx, JSValueConst obj,
- BOOL *pdone)
- {
- JSValue done_val, value;
- BOOL done;
- done_val = JS_GetProperty(ctx, obj, JS_ATOM_done);
- if (JS_IsException(done_val))
- goto fail;
- done = JS_ToBoolFree(ctx, done_val);
- value = JS_GetProperty(ctx, obj, JS_ATOM_value);
- if (JS_IsException(value))
- goto fail;
- *pdone = done;
- return value;
- fail:
- *pdone = FALSE;
- return JS_EXCEPTION;
- }
- static __exception int js_iterator_get_value_done(JSContext *ctx, JSValue *sp)
- {
- JSValue obj, value;
- BOOL done;
- obj = sp[-1];
- if (!JS_IsObject(obj)) {
- JS_ThrowTypeError(ctx, "iterator must return an object");
- return -1;
- }
- value = JS_IteratorGetCompleteValue(ctx, obj, &done);
- if (JS_IsException(value))
- return -1;
- JS_FreeValue(ctx, obj);
- /* put again the catch offset so that exceptions close the
- iterator */
- sp[-2] = JS_NewCatchOffset(ctx, 0);
- sp[-1] = value;
- sp[0] = JS_NewBool(ctx, done);
- return 0;
- }
- static JSValue js_create_iterator_result(JSContext *ctx,
- JSValue val,
- BOOL done)
- {
- JSValue obj;
- obj = JS_NewObject(ctx);
- if (JS_IsException(obj)) {
- JS_FreeValue(ctx, val);
- return obj;
- }
- if (JS_DefinePropertyValue(ctx, obj, JS_ATOM_value,
- val, JS_PROP_C_W_E) < 0) {
- goto fail;
- }
- if (JS_DefinePropertyValue(ctx, obj, JS_ATOM_done,
- JS_NewBool(ctx, done), JS_PROP_C_W_E) < 0) {
- fail:
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- return obj;
- }
- static JSValue js_array_iterator_next(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv,
- BOOL *pdone, int magic);
- static JSValue js_create_array_iterator(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic);
- static BOOL js_is_fast_array(JSContext *ctx, JSValueConst obj)
- {
- /* Try and handle fast arrays explicitly */
- if (JS_VALUE_GET_TAG(obj) == JS_TAG_OBJECT) {
- JSObject *p = JS_VALUE_GET_OBJ(obj);
- if (p->class_id == JS_CLASS_ARRAY && p->fast_array) {
- return TRUE;
- }
- }
- return FALSE;
- }
- /* Access an Array's internal JSValue array if available */
- static BOOL js_get_fast_array(JSContext *ctx, JSValueConst obj,
- JSValue **arrpp, uint32_t *countp)
- {
- /* Try and handle fast arrays explicitly */
- if (JS_VALUE_GET_TAG(obj) == JS_TAG_OBJECT) {
- JSObject *p = JS_VALUE_GET_OBJ(obj);
- if (p->class_id == JS_CLASS_ARRAY && p->fast_array) {
- *countp = p->u.array.count;
- *arrpp = p->u.array.u.values;
- return TRUE;
- }
- }
- return FALSE;
- }
- static __exception int js_append_enumerate(JSContext *ctx, JSValue *sp)
- {
- JSValue iterator, enumobj, method, value;
- int is_array_iterator;
- JSValue *arrp;
- uint32_t i, count32, pos;
- JSCFunctionType ft;
- if (JS_VALUE_GET_TAG(sp[-2]) != JS_TAG_INT) {
- JS_ThrowInternalError(ctx, "invalid index for append");
- return -1;
- }
- pos = JS_VALUE_GET_INT(sp[-2]);
- /* XXX: further optimisations:
- - use ctx->array_proto_values?
- - check if array_iterator_prototype next method is built-in and
- avoid constructing actual iterator object?
- - build this into js_for_of_start and use in all `for (x of o)` loops
- */
- iterator = JS_GetProperty(ctx, sp[-1], JS_ATOM_Symbol_iterator);
- if (JS_IsException(iterator))
- return -1;
- ft.generic_magic = js_create_array_iterator;
- is_array_iterator = JS_IsCFunction(ctx, iterator, ft.generic,
- JS_ITERATOR_KIND_VALUE);
- JS_FreeValue(ctx, iterator);
- enumobj = JS_GetIterator(ctx, sp[-1], FALSE);
- if (JS_IsException(enumobj))
- return -1;
- method = JS_GetProperty(ctx, enumobj, JS_ATOM_next);
- if (JS_IsException(method)) {
- JS_FreeValue(ctx, enumobj);
- return -1;
- }
- ft.iterator_next = js_array_iterator_next;
- if (is_array_iterator
- && JS_IsCFunction(ctx, method, ft.generic, 0)
- && js_get_fast_array(ctx, sp[-1], &arrp, &count32)) {
- uint32_t len;
- if (js_get_length32(ctx, &len, sp[-1]))
- goto exception;
- /* if len > count32, the elements >= count32 might be read in
- the prototypes and might have side effects */
- if (len != count32)
- goto general_case;
- /* Handle fast arrays explicitly */
- for (i = 0; i < count32; i++) {
- if (JS_DefinePropertyValueUint32(ctx, sp[-3], pos++,
- JS_DupValue(ctx, arrp[i]), JS_PROP_C_W_E) < 0)
- goto exception;
- }
- } else {
- general_case:
- for (;;) {
- BOOL done;
- value = JS_IteratorNext(ctx, enumobj, method, 0, NULL, &done);
- if (JS_IsException(value))
- goto exception;
- if (done) {
- /* value is JS_UNDEFINED */
- break;
- }
- if (JS_DefinePropertyValueUint32(ctx, sp[-3], pos++, value, JS_PROP_C_W_E) < 0)
- goto exception;
- }
- }
- /* Note: could raise an error if too many elements */
- sp[-2] = JS_NewInt32(ctx, pos);
- JS_FreeValue(ctx, enumobj);
- JS_FreeValue(ctx, method);
- return 0;
- exception:
- JS_IteratorClose(ctx, enumobj, TRUE);
- JS_FreeValue(ctx, enumobj);
- JS_FreeValue(ctx, method);
- return -1;
- }
- static __exception int JS_CopyDataProperties(JSContext *ctx,
- JSValueConst target,
- JSValueConst source,
- JSValueConst excluded,
- BOOL setprop)
- {
- JSPropertyEnum *tab_atom;
- JSValue val;
- uint32_t i, tab_atom_count;
- JSObject *p;
- JSObject *pexcl = NULL;
- int ret, gpn_flags;
- JSPropertyDescriptor desc;
- BOOL is_enumerable;
- if (JS_VALUE_GET_TAG(source) != JS_TAG_OBJECT)
- return 0;
- if (JS_VALUE_GET_TAG(excluded) == JS_TAG_OBJECT)
- pexcl = JS_VALUE_GET_OBJ(excluded);
- p = JS_VALUE_GET_OBJ(source);
- gpn_flags = JS_GPN_STRING_MASK | JS_GPN_SYMBOL_MASK | JS_GPN_ENUM_ONLY;
- if (p->is_exotic) {
- const JSClassExoticMethods *em = ctx->rt->class_array[p->class_id].exotic;
- /* cannot use JS_GPN_ENUM_ONLY with e.g. proxies because it
- introduces a visible change */
- if (em && em->get_own_property_names) {
- gpn_flags &= ~JS_GPN_ENUM_ONLY;
- }
- }
- if (JS_GetOwnPropertyNamesInternal(ctx, &tab_atom, &tab_atom_count, p,
- gpn_flags))
- return -1;
- for (i = 0; i < tab_atom_count; i++) {
- if (pexcl) {
- ret = JS_GetOwnPropertyInternal(ctx, NULL, pexcl, tab_atom[i].atom);
- if (ret) {
- if (ret < 0)
- goto exception;
- continue;
- }
- }
- if (!(gpn_flags & JS_GPN_ENUM_ONLY)) {
- /* test if the property is enumerable */
- ret = JS_GetOwnPropertyInternal(ctx, &desc, p, tab_atom[i].atom);
- if (ret < 0)
- goto exception;
- if (!ret)
- continue;
- is_enumerable = (desc.flags & JS_PROP_ENUMERABLE) != 0;
- js_free_desc(ctx, &desc);
- if (!is_enumerable)
- continue;
- }
- val = JS_GetProperty(ctx, source, tab_atom[i].atom);
- if (JS_IsException(val))
- goto exception;
- if (setprop)
- ret = JS_SetProperty(ctx, target, tab_atom[i].atom, val);
- else
- ret = JS_DefinePropertyValue(ctx, target, tab_atom[i].atom, val,
- JS_PROP_C_W_E);
- if (ret < 0)
- goto exception;
- }
- JS_FreePropertyEnum(ctx, tab_atom, tab_atom_count);
- return 0;
- exception:
- JS_FreePropertyEnum(ctx, tab_atom, tab_atom_count);
- return -1;
- }
- /* only valid inside C functions */
- static JSValueConst JS_GetActiveFunction(JSContext *ctx)
- {
- return ctx->rt->current_stack_frame->cur_func;
- }
- static JSVarRef *js_create_var_ref(JSContext *ctx, BOOL is_lexical)
- {
- JSVarRef *var_ref;
- var_ref = js_malloc(ctx, sizeof(JSVarRef));
- if (!var_ref)
- return NULL;
- js_rc(var_ref)->ref_count = 1;
- if (is_lexical)
- var_ref->value = JS_UNINITIALIZED;
- else
- var_ref->value = JS_UNDEFINED;
- var_ref->pvalue = &var_ref->value;
- var_ref->is_detached = TRUE;
- var_ref->is_lexical = FALSE;
- var_ref->is_const = FALSE;
- add_gc_object(ctx->rt, &var_ref->header, JS_GC_OBJ_TYPE_VAR_REF);
- return var_ref;
- }
- static JSVarRef *get_var_ref(JSContext *ctx, JSStackFrame *sf, int var_idx,
- BOOL is_arg)
- {
- JSObject *p;
- JSFunctionBytecode *b;
- JSVarRef *var_ref;
- JSValue *pvalue;
- int var_ref_idx;
- JSBytecodeVarDef *vd;
-
- p = JS_VALUE_GET_OBJ(sf->cur_func);
- b = p->u.func.function_bytecode;
-
- if (is_arg) {
- vd = &b->vardefs[var_idx];
- pvalue = &sf->arg_buf[var_idx];
- } else {
- vd = &b->vardefs[b->arg_count + var_idx];
- pvalue = &sf->var_buf[var_idx];
- }
- assert(vd->is_captured);
- var_ref_idx = vd->var_ref_idx;
- assert(var_ref_idx < b->var_ref_count);
- var_ref = sf->var_refs[var_ref_idx];
- if (var_ref) {
- /* reference to the already created local variable */
- assert(var_ref->pvalue == pvalue);
- js_rc(var_ref)->ref_count++;
- return var_ref;
- }
- /* create a new one */
- var_ref = js_malloc(ctx, sizeof(JSVarRef));
- if (!var_ref)
- return NULL;
- js_rc(var_ref)->ref_count = 1;
- add_gc_object(ctx->rt, &var_ref->header, JS_GC_OBJ_TYPE_VAR_REF);
- var_ref->is_detached = FALSE;
- var_ref->is_lexical = FALSE;
- var_ref->is_const = FALSE;
- var_ref->var_ref_idx = var_ref_idx;
- var_ref->stack_frame = sf;
- sf->var_refs[var_ref_idx] = var_ref;
- if (sf->js_mode & JS_MODE_ASYNC) {
- JSAsyncFunctionState *async_func = container_of(sf, JSAsyncFunctionState, frame);
- /* The stack frame is detached and may be destroyed at any
- time so its reference count must be increased. Calling
- close_var_refs() when destroying the stack frame is not
- possible because it would change the graph between the GC
- objects. Another solution could be to temporarily detach
- the JSVarRef of async functions during the GC. It would
- have the advantage of allowing the release of unused stack
- frames in a cycle. */
- js_rc(async_func)->ref_count++;
- }
- var_ref->pvalue = pvalue;
- return var_ref;
- }
- static void js_global_object_finalizer(JSRuntime *rt, JSValue obj)
- {
- JSObject *p = JS_VALUE_GET_OBJ(obj);
- JS_FreeValueRT(rt, p->u.global_object.uninitialized_vars);
- }
- static void js_global_object_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JS_MarkValue(rt, p->u.global_object.uninitialized_vars, mark_func);
- }
- static JSVarRef *js_global_object_get_uninitialized_var(JSContext *ctx, JSObject *p1,
- JSAtom atom)
- {
- JSObject *p = JS_VALUE_GET_OBJ(p1->u.global_object.uninitialized_vars);
- JSShapeProperty *prs;
- JSProperty *pr;
- JSVarRef *var_ref;
-
- prs = find_own_property(&pr, p, atom);
- if (prs) {
- assert((prs->flags & JS_PROP_TMASK) == JS_PROP_VARREF);
- var_ref = pr->u.var_ref;
- js_rc(var_ref)->ref_count++;
- return var_ref;
- }
- var_ref = js_create_var_ref(ctx, TRUE);
- if (!var_ref)
- return NULL;
- pr = add_property(ctx, p, atom, JS_PROP_C_W_E | JS_PROP_VARREF);
- if (unlikely(!pr)) {
- free_var_ref(ctx->rt, var_ref);
- return NULL;
- }
- pr->u.var_ref = var_ref;
- js_rc(var_ref)->ref_count++;
- return var_ref;
- }
- /* return a new variable reference. Get it from the uninitialized
- variables if it is present. Return NULL in case of memory error. */
- static JSVarRef *js_global_object_find_uninitialized_var(JSContext *ctx, JSObject *p,
- JSAtom atom, BOOL is_lexical)
- {
- JSObject *p1;
- JSShapeProperty *prs;
- JSProperty *pr;
- JSVarRef *var_ref;
-
- p1 = JS_VALUE_GET_OBJ(p->u.global_object.uninitialized_vars);
- prs = find_own_property(&pr, p1, atom);
- if (prs) {
- assert((prs->flags & JS_PROP_TMASK) == JS_PROP_VARREF);
- var_ref = pr->u.var_ref;
- js_rc(var_ref)->ref_count++;
- delete_property(ctx, p1, atom);
- if (!is_lexical)
- var_ref->value = JS_UNDEFINED;
- } else {
- var_ref = js_create_var_ref(ctx, is_lexical);
- if (!var_ref)
- return NULL;
- }
- return var_ref;
- }
- static JSVarRef *js_closure_define_global_var(JSContext *ctx, JSClosureVar *cv,
- BOOL is_direct_or_indirect_eval)
- {
- JSObject *p, *p1;
- JSShapeProperty *prs;
- int flags;
- JSProperty *pr;
- JSVarRef *var_ref;
-
- if (cv->is_lexical) {
- p = JS_VALUE_GET_OBJ(ctx->global_var_obj);
- flags = JS_PROP_ENUMERABLE | JS_PROP_CONFIGURABLE;
- if (!cv->is_const)
- flags |= JS_PROP_WRITABLE;
- prs = find_own_property(&pr, p, cv->var_name);
- if (prs) {
- assert((prs->flags & JS_PROP_TMASK) == JS_PROP_VARREF);
- var_ref = pr->u.var_ref;
- js_rc(var_ref)->ref_count++;
- return var_ref;
- }
- /* if there is a corresponding global variable, reuse its
- reference and create a new one for the global variable */
- p1 = JS_VALUE_GET_OBJ(ctx->global_obj);
- prs = find_own_property(&pr, p1, cv->var_name);
- if (prs && (prs->flags & JS_PROP_TMASK) == JS_PROP_VARREF) {
- JSVarRef *var_ref1;
- var_ref1 = js_create_var_ref(ctx, FALSE);
- if (!var_ref1)
- return NULL;
- var_ref = pr->u.var_ref;
- var_ref1->value = var_ref->value;
- var_ref->value = JS_UNINITIALIZED;
- pr->u.var_ref = var_ref1;
- goto add_var_ref;
- }
- } else {
- p = JS_VALUE_GET_OBJ(ctx->global_obj);
- flags = JS_PROP_ENUMERABLE | JS_PROP_WRITABLE;
- if (is_direct_or_indirect_eval)
- flags |= JS_PROP_CONFIGURABLE;
- retry:
- prs = find_own_property(&pr, p, cv->var_name);
- if (prs) {
- if (unlikely((prs->flags & JS_PROP_TMASK) == JS_PROP_AUTOINIT)) {
- if (JS_AutoInitProperty(ctx, p, cv->var_name, pr, prs))
- return NULL;
- goto retry;
- } else if ((prs->flags & JS_PROP_TMASK) != JS_PROP_VARREF) {
- var_ref = js_global_object_get_uninitialized_var(ctx, p, cv->var_name);
- if (!var_ref)
- return NULL;
- } else {
- var_ref = pr->u.var_ref;
- js_rc(var_ref)->ref_count++;
- }
- if (cv->var_kind == JS_VAR_GLOBAL_FUNCTION_DECL &&
- (prs->flags & JS_PROP_CONFIGURABLE)) {
- /* update the property flags if possible when
- declaring a global function */
- if ((prs->flags & JS_PROP_TMASK) == JS_PROP_GETSET) {
- free_property(ctx->rt, pr, prs->flags);
- prs->flags = flags | JS_PROP_VARREF;
- pr->u.var_ref = var_ref;
- js_rc(var_ref)->ref_count++;
- } else {
- assert((prs->flags & JS_PROP_TMASK) == JS_PROP_VARREF);
- prs->flags = (prs->flags & ~JS_PROP_C_W_E) | flags;
- }
- var_ref->is_const = FALSE;
- }
- return var_ref;
- }
-
- if (!p->extensible) {
- return js_global_object_get_uninitialized_var(ctx, p, cv->var_name);
- }
- }
-
- /* if there is a corresponding uninitialized variable, use it */
- p1 = JS_VALUE_GET_OBJ(ctx->global_obj);
- var_ref = js_global_object_find_uninitialized_var(ctx, p1, cv->var_name, cv->is_lexical);
- if (!var_ref)
- return NULL;
- add_var_ref:
- if (cv->is_lexical) {
- var_ref->is_lexical = TRUE;
- var_ref->is_const = cv->is_const;
- }
- pr = add_property(ctx, p, cv->var_name, flags | JS_PROP_VARREF);
- if (unlikely(!pr)) {
- free_var_ref(ctx->rt, var_ref);
- return NULL;
- }
- pr->u.var_ref = var_ref;
- js_rc(var_ref)->ref_count++;
- return var_ref;
- }
- static JSVarRef *js_closure_global_var(JSContext *ctx, JSClosureVar *cv)
- {
- JSObject *p;
- JSShapeProperty *prs;
- JSProperty *pr;
- JSVarRef *var_ref;
-
- p = JS_VALUE_GET_OBJ(ctx->global_var_obj);
- prs = find_own_property(&pr, p, cv->var_name);
- if (prs) {
- assert((prs->flags & JS_PROP_TMASK) == JS_PROP_VARREF);
- var_ref = pr->u.var_ref;
- js_rc(var_ref)->ref_count++;
- return var_ref;
- }
- p = JS_VALUE_GET_OBJ(ctx->global_obj);
- redo:
- prs = find_own_property(&pr, p, cv->var_name);
- if (prs) {
- if (unlikely((prs->flags & JS_PROP_TMASK) == JS_PROP_AUTOINIT)) {
- /* Instantiate property and retry */
- if (JS_AutoInitProperty(ctx, p, cv->var_name, pr, prs))
- return NULL;
- goto redo;
- }
- if ((prs->flags & JS_PROP_TMASK) == JS_PROP_VARREF) {
- var_ref = pr->u.var_ref;
- js_rc(var_ref)->ref_count++;
- return var_ref;
- }
- }
- return js_global_object_get_uninitialized_var(ctx, p, cv->var_name);
- }
- static JSValue js_closure2(JSContext *ctx, JSValue func_obj,
- JSFunctionBytecode *b,
- JSVarRef **cur_var_refs,
- JSStackFrame *sf,
- BOOL is_eval, JSModuleDef *m)
- {
- JSObject *p;
- JSVarRef **var_refs;
- int i;
- p = JS_VALUE_GET_OBJ(func_obj);
- p->u.func.function_bytecode = b;
- p->u.func.home_object = NULL;
- p->u.func.var_refs = NULL;
- if (b->closure_var_count) {
- var_refs = js_mallocz(ctx, sizeof(var_refs[0]) * b->closure_var_count);
- if (!var_refs)
- goto fail;
- p->u.func.var_refs = var_refs;
- if (is_eval) {
- /* first pass to check the global variable definitions */
- for(i = 0; i < b->closure_var_count; i++) {
- JSClosureVar *cv = &b->closure_var[i];
- if (cv->closure_type == JS_CLOSURE_GLOBAL_DECL) {
- int flags;
- flags = 0;
- if (cv->is_lexical)
- flags |= DEFINE_GLOBAL_LEX_VAR;
- if (cv->var_kind == JS_VAR_GLOBAL_FUNCTION_DECL)
- flags |= DEFINE_GLOBAL_FUNC_VAR;
- if (JS_CheckDefineGlobalVar(ctx, cv->var_name, flags))
- goto fail;
- }
- }
- }
- for(i = 0; i < b->closure_var_count; i++) {
- JSClosureVar *cv = &b->closure_var[i];
- JSVarRef *var_ref;
- switch(cv->closure_type) {
- case JS_CLOSURE_MODULE_IMPORT:
- /* imported from other modules */
- continue;
- case JS_CLOSURE_MODULE_DECL:
- var_ref = js_create_var_ref(ctx, cv->is_lexical);
- break;
- case JS_CLOSURE_GLOBAL_DECL:
- var_ref = js_closure_define_global_var(ctx, cv, b->is_direct_or_indirect_eval);
- break;
- case JS_CLOSURE_GLOBAL:
- var_ref = js_closure_global_var(ctx, cv);
- break;
- case JS_CLOSURE_LOCAL:
- /* reuse the existing variable reference if it already exists */
- var_ref = get_var_ref(ctx, sf, cv->var_idx, FALSE);
- break;
- case JS_CLOSURE_ARG:
- /* reuse the existing variable reference if it already exists */
- var_ref = get_var_ref(ctx, sf, cv->var_idx, TRUE);
- break;
- case JS_CLOSURE_REF:
- case JS_CLOSURE_GLOBAL_REF:
- var_ref = cur_var_refs[cv->var_idx];
- js_rc(var_ref)->ref_count++;
- break;
- default:
- abort();
- }
- if (!var_ref)
- goto fail;
- var_refs[i] = var_ref;
- }
- }
- return func_obj;
- fail:
- /* bfunc is freed when func_obj is freed */
- JS_FreeValue(ctx, func_obj);
- return JS_EXCEPTION;
- }
- static JSValue js_instantiate_prototype(JSContext *ctx, JSObject *p, JSAtom atom, void *opaque)
- {
- JSValue obj, this_val;
- int ret;
- this_val = JS_MKPTR(JS_TAG_OBJECT, p);
- obj = JS_NewObject(ctx);
- if (JS_IsException(obj))
- return JS_EXCEPTION;
- set_cycle_flag(ctx, obj);
- set_cycle_flag(ctx, this_val);
- ret = JS_DefinePropertyValue(ctx, obj, JS_ATOM_constructor,
- JS_DupValue(ctx, this_val),
- JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
- if (ret < 0) {
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- return obj;
- }
- static const uint16_t func_kind_to_class_id[] = {
- [JS_FUNC_NORMAL] = JS_CLASS_BYTECODE_FUNCTION,
- [JS_FUNC_GENERATOR] = JS_CLASS_GENERATOR_FUNCTION,
- [JS_FUNC_ASYNC] = JS_CLASS_ASYNC_FUNCTION,
- [JS_FUNC_ASYNC_GENERATOR] = JS_CLASS_ASYNC_GENERATOR_FUNCTION,
- };
- static JSValue js_closure(JSContext *ctx, JSValue bfunc,
- JSVarRef **cur_var_refs,
- JSStackFrame *sf, BOOL is_eval)
- {
- JSFunctionBytecode *b;
- JSValue func_obj;
- JSAtom name_atom;
- b = JS_VALUE_GET_PTR(bfunc);
- func_obj = JS_NewObjectClass(ctx, func_kind_to_class_id[b->func_kind]);
- if (JS_IsException(func_obj)) {
- JS_FreeValue(ctx, bfunc);
- return JS_EXCEPTION;
- }
- func_obj = js_closure2(ctx, func_obj, b, cur_var_refs, sf, is_eval, NULL);
- if (JS_IsException(func_obj)) {
- /* bfunc has been freed */
- goto fail;
- }
- name_atom = b->func_name;
- if (name_atom == JS_ATOM_NULL)
- name_atom = JS_ATOM_empty_string;
- js_function_set_properties(ctx, func_obj, name_atom,
- b->defined_arg_count);
- if (b->func_kind & JS_FUNC_GENERATOR) {
- JSValue proto;
- int proto_class_id;
- /* generators have a prototype field which is used as
- prototype for the generator object */
- if (b->func_kind == JS_FUNC_ASYNC_GENERATOR)
- proto_class_id = JS_CLASS_ASYNC_GENERATOR;
- else
- proto_class_id = JS_CLASS_GENERATOR;
- proto = JS_NewObjectProto(ctx, ctx->class_proto[proto_class_id]);
- if (JS_IsException(proto))
- goto fail;
- JS_DefinePropertyValue(ctx, func_obj, JS_ATOM_prototype, proto,
- JS_PROP_WRITABLE);
- } else if (b->has_prototype) {
- /* add the 'prototype' property: delay instantiation to avoid
- creating cycles for every javascript function. The prototype
- object is created on the fly when first accessed */
- JS_SetConstructorBit(ctx, func_obj, TRUE);
- JS_DefineAutoInitProperty(ctx, func_obj, JS_ATOM_prototype,
- JS_AUTOINIT_ID_PROTOTYPE, NULL,
- JS_PROP_WRITABLE);
- }
- return func_obj;
- fail:
- /* bfunc is freed when func_obj is freed */
- JS_FreeValue(ctx, func_obj);
- return JS_EXCEPTION;
- }
- #define JS_DEFINE_CLASS_HAS_HERITAGE (1 << 0)
- static int js_op_define_class(JSContext *ctx, JSValue *sp,
- JSAtom class_name, int class_flags,
- JSVarRef **cur_var_refs,
- JSStackFrame *sf, BOOL is_computed_name)
- {
- JSValue bfunc, parent_class, proto = JS_UNDEFINED;
- JSValue ctor = JS_UNDEFINED, parent_proto = JS_UNDEFINED;
- JSFunctionBytecode *b;
- parent_class = sp[-2];
- bfunc = sp[-1];
- if (class_flags & JS_DEFINE_CLASS_HAS_HERITAGE) {
- if (JS_IsNull(parent_class)) {
- parent_proto = JS_NULL;
- parent_class = JS_DupValue(ctx, ctx->function_proto);
- } else {
- if (!JS_IsConstructor(ctx, parent_class)) {
- JS_ThrowTypeError(ctx, "parent class must be constructor");
- goto fail;
- }
- parent_proto = JS_GetProperty(ctx, parent_class, JS_ATOM_prototype);
- if (JS_IsException(parent_proto))
- goto fail;
- if (!JS_IsNull(parent_proto) && !JS_IsObject(parent_proto)) {
- JS_ThrowTypeError(ctx, "parent prototype must be an object or null");
- goto fail;
- }
- }
- } else {
- /* parent_class is JS_UNDEFINED in this case */
- parent_proto = JS_DupValue(ctx, ctx->class_proto[JS_CLASS_OBJECT]);
- parent_class = JS_DupValue(ctx, ctx->function_proto);
- }
- proto = JS_NewObjectProto(ctx, parent_proto);
- if (JS_IsException(proto))
- goto fail;
- b = JS_VALUE_GET_PTR(bfunc);
- assert(b->func_kind == JS_FUNC_NORMAL);
- ctor = JS_NewObjectProtoClass(ctx, parent_class,
- JS_CLASS_BYTECODE_FUNCTION);
- if (JS_IsException(ctor))
- goto fail;
- ctor = js_closure2(ctx, ctor, b, cur_var_refs, sf, FALSE, NULL);
- bfunc = JS_UNDEFINED;
- if (JS_IsException(ctor))
- goto fail;
- js_method_set_home_object(ctx, ctor, proto);
- JS_SetConstructorBit(ctx, ctor, TRUE);
- JS_DefinePropertyValue(ctx, ctor, JS_ATOM_length,
- JS_NewInt32(ctx, b->defined_arg_count),
- JS_PROP_CONFIGURABLE);
- if (is_computed_name) {
- if (JS_DefineObjectNameComputed(ctx, ctor, sp[-3],
- JS_PROP_CONFIGURABLE) < 0)
- goto fail;
- } else {
- if (JS_DefineObjectName(ctx, ctor, class_name, JS_PROP_CONFIGURABLE) < 0)
- goto fail;
- }
- /* the constructor property must be first. It can be overriden by
- computed property names */
- if (JS_DefinePropertyValue(ctx, proto, JS_ATOM_constructor,
- JS_DupValue(ctx, ctor),
- JS_PROP_CONFIGURABLE |
- JS_PROP_WRITABLE | JS_PROP_THROW) < 0)
- goto fail;
- /* set the prototype property */
- if (JS_DefinePropertyValue(ctx, ctor, JS_ATOM_prototype,
- JS_DupValue(ctx, proto), JS_PROP_THROW) < 0)
- goto fail;
- set_cycle_flag(ctx, ctor);
- set_cycle_flag(ctx, proto);
- JS_FreeValue(ctx, parent_proto);
- JS_FreeValue(ctx, parent_class);
- sp[-2] = ctor;
- sp[-1] = proto;
- return 0;
- fail:
- JS_FreeValue(ctx, parent_class);
- JS_FreeValue(ctx, parent_proto);
- JS_FreeValue(ctx, bfunc);
- JS_FreeValue(ctx, proto);
- JS_FreeValue(ctx, ctor);
- sp[-2] = JS_UNDEFINED;
- sp[-1] = JS_UNDEFINED;
- return -1;
- }
- static void close_var_ref(JSRuntime *rt, JSStackFrame *sf, JSVarRef *var_ref)
- {
- if (sf->js_mode & JS_MODE_ASYNC) {
- JSAsyncFunctionState *async_func = container_of(sf, JSAsyncFunctionState, frame);
- async_func_free(rt, async_func);
- }
- var_ref->value = JS_DupValueRT(rt, *var_ref->pvalue);
- var_ref->pvalue = &var_ref->value;
- /* the reference is no longer to a local variable */
- var_ref->is_detached = TRUE;
- }
- static void close_var_refs(JSRuntime *rt, JSFunctionBytecode *b, JSStackFrame *sf)
- {
- JSVarRef *var_ref;
- int i;
- for(i = 0; i < b->var_ref_count; i++) {
- var_ref = sf->var_refs[i];
- if (var_ref)
- close_var_ref(rt, sf, var_ref);
- }
- }
- static void close_lexical_var(JSContext *ctx, JSFunctionBytecode *b,
- JSStackFrame *sf, int var_idx)
- {
- JSVarRef *var_ref;
- int var_ref_idx;
-
- var_ref_idx = b->vardefs[b->arg_count + var_idx].var_ref_idx;
- var_ref = sf->var_refs[var_ref_idx];
- if (var_ref) {
- close_var_ref(ctx->rt, sf, var_ref);
- sf->var_refs[var_ref_idx] = NULL;
- }
- }
- #define JS_CALL_FLAG_COPY_ARGV (1 << 1)
- #define JS_CALL_FLAG_GENERATOR (1 << 2)
- static JSValue js_call_c_function(JSContext *ctx, JSValueConst func_obj,
- JSValueConst this_obj,
- int argc, JSValueConst *argv, int flags)
- {
- JSRuntime *rt = ctx->rt;
- JSCFunctionType func;
- JSObject *p;
- JSStackFrame sf_s, *sf = &sf_s, *prev_sf;
- JSValue ret_val;
- JSValueConst *arg_buf;
- int arg_count, i;
- JSCFunctionEnum cproto;
- p = JS_VALUE_GET_OBJ(func_obj);
- cproto = p->u.cfunc.cproto;
- arg_count = p->u.cfunc.length;
- /* better to always check stack overflow */
- if (js_check_stack_overflow(rt, sizeof(arg_buf[0]) * arg_count))
- return JS_ThrowStackOverflow(ctx);
- prev_sf = rt->current_stack_frame;
- sf->prev_frame = prev_sf;
- rt->current_stack_frame = sf;
- ctx = p->u.cfunc.realm; /* change the current realm */
- sf->js_mode = 0;
- sf->cur_func = (JSValue)func_obj;
- sf->arg_count = argc;
- arg_buf = argv;
- if (unlikely(argc < arg_count)) {
- /* ensure that at least argc_count arguments are readable */
- arg_buf = alloca(sizeof(arg_buf[0]) * arg_count);
- for(i = 0; i < argc; i++)
- arg_buf[i] = argv[i];
- for(i = argc; i < arg_count; i++)
- arg_buf[i] = JS_UNDEFINED;
- sf->arg_count = arg_count;
- }
- sf->arg_buf = (JSValue*)arg_buf;
- func = p->u.cfunc.c_function;
- switch(cproto) {
- case JS_CFUNC_constructor:
- case JS_CFUNC_constructor_or_func:
- if (!(flags & JS_CALL_FLAG_CONSTRUCTOR)) {
- if (cproto == JS_CFUNC_constructor) {
- not_a_constructor:
- ret_val = JS_ThrowTypeError(ctx, "must be called with new");
- break;
- } else {
- this_obj = JS_UNDEFINED;
- }
- }
- /* here this_obj is new_target */
- /* fall thru */
- case JS_CFUNC_generic:
- ret_val = func.generic(ctx, this_obj, argc, arg_buf);
- break;
- case JS_CFUNC_constructor_magic:
- case JS_CFUNC_constructor_or_func_magic:
- if (!(flags & JS_CALL_FLAG_CONSTRUCTOR)) {
- if (cproto == JS_CFUNC_constructor_magic) {
- goto not_a_constructor;
- } else {
- this_obj = JS_UNDEFINED;
- }
- }
- /* fall thru */
- case JS_CFUNC_generic_magic:
- ret_val = func.generic_magic(ctx, this_obj, argc, arg_buf,
- p->u.cfunc.magic);
- break;
- case JS_CFUNC_getter:
- ret_val = func.getter(ctx, this_obj);
- break;
- case JS_CFUNC_setter:
- ret_val = func.setter(ctx, this_obj, arg_buf[0]);
- break;
- case JS_CFUNC_getter_magic:
- ret_val = func.getter_magic(ctx, this_obj, p->u.cfunc.magic);
- break;
- case JS_CFUNC_setter_magic:
- ret_val = func.setter_magic(ctx, this_obj, arg_buf[0], p->u.cfunc.magic);
- break;
- case JS_CFUNC_f_f:
- {
- double d1;
- if (unlikely(JS_ToFloat64(ctx, &d1, arg_buf[0]))) {
- ret_val = JS_EXCEPTION;
- break;
- }
- ret_val = JS_NewFloat64(ctx, func.f_f(d1));
- }
- break;
- case JS_CFUNC_f_f_f:
- {
- double d1, d2;
- if (unlikely(JS_ToFloat64(ctx, &d1, arg_buf[0]))) {
- ret_val = JS_EXCEPTION;
- break;
- }
- if (unlikely(JS_ToFloat64(ctx, &d2, arg_buf[1]))) {
- ret_val = JS_EXCEPTION;
- break;
- }
- ret_val = JS_NewFloat64(ctx, func.f_f_f(d1, d2));
- }
- break;
- case JS_CFUNC_iterator_next:
- {
- int done;
- ret_val = func.iterator_next(ctx, this_obj, argc, arg_buf,
- &done, p->u.cfunc.magic);
- if (!JS_IsException(ret_val) && done != 2) {
- ret_val = js_create_iterator_result(ctx, ret_val, done);
- }
- }
- break;
- default:
- abort();
- }
- rt->current_stack_frame = sf->prev_frame;
- return ret_val;
- }
- static JSValue js_call_bound_function(JSContext *ctx, JSValueConst func_obj,
- JSValueConst this_obj,
- int argc, JSValueConst *argv, int flags)
- {
- JSObject *p;
- JSBoundFunction *bf;
- JSValueConst *arg_buf, new_target;
- int arg_count, i;
- p = JS_VALUE_GET_OBJ(func_obj);
- bf = p->u.bound_function;
- arg_count = bf->argc + argc;
- if (js_check_stack_overflow(ctx->rt, sizeof(JSValue) * arg_count))
- return JS_ThrowStackOverflow(ctx);
- arg_buf = alloca(sizeof(JSValue) * arg_count);
- for(i = 0; i < bf->argc; i++) {
- arg_buf[i] = bf->argv[i];
- }
- for(i = 0; i < argc; i++) {
- arg_buf[bf->argc + i] = argv[i];
- }
- if (flags & JS_CALL_FLAG_CONSTRUCTOR) {
- new_target = this_obj;
- if (js_same_value(ctx, func_obj, new_target))
- new_target = bf->func_obj;
- return JS_CallConstructor2(ctx, bf->func_obj, new_target,
- arg_count, arg_buf);
- } else {
- return JS_Call(ctx, bf->func_obj, bf->this_val,
- arg_count, arg_buf);
- }
- }
- /* argument of OP_special_object */
- typedef enum {
- OP_SPECIAL_OBJECT_ARGUMENTS,
- OP_SPECIAL_OBJECT_MAPPED_ARGUMENTS,
- OP_SPECIAL_OBJECT_THIS_FUNC,
- OP_SPECIAL_OBJECT_NEW_TARGET,
- OP_SPECIAL_OBJECT_HOME_OBJECT,
- OP_SPECIAL_OBJECT_VAR_OBJECT,
- OP_SPECIAL_OBJECT_IMPORT_META,
- } OPSpecialObjectEnum;
- #define FUNC_RET_AWAIT 0
- #define FUNC_RET_YIELD 1
- #define FUNC_RET_YIELD_STAR 2
- #define FUNC_RET_INITIAL_YIELD 3
- #ifdef OPCODE_ASM_LABEL
- #pragma GCC diagnostic push
- #pragma GCC diagnostic ignored "-Wunused-label"
- #endif
- /* argv[] is modified if (flags & JS_CALL_FLAG_COPY_ARGV) = 0. */
- static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
- JSValueConst this_obj, JSValueConst new_target,
- int argc, JSValue *argv, int flags)
- {
- JSRuntime *rt = caller_ctx->rt;
- JSContext *ctx;
- JSObject *p;
- JSFunctionBytecode *b;
- JSStackFrame sf_s, *sf = &sf_s;
- const uint8_t *pc;
- int opcode, arg_allocated_size, i;
- JSValue *local_buf, *stack_buf, *var_buf, *arg_buf, *sp, ret_val, *pval;
- JSVarRef **var_refs;
- size_t alloca_size;
- #if !DIRECT_DISPATCH
- #define SWITCH(pc) switch (opcode = *pc++)
- #define CASE(op) case op
- #define DEFAULT default
- #define BREAK break
- #else
- static const void * const dispatch_table[256] = {
- #define DEF(id, size, n_pop, n_push, f) && case_OP_ ## id,
- #if SHORT_OPCODES
- #define def(id, size, n_pop, n_push, f)
- #else
- #define def(id, size, n_pop, n_push, f) && case_default,
- #endif
- #include "quickjs-opcode.h"
- [ OP_COUNT ... 255 ] = &&case_default
- };
- #define SWITCH(pc) goto *dispatch_table[opcode = *pc++];
- #ifdef OPCODE_ASM_LABEL
- #define CASE(op) case_ ## op: asm volatile("label_" #op ":\n.globl label_" #op); dummy_case_ ## op
- #else
- #define CASE(op) case_ ## op
- #endif
- #define DEFAULT case_default
- #define BREAK SWITCH(pc)
- #endif
- if (js_poll_interrupts(caller_ctx))
- return JS_EXCEPTION;
- if (unlikely(JS_VALUE_GET_TAG(func_obj) != JS_TAG_OBJECT)) {
- if (flags & JS_CALL_FLAG_GENERATOR) {
- JSAsyncFunctionState *s = JS_VALUE_GET_PTR(func_obj);
- /* func_obj get contains a pointer to JSFuncAsyncState */
- /* the stack frame is already allocated */
- sf = &s->frame;
- p = JS_VALUE_GET_OBJ(sf->cur_func);
- b = p->u.func.function_bytecode;
- ctx = b->realm;
- var_refs = p->u.func.var_refs;
- local_buf = arg_buf = sf->arg_buf;
- var_buf = sf->var_buf;
- stack_buf = sf->var_buf + b->var_count;
- sp = sf->cur_sp;
- sf->cur_sp = NULL; /* cur_sp is NULL if the function is running */
- pc = sf->cur_pc;
- sf->prev_frame = rt->current_stack_frame;
- rt->current_stack_frame = sf;
- if (s->throw_flag)
- goto exception;
- else
- goto restart;
- } else {
- goto not_a_function;
- }
- }
- p = JS_VALUE_GET_OBJ(func_obj);
- if (unlikely(p->class_id != JS_CLASS_BYTECODE_FUNCTION)) {
- JSClassCall *call_func;
- call_func = rt->class_array[p->class_id].call;
- if (!call_func) {
- not_a_function:
- return JS_ThrowTypeError(caller_ctx, "not a function");
- }
- return call_func(caller_ctx, func_obj, this_obj, argc,
- (JSValueConst *)argv, flags);
- }
- b = p->u.func.function_bytecode;
- if (unlikely(argc < b->arg_count || (flags & JS_CALL_FLAG_COPY_ARGV))) {
- arg_allocated_size = b->arg_count;
- } else {
- arg_allocated_size = 0;
- }
- alloca_size = sizeof(JSValue) * (arg_allocated_size + b->var_count +
- b->stack_size) +
- sizeof(JSVarRef *) * b->var_ref_count;
- if (js_check_stack_overflow(rt, alloca_size))
- return JS_ThrowStackOverflow(caller_ctx);
- sf->js_mode = b->js_mode;
- arg_buf = argv;
- sf->arg_count = argc;
- sf->cur_func = (JSValue)func_obj;
- var_refs = p->u.func.var_refs;
- local_buf = alloca(alloca_size);
- if (unlikely(arg_allocated_size)) {
- int n = min_int(argc, b->arg_count);
- arg_buf = local_buf;
- for(i = 0; i < n; i++)
- arg_buf[i] = JS_DupValue(caller_ctx, argv[i]);
- for(; i < b->arg_count; i++)
- arg_buf[i] = JS_UNDEFINED;
- sf->arg_count = b->arg_count;
- }
- var_buf = local_buf + arg_allocated_size;
- sf->var_buf = var_buf;
- sf->arg_buf = arg_buf;
- for(i = 0; i < b->var_count; i++)
- var_buf[i] = JS_UNDEFINED;
- stack_buf = var_buf + b->var_count;
- sf->var_refs = (JSVarRef **)(stack_buf + b->stack_size);
- for(i = 0; i < b->var_ref_count; i++)
- sf->var_refs[i] = NULL;
- sp = stack_buf;
- pc = b->byte_code_buf;
- sf->prev_frame = rt->current_stack_frame;
- rt->current_stack_frame = sf;
- ctx = b->realm; /* set the current realm */
- restart:
- for(;;) {
- int call_argc;
- JSValue *call_argv;
- SWITCH(pc) {
- CASE(OP_push_i32):
- *sp++ = JS_NewInt32(ctx, get_u32(pc));
- pc += 4;
- BREAK;
- CASE(OP_push_bigint_i32):
- *sp++ = __JS_NewShortBigInt(ctx, (int)get_u32(pc));
- pc += 4;
- BREAK;
- CASE(OP_push_const):
- *sp++ = JS_DupValue(ctx, b->cpool[get_u32(pc)]);
- pc += 4;
- BREAK;
- #if SHORT_OPCODES
- CASE(OP_push_minus1):
- CASE(OP_push_0):
- CASE(OP_push_1):
- CASE(OP_push_2):
- CASE(OP_push_3):
- CASE(OP_push_4):
- CASE(OP_push_5):
- CASE(OP_push_6):
- CASE(OP_push_7):
- *sp++ = JS_NewInt32(ctx, opcode - OP_push_0);
- BREAK;
- CASE(OP_push_i8):
- *sp++ = JS_NewInt32(ctx, get_i8(pc));
- pc += 1;
- BREAK;
- CASE(OP_push_i16):
- *sp++ = JS_NewInt32(ctx, get_i16(pc));
- pc += 2;
- BREAK;
- CASE(OP_push_const8):
- *sp++ = JS_DupValue(ctx, b->cpool[*pc++]);
- BREAK;
- CASE(OP_fclosure8):
- *sp++ = js_closure(ctx, JS_DupValue(ctx, b->cpool[*pc++]), var_refs, sf, FALSE);
- if (unlikely(JS_IsException(sp[-1])))
- goto exception;
- BREAK;
- CASE(OP_push_empty_string):
- *sp++ = JS_AtomToString(ctx, JS_ATOM_empty_string);
- BREAK;
- #endif
- CASE(OP_push_atom_value):
- *sp++ = JS_AtomToValue(ctx, get_u32(pc));
- pc += 4;
- BREAK;
- CASE(OP_undefined):
- *sp++ = JS_UNDEFINED;
- BREAK;
- CASE(OP_null):
- *sp++ = JS_NULL;
- BREAK;
- CASE(OP_push_this):
- /* OP_push_this is only called at the start of a function */
- {
- JSValue val;
- if (!(b->js_mode & JS_MODE_STRICT)) {
- uint32_t tag = JS_VALUE_GET_TAG(this_obj);
- if (likely(tag == JS_TAG_OBJECT))
- goto normal_this;
- if (tag == JS_TAG_NULL || tag == JS_TAG_UNDEFINED) {
- val = JS_DupValue(ctx, ctx->global_obj);
- } else {
- val = JS_ToObject(ctx, this_obj);
- if (JS_IsException(val))
- goto exception;
- }
- } else {
- normal_this:
- val = JS_DupValue(ctx, this_obj);
- }
- *sp++ = val;
- }
- BREAK;
- CASE(OP_push_false):
- *sp++ = JS_FALSE;
- BREAK;
- CASE(OP_push_true):
- *sp++ = JS_TRUE;
- BREAK;
- CASE(OP_object):
- *sp++ = JS_NewObject(ctx);
- if (unlikely(JS_IsException(sp[-1])))
- goto exception;
- BREAK;
- CASE(OP_special_object):
- {
- int arg = *pc++;
- switch(arg) {
- case OP_SPECIAL_OBJECT_ARGUMENTS:
- *sp++ = js_build_arguments(ctx, argc, (JSValueConst *)argv);
- if (unlikely(JS_IsException(sp[-1])))
- goto exception;
- break;
- case OP_SPECIAL_OBJECT_MAPPED_ARGUMENTS:
- *sp++ = js_build_mapped_arguments(ctx, argc, (JSValueConst *)argv,
- sf, min_int(argc, b->arg_count));
- if (unlikely(JS_IsException(sp[-1])))
- goto exception;
- break;
- case OP_SPECIAL_OBJECT_THIS_FUNC:
- *sp++ = JS_DupValue(ctx, sf->cur_func);
- break;
- case OP_SPECIAL_OBJECT_NEW_TARGET:
- *sp++ = JS_DupValue(ctx, new_target);
- break;
- case OP_SPECIAL_OBJECT_HOME_OBJECT:
- {
- JSObject *p1;
- p1 = p->u.func.home_object;
- if (unlikely(!p1))
- *sp++ = JS_UNDEFINED;
- else
- *sp++ = JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, p1));
- }
- break;
- case OP_SPECIAL_OBJECT_VAR_OBJECT:
- *sp++ = JS_NewObjectProto(ctx, JS_NULL);
- if (unlikely(JS_IsException(sp[-1])))
- goto exception;
- break;
- case OP_SPECIAL_OBJECT_IMPORT_META:
- *sp++ = js_import_meta(ctx);
- if (unlikely(JS_IsException(sp[-1])))
- goto exception;
- break;
- default:
- abort();
- }
- }
- BREAK;
- CASE(OP_rest):
- {
- int first = get_u16(pc);
- pc += 2;
- first = min_int(first, argc);
- *sp++ = js_create_array(ctx, argc - first, (JSValueConst *)(argv + first));
- if (unlikely(JS_IsException(sp[-1])))
- goto exception;
- }
- BREAK;
- CASE(OP_drop):
- JS_FreeValue(ctx, sp[-1]);
- sp--;
- BREAK;
- CASE(OP_nip):
- JS_FreeValue(ctx, sp[-2]);
- sp[-2] = sp[-1];
- sp--;
- BREAK;
- CASE(OP_nip1): /* a b c -> b c */
- JS_FreeValue(ctx, sp[-3]);
- sp[-3] = sp[-2];
- sp[-2] = sp[-1];
- sp--;
- BREAK;
- CASE(OP_dup):
- sp[0] = JS_DupValue(ctx, sp[-1]);
- sp++;
- BREAK;
- CASE(OP_dup2): /* a b -> a b a b */
- sp[0] = JS_DupValue(ctx, sp[-2]);
- sp[1] = JS_DupValue(ctx, sp[-1]);
- sp += 2;
- BREAK;
- CASE(OP_dup3): /* a b c -> a b c a b c */
- sp[0] = JS_DupValue(ctx, sp[-3]);
- sp[1] = JS_DupValue(ctx, sp[-2]);
- sp[2] = JS_DupValue(ctx, sp[-1]);
- sp += 3;
- BREAK;
- CASE(OP_dup1): /* a b -> a a b */
- sp[0] = sp[-1];
- sp[-1] = JS_DupValue(ctx, sp[-2]);
- sp++;
- BREAK;
- CASE(OP_insert2): /* obj a -> a obj a (dup_x1) */
- sp[0] = sp[-1];
- sp[-1] = sp[-2];
- sp[-2] = JS_DupValue(ctx, sp[0]);
- sp++;
- BREAK;
- CASE(OP_insert3): /* obj prop a -> a obj prop a (dup_x2) */
- sp[0] = sp[-1];
- sp[-1] = sp[-2];
- sp[-2] = sp[-3];
- sp[-3] = JS_DupValue(ctx, sp[0]);
- sp++;
- BREAK;
- CASE(OP_insert4): /* this obj prop a -> a this obj prop a */
- sp[0] = sp[-1];
- sp[-1] = sp[-2];
- sp[-2] = sp[-3];
- sp[-3] = sp[-4];
- sp[-4] = JS_DupValue(ctx, sp[0]);
- sp++;
- BREAK;
- CASE(OP_perm3): /* obj a b -> a obj b (213) */
- {
- JSValue tmp;
- tmp = sp[-2];
- sp[-2] = sp[-3];
- sp[-3] = tmp;
- }
- BREAK;
- CASE(OP_rot3l): /* x a b -> a b x (231) */
- {
- JSValue tmp;
- tmp = sp[-3];
- sp[-3] = sp[-2];
- sp[-2] = sp[-1];
- sp[-1] = tmp;
- }
- BREAK;
- CASE(OP_rot4l): /* x a b c -> a b c x */
- {
- JSValue tmp;
- tmp = sp[-4];
- sp[-4] = sp[-3];
- sp[-3] = sp[-2];
- sp[-2] = sp[-1];
- sp[-1] = tmp;
- }
- BREAK;
- CASE(OP_rot5l): /* x a b c d -> a b c d x */
- {
- JSValue tmp;
- tmp = sp[-5];
- sp[-5] = sp[-4];
- sp[-4] = sp[-3];
- sp[-3] = sp[-2];
- sp[-2] = sp[-1];
- sp[-1] = tmp;
- }
- BREAK;
- CASE(OP_rot3r): /* a b x -> x a b (312) */
- {
- JSValue tmp;
- tmp = sp[-1];
- sp[-1] = sp[-2];
- sp[-2] = sp[-3];
- sp[-3] = tmp;
- }
- BREAK;
- CASE(OP_perm4): /* obj prop a b -> a obj prop b */
- {
- JSValue tmp;
- tmp = sp[-2];
- sp[-2] = sp[-3];
- sp[-3] = sp[-4];
- sp[-4] = tmp;
- }
- BREAK;
- CASE(OP_perm5): /* this obj prop a b -> a this obj prop b */
- {
- JSValue tmp;
- tmp = sp[-2];
- sp[-2] = sp[-3];
- sp[-3] = sp[-4];
- sp[-4] = sp[-5];
- sp[-5] = tmp;
- }
- BREAK;
- CASE(OP_swap): /* a b -> b a */
- {
- JSValue tmp;
- tmp = sp[-2];
- sp[-2] = sp[-1];
- sp[-1] = tmp;
- }
- BREAK;
- CASE(OP_swap2): /* a b c d -> c d a b */
- {
- JSValue tmp1, tmp2;
- tmp1 = sp[-4];
- tmp2 = sp[-3];
- sp[-4] = sp[-2];
- sp[-3] = sp[-1];
- sp[-2] = tmp1;
- sp[-1] = tmp2;
- }
- BREAK;
- CASE(OP_fclosure):
- {
- JSValue bfunc = JS_DupValue(ctx, b->cpool[get_u32(pc)]);
- pc += 4;
- *sp++ = js_closure(ctx, bfunc, var_refs, sf, FALSE);
- if (unlikely(JS_IsException(sp[-1])))
- goto exception;
- }
- BREAK;
- #if SHORT_OPCODES
- CASE(OP_call0):
- CASE(OP_call1):
- CASE(OP_call2):
- CASE(OP_call3):
- call_argc = opcode - OP_call0;
- goto has_call_argc;
- #endif
- CASE(OP_call):
- CASE(OP_tail_call):
- {
- call_argc = get_u16(pc);
- pc += 2;
- goto has_call_argc;
- has_call_argc:
- call_argv = sp - call_argc;
- sf->cur_pc = pc;
- ret_val = JS_CallInternal(ctx, call_argv[-1], JS_UNDEFINED,
- JS_UNDEFINED, call_argc, call_argv, 0);
- if (unlikely(JS_IsException(ret_val)))
- goto exception;
- if (opcode == OP_tail_call)
- goto done;
- for(i = -1; i < call_argc; i++)
- JS_FreeValue(ctx, call_argv[i]);
- sp -= call_argc + 1;
- *sp++ = ret_val;
- }
- BREAK;
- CASE(OP_call_constructor):
- {
- call_argc = get_u16(pc);
- pc += 2;
- call_argv = sp - call_argc;
- sf->cur_pc = pc;
- ret_val = JS_CallConstructorInternal(ctx, call_argv[-2],
- call_argv[-1],
- call_argc, call_argv, 0);
- if (unlikely(JS_IsException(ret_val)))
- goto exception;
- for(i = -2; i < call_argc; i++)
- JS_FreeValue(ctx, call_argv[i]);
- sp -= call_argc + 2;
- *sp++ = ret_val;
- }
- BREAK;
- CASE(OP_call_method):
- CASE(OP_tail_call_method):
- {
- call_argc = get_u16(pc);
- pc += 2;
- call_argv = sp - call_argc;
- sf->cur_pc = pc;
- ret_val = JS_CallInternal(ctx, call_argv[-1], call_argv[-2],
- JS_UNDEFINED, call_argc, call_argv, 0);
- if (unlikely(JS_IsException(ret_val)))
- goto exception;
- if (opcode == OP_tail_call_method)
- goto done;
- for(i = -2; i < call_argc; i++)
- JS_FreeValue(ctx, call_argv[i]);
- sp -= call_argc + 2;
- *sp++ = ret_val;
- }
- BREAK;
- CASE(OP_array_from):
- call_argc = get_u16(pc);
- pc += 2;
- ret_val = js_create_array_free(ctx, call_argc, sp - call_argc);
- sp -= call_argc;
- if (unlikely(JS_IsException(ret_val)))
- goto exception;
- *sp++ = ret_val;
- BREAK;
- CASE(OP_apply):
- {
- int magic;
- magic = get_u16(pc);
- pc += 2;
- sf->cur_pc = pc;
- ret_val = js_function_apply(ctx, sp[-3], 2, (JSValueConst *)&sp[-2], magic);
- if (unlikely(JS_IsException(ret_val)))
- goto exception;
- JS_FreeValue(ctx, sp[-3]);
- JS_FreeValue(ctx, sp[-2]);
- JS_FreeValue(ctx, sp[-1]);
- sp -= 3;
- *sp++ = ret_val;
- }
- BREAK;
- CASE(OP_return):
- ret_val = *--sp;
- goto done;
- CASE(OP_return_undef):
- ret_val = JS_UNDEFINED;
- goto done;
- CASE(OP_check_ctor_return):
- /* return TRUE if 'this' should be returned */
- if (!JS_IsObject(sp[-1])) {
- if (!JS_IsUndefined(sp[-1])) {
- JS_ThrowTypeError(caller_ctx, "derived class constructor must return an object or undefined");
- goto exception;
- }
- sp[0] = JS_TRUE;
- } else {
- sp[0] = JS_FALSE;
- }
- sp++;
- BREAK;
- CASE(OP_check_ctor):
- if (JS_IsUndefined(new_target)) {
- non_ctor_call:
- JS_ThrowTypeError(ctx, "class constructors must be invoked with 'new'");
- goto exception;
- }
- BREAK;
- CASE(OP_init_ctor):
- {
- JSValue super, ret;
- sf->cur_pc = pc;
- if (JS_IsUndefined(new_target))
- goto non_ctor_call;
- super = JS_GetPrototype(ctx, func_obj);
- if (JS_IsException(super))
- goto exception;
- ret = JS_CallConstructor2(ctx, super, new_target, argc, (JSValueConst *)argv);
- JS_FreeValue(ctx, super);
- if (JS_IsException(ret))
- goto exception;
- *sp++ = ret;
- }
- BREAK;
- CASE(OP_check_brand):
- {
- int ret = JS_CheckBrand(ctx, sp[-2], sp[-1]);
- if (ret < 0)
- goto exception;
- if (!ret) {
- JS_ThrowTypeError(ctx, "invalid brand on object");
- goto exception;
- }
- }
- BREAK;
- CASE(OP_add_brand):
- if (JS_AddBrand(ctx, sp[-2], sp[-1]) < 0)
- goto exception;
- JS_FreeValue(ctx, sp[-2]);
- JS_FreeValue(ctx, sp[-1]);
- sp -= 2;
- BREAK;
- CASE(OP_throw):
- JS_Throw(ctx, *--sp);
- goto exception;
- CASE(OP_throw_error):
- #define JS_THROW_VAR_RO 0
- #define JS_THROW_VAR_REDECL 1
- #define JS_THROW_VAR_UNINITIALIZED 2
- #define JS_THROW_ERROR_DELETE_SUPER 3
- #define JS_THROW_ERROR_ITERATOR_THROW 4
- {
- JSAtom atom;
- int type;
- atom = get_u32(pc);
- type = pc[4];
- pc += 5;
- if (type == JS_THROW_VAR_RO)
- JS_ThrowTypeErrorReadOnly(ctx, JS_PROP_THROW, atom);
- else
- if (type == JS_THROW_VAR_REDECL)
- JS_ThrowSyntaxErrorVarRedeclaration(ctx, atom);
- else
- if (type == JS_THROW_VAR_UNINITIALIZED)
- JS_ThrowReferenceErrorUninitialized(ctx, atom);
- else
- if (type == JS_THROW_ERROR_DELETE_SUPER)
- JS_ThrowReferenceError(ctx, "unsupported reference to 'super'");
- else
- if (type == JS_THROW_ERROR_ITERATOR_THROW)
- JS_ThrowTypeError(ctx, "iterator does not have a throw method");
- else
- JS_ThrowInternalError(ctx, "invalid throw var type %d", type);
- }
- goto exception;
- CASE(OP_eval):
- {
- JSValueConst obj;
- int scope_idx;
- call_argc = get_u16(pc);
- scope_idx = get_u16(pc + 2) + ARG_SCOPE_END;
- pc += 4;
- call_argv = sp - call_argc;
- sf->cur_pc = pc;
- if (js_same_value(ctx, call_argv[-1], ctx->eval_obj)) {
- if (call_argc >= 1)
- obj = call_argv[0];
- else
- obj = JS_UNDEFINED;
- ret_val = JS_EvalObject(ctx, JS_UNDEFINED, obj,
- JS_EVAL_TYPE_DIRECT, scope_idx);
- } else {
- ret_val = JS_CallInternal(ctx, call_argv[-1], JS_UNDEFINED,
- JS_UNDEFINED, call_argc, call_argv, 0);
- }
- if (unlikely(JS_IsException(ret_val)))
- goto exception;
- for(i = -1; i < call_argc; i++)
- JS_FreeValue(ctx, call_argv[i]);
- sp -= call_argc + 1;
- *sp++ = ret_val;
- }
- BREAK;
- /* could merge with OP_apply */
- CASE(OP_apply_eval):
- {
- int scope_idx;
- uint32_t len;
- JSValue *tab;
- JSValueConst obj;
- scope_idx = get_u16(pc) + ARG_SCOPE_END;
- pc += 2;
- sf->cur_pc = pc;
- tab = build_arg_list(ctx, &len, sp[-1]);
- if (!tab)
- goto exception;
- if (js_same_value(ctx, sp[-2], ctx->eval_obj)) {
- if (len >= 1)
- obj = tab[0];
- else
- obj = JS_UNDEFINED;
- ret_val = JS_EvalObject(ctx, JS_UNDEFINED, obj,
- JS_EVAL_TYPE_DIRECT, scope_idx);
- } else {
- ret_val = JS_Call(ctx, sp[-2], JS_UNDEFINED, len,
- (JSValueConst *)tab);
- }
- free_arg_list(ctx, tab, len);
- if (unlikely(JS_IsException(ret_val)))
- goto exception;
- JS_FreeValue(ctx, sp[-2]);
- JS_FreeValue(ctx, sp[-1]);
- sp -= 2;
- *sp++ = ret_val;
- }
- BREAK;
- CASE(OP_regexp):
- {
- sp[-2] = JS_NewRegexp(ctx, sp[-2], sp[-1]);
- sp--;
- if (JS_IsException(sp[-1]))
- goto exception;
- }
- BREAK;
- CASE(OP_get_super):
- {
- JSValue proto;
- sf->cur_pc = pc;
- proto = JS_GetPrototype(ctx, sp[-1]);
- if (JS_IsException(proto))
- goto exception;
- JS_FreeValue(ctx, sp[-1]);
- sp[-1] = proto;
- }
- BREAK;
- CASE(OP_import):
- {
- JSValue val;
- sf->cur_pc = pc;
- val = js_dynamic_import(ctx, sp[-2], sp[-1]);
- if (JS_IsException(val))
- goto exception;
- JS_FreeValue(ctx, sp[-2]);
- JS_FreeValue(ctx, sp[-1]);
- sp--;
- sp[-1] = val;
- }
- BREAK;
- CASE(OP_get_var_undef):
- CASE(OP_get_var):
- {
- int idx;
- JSValue val;
- idx = get_u16(pc);
- pc += 2;
- val = *var_refs[idx]->pvalue;
- if (unlikely(JS_IsUninitialized(val))) {
- JSClosureVar *cv = &b->closure_var[idx];
- if (cv->is_lexical) {
- JS_ThrowReferenceErrorUninitialized(ctx, cv->var_name);
- goto exception;
- } else {
- sf->cur_pc = pc;
- sp[0] = JS_GetPropertyInternal(ctx, ctx->global_obj,
- cv->var_name,
- ctx->global_obj,
- opcode - OP_get_var_undef);
- if (JS_IsException(sp[0]))
- goto exception;
- }
- } else {
- sp[0] = JS_DupValue(ctx, val);
- }
- sp++;
- }
- BREAK;
- CASE(OP_put_var):
- CASE(OP_put_var_init):
- {
- int idx, ret;
- JSVarRef *var_ref;
- idx = get_u16(pc);
- pc += 2;
- var_ref = var_refs[idx];
- if (unlikely(JS_IsUninitialized(*var_ref->pvalue) ||
- var_ref->is_const)) {
- JSClosureVar *cv = &b->closure_var[idx];
- if (var_ref->is_lexical) {
- if (opcode == OP_put_var_init)
- goto put_var_ok;
- if (JS_IsUninitialized(*var_ref->pvalue))
- JS_ThrowReferenceErrorUninitialized(ctx, cv->var_name);
- else
- JS_ThrowTypeErrorReadOnly(ctx, JS_PROP_THROW, cv->var_name);
- goto exception;
- } else {
- sf->cur_pc = pc;
- ret = JS_HasProperty(ctx, ctx->global_obj, cv->var_name);
- if (ret < 0)
- goto exception;
- if (ret == 0 && is_strict_mode(ctx)) {
- JS_ThrowReferenceErrorNotDefined(ctx, cv->var_name);
- goto exception;
- }
- ret = JS_SetPropertyInternal(ctx, ctx->global_obj, cv->var_name, sp[-1],
- ctx->global_obj, JS_PROP_THROW_STRICT);
- sp--;
- if (ret < 0)
- goto exception;
- }
- } else {
- put_var_ok:
- set_value(ctx, var_ref->pvalue, sp[-1]);
- sp--;
- }
- }
- BREAK;
- CASE(OP_get_loc):
- {
- int idx;
- idx = get_u16(pc);
- pc += 2;
- sp[0] = JS_DupValue(ctx, var_buf[idx]);
- sp++;
- }
- BREAK;
- CASE(OP_put_loc):
- {
- int idx;
- idx = get_u16(pc);
- pc += 2;
- set_value(ctx, &var_buf[idx], sp[-1]);
- sp--;
- }
- BREAK;
- CASE(OP_set_loc):
- {
- int idx;
- idx = get_u16(pc);
- pc += 2;
- set_value(ctx, &var_buf[idx], JS_DupValue(ctx, sp[-1]));
- }
- BREAK;
- CASE(OP_get_arg):
- {
- int idx;
- idx = get_u16(pc);
- pc += 2;
- sp[0] = JS_DupValue(ctx, arg_buf[idx]);
- sp++;
- }
- BREAK;
- CASE(OP_put_arg):
- {
- int idx;
- idx = get_u16(pc);
- pc += 2;
- set_value(ctx, &arg_buf[idx], sp[-1]);
- sp--;
- }
- BREAK;
- CASE(OP_set_arg):
- {
- int idx;
- idx = get_u16(pc);
- pc += 2;
- set_value(ctx, &arg_buf[idx], JS_DupValue(ctx, sp[-1]));
- }
- BREAK;
- #if SHORT_OPCODES
- CASE(OP_get_loc8): *sp++ = JS_DupValue(ctx, var_buf[*pc++]); BREAK;
- CASE(OP_put_loc8): set_value(ctx, &var_buf[*pc++], *--sp); BREAK;
- CASE(OP_set_loc8): set_value(ctx, &var_buf[*pc++], JS_DupValue(ctx, sp[-1])); BREAK;
- CASE(OP_get_loc0): *sp++ = JS_DupValue(ctx, var_buf[0]); BREAK;
- CASE(OP_get_loc1): *sp++ = JS_DupValue(ctx, var_buf[1]); BREAK;
- CASE(OP_get_loc2): *sp++ = JS_DupValue(ctx, var_buf[2]); BREAK;
- CASE(OP_get_loc3): *sp++ = JS_DupValue(ctx, var_buf[3]); BREAK;
- CASE(OP_put_loc0): set_value(ctx, &var_buf[0], *--sp); BREAK;
- CASE(OP_put_loc1): set_value(ctx, &var_buf[1], *--sp); BREAK;
- CASE(OP_put_loc2): set_value(ctx, &var_buf[2], *--sp); BREAK;
- CASE(OP_put_loc3): set_value(ctx, &var_buf[3], *--sp); BREAK;
- CASE(OP_set_loc0): set_value(ctx, &var_buf[0], JS_DupValue(ctx, sp[-1])); BREAK;
- CASE(OP_set_loc1): set_value(ctx, &var_buf[1], JS_DupValue(ctx, sp[-1])); BREAK;
- CASE(OP_set_loc2): set_value(ctx, &var_buf[2], JS_DupValue(ctx, sp[-1])); BREAK;
- CASE(OP_set_loc3): set_value(ctx, &var_buf[3], JS_DupValue(ctx, sp[-1])); BREAK;
- CASE(OP_get_arg0): *sp++ = JS_DupValue(ctx, arg_buf[0]); BREAK;
- CASE(OP_get_arg1): *sp++ = JS_DupValue(ctx, arg_buf[1]); BREAK;
- CASE(OP_get_arg2): *sp++ = JS_DupValue(ctx, arg_buf[2]); BREAK;
- CASE(OP_get_arg3): *sp++ = JS_DupValue(ctx, arg_buf[3]); BREAK;
- CASE(OP_put_arg0): set_value(ctx, &arg_buf[0], *--sp); BREAK;
- CASE(OP_put_arg1): set_value(ctx, &arg_buf[1], *--sp); BREAK;
- CASE(OP_put_arg2): set_value(ctx, &arg_buf[2], *--sp); BREAK;
- CASE(OP_put_arg3): set_value(ctx, &arg_buf[3], *--sp); BREAK;
- CASE(OP_set_arg0): set_value(ctx, &arg_buf[0], JS_DupValue(ctx, sp[-1])); BREAK;
- CASE(OP_set_arg1): set_value(ctx, &arg_buf[1], JS_DupValue(ctx, sp[-1])); BREAK;
- CASE(OP_set_arg2): set_value(ctx, &arg_buf[2], JS_DupValue(ctx, sp[-1])); BREAK;
- CASE(OP_set_arg3): set_value(ctx, &arg_buf[3], JS_DupValue(ctx, sp[-1])); BREAK;
- CASE(OP_get_var_ref0): *sp++ = JS_DupValue(ctx, *var_refs[0]->pvalue); BREAK;
- CASE(OP_get_var_ref1): *sp++ = JS_DupValue(ctx, *var_refs[1]->pvalue); BREAK;
- CASE(OP_get_var_ref2): *sp++ = JS_DupValue(ctx, *var_refs[2]->pvalue); BREAK;
- CASE(OP_get_var_ref3): *sp++ = JS_DupValue(ctx, *var_refs[3]->pvalue); BREAK;
- CASE(OP_put_var_ref0): set_value(ctx, var_refs[0]->pvalue, *--sp); BREAK;
- CASE(OP_put_var_ref1): set_value(ctx, var_refs[1]->pvalue, *--sp); BREAK;
- CASE(OP_put_var_ref2): set_value(ctx, var_refs[2]->pvalue, *--sp); BREAK;
- CASE(OP_put_var_ref3): set_value(ctx, var_refs[3]->pvalue, *--sp); BREAK;
- CASE(OP_set_var_ref0): set_value(ctx, var_refs[0]->pvalue, JS_DupValue(ctx, sp[-1])); BREAK;
- CASE(OP_set_var_ref1): set_value(ctx, var_refs[1]->pvalue, JS_DupValue(ctx, sp[-1])); BREAK;
- CASE(OP_set_var_ref2): set_value(ctx, var_refs[2]->pvalue, JS_DupValue(ctx, sp[-1])); BREAK;
- CASE(OP_set_var_ref3): set_value(ctx, var_refs[3]->pvalue, JS_DupValue(ctx, sp[-1])); BREAK;
- #endif
- CASE(OP_get_var_ref):
- {
- int idx;
- JSValue val;
- idx = get_u16(pc);
- pc += 2;
- val = *var_refs[idx]->pvalue;
- sp[0] = JS_DupValue(ctx, val);
- sp++;
- }
- BREAK;
- CASE(OP_put_var_ref):
- {
- int idx;
- idx = get_u16(pc);
- pc += 2;
- set_value(ctx, var_refs[idx]->pvalue, sp[-1]);
- sp--;
- }
- BREAK;
- CASE(OP_set_var_ref):
- {
- int idx;
- idx = get_u16(pc);
- pc += 2;
- set_value(ctx, var_refs[idx]->pvalue, JS_DupValue(ctx, sp[-1]));
- }
- BREAK;
- CASE(OP_get_var_ref_check):
- {
- int idx;
- JSValue val;
- idx = get_u16(pc);
- pc += 2;
- val = *var_refs[idx]->pvalue;
- if (unlikely(JS_IsUninitialized(val))) {
- JS_ThrowReferenceErrorUninitialized2(ctx, b, idx, TRUE);
- goto exception;
- }
- sp[0] = JS_DupValue(ctx, val);
- sp++;
- }
- BREAK;
- CASE(OP_put_var_ref_check):
- {
- int idx;
- idx = get_u16(pc);
- pc += 2;
- if (unlikely(JS_IsUninitialized(*var_refs[idx]->pvalue))) {
- JS_ThrowReferenceErrorUninitialized2(ctx, b, idx, TRUE);
- goto exception;
- }
- set_value(ctx, var_refs[idx]->pvalue, sp[-1]);
- sp--;
- }
- BREAK;
- CASE(OP_put_var_ref_check_init):
- {
- int idx;
- idx = get_u16(pc);
- pc += 2;
- if (unlikely(!JS_IsUninitialized(*var_refs[idx]->pvalue))) {
- JS_ThrowReferenceErrorUninitialized2(ctx, b, idx, TRUE);
- goto exception;
- }
- set_value(ctx, var_refs[idx]->pvalue, sp[-1]);
- sp--;
- }
- BREAK;
- CASE(OP_set_loc_uninitialized):
- {
- int idx;
- idx = get_u16(pc);
- pc += 2;
- set_value(ctx, &var_buf[idx], JS_UNINITIALIZED);
- }
- BREAK;
- CASE(OP_get_loc_check):
- {
- int idx;
- idx = get_u16(pc);
- pc += 2;
- if (unlikely(JS_IsUninitialized(var_buf[idx]))) {
- JS_ThrowReferenceErrorUninitialized2(ctx, b, idx, FALSE);
- goto exception;
- }
- sp[0] = JS_DupValue(ctx, var_buf[idx]);
- sp++;
- }
- BREAK;
- CASE(OP_get_loc_checkthis):
- {
- int idx;
- idx = get_u16(pc);
- pc += 2;
- if (unlikely(JS_IsUninitialized(var_buf[idx]))) {
- JS_ThrowReferenceErrorUninitialized2(caller_ctx, b, idx, FALSE);
- goto exception;
- }
- sp[0] = JS_DupValue(ctx, var_buf[idx]);
- sp++;
- }
- BREAK;
- CASE(OP_put_loc_check):
- {
- int idx;
- idx = get_u16(pc);
- pc += 2;
- if (unlikely(JS_IsUninitialized(var_buf[idx]))) {
- JS_ThrowReferenceErrorUninitialized2(ctx, b, idx, FALSE);
- goto exception;
- }
- set_value(ctx, &var_buf[idx], sp[-1]);
- sp--;
- }
- BREAK;
- CASE(OP_set_loc_check):
- {
- int idx;
- idx = get_u16(pc);
- pc += 2;
- if (unlikely(JS_IsUninitialized(var_buf[idx]))) {
- JS_ThrowReferenceErrorUninitialized2(ctx, b, idx, FALSE);
- goto exception;
- }
- set_value(ctx, &var_buf[idx], JS_DupValue(ctx, sp[-1]));
- }
- BREAK;
- CASE(OP_put_loc_check_init):
- {
- int idx;
- idx = get_u16(pc);
- pc += 2;
- if (unlikely(!JS_IsUninitialized(var_buf[idx]))) {
- JS_ThrowReferenceError(ctx, "'this' can be initialized only once");
- goto exception;
- }
- set_value(ctx, &var_buf[idx], sp[-1]);
- sp--;
- }
- BREAK;
- CASE(OP_close_loc):
- {
- int idx;
- idx = get_u16(pc);
- pc += 2;
- close_lexical_var(ctx, b, sf, idx);
- }
- BREAK;
- CASE(OP_make_loc_ref):
- CASE(OP_make_arg_ref):
- CASE(OP_make_var_ref_ref):
- {
- JSVarRef *var_ref;
- JSProperty *pr;
- JSAtom atom;
- int idx;
- atom = get_u32(pc);
- idx = get_u16(pc + 4);
- pc += 6;
- *sp++ = JS_NewObjectProto(ctx, JS_NULL);
- if (unlikely(JS_IsException(sp[-1])))
- goto exception;
- if (opcode == OP_make_var_ref_ref) {
- var_ref = var_refs[idx];
- js_rc(var_ref)->ref_count++;
- } else {
- var_ref = get_var_ref(ctx, sf, idx, opcode == OP_make_arg_ref);
- if (!var_ref)
- goto exception;
- }
- pr = add_property(ctx, JS_VALUE_GET_OBJ(sp[-1]), atom,
- JS_PROP_WRITABLE | JS_PROP_VARREF);
- if (!pr) {
- free_var_ref(rt, var_ref);
- goto exception;
- }
- pr->u.var_ref = var_ref;
- *sp++ = JS_AtomToValue(ctx, atom);
- }
- BREAK;
- CASE(OP_make_var_ref):
- {
- JSAtom atom;
- atom = get_u32(pc);
- pc += 4;
- sf->cur_pc = pc;
- if (JS_GetGlobalVarRef(ctx, atom, sp))
- goto exception;
- sp += 2;
- }
- BREAK;
- CASE(OP_goto):
- pc += (int32_t)get_u32(pc);
- if (unlikely(js_poll_interrupts(ctx)))
- goto exception;
- BREAK;
- #if SHORT_OPCODES
- CASE(OP_goto16):
- pc += (int16_t)get_u16(pc);
- if (unlikely(js_poll_interrupts(ctx)))
- goto exception;
- BREAK;
- CASE(OP_goto8):
- pc += (int8_t)pc[0];
- if (unlikely(js_poll_interrupts(ctx)))
- goto exception;
- BREAK;
- #endif
- CASE(OP_if_true):
- {
- int res;
- JSValue op1;
- op1 = sp[-1];
- pc += 4;
- if ((uint32_t)JS_VALUE_GET_TAG(op1) <= JS_TAG_UNDEFINED) {
- res = JS_VALUE_GET_INT(op1);
- } else {
- res = JS_ToBoolFree(ctx, op1);
- }
- sp--;
- if (res) {
- pc += (int32_t)get_u32(pc - 4) - 4;
- }
- if (unlikely(js_poll_interrupts(ctx)))
- goto exception;
- }
- BREAK;
- CASE(OP_if_false):
- {
- int res;
- JSValue op1;
- op1 = sp[-1];
- pc += 4;
- /* quick and dirty test for JS_TAG_INT, JS_TAG_BOOL, JS_TAG_NULL and JS_TAG_UNDEFINED */
- if ((uint32_t)JS_VALUE_GET_TAG(op1) <= JS_TAG_UNDEFINED) {
- res = JS_VALUE_GET_INT(op1);
- } else {
- res = JS_ToBoolFree(ctx, op1);
- }
- sp--;
- if (!res) {
- pc += (int32_t)get_u32(pc - 4) - 4;
- }
- if (unlikely(js_poll_interrupts(ctx)))
- goto exception;
- }
- BREAK;
- #if SHORT_OPCODES
- CASE(OP_if_true8):
- {
- int res;
- JSValue op1;
- op1 = sp[-1];
- pc += 1;
- if ((uint32_t)JS_VALUE_GET_TAG(op1) <= JS_TAG_UNDEFINED) {
- res = JS_VALUE_GET_INT(op1);
- } else {
- res = JS_ToBoolFree(ctx, op1);
- }
- sp--;
- if (res) {
- pc += (int8_t)pc[-1] - 1;
- }
- if (unlikely(js_poll_interrupts(ctx)))
- goto exception;
- }
- BREAK;
- CASE(OP_if_false8):
- {
- int res;
- JSValue op1;
- op1 = sp[-1];
- pc += 1;
- if ((uint32_t)JS_VALUE_GET_TAG(op1) <= JS_TAG_UNDEFINED) {
- res = JS_VALUE_GET_INT(op1);
- } else {
- res = JS_ToBoolFree(ctx, op1);
- }
- sp--;
- if (!res) {
- pc += (int8_t)pc[-1] - 1;
- }
- if (unlikely(js_poll_interrupts(ctx)))
- goto exception;
- }
- BREAK;
- #endif
- CASE(OP_catch):
- {
- int32_t diff;
- diff = get_u32(pc);
- sp[0] = JS_NewCatchOffset(ctx, pc + diff - b->byte_code_buf);
- sp++;
- pc += 4;
- }
- BREAK;
- CASE(OP_gosub):
- {
- int32_t diff;
- diff = get_u32(pc);
- /* XXX: should have a different tag to avoid security flaw */
- sp[0] = JS_NewInt32(ctx, pc + 4 - b->byte_code_buf);
- sp++;
- pc += diff;
- }
- BREAK;
- CASE(OP_ret):
- {
- JSValue op1;
- uint32_t pos;
- op1 = sp[-1];
- if (unlikely(JS_VALUE_GET_TAG(op1) != JS_TAG_INT))
- goto ret_fail;
- pos = JS_VALUE_GET_INT(op1);
- if (unlikely(pos >= b->byte_code_len)) {
- ret_fail:
- JS_ThrowInternalError(ctx, "invalid ret value");
- goto exception;
- }
- sp--;
- pc = b->byte_code_buf + pos;
- }
- BREAK;
- CASE(OP_for_in_start):
- sf->cur_pc = pc;
- if (js_for_in_start(ctx, sp))
- goto exception;
- BREAK;
- CASE(OP_for_in_next):
- sf->cur_pc = pc;
- if (js_for_in_next(ctx, sp))
- goto exception;
- sp += 2;
- BREAK;
- CASE(OP_for_of_start):
- sf->cur_pc = pc;
- if (js_for_of_start(ctx, sp, FALSE))
- goto exception;
- sp += 1;
- *sp++ = JS_NewCatchOffset(ctx, 0);
- BREAK;
- CASE(OP_for_of_next):
- {
- int offset = -3 - pc[0];
- pc += 1;
- sf->cur_pc = pc;
- if (js_for_of_next(ctx, sp, offset))
- goto exception;
- sp += 2;
- }
- BREAK;
- CASE(OP_for_await_of_next):
- sf->cur_pc = pc;
- if (js_for_await_of_next(ctx, sp))
- goto exception;
- sp++;
- BREAK;
- CASE(OP_for_await_of_start):
- sf->cur_pc = pc;
- if (js_for_of_start(ctx, sp, TRUE))
- goto exception;
- sp += 1;
- *sp++ = JS_NewCatchOffset(ctx, 0);
- BREAK;
- CASE(OP_iterator_get_value_done):
- sf->cur_pc = pc;
- if (js_iterator_get_value_done(ctx, sp))
- goto exception;
- sp += 1;
- BREAK;
- CASE(OP_iterator_check_object):
- if (unlikely(!JS_IsObject(sp[-1]))) {
- JS_ThrowTypeError(ctx, "iterator must return an object");
- goto exception;
- }
- BREAK;
- CASE(OP_iterator_close):
- /* iter_obj next catch_offset -> */
- sp--; /* drop the catch offset to avoid getting caught by exception */
- JS_FreeValue(ctx, sp[-1]); /* drop the next method */
- sp--;
- if (!JS_IsUndefined(sp[-1])) {
- sf->cur_pc = pc;
- if (JS_IteratorClose(ctx, sp[-1], FALSE))
- goto exception;
- JS_FreeValue(ctx, sp[-1]);
- }
- sp--;
- BREAK;
- CASE(OP_nip_catch):
- {
- JSValue ret_val;
- /* catch_offset ... ret_val -> ret_eval */
- ret_val = *--sp;
- while (sp > stack_buf &&
- JS_VALUE_GET_TAG(sp[-1]) != JS_TAG_CATCH_OFFSET) {
- JS_FreeValue(ctx, *--sp);
- }
- if (unlikely(sp == stack_buf)) {
- JS_ThrowInternalError(ctx, "nip_catch");
- JS_FreeValue(ctx, ret_val);
- goto exception;
- }
- sp[-1] = ret_val;
- }
- BREAK;
- CASE(OP_iterator_next):
- /* stack: iter_obj next catch_offset val */
- {
- JSValue ret;
- sf->cur_pc = pc;
- ret = JS_Call(ctx, sp[-3], sp[-4],
- 1, (JSValueConst *)(sp - 1));
- if (JS_IsException(ret))
- goto exception;
- JS_FreeValue(ctx, sp[-1]);
- sp[-1] = ret;
- }
- BREAK;
- CASE(OP_iterator_call):
- /* stack: iter_obj next catch_offset val */
- {
- JSValue method, ret;
- BOOL ret_flag;
- int flags;
- flags = *pc++;
- sf->cur_pc = pc;
- method = JS_GetProperty(ctx, sp[-4], (flags & 1) ?
- JS_ATOM_throw : JS_ATOM_return);
- if (JS_IsException(method))
- goto exception;
- if (JS_IsUndefined(method) || JS_IsNull(method)) {
- ret_flag = TRUE;
- } else {
- if (flags & 2) {
- /* no argument */
- ret = JS_CallFree(ctx, method, sp[-4],
- 0, NULL);
- } else {
- ret = JS_CallFree(ctx, method, sp[-4],
- 1, (JSValueConst *)(sp - 1));
- }
- if (JS_IsException(ret))
- goto exception;
- JS_FreeValue(ctx, sp[-1]);
- sp[-1] = ret;
- ret_flag = FALSE;
- }
- sp[0] = JS_NewBool(ctx, ret_flag);
- sp += 1;
- }
- BREAK;
- CASE(OP_lnot):
- {
- int res;
- JSValue op1;
- op1 = sp[-1];
- if ((uint32_t)JS_VALUE_GET_TAG(op1) <= JS_TAG_UNDEFINED) {
- res = JS_VALUE_GET_INT(op1) != 0;
- } else {
- res = JS_ToBoolFree(ctx, op1);
- }
- sp[-1] = JS_NewBool(ctx, !res);
- }
- BREAK;
- #define GET_FIELD_INLINE(name, keep, is_length) \
- { \
- JSValue val, obj; \
- JSAtom atom; \
- JSObject *p; \
- JSProperty *pr; \
- JSShapeProperty *prs; \
- \
- if (is_length) { \
- atom = JS_ATOM_length; \
- } else { \
- atom = get_u32(pc); \
- pc += 4; \
- } \
- \
- obj = sp[-1]; \
- if (likely(JS_VALUE_GET_TAG(obj) == JS_TAG_OBJECT)) { \
- p = JS_VALUE_GET_OBJ(obj); \
- for(;;) { \
- prs = find_own_property(&pr, p, atom); \
- if (prs) { \
- /* found */ \
- if (unlikely(prs->flags & JS_PROP_TMASK)) \
- goto name ## _slow_path; \
- val = JS_DupValue(ctx, pr->u.value); \
- break; \
- } \
- if (unlikely(p->is_exotic)) { \
- /* XXX: should avoid the slow path for arrays \
- and typed arrays by ensuring that 'prop' is \
- not numeric */ \
- obj = JS_MKPTR(JS_TAG_OBJECT, p); \
- goto name ## _slow_path; \
- } \
- p = p->shape->proto; \
- if (!p) { \
- val = JS_UNDEFINED; \
- break; \
- } \
- } \
- } else { \
- name ## _slow_path: \
- sf->cur_pc = pc; \
- val = JS_GetPropertyInternal(ctx, obj, atom, sp[-1], 0); \
- if (unlikely(JS_IsException(val))) \
- goto exception; \
- } \
- if (keep) { \
- *sp++ = val; \
- } else { \
- JS_FreeValue(ctx, sp[-1]); \
- sp[-1] = val; \
- } \
- }
-
- CASE(OP_get_field):
- GET_FIELD_INLINE(get_field, 0, 0);
- BREAK;
- CASE(OP_get_field2):
- GET_FIELD_INLINE(get_field2, 1, 0);
- BREAK;
- #if SHORT_OPCODES
- CASE(OP_get_length):
- GET_FIELD_INLINE(get_length, 0, 1);
- BREAK;
- #endif
-
- CASE(OP_put_field):
- {
- int ret;
- JSValue obj;
- JSAtom atom;
- JSObject *p;
- JSProperty *pr;
- JSShapeProperty *prs;
- atom = get_u32(pc);
- pc += 4;
- obj = sp[-2];
- if (likely(JS_VALUE_GET_TAG(obj) == JS_TAG_OBJECT)) {
- p = JS_VALUE_GET_OBJ(obj);
- prs = find_own_property(&pr, p, atom);
- if (!prs)
- goto put_field_slow_path;
- if (likely((prs->flags & (JS_PROP_TMASK | JS_PROP_WRITABLE |
- JS_PROP_LENGTH)) == JS_PROP_WRITABLE)) {
- /* fast path */
- set_value(ctx, &pr->u.value, sp[-1]);
- } else {
- goto put_field_slow_path;
- }
- JS_FreeValue(ctx, obj);
- sp -= 2;
- } else {
- put_field_slow_path:
- sf->cur_pc = pc;
- ret = JS_SetPropertyInternal(ctx, obj, atom, sp[-1], obj,
- JS_PROP_THROW_STRICT);
- JS_FreeValue(ctx, obj);
- sp -= 2;
- if (unlikely(ret < 0))
- goto exception;
- }
-
- }
- BREAK;
- CASE(OP_private_symbol):
- {
- JSAtom atom;
- JSValue val;
- atom = get_u32(pc);
- pc += 4;
- val = JS_NewSymbolFromAtom(ctx, atom, JS_ATOM_TYPE_PRIVATE);
- if (JS_IsException(val))
- goto exception;
- *sp++ = val;
- }
- BREAK;
- CASE(OP_get_private_field):
- {
- JSValue val;
- val = JS_GetPrivateField(ctx, sp[-2], sp[-1]);
- JS_FreeValue(ctx, sp[-1]);
- JS_FreeValue(ctx, sp[-2]);
- sp[-2] = val;
- sp--;
- if (unlikely(JS_IsException(val)))
- goto exception;
- }
- BREAK;
- CASE(OP_put_private_field):
- {
- int ret;
- ret = JS_SetPrivateField(ctx, sp[-3], sp[-1], sp[-2]);
- JS_FreeValue(ctx, sp[-3]);
- JS_FreeValue(ctx, sp[-1]);
- sp -= 3;
- if (unlikely(ret < 0))
- goto exception;
- }
- BREAK;
- CASE(OP_define_private_field):
- {
- int ret;
- ret = JS_DefinePrivateField(ctx, sp[-3], sp[-2], sp[-1]);
- JS_FreeValue(ctx, sp[-2]);
- sp -= 2;
- if (unlikely(ret < 0))
- goto exception;
- }
- BREAK;
- CASE(OP_define_field):
- {
- int ret;
- JSAtom atom;
- atom = get_u32(pc);
- pc += 4;
- ret = JS_DefinePropertyValue(ctx, sp[-2], atom, sp[-1],
- JS_PROP_C_W_E | JS_PROP_THROW);
- sp--;
- if (unlikely(ret < 0))
- goto exception;
- }
- BREAK;
- CASE(OP_set_name):
- {
- int ret;
- JSAtom atom;
- atom = get_u32(pc);
- pc += 4;
- ret = JS_DefineObjectName(ctx, sp[-1], atom, JS_PROP_CONFIGURABLE);
- if (unlikely(ret < 0))
- goto exception;
- }
- BREAK;
- CASE(OP_set_name_computed):
- {
- int ret;
- ret = JS_DefineObjectNameComputed(ctx, sp[-1], sp[-2], JS_PROP_CONFIGURABLE);
- if (unlikely(ret < 0))
- goto exception;
- }
- BREAK;
- CASE(OP_set_proto):
- {
- JSValue proto;
- sf->cur_pc = pc;
- proto = sp[-1];
- if (JS_IsObject(proto) || JS_IsNull(proto)) {
- if (JS_SetPrototypeInternal(ctx, sp[-2], proto, TRUE) < 0)
- goto exception;
- }
- JS_FreeValue(ctx, proto);
- sp--;
- }
- BREAK;
- CASE(OP_set_home_object):
- js_method_set_home_object(ctx, sp[-1], sp[-2]);
- BREAK;
- CASE(OP_define_method):
- CASE(OP_define_method_computed):
- {
- JSValue getter, setter, value;
- JSValueConst obj;
- JSAtom atom;
- int flags, ret, op_flags;
- BOOL is_computed;
- #define OP_DEFINE_METHOD_METHOD 0
- #define OP_DEFINE_METHOD_GETTER 1
- #define OP_DEFINE_METHOD_SETTER 2
- #define OP_DEFINE_METHOD_ENUMERABLE 4
- is_computed = (opcode == OP_define_method_computed);
- if (is_computed) {
- atom = JS_ValueToAtom(ctx, sp[-2]);
- if (unlikely(atom == JS_ATOM_NULL))
- goto exception;
- opcode += OP_define_method - OP_define_method_computed;
- } else {
- atom = get_u32(pc);
- pc += 4;
- }
- op_flags = *pc++;
- obj = sp[-2 - is_computed];
- flags = JS_PROP_HAS_CONFIGURABLE | JS_PROP_CONFIGURABLE |
- JS_PROP_HAS_ENUMERABLE | JS_PROP_THROW;
- if (op_flags & OP_DEFINE_METHOD_ENUMERABLE)
- flags |= JS_PROP_ENUMERABLE;
- op_flags &= 3;
- value = JS_UNDEFINED;
- getter = JS_UNDEFINED;
- setter = JS_UNDEFINED;
- if (op_flags == OP_DEFINE_METHOD_METHOD) {
- value = sp[-1];
- flags |= JS_PROP_HAS_VALUE | JS_PROP_HAS_WRITABLE | JS_PROP_WRITABLE;
- } else if (op_flags == OP_DEFINE_METHOD_GETTER) {
- getter = sp[-1];
- flags |= JS_PROP_HAS_GET;
- } else {
- setter = sp[-1];
- flags |= JS_PROP_HAS_SET;
- }
- ret = js_method_set_properties(ctx, sp[-1], atom, flags, obj);
- if (ret >= 0) {
- ret = JS_DefineProperty(ctx, obj, atom, value,
- getter, setter, flags);
- }
- JS_FreeValue(ctx, sp[-1]);
- if (is_computed) {
- JS_FreeAtom(ctx, atom);
- JS_FreeValue(ctx, sp[-2]);
- }
- sp -= 1 + is_computed;
- if (unlikely(ret < 0))
- goto exception;
- }
- BREAK;
- CASE(OP_define_class):
- CASE(OP_define_class_computed):
- {
- int class_flags;
- JSAtom atom;
- atom = get_u32(pc);
- class_flags = pc[4];
- pc += 5;
- if (js_op_define_class(ctx, sp, atom, class_flags,
- var_refs, sf,
- (opcode == OP_define_class_computed)) < 0)
- goto exception;
- }
- BREAK;
- #define GET_ARRAY_EL_INLINE(name, keep) \
- { \
- JSValue val, obj, prop; \
- JSObject *p; \
- uint32_t idx; \
- \
- obj = sp[-2]; \
- prop = sp[-1]; \
- if (likely(JS_VALUE_GET_TAG(obj) == JS_TAG_OBJECT && \
- JS_VALUE_GET_TAG(prop) == JS_TAG_INT)) { \
- p = JS_VALUE_GET_OBJ(obj); \
- idx = JS_VALUE_GET_INT(prop); \
- if (unlikely(p->class_id != JS_CLASS_ARRAY)) \
- goto name ## _slow_path; \
- if (unlikely(idx >= p->u.array.count)) \
- goto name ## _slow_path; \
- val = JS_DupValue(ctx, p->u.array.u.values[idx]); \
- } else { \
- name ## _slow_path: \
- sf->cur_pc = pc; \
- val = JS_GetPropertyValue(ctx, obj, prop); \
- if (unlikely(JS_IsException(val))) { \
- if (keep) \
- sp[-1] = JS_UNDEFINED; \
- else \
- sp--; \
- goto exception; \
- } \
- } \
- if (keep) { \
- sp[-1] = val; \
- } else { \
- JS_FreeValue(ctx, obj); \
- sp[-2] = val; \
- sp--; \
- } \
- }
-
- CASE(OP_get_array_el):
- GET_ARRAY_EL_INLINE(get_array_el, 0);
- BREAK;
- CASE(OP_get_array_el2):
- GET_ARRAY_EL_INLINE(get_array_el2, 1);
- BREAK;
- CASE(OP_get_array_el3):
- {
- JSValue val;
- JSObject *p;
- uint32_t idx;
- if (likely(JS_VALUE_GET_TAG(sp[-2]) == JS_TAG_OBJECT &&
- JS_VALUE_GET_TAG(sp[-1]) == JS_TAG_INT)) {
- p = JS_VALUE_GET_OBJ(sp[-2]);
- idx = JS_VALUE_GET_INT(sp[-1]);
- if (unlikely(p->class_id != JS_CLASS_ARRAY))
- goto get_array_el3_slow_path;
- if (unlikely(idx >= p->u.array.count))
- goto get_array_el3_slow_path;
- val = JS_DupValue(ctx, p->u.array.u.values[idx]);
- } else {
- get_array_el3_slow_path:
- switch (JS_VALUE_GET_TAG(sp[-1])) {
- case JS_TAG_INT:
- case JS_TAG_STRING:
- case JS_TAG_SYMBOL:
- /* undefined and null are tested in JS_GetPropertyValue() */
- break;
- default:
- /* must be tested before JS_ToPropertyKey */
- if (unlikely(JS_IsUndefined(sp[-2]) || JS_IsNull(sp[-2]))) {
- JS_ThrowTypeError(ctx, "value has no property");
- goto exception;
- }
- sf->cur_pc = pc;
- ret_val = JS_ToPropertyKey(ctx, sp[-1]);
- if (JS_IsException(ret_val))
- goto exception;
- JS_FreeValue(ctx, sp[-1]);
- sp[-1] = ret_val;
- break;
- }
- sf->cur_pc = pc;
- val = JS_GetPropertyValue(ctx, sp[-2], JS_DupValue(ctx, sp[-1]));
- if (unlikely(JS_IsException(val)))
- goto exception;
- }
- *sp++ = val;
- }
- BREAK;
-
- CASE(OP_get_ref_value):
- {
- JSValue val;
- JSAtom atom;
- int ret;
-
- sf->cur_pc = pc;
- atom = JS_ValueToAtom(ctx, sp[-1]);
- if (atom == JS_ATOM_NULL)
- goto exception;
- if (unlikely(JS_IsUndefined(sp[-2]))) {
- JS_ThrowReferenceErrorNotDefined(ctx, atom);
- JS_FreeAtom(ctx, atom);
- goto exception;
- }
- ret = JS_HasProperty(ctx, sp[-2], atom);
- if (unlikely(ret <= 0)) {
- if (ret < 0) {
- JS_FreeAtom(ctx, atom);
- goto exception;
- }
- if (is_strict_mode(ctx)) {
- JS_ThrowReferenceErrorNotDefined(ctx, atom);
- JS_FreeAtom(ctx, atom);
- goto exception;
- }
- val = JS_UNDEFINED;
- } else {
- val = JS_GetProperty(ctx, sp[-2], atom);
- }
- JS_FreeAtom(ctx, atom);
- if (unlikely(JS_IsException(val)))
- goto exception;
- sp[0] = val;
- sp++;
- }
- BREAK;
- CASE(OP_get_super_value):
- {
- JSValue val;
- JSAtom atom;
- sf->cur_pc = pc;
- atom = JS_ValueToAtom(ctx, sp[-1]);
- if (unlikely(atom == JS_ATOM_NULL))
- goto exception;
- val = JS_GetPropertyInternal(ctx, sp[-2], atom, sp[-3], FALSE);
- JS_FreeAtom(ctx, atom);
- if (unlikely(JS_IsException(val)))
- goto exception;
- JS_FreeValue(ctx, sp[-1]);
- JS_FreeValue(ctx, sp[-2]);
- JS_FreeValue(ctx, sp[-3]);
- sp[-3] = val;
- sp -= 2;
- }
- BREAK;
- CASE(OP_put_array_el):
- {
- int ret;
- JSObject *p;
- uint32_t idx;
- if (likely(JS_VALUE_GET_TAG(sp[-3]) == JS_TAG_OBJECT &&
- JS_VALUE_GET_TAG(sp[-2]) == JS_TAG_INT)) {
- p = JS_VALUE_GET_OBJ(sp[-3]);
- idx = JS_VALUE_GET_INT(sp[-2]);
- if (unlikely(p->class_id != JS_CLASS_ARRAY))
- goto put_array_el_slow_path;
- if (unlikely(idx >= (uint32_t)p->u.array.count)) {
- uint32_t new_len, array_len;
- if (unlikely(idx != (uint32_t)p->u.array.count ||
- !p->fast_array ||
- !can_extend_fast_array(p))) {
- goto put_array_el_slow_path;
- }
- if (likely(JS_VALUE_GET_TAG(p->prop[0].u.value) != JS_TAG_INT))
- goto put_array_el_slow_path;
- /* cannot overflow otherwise the length would not be an integer */
- new_len = idx + 1;
- if (unlikely(new_len > p->u.array.u1.size))
- goto put_array_el_slow_path;
- array_len = JS_VALUE_GET_INT(p->prop[0].u.value);
- if (new_len > array_len) {
- if (unlikely(!(get_shape_prop(p->shape)->flags & JS_PROP_WRITABLE)))
- goto put_array_el_slow_path;
- p->prop[0].u.value = JS_NewInt32(ctx, new_len);
- }
- p->u.array.count = new_len;
- p->u.array.u.values[idx] = sp[-1];
- } else {
- set_value(ctx, &p->u.array.u.values[idx], sp[-1]);
- }
- JS_FreeValue(ctx, sp[-3]);
- sp -= 3;
- } else {
- put_array_el_slow_path:
- sf->cur_pc = pc;
- ret = JS_SetPropertyValue(ctx, sp[-3], sp[-2], sp[-1], JS_PROP_THROW_STRICT);
- JS_FreeValue(ctx, sp[-3]);
- sp -= 3;
- if (unlikely(ret < 0))
- goto exception;
- }
- }
- BREAK;
- CASE(OP_put_ref_value):
- {
- int ret;
- JSAtom atom;
- sf->cur_pc = pc;
- atom = JS_ValueToAtom(ctx, sp[-2]);
- if (unlikely(atom == JS_ATOM_NULL))
- goto exception;
- if (unlikely(JS_IsUndefined(sp[-3]))) {
- if (is_strict_mode(ctx)) {
- JS_ThrowReferenceErrorNotDefined(ctx, atom);
- JS_FreeAtom(ctx, atom);
- goto exception;
- } else {
- sp[-3] = JS_DupValue(ctx, ctx->global_obj);
- }
- }
- ret = JS_HasProperty(ctx, sp[-3], atom);
- if (unlikely(ret <= 0)) {
- if (unlikely(ret < 0)) {
- JS_FreeAtom(ctx, atom);
- goto exception;
- }
- if (is_strict_mode(ctx)) {
- JS_ThrowReferenceErrorNotDefined(ctx, atom);
- JS_FreeAtom(ctx, atom);
- goto exception;
- }
- }
- ret = JS_SetPropertyInternal(ctx, sp[-3], atom, sp[-1], sp[-3], JS_PROP_THROW_STRICT);
- JS_FreeAtom(ctx, atom);
- JS_FreeValue(ctx, sp[-2]);
- JS_FreeValue(ctx, sp[-3]);
- sp -= 3;
- if (unlikely(ret < 0))
- goto exception;
- }
- BREAK;
- CASE(OP_put_super_value):
- {
- int ret;
- JSAtom atom;
- sf->cur_pc = pc;
- if (JS_VALUE_GET_TAG(sp[-3]) != JS_TAG_OBJECT) {
- JS_ThrowTypeErrorNotAnObject(ctx);
- goto exception;
- }
- atom = JS_ValueToAtom(ctx, sp[-2]);
- if (unlikely(atom == JS_ATOM_NULL))
- goto exception;
- ret = JS_SetPropertyInternal(ctx, sp[-3], atom, sp[-1], sp[-4],
- JS_PROP_THROW_STRICT);
- JS_FreeAtom(ctx, atom);
- JS_FreeValue(ctx, sp[-4]);
- JS_FreeValue(ctx, sp[-3]);
- JS_FreeValue(ctx, sp[-2]);
- sp -= 4;
- if (ret < 0)
- goto exception;
- }
- BREAK;
- CASE(OP_define_array_el):
- {
- int ret;
- ret = JS_DefinePropertyValueValue(ctx, sp[-3], JS_DupValue(ctx, sp[-2]), sp[-1],
- JS_PROP_C_W_E | JS_PROP_THROW);
- sp -= 1;
- if (unlikely(ret < 0))
- goto exception;
- }
- BREAK;
- CASE(OP_append): /* array pos enumobj -- array pos */
- {
- sf->cur_pc = pc;
- if (js_append_enumerate(ctx, sp))
- goto exception;
- JS_FreeValue(ctx, *--sp);
- }
- BREAK;
- CASE(OP_copy_data_properties): /* target source excludeList */
- {
- /* stack offsets (-1 based):
- 2 bits for target,
- 3 bits for source,
- 2 bits for exclusionList */
- int mask;
- mask = *pc++;
- sf->cur_pc = pc;
- if (JS_CopyDataProperties(ctx, sp[-1 - (mask & 3)],
- sp[-1 - ((mask >> 2) & 7)],
- sp[-1 - ((mask >> 5) & 7)], 0))
- goto exception;
- }
- BREAK;
- CASE(OP_add):
- {
- JSValue op1, op2;
- op1 = sp[-2];
- op2 = sp[-1];
- if (likely(JS_VALUE_IS_BOTH_INT(op1, op2))) {
- int64_t r;
- r = (int64_t)JS_VALUE_GET_INT(op1) + JS_VALUE_GET_INT(op2);
- if (unlikely((int)r != r)) {
- sp[-2] = __JS_NewFloat64(ctx, (double)r);
- } else {
- sp[-2] = JS_NewInt32(ctx, r);
- }
- sp--;
- } else if (JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(op1)) ||
- JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(op2))) {
- double d1, d2;
- if (JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(op1))) {
- d1 = JS_VALUE_GET_FLOAT64(op1);
- } else if (JS_VALUE_GET_TAG(op1) == JS_TAG_INT) {
- d1 = JS_VALUE_GET_INT(op1);
- } else {
- goto add_slow_case;
- }
- if (JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(op2))) {
- d2 = JS_VALUE_GET_FLOAT64(op2);
- } else if (JS_VALUE_GET_TAG(op2) == JS_TAG_INT) {
- d2 = JS_VALUE_GET_INT(op2);
- } else {
- goto add_slow_case;
- }
- sp[-2] = __JS_NewFloat64(ctx, d1 + d2);
- sp--;
- } else if (JS_IsString(op1) && JS_IsString(op2)) {
- sp[-2] = JS_ConcatString(ctx, op1, op2);
- sp--;
- if (JS_IsException(sp[-1]))
- goto exception;
- } else {
- add_slow_case:
- sf->cur_pc = pc;
- if (js_add_slow(ctx, sp))
- goto exception;
- sp--;
- }
- }
- BREAK;
- CASE(OP_add_loc):
- {
- JSValue op2;
- JSValue *pv;
- int idx;
- idx = *pc;
- pc += 1;
- op2 = sp[-1];
- pv = &var_buf[idx];
- if (likely(JS_VALUE_IS_BOTH_INT(*pv, op2))) {
- int64_t r;
- r = (int64_t)JS_VALUE_GET_INT(*pv) + JS_VALUE_GET_INT(op2);
- if (unlikely((int)r != r)) {
- *pv = __JS_NewFloat64(ctx, (double)r);
- } else {
- *pv = JS_NewInt32(ctx, r);
- }
- sp--;
- } else if (JS_VALUE_IS_BOTH_FLOAT(*pv, op2)) {
- *pv = __JS_NewFloat64(ctx, JS_VALUE_GET_FLOAT64(*pv) +
- JS_VALUE_GET_FLOAT64(op2));
- sp--;
- } else if (JS_VALUE_GET_TAG(*pv) == JS_TAG_STRING &&
- JS_VALUE_GET_TAG(op2) == JS_TAG_STRING) {
- sp--;
- sf->cur_pc = pc;
- if (JS_ConcatStringInPlace(ctx, JS_VALUE_GET_STRING(*pv), op2)) {
- JS_FreeValue(ctx, op2);
- } else {
- op2 = JS_ConcatString(ctx, JS_DupValue(ctx, *pv), op2);
- if (JS_IsException(op2))
- goto exception;
- set_value(ctx, pv, op2);
- }
- } else {
- JSValue ops[2];
- /* In case of exception, js_add_slow frees ops[0]
- and ops[1], so we must duplicate *pv */
- sf->cur_pc = pc;
- ops[0] = JS_DupValue(ctx, *pv);
- ops[1] = op2;
- sp--;
- if (js_add_slow(ctx, ops + 2))
- goto exception;
- set_value(ctx, pv, ops[0]);
- }
- }
- BREAK;
- CASE(OP_sub):
- {
- JSValue op1, op2;
- op1 = sp[-2];
- op2 = sp[-1];
- if (likely(JS_VALUE_IS_BOTH_INT(op1, op2))) {
- int64_t r;
- r = (int64_t)JS_VALUE_GET_INT(op1) - JS_VALUE_GET_INT(op2);
- if (unlikely((int)r != r)) {
- sp[-2] = __JS_NewFloat64(ctx, (double)r);
- } else {
- sp[-2] = JS_NewInt32(ctx, r);
- }
- sp--;
- } else if (JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(op1)) ||
- JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(op2))) {
- double d1, d2;
- if (JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(op1))) {
- d1 = JS_VALUE_GET_FLOAT64(op1);
- } else if (JS_VALUE_GET_TAG(op1) == JS_TAG_INT) {
- d1 = JS_VALUE_GET_INT(op1);
- } else {
- goto binary_arith_slow;
- }
- if (JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(op2))) {
- d2 = JS_VALUE_GET_FLOAT64(op2);
- } else if (JS_VALUE_GET_TAG(op2) == JS_TAG_INT) {
- d2 = JS_VALUE_GET_INT(op2);
- } else {
- goto binary_arith_slow;
- }
- sp[-2] = __JS_NewFloat64(ctx, d1 - d2);
- sp--;
- } else {
- goto binary_arith_slow;
- }
- }
- BREAK;
- CASE(OP_mul):
- {
- JSValue op1, op2;
- double d;
- op1 = sp[-2];
- op2 = sp[-1];
- if (likely(JS_VALUE_IS_BOTH_INT(op1, op2))) {
- int32_t v1, v2;
- int64_t r;
- v1 = JS_VALUE_GET_INT(op1);
- v2 = JS_VALUE_GET_INT(op2);
- r = (int64_t)v1 * v2;
- if (unlikely((int)r != r)) {
- d = (double)r;
- goto mul_fp_res;
- }
- /* need to test zero case for -0 result */
- if (unlikely(r == 0 && (v1 | v2) < 0)) {
- d = -0.0;
- goto mul_fp_res;
- }
- sp[-2] = JS_NewInt32(ctx, r);
- sp--;
- } else if (JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(op1)) ||
- JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(op2))) {
- double d1, d2;
- if (JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(op1))) {
- d1 = JS_VALUE_GET_FLOAT64(op1);
- } else if (JS_VALUE_GET_TAG(op1) == JS_TAG_INT) {
- d1 = JS_VALUE_GET_INT(op1);
- } else {
- goto binary_arith_slow;
- }
- if (JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(op2))) {
- d2 = JS_VALUE_GET_FLOAT64(op2);
- } else if (JS_VALUE_GET_TAG(op2) == JS_TAG_INT) {
- d2 = JS_VALUE_GET_INT(op2);
- } else {
- goto binary_arith_slow;
- }
- d = d1 * d2;
- mul_fp_res:
- sp[-2] = __JS_NewFloat64(ctx, d);
- sp--;
- } else {
- goto binary_arith_slow;
- }
- }
- BREAK;
- CASE(OP_div):
- {
- JSValue op1, op2;
- op1 = sp[-2];
- op2 = sp[-1];
- if (likely(JS_VALUE_IS_BOTH_INT(op1, op2))) {
- int v1, v2;
- v1 = JS_VALUE_GET_INT(op1);
- v2 = JS_VALUE_GET_INT(op2);
- sp[-2] = JS_NewFloat64(ctx, (double)v1 / (double)v2);
- sp--;
- } else {
- goto binary_arith_slow;
- }
- }
- BREAK;
- CASE(OP_mod):
- {
- JSValue op1, op2;
- op1 = sp[-2];
- op2 = sp[-1];
- if (likely(JS_VALUE_IS_BOTH_INT(op1, op2))) {
- int v1, v2, r;
- v1 = JS_VALUE_GET_INT(op1);
- v2 = JS_VALUE_GET_INT(op2);
- /* We must avoid v2 = 0, v1 = INT32_MIN and v2 =
- -1 and the cases where the result is -0. */
- if (unlikely(v1 < 0 || v2 <= 0))
- goto binary_arith_slow;
- r = v1 % v2;
- sp[-2] = JS_NewInt32(ctx, r);
- sp--;
- } else {
- goto binary_arith_slow;
- }
- }
- BREAK;
- CASE(OP_pow):
- binary_arith_slow:
- sf->cur_pc = pc;
- if (js_binary_arith_slow(ctx, sp, opcode))
- goto exception;
- sp--;
- BREAK;
- CASE(OP_plus):
- {
- JSValue op1;
- uint32_t tag;
- op1 = sp[-1];
- tag = JS_VALUE_GET_TAG(op1);
- if (tag == JS_TAG_INT || JS_TAG_IS_FLOAT64(tag)) {
- } else if (tag == JS_TAG_NULL || tag == JS_TAG_BOOL) {
- sp[-1] = JS_NewInt32(ctx, JS_VALUE_GET_INT(op1));
- } else {
- sf->cur_pc = pc;
- if (js_unary_arith_slow(ctx, sp, opcode))
- goto exception;
- }
- }
- BREAK;
- CASE(OP_neg):
- {
- JSValue op1;
- uint32_t tag;
- int val;
- double d;
- op1 = sp[-1];
- tag = JS_VALUE_GET_TAG(op1);
- if (tag == JS_TAG_INT ||
- tag == JS_TAG_BOOL ||
- tag == JS_TAG_NULL) {
- val = JS_VALUE_GET_INT(op1);
- /* Note: -0 cannot be expressed as integer */
- if (unlikely(val == 0)) {
- d = -0.0;
- goto neg_fp_res;
- }
- if (unlikely(val == INT32_MIN)) {
- d = -(double)val;
- goto neg_fp_res;
- }
- sp[-1] = JS_NewInt32(ctx, -val);
- } else if (JS_TAG_IS_FLOAT64(tag)) {
- d = -JS_VALUE_GET_FLOAT64(op1);
- neg_fp_res:
- sp[-1] = __JS_NewFloat64(ctx, d);
- } else {
- sf->cur_pc = pc;
- if (js_unary_arith_slow(ctx, sp, opcode))
- goto exception;
- }
- }
- BREAK;
- CASE(OP_inc):
- {
- JSValue op1;
- int val;
- op1 = sp[-1];
- if (JS_VALUE_GET_TAG(op1) == JS_TAG_INT) {
- val = JS_VALUE_GET_INT(op1);
- if (unlikely(val == INT32_MAX))
- goto inc_slow;
- sp[-1] = JS_NewInt32(ctx, val + 1);
- } else {
- inc_slow:
- sf->cur_pc = pc;
- if (js_unary_arith_slow(ctx, sp, opcode))
- goto exception;
- }
- }
- BREAK;
- CASE(OP_dec):
- {
- JSValue op1;
- int val;
- op1 = sp[-1];
- if (JS_VALUE_GET_TAG(op1) == JS_TAG_INT) {
- val = JS_VALUE_GET_INT(op1);
- if (unlikely(val == INT32_MIN))
- goto dec_slow;
- sp[-1] = JS_NewInt32(ctx, val - 1);
- } else {
- dec_slow:
- sf->cur_pc = pc;
- if (js_unary_arith_slow(ctx, sp, opcode))
- goto exception;
- }
- }
- BREAK;
- CASE(OP_post_inc):
- {
- JSValue op1;
- int val;
- op1 = sp[-1];
- if (JS_VALUE_GET_TAG(op1) == JS_TAG_INT) {
- val = JS_VALUE_GET_INT(op1);
- if (unlikely(val == INT32_MAX))
- goto post_inc_slow;
- sp[0] = JS_NewInt32(ctx, val + 1);
- } else {
- post_inc_slow:
- sf->cur_pc = pc;
- if (js_post_inc_slow(ctx, sp, opcode))
- goto exception;
- }
- sp++;
- }
- BREAK;
- CASE(OP_post_dec):
- {
- JSValue op1;
- int val;
- op1 = sp[-1];
- if (JS_VALUE_GET_TAG(op1) == JS_TAG_INT) {
- val = JS_VALUE_GET_INT(op1);
- if (unlikely(val == INT32_MIN))
- goto post_dec_slow;
- sp[0] = JS_NewInt32(ctx, val - 1);
- } else {
- post_dec_slow:
- sf->cur_pc = pc;
- if (js_post_inc_slow(ctx, sp, opcode))
- goto exception;
- }
- sp++;
- }
- BREAK;
- CASE(OP_inc_loc):
- {
- JSValue op1;
- int val;
- int idx;
- idx = *pc;
- pc += 1;
- op1 = var_buf[idx];
- if (JS_VALUE_GET_TAG(op1) == JS_TAG_INT) {
- val = JS_VALUE_GET_INT(op1);
- if (unlikely(val == INT32_MAX))
- goto inc_loc_slow;
- var_buf[idx] = JS_NewInt32(ctx, val + 1);
- } else {
- inc_loc_slow:
- sf->cur_pc = pc;
- /* must duplicate otherwise the variable value may
- be destroyed before JS code accesses it */
- op1 = JS_DupValue(ctx, op1);
- if (js_unary_arith_slow(ctx, &op1 + 1, OP_inc))
- goto exception;
- set_value(ctx, &var_buf[idx], op1);
- }
- }
- BREAK;
- CASE(OP_dec_loc):
- {
- JSValue op1;
- int val;
- int idx;
- idx = *pc;
- pc += 1;
- op1 = var_buf[idx];
- if (JS_VALUE_GET_TAG(op1) == JS_TAG_INT) {
- val = JS_VALUE_GET_INT(op1);
- if (unlikely(val == INT32_MIN))
- goto dec_loc_slow;
- var_buf[idx] = JS_NewInt32(ctx, val - 1);
- } else {
- dec_loc_slow:
- sf->cur_pc = pc;
- /* must duplicate otherwise the variable value may
- be destroyed before JS code accesses it */
- op1 = JS_DupValue(ctx, op1);
- if (js_unary_arith_slow(ctx, &op1 + 1, OP_dec))
- goto exception;
- set_value(ctx, &var_buf[idx], op1);
- }
- }
- BREAK;
- CASE(OP_not):
- {
- JSValue op1;
- op1 = sp[-1];
- if (JS_VALUE_GET_TAG(op1) == JS_TAG_INT) {
- sp[-1] = JS_NewInt32(ctx, ~JS_VALUE_GET_INT(op1));
- } else {
- sf->cur_pc = pc;
- if (js_not_slow(ctx, sp))
- goto exception;
- }
- }
- BREAK;
- CASE(OP_shl):
- {
- JSValue op1, op2;
- op1 = sp[-2];
- op2 = sp[-1];
- if (likely(JS_VALUE_IS_BOTH_INT(op1, op2))) {
- uint32_t v1, v2;
- v1 = JS_VALUE_GET_INT(op1);
- v2 = JS_VALUE_GET_INT(op2);
- v2 &= 0x1f;
- sp[-2] = JS_NewInt32(ctx, v1 << v2);
- sp--;
- } else {
- sf->cur_pc = pc;
- if (js_binary_logic_slow(ctx, sp, opcode))
- goto exception;
- sp--;
- }
- }
- BREAK;
- CASE(OP_shr):
- {
- JSValue op1, op2;
- op1 = sp[-2];
- op2 = sp[-1];
- if (likely(JS_VALUE_IS_BOTH_INT(op1, op2))) {
- uint32_t v2;
- v2 = JS_VALUE_GET_INT(op2);
- v2 &= 0x1f;
- sp[-2] = JS_NewUint32(ctx,
- (uint32_t)JS_VALUE_GET_INT(op1) >>
- v2);
- sp--;
- } else {
- sf->cur_pc = pc;
- if (js_shr_slow(ctx, sp))
- goto exception;
- sp--;
- }
- }
- BREAK;
- CASE(OP_sar):
- {
- JSValue op1, op2;
- op1 = sp[-2];
- op2 = sp[-1];
- if (likely(JS_VALUE_IS_BOTH_INT(op1, op2))) {
- uint32_t v2;
- v2 = JS_VALUE_GET_INT(op2);
- v2 &= 0x1f;
- sp[-2] = JS_NewInt32(ctx,
- (int)JS_VALUE_GET_INT(op1) >> v2);
- sp--;
- } else {
- sf->cur_pc = pc;
- if (js_binary_logic_slow(ctx, sp, opcode))
- goto exception;
- sp--;
- }
- }
- BREAK;
- CASE(OP_and):
- {
- JSValue op1, op2;
- op1 = sp[-2];
- op2 = sp[-1];
- if (likely(JS_VALUE_IS_BOTH_INT(op1, op2))) {
- sp[-2] = JS_NewInt32(ctx,
- JS_VALUE_GET_INT(op1) &
- JS_VALUE_GET_INT(op2));
- sp--;
- } else {
- sf->cur_pc = pc;
- if (js_binary_logic_slow(ctx, sp, opcode))
- goto exception;
- sp--;
- }
- }
- BREAK;
- CASE(OP_or):
- {
- JSValue op1, op2;
- op1 = sp[-2];
- op2 = sp[-1];
- if (likely(JS_VALUE_IS_BOTH_INT(op1, op2))) {
- sp[-2] = JS_NewInt32(ctx,
- JS_VALUE_GET_INT(op1) |
- JS_VALUE_GET_INT(op2));
- sp--;
- } else {
- sf->cur_pc = pc;
- if (js_binary_logic_slow(ctx, sp, opcode))
- goto exception;
- sp--;
- }
- }
- BREAK;
- CASE(OP_xor):
- {
- JSValue op1, op2;
- op1 = sp[-2];
- op2 = sp[-1];
- if (likely(JS_VALUE_IS_BOTH_INT(op1, op2))) {
- sp[-2] = JS_NewInt32(ctx,
- JS_VALUE_GET_INT(op1) ^
- JS_VALUE_GET_INT(op2));
- sp--;
- } else {
- sf->cur_pc = pc;
- if (js_binary_logic_slow(ctx, sp, opcode))
- goto exception;
- sp--;
- }
- }
- BREAK;
- #define OP_CMP(opcode, binary_op, slow_call) \
- CASE(opcode): \
- { \
- JSValue op1, op2; \
- op1 = sp[-2]; \
- op2 = sp[-1]; \
- if (likely(JS_VALUE_IS_BOTH_INT(op1, op2))) { \
- sp[-2] = JS_NewBool(ctx, JS_VALUE_GET_INT(op1) binary_op JS_VALUE_GET_INT(op2)); \
- sp--; \
- } else { \
- sf->cur_pc = pc; \
- if (slow_call) \
- goto exception; \
- sp--; \
- } \
- } \
- BREAK
- OP_CMP(OP_lt, <, js_relational_slow(ctx, sp, opcode));
- OP_CMP(OP_lte, <=, js_relational_slow(ctx, sp, opcode));
- OP_CMP(OP_gt, >, js_relational_slow(ctx, sp, opcode));
- OP_CMP(OP_gte, >=, js_relational_slow(ctx, sp, opcode));
- OP_CMP(OP_eq, ==, js_eq_slow(ctx, sp, 0));
- OP_CMP(OP_neq, !=, js_eq_slow(ctx, sp, 1));
- OP_CMP(OP_strict_eq, ==, js_strict_eq_slow(ctx, sp, 0));
- OP_CMP(OP_strict_neq, !=, js_strict_eq_slow(ctx, sp, 1));
- CASE(OP_in):
- sf->cur_pc = pc;
- if (js_operator_in(ctx, sp))
- goto exception;
- sp--;
- BREAK;
- CASE(OP_private_in):
- sf->cur_pc = pc;
- if (js_operator_private_in(ctx, sp))
- goto exception;
- sp--;
- BREAK;
- CASE(OP_instanceof):
- sf->cur_pc = pc;
- if (js_operator_instanceof(ctx, sp))
- goto exception;
- sp--;
- BREAK;
- CASE(OP_typeof):
- {
- JSValue op1;
- JSAtom atom;
- op1 = sp[-1];
- atom = js_operator_typeof(ctx, op1);
- JS_FreeValue(ctx, op1);
- sp[-1] = JS_AtomToString(ctx, atom);
- }
- BREAK;
- CASE(OP_delete):
- sf->cur_pc = pc;
- if (js_operator_delete(ctx, sp))
- goto exception;
- sp--;
- BREAK;
- CASE(OP_delete_var):
- {
- JSAtom atom;
- int ret;
- atom = get_u32(pc);
- pc += 4;
- sf->cur_pc = pc;
- ret = JS_DeleteGlobalVar(ctx, atom);
- if (unlikely(ret < 0))
- goto exception;
- *sp++ = JS_NewBool(ctx, ret);
- }
- BREAK;
- CASE(OP_to_object):
- if (JS_VALUE_GET_TAG(sp[-1]) != JS_TAG_OBJECT) {
- sf->cur_pc = pc;
- ret_val = JS_ToObject(ctx, sp[-1]);
- if (JS_IsException(ret_val))
- goto exception;
- JS_FreeValue(ctx, sp[-1]);
- sp[-1] = ret_val;
- }
- BREAK;
- CASE(OP_to_propkey):
- switch (JS_VALUE_GET_TAG(sp[-1])) {
- case JS_TAG_INT:
- case JS_TAG_STRING:
- case JS_TAG_SYMBOL:
- break;
- default:
- sf->cur_pc = pc;
- ret_val = JS_ToPropertyKey(ctx, sp[-1]);
- if (JS_IsException(ret_val))
- goto exception;
- JS_FreeValue(ctx, sp[-1]);
- sp[-1] = ret_val;
- break;
- }
- BREAK;
- #if 0
- CASE(OP_to_string):
- if (JS_VALUE_GET_TAG(sp[-1]) != JS_TAG_STRING) {
- ret_val = JS_ToString(ctx, sp[-1]);
- if (JS_IsException(ret_val))
- goto exception;
- JS_FreeValue(ctx, sp[-1]);
- sp[-1] = ret_val;
- }
- BREAK;
- #endif
- CASE(OP_with_get_var):
- CASE(OP_with_put_var):
- CASE(OP_with_delete_var):
- CASE(OP_with_make_ref):
- CASE(OP_with_get_ref):
- {
- JSAtom atom;
- int32_t diff;
- JSValue obj, val;
- int ret, is_with;
- atom = get_u32(pc);
- diff = get_u32(pc + 4);
- is_with = pc[8];
- pc += 9;
- sf->cur_pc = pc;
- obj = sp[-1];
- ret = JS_HasProperty(ctx, obj, atom);
- if (unlikely(ret < 0))
- goto exception;
- if (ret) {
- if (is_with) {
- ret = js_has_unscopable(ctx, obj, atom);
- if (unlikely(ret < 0))
- goto exception;
- if (ret)
- goto no_with;
- }
- switch (opcode) {
- case OP_with_get_var:
- /* in Object Environment Records, GetBindingValue() calls HasProperty() */
- ret = JS_HasProperty(ctx, obj, atom);
- if (unlikely(ret <= 0)) {
- if (ret < 0)
- goto exception;
- if (is_strict_mode(ctx)) {
- JS_ThrowReferenceErrorNotDefined(ctx, atom);
- goto exception;
- }
- val = JS_UNDEFINED;
- } else {
- val = JS_GetProperty(ctx, obj, atom);
- if (unlikely(JS_IsException(val)))
- goto exception;
- }
- set_value(ctx, &sp[-1], val);
- break;
- case OP_with_put_var: /* used e.g. in for in/of */
- /* in Object Environment Records, SetMutableBinding() calls HasProperty() */
- ret = JS_HasProperty(ctx, obj, atom);
- if (unlikely(ret <= 0)) {
- if (ret < 0)
- goto exception;
- if (is_strict_mode(ctx)) {
- JS_ThrowReferenceErrorNotDefined(ctx, atom);
- goto exception;
- }
- }
- ret = JS_SetPropertyInternal(ctx, obj, atom, sp[-2], obj,
- JS_PROP_THROW_STRICT);
- JS_FreeValue(ctx, sp[-1]);
- sp -= 2;
- if (unlikely(ret < 0))
- goto exception;
- break;
- case OP_with_delete_var:
- ret = JS_DeleteProperty(ctx, obj, atom, 0);
- if (unlikely(ret < 0))
- goto exception;
- JS_FreeValue(ctx, sp[-1]);
- sp[-1] = JS_NewBool(ctx, ret);
- break;
- case OP_with_make_ref:
- /* produce a pair object/propname on the stack */
- *sp++ = JS_AtomToValue(ctx, atom);
- break;
- case OP_with_get_ref:
- /* produce a pair object/method on the stack */
- /* in Object Environment Records, GetBindingValue() calls HasProperty() */
- ret = JS_HasProperty(ctx, obj, atom);
- if (unlikely(ret < 0))
- goto exception;
- if (!ret) {
- val = JS_UNDEFINED;
- } else {
- val = JS_GetProperty(ctx, obj, atom);
- if (unlikely(JS_IsException(val)))
- goto exception;
- }
- *sp++ = val;
- break;
- }
- pc += diff - 5;
- } else {
- no_with:
- /* if not jumping, drop the object argument */
- JS_FreeValue(ctx, sp[-1]);
- sp--;
- }
- }
- BREAK;
- CASE(OP_await):
- ret_val = JS_NewInt32(ctx, FUNC_RET_AWAIT);
- goto done_generator;
- CASE(OP_yield):
- ret_val = JS_NewInt32(ctx, FUNC_RET_YIELD);
- goto done_generator;
- CASE(OP_yield_star):
- CASE(OP_async_yield_star):
- ret_val = JS_NewInt32(ctx, FUNC_RET_YIELD_STAR);
- goto done_generator;
- CASE(OP_return_async):
- ret_val = JS_UNDEFINED;
- goto done_generator;
- CASE(OP_initial_yield):
- ret_val = JS_NewInt32(ctx, FUNC_RET_INITIAL_YIELD);
- goto done_generator;
- CASE(OP_nop):
- BREAK;
- CASE(OP_is_undefined_or_null):
- if (JS_VALUE_GET_TAG(sp[-1]) == JS_TAG_UNDEFINED ||
- JS_VALUE_GET_TAG(sp[-1]) == JS_TAG_NULL) {
- goto set_true;
- } else {
- goto free_and_set_false;
- }
- #if SHORT_OPCODES
- CASE(OP_is_undefined):
- if (JS_VALUE_GET_TAG(sp[-1]) == JS_TAG_UNDEFINED) {
- goto set_true;
- } else {
- goto free_and_set_false;
- }
- CASE(OP_is_null):
- if (JS_VALUE_GET_TAG(sp[-1]) == JS_TAG_NULL) {
- goto set_true;
- } else {
- goto free_and_set_false;
- }
- /* XXX: could merge to a single opcode */
- CASE(OP_typeof_is_undefined):
- /* different from OP_is_undefined because of isHTMLDDA */
- if (js_operator_typeof(ctx, sp[-1]) == JS_ATOM_undefined) {
- goto free_and_set_true;
- } else {
- goto free_and_set_false;
- }
- CASE(OP_typeof_is_function):
- if (js_operator_typeof(ctx, sp[-1]) == JS_ATOM_function) {
- goto free_and_set_true;
- } else {
- goto free_and_set_false;
- }
- free_and_set_true:
- JS_FreeValue(ctx, sp[-1]);
- #endif
- set_true:
- sp[-1] = JS_TRUE;
- BREAK;
- free_and_set_false:
- JS_FreeValue(ctx, sp[-1]);
- sp[-1] = JS_FALSE;
- BREAK;
- CASE(OP_invalid):
- DEFAULT:
- JS_ThrowInternalError(ctx, "invalid opcode: pc=%u opcode=0x%02x",
- (int)(pc - b->byte_code_buf - 1), opcode);
- goto exception;
- }
- }
- exception:
- if (is_backtrace_needed(ctx, rt->current_exception)) {
- /* add the backtrace information now (it is not done
- before if the exception happens in a bytecode
- operation */
- sf->cur_pc = pc;
- build_backtrace(ctx, rt->current_exception, NULL, 0, 0, 0);
- }
- if (!rt->current_exception_is_uncatchable) {
- while (sp > stack_buf) {
- JSValue val = *--sp;
- JS_FreeValue(ctx, val);
- if (JS_VALUE_GET_TAG(val) == JS_TAG_CATCH_OFFSET) {
- int pos = JS_VALUE_GET_INT(val);
- if (pos == 0) {
- /* enumerator: close it with a throw */
- JS_FreeValue(ctx, sp[-1]); /* drop the next method */
- sp--;
- JS_IteratorClose(ctx, sp[-1], TRUE);
- } else {
- *sp++ = rt->current_exception;
- rt->current_exception = JS_UNINITIALIZED;
- pc = b->byte_code_buf + pos;
- goto restart;
- }
- }
- }
- }
- ret_val = JS_EXCEPTION;
- /* the local variables are freed by the caller in the generator
- case. Hence the label 'done' should never be reached in a
- generator function. */
- if (b->func_kind != JS_FUNC_NORMAL) {
- done_generator:
- sf->cur_pc = pc;
- sf->cur_sp = sp;
- } else {
- done:
- if (unlikely(b->var_ref_count != 0)) {
- /* variable references reference the stack: must close them */
- close_var_refs(rt, b, sf);
- }
- /* free the local variables and stack */
- for(pval = local_buf; pval < sp; pval++) {
- JS_FreeValue(ctx, *pval);
- }
- }
- rt->current_stack_frame = sf->prev_frame;
- return ret_val;
- }
- #ifdef OPCODE_ASM_LABEL
- #pragma GCC diagnostic pop
- #endif
- JSValue JS_Call(JSContext *ctx, JSValueConst func_obj, JSValueConst this_obj,
- int argc, JSValueConst *argv)
- {
- return JS_CallInternal(ctx, func_obj, this_obj, JS_UNDEFINED,
- argc, (JSValue *)argv, JS_CALL_FLAG_COPY_ARGV);
- }
- static JSValue JS_CallFree(JSContext *ctx, JSValue func_obj, JSValueConst this_obj,
- int argc, JSValueConst *argv)
- {
- JSValue res = JS_CallInternal(ctx, func_obj, this_obj, JS_UNDEFINED,
- argc, (JSValue *)argv, JS_CALL_FLAG_COPY_ARGV);
- JS_FreeValue(ctx, func_obj);
- return res;
- }
- /* warning: the refcount of the context is not incremented. Return
- NULL in case of exception (case of revoked proxy only) */
- static JSContext *JS_GetFunctionRealm(JSContext *ctx, JSValueConst func_obj)
- {
- JSObject *p;
- JSContext *realm;
- if (JS_VALUE_GET_TAG(func_obj) != JS_TAG_OBJECT)
- return ctx;
- p = JS_VALUE_GET_OBJ(func_obj);
- switch(p->class_id) {
- case JS_CLASS_C_FUNCTION:
- realm = p->u.cfunc.realm;
- break;
- case JS_CLASS_BYTECODE_FUNCTION:
- case JS_CLASS_GENERATOR_FUNCTION:
- case JS_CLASS_ASYNC_FUNCTION:
- case JS_CLASS_ASYNC_GENERATOR_FUNCTION:
- {
- JSFunctionBytecode *b;
- b = p->u.func.function_bytecode;
- realm = b->realm;
- }
- break;
- case JS_CLASS_PROXY:
- {
- JSProxyData *s = p->u.opaque;
- if (!s)
- return ctx;
- if (s->is_revoked) {
- JS_ThrowTypeErrorRevokedProxy(ctx);
- return NULL;
- } else {
- realm = JS_GetFunctionRealm(ctx, s->target);
- }
- }
- break;
- case JS_CLASS_BOUND_FUNCTION:
- {
- JSBoundFunction *bf = p->u.bound_function;
- realm = JS_GetFunctionRealm(ctx, bf->func_obj);
- }
- break;
- default:
- realm = ctx;
- break;
- }
- return realm;
- }
- static JSValue js_create_from_ctor(JSContext *ctx, JSValueConst ctor,
- int class_id)
- {
- JSValue proto, obj;
- JSContext *realm;
- if (JS_IsUndefined(ctor)) {
- proto = JS_DupValue(ctx, ctx->class_proto[class_id]);
- } else {
- proto = JS_GetProperty(ctx, ctor, JS_ATOM_prototype);
- if (JS_IsException(proto))
- return proto;
- if (!JS_IsObject(proto)) {
- JS_FreeValue(ctx, proto);
- realm = JS_GetFunctionRealm(ctx, ctor);
- if (!realm)
- return JS_EXCEPTION;
- proto = JS_DupValue(ctx, realm->class_proto[class_id]);
- }
- }
- obj = JS_NewObjectProtoClass(ctx, proto, class_id);
- JS_FreeValue(ctx, proto);
- return obj;
- }
- /* argv[] is modified if (flags & JS_CALL_FLAG_COPY_ARGV) = 0. */
- static JSValue JS_CallConstructorInternal(JSContext *ctx,
- JSValueConst func_obj,
- JSValueConst new_target,
- int argc, JSValue *argv, int flags)
- {
- JSObject *p;
- JSFunctionBytecode *b;
- if (js_poll_interrupts(ctx))
- return JS_EXCEPTION;
- flags |= JS_CALL_FLAG_CONSTRUCTOR;
- if (unlikely(JS_VALUE_GET_TAG(func_obj) != JS_TAG_OBJECT))
- goto not_a_function;
- p = JS_VALUE_GET_OBJ(func_obj);
- if (unlikely(!p->is_constructor))
- return JS_ThrowTypeErrorNotAConstructor(ctx, func_obj);
- if (unlikely(p->class_id != JS_CLASS_BYTECODE_FUNCTION)) {
- JSClassCall *call_func;
- call_func = ctx->rt->class_array[p->class_id].call;
- if (!call_func) {
- not_a_function:
- return JS_ThrowTypeError(ctx, "not a function");
- }
- return call_func(ctx, func_obj, new_target, argc,
- (JSValueConst *)argv, flags);
- }
- b = p->u.func.function_bytecode;
- if (b->is_derived_class_constructor) {
- return JS_CallInternal(ctx, func_obj, JS_UNDEFINED, new_target, argc, argv, flags);
- } else {
- JSValue obj, ret;
- /* legacy constructor behavior */
- obj = js_create_from_ctor(ctx, new_target, JS_CLASS_OBJECT);
- if (JS_IsException(obj))
- return JS_EXCEPTION;
- ret = JS_CallInternal(ctx, func_obj, obj, new_target, argc, argv, flags);
- if (JS_VALUE_GET_TAG(ret) == JS_TAG_OBJECT ||
- JS_IsException(ret)) {
- JS_FreeValue(ctx, obj);
- return ret;
- } else {
- JS_FreeValue(ctx, ret);
- return obj;
- }
- }
- }
- JSValue JS_CallConstructor2(JSContext *ctx, JSValueConst func_obj,
- JSValueConst new_target,
- int argc, JSValueConst *argv)
- {
- return JS_CallConstructorInternal(ctx, func_obj, new_target,
- argc, (JSValue *)argv,
- JS_CALL_FLAG_COPY_ARGV);
- }
- JSValue JS_CallConstructor(JSContext *ctx, JSValueConst func_obj,
- int argc, JSValueConst *argv)
- {
- return JS_CallConstructorInternal(ctx, func_obj, func_obj,
- argc, (JSValue *)argv,
- JS_CALL_FLAG_COPY_ARGV);
- }
- JSValue JS_Invoke(JSContext *ctx, JSValueConst this_val, JSAtom atom,
- int argc, JSValueConst *argv)
- {
- JSValue func_obj;
- func_obj = JS_GetProperty(ctx, this_val, atom);
- if (JS_IsException(func_obj))
- return func_obj;
- return JS_CallFree(ctx, func_obj, this_val, argc, argv);
- }
- static JSValue JS_InvokeFree(JSContext *ctx, JSValue this_val, JSAtom atom,
- int argc, JSValueConst *argv)
- {
- JSValue res = JS_Invoke(ctx, this_val, atom, argc, argv);
- JS_FreeValue(ctx, this_val);
- return res;
- }
- /* JSAsyncFunctionState (used by generator and async functions) */
- static JSAsyncFunctionState *async_func_init(JSContext *ctx,
- JSValueConst func_obj, JSValueConst this_obj,
- int argc, JSValueConst *argv)
- {
- JSAsyncFunctionState *s;
- JSObject *p;
- JSFunctionBytecode *b;
- JSStackFrame *sf;
- int i, arg_buf_len, n;
- p = JS_VALUE_GET_OBJ(func_obj);
- b = p->u.func.function_bytecode;
- arg_buf_len = max_int(b->arg_count, argc);
- s = js_malloc(ctx, sizeof(*s) + sizeof(JSValue) * (arg_buf_len + b->var_count + b->stack_size) + sizeof(JSVarRef *) * b->var_ref_count);
- if (!s)
- return NULL;
- memset(s, 0, sizeof(*s));
- js_rc(s)->ref_count = 1;
- add_gc_object(ctx->rt, &s->header, JS_GC_OBJ_TYPE_ASYNC_FUNCTION);
- sf = &s->frame;
- sf->js_mode = b->js_mode | JS_MODE_ASYNC;
- sf->cur_pc = b->byte_code_buf;
- sf->arg_buf = (JSValue *)(s + 1);
- sf->cur_func = JS_DupValue(ctx, func_obj);
- s->this_val = JS_DupValue(ctx, this_obj);
- s->argc = argc;
- sf->arg_count = arg_buf_len;
- sf->var_buf = sf->arg_buf + arg_buf_len;
- sf->cur_sp = sf->var_buf + b->var_count;
- sf->var_refs = (JSVarRef **)(sf->cur_sp + b->stack_size);
- for(i = 0; i < b->var_ref_count; i++)
- sf->var_refs[i] = NULL;
- for(i = 0; i < argc; i++)
- sf->arg_buf[i] = JS_DupValue(ctx, argv[i]);
- n = arg_buf_len + b->var_count;
- for(i = argc; i < n; i++)
- sf->arg_buf[i] = JS_UNDEFINED;
- s->resolving_funcs[0] = JS_UNDEFINED;
- s->resolving_funcs[1] = JS_UNDEFINED;
- s->is_completed = FALSE;
- return s;
- }
- static void async_func_free_frame(JSRuntime *rt, JSAsyncFunctionState *s)
- {
- JSStackFrame *sf = &s->frame;
- JSValue *sp;
- /* cannot free the function if it is running */
- assert(sf->cur_sp != NULL);
- for(sp = sf->arg_buf; sp < sf->cur_sp; sp++) {
- JS_FreeValueRT(rt, *sp);
- }
- JS_FreeValueRT(rt, sf->cur_func);
- JS_FreeValueRT(rt, s->this_val);
- }
- static JSValue async_func_resume(JSContext *ctx, JSAsyncFunctionState *s)
- {
- JSRuntime *rt = ctx->rt;
- JSStackFrame *sf = &s->frame;
- JSValue func_obj, ret;
- assert(!s->is_completed);
- if (js_check_stack_overflow(ctx->rt, 0)) {
- ret = JS_ThrowStackOverflow(ctx);
- } else {
- /* the tag does not matter provided it is not an object */
- func_obj = JS_MKPTR(JS_TAG_INT, s);
- ret = JS_CallInternal(ctx, func_obj, s->this_val, JS_UNDEFINED,
- s->argc, sf->arg_buf, JS_CALL_FLAG_GENERATOR);
- }
- if (JS_IsException(ret) || JS_IsUndefined(ret)) {
- JSObject *p;
- JSFunctionBytecode *b;
-
- p = JS_VALUE_GET_OBJ(sf->cur_func);
- b = p->u.func.function_bytecode;
-
- if (JS_IsUndefined(ret)) {
- ret = sf->cur_sp[-1];
- sf->cur_sp[-1] = JS_UNDEFINED;
- }
- /* end of execution */
- s->is_completed = TRUE;
- /* close the closure variables. */
- close_var_refs(rt, b, sf);
-
- async_func_free_frame(rt, s);
- }
- return ret;
- }
- static void __async_func_free(JSRuntime *rt, JSAsyncFunctionState *s)
- {
- /* cannot close the closure variables here because it would
- potentially modify the object graph */
- if (!s->is_completed) {
- async_func_free_frame(rt, s);
- }
- JS_FreeValueRT(rt, s->resolving_funcs[0]);
- JS_FreeValueRT(rt, s->resolving_funcs[1]);
- remove_gc_object(&s->header);
- if (rt->gc_phase == JS_GC_PHASE_REMOVE_CYCLES && js_rc(s)->ref_count != 0) {
- list_add_tail(&s->header.link, &rt->gc_zero_ref_count_list);
- } else {
- js_free_rt(rt, s);
- }
- }
- static void async_func_free(JSRuntime *rt, JSAsyncFunctionState *s)
- {
- if (--js_rc(s)->ref_count == 0) {
- if (rt->gc_phase != JS_GC_PHASE_REMOVE_CYCLES) {
- list_del(&s->header.link);
- list_add(&s->header.link, &rt->gc_zero_ref_count_list);
- if (rt->gc_phase == JS_GC_PHASE_NONE) {
- free_zero_refcount(rt);
- }
- }
- }
- }
- /* Generators */
- typedef enum JSGeneratorStateEnum {
- JS_GENERATOR_STATE_SUSPENDED_START,
- JS_GENERATOR_STATE_SUSPENDED_YIELD,
- JS_GENERATOR_STATE_SUSPENDED_YIELD_STAR,
- JS_GENERATOR_STATE_EXECUTING,
- JS_GENERATOR_STATE_COMPLETED,
- } JSGeneratorStateEnum;
- typedef struct JSGeneratorData {
- JSGeneratorStateEnum state;
- JSAsyncFunctionState *func_state;
- } JSGeneratorData;
- static void free_generator_stack_rt(JSRuntime *rt, JSGeneratorData *s)
- {
- if (s->state == JS_GENERATOR_STATE_COMPLETED)
- return;
- if (s->func_state) {
- async_func_free(rt, s->func_state);
- s->func_state = NULL;
- }
- s->state = JS_GENERATOR_STATE_COMPLETED;
- }
- static void js_generator_finalizer(JSRuntime *rt, JSValue obj)
- {
- JSGeneratorData *s = JS_GetOpaque(obj, JS_CLASS_GENERATOR);
- if (s) {
- free_generator_stack_rt(rt, s);
- js_free_rt(rt, s);
- }
- }
- static void free_generator_stack(JSContext *ctx, JSGeneratorData *s)
- {
- free_generator_stack_rt(ctx->rt, s);
- }
- static void js_generator_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JSGeneratorData *s = p->u.generator_data;
- if (!s || !s->func_state)
- return;
- mark_func(rt, &s->func_state->header);
- }
- /* XXX: use enum */
- #define GEN_MAGIC_NEXT 0
- #define GEN_MAGIC_RETURN 1
- #define GEN_MAGIC_THROW 2
- static JSValue js_generator_next(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv,
- BOOL *pdone, int magic)
- {
- JSGeneratorData *s = JS_GetOpaque(this_val, JS_CLASS_GENERATOR);
- JSStackFrame *sf;
- JSValue ret, func_ret;
- *pdone = TRUE;
- if (!s)
- return JS_ThrowTypeError(ctx, "not a generator");
- switch(s->state) {
- default:
- case JS_GENERATOR_STATE_SUSPENDED_START:
- sf = &s->func_state->frame;
- if (magic == GEN_MAGIC_NEXT) {
- goto exec_no_arg;
- } else {
- free_generator_stack(ctx, s);
- goto done;
- }
- break;
- case JS_GENERATOR_STATE_SUSPENDED_YIELD_STAR:
- case JS_GENERATOR_STATE_SUSPENDED_YIELD:
- sf = &s->func_state->frame;
- /* cur_sp[-1] was set to JS_UNDEFINED in the previous call */
- ret = JS_DupValue(ctx, argv[0]);
- if (magic == GEN_MAGIC_THROW &&
- s->state == JS_GENERATOR_STATE_SUSPENDED_YIELD) {
- JS_Throw(ctx, ret);
- s->func_state->throw_flag = TRUE;
- } else {
- sf->cur_sp[-1] = ret;
- sf->cur_sp[0] = JS_NewInt32(ctx, magic);
- sf->cur_sp++;
- exec_no_arg:
- s->func_state->throw_flag = FALSE;
- }
- s->state = JS_GENERATOR_STATE_EXECUTING;
- func_ret = async_func_resume(ctx, s->func_state);
- s->state = JS_GENERATOR_STATE_SUSPENDED_YIELD;
- if (s->func_state->is_completed) {
- /* finalize the execution in case of exception or normal return */
- free_generator_stack(ctx, s);
- return func_ret;
- } else {
- assert(JS_VALUE_GET_TAG(func_ret) == JS_TAG_INT);
- /* get the returned yield value at the top of the stack */
- ret = sf->cur_sp[-1];
- sf->cur_sp[-1] = JS_UNDEFINED;
- if (JS_VALUE_GET_INT(func_ret) == FUNC_RET_YIELD_STAR) {
- s->state = JS_GENERATOR_STATE_SUSPENDED_YIELD_STAR;
- /* return (value, done) object */
- *pdone = 2;
- } else {
- *pdone = FALSE;
- }
- }
- break;
- case JS_GENERATOR_STATE_COMPLETED:
- done:
- /* execution is finished */
- switch(magic) {
- default:
- case GEN_MAGIC_NEXT:
- ret = JS_UNDEFINED;
- break;
- case GEN_MAGIC_RETURN:
- ret = JS_DupValue(ctx, argv[0]);
- break;
- case GEN_MAGIC_THROW:
- ret = JS_Throw(ctx, JS_DupValue(ctx, argv[0]));
- break;
- }
- break;
- case JS_GENERATOR_STATE_EXECUTING:
- ret = JS_ThrowTypeError(ctx, "cannot invoke a running generator");
- break;
- }
- return ret;
- }
- static JSValue js_generator_function_call(JSContext *ctx, JSValueConst func_obj,
- JSValueConst this_obj,
- int argc, JSValueConst *argv,
- int flags)
- {
- JSValue obj, func_ret;
- JSGeneratorData *s;
- s = js_mallocz(ctx, sizeof(*s));
- if (!s)
- return JS_EXCEPTION;
- s->state = JS_GENERATOR_STATE_SUSPENDED_START;
- s->func_state = async_func_init(ctx, func_obj, this_obj, argc, argv);
- if (!s->func_state) {
- s->state = JS_GENERATOR_STATE_COMPLETED;
- goto fail;
- }
- /* execute the function up to 'OP_initial_yield' */
- func_ret = async_func_resume(ctx, s->func_state);
- if (JS_IsException(func_ret))
- goto fail;
- JS_FreeValue(ctx, func_ret);
- obj = js_create_from_ctor(ctx, func_obj, JS_CLASS_GENERATOR);
- if (JS_IsException(obj))
- goto fail;
- JS_SetOpaque(obj, s);
- return obj;
- fail:
- free_generator_stack_rt(ctx->rt, s);
- js_free(ctx, s);
- return JS_EXCEPTION;
- }
- /* AsyncFunction */
- static void js_async_function_resolve_finalizer(JSRuntime *rt, JSValue val)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JSAsyncFunctionState *s = p->u.async_function_data;
- if (s) {
- async_func_free(rt, s);
- }
- }
- static void js_async_function_resolve_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JSAsyncFunctionState *s = p->u.async_function_data;
- if (s) {
- mark_func(rt, &s->header);
- }
- }
- static int js_async_function_resolve_create(JSContext *ctx,
- JSAsyncFunctionState *s,
- JSValue *resolving_funcs)
- {
- int i;
- JSObject *p;
- for(i = 0; i < 2; i++) {
- resolving_funcs[i] =
- JS_NewObjectProtoClass(ctx, ctx->function_proto,
- JS_CLASS_ASYNC_FUNCTION_RESOLVE + i);
- if (JS_IsException(resolving_funcs[i])) {
- if (i == 1)
- JS_FreeValue(ctx, resolving_funcs[0]);
- return -1;
- }
- p = JS_VALUE_GET_OBJ(resolving_funcs[i]);
- js_rc(s)->ref_count++;
- p->u.async_function_data = s;
- }
- return 0;
- }
- static void js_async_function_resume(JSContext *ctx, JSAsyncFunctionState *s)
- {
- JSValue func_ret, ret2;
- func_ret = async_func_resume(ctx, s);
- if (s->is_completed) {
- if (JS_IsException(func_ret)) {
- JSValue error;
- fail:
- error = JS_GetException(ctx);
- ret2 = JS_Call(ctx, s->resolving_funcs[1], JS_UNDEFINED,
- 1, (JSValueConst *)&error);
- JS_FreeValue(ctx, error);
- JS_FreeValue(ctx, ret2); /* XXX: what to do if exception ? */
- } else {
- /* normal return */
- ret2 = JS_Call(ctx, s->resolving_funcs[0], JS_UNDEFINED,
- 1, (JSValueConst *)&func_ret);
- JS_FreeValue(ctx, func_ret);
- JS_FreeValue(ctx, ret2); /* XXX: what to do if exception ? */
- }
- } else {
- JSValue value, promise, resolving_funcs[2], resolving_funcs1[2];
- int i, res;
- value = s->frame.cur_sp[-1];
- s->frame.cur_sp[-1] = JS_UNDEFINED;
- /* await */
- JS_FreeValue(ctx, func_ret); /* not used */
- promise = js_promise_resolve(ctx, ctx->promise_ctor,
- 1, (JSValueConst *)&value, 0);
- JS_FreeValue(ctx, value);
- if (JS_IsException(promise))
- goto fail;
- if (js_async_function_resolve_create(ctx, s, resolving_funcs)) {
- JS_FreeValue(ctx, promise);
- goto fail;
- }
- /* Note: no need to create 'thrownawayCapability' as in
- the spec */
- for(i = 0; i < 2; i++)
- resolving_funcs1[i] = JS_UNDEFINED;
- res = perform_promise_then(ctx, promise,
- (JSValueConst *)resolving_funcs,
- (JSValueConst *)resolving_funcs1);
- JS_FreeValue(ctx, promise);
- for(i = 0; i < 2; i++)
- JS_FreeValue(ctx, resolving_funcs[i]);
- if (res)
- goto fail;
- }
- }
- static JSValue js_async_function_resolve_call(JSContext *ctx,
- JSValueConst func_obj,
- JSValueConst this_obj,
- int argc, JSValueConst *argv,
- int flags)
- {
- JSObject *p = JS_VALUE_GET_OBJ(func_obj);
- JSAsyncFunctionState *s = p->u.async_function_data;
- BOOL is_reject = p->class_id - JS_CLASS_ASYNC_FUNCTION_RESOLVE;
- JSValueConst arg;
- if (argc > 0)
- arg = argv[0];
- else
- arg = JS_UNDEFINED;
- s->throw_flag = is_reject;
- if (is_reject) {
- JS_Throw(ctx, JS_DupValue(ctx, arg));
- } else {
- /* return value of await */
- s->frame.cur_sp[-1] = JS_DupValue(ctx, arg);
- }
- js_async_function_resume(ctx, s);
- return JS_UNDEFINED;
- }
- static JSValue js_async_function_call(JSContext *ctx, JSValueConst func_obj,
- JSValueConst this_obj,
- int argc, JSValueConst *argv, int flags)
- {
- JSValue promise;
- JSAsyncFunctionState *s;
- s = async_func_init(ctx, func_obj, this_obj, argc, argv);
- if (!s)
- return JS_EXCEPTION;
- promise = JS_NewPromiseCapability(ctx, s->resolving_funcs);
- if (JS_IsException(promise)) {
- async_func_free(ctx->rt, s);
- return JS_EXCEPTION;
- }
- js_async_function_resume(ctx, s);
- async_func_free(ctx->rt, s);
- return promise;
- }
- /* AsyncGenerator */
- typedef enum JSAsyncGeneratorStateEnum {
- JS_ASYNC_GENERATOR_STATE_SUSPENDED_START,
- JS_ASYNC_GENERATOR_STATE_SUSPENDED_YIELD,
- JS_ASYNC_GENERATOR_STATE_SUSPENDED_YIELD_STAR,
- JS_ASYNC_GENERATOR_STATE_EXECUTING,
- JS_ASYNC_GENERATOR_STATE_AWAITING_RETURN,
- JS_ASYNC_GENERATOR_STATE_COMPLETED,
- } JSAsyncGeneratorStateEnum;
- typedef struct JSAsyncGeneratorRequest {
- struct list_head link;
- /* completion */
- int completion_type; /* GEN_MAGIC_x */
- JSValue result;
- /* promise capability */
- JSValue promise;
- JSValue resolving_funcs[2];
- } JSAsyncGeneratorRequest;
- typedef struct JSAsyncGeneratorData {
- JSObject *generator; /* back pointer to the object (const) */
- JSAsyncGeneratorStateEnum state;
- /* func_state is NULL is state AWAITING_RETURN and COMPLETED */
- JSAsyncFunctionState *func_state;
- struct list_head queue; /* list of JSAsyncGeneratorRequest.link */
- } JSAsyncGeneratorData;
- static void js_async_generator_free(JSRuntime *rt,
- JSAsyncGeneratorData *s)
- {
- struct list_head *el, *el1;
- JSAsyncGeneratorRequest *req;
- list_for_each_safe(el, el1, &s->queue) {
- req = list_entry(el, JSAsyncGeneratorRequest, link);
- JS_FreeValueRT(rt, req->result);
- JS_FreeValueRT(rt, req->promise);
- JS_FreeValueRT(rt, req->resolving_funcs[0]);
- JS_FreeValueRT(rt, req->resolving_funcs[1]);
- js_free_rt(rt, req);
- }
- if (s->func_state)
- async_func_free(rt, s->func_state);
- js_free_rt(rt, s);
- }
- static void js_async_generator_finalizer(JSRuntime *rt, JSValue obj)
- {
- JSAsyncGeneratorData *s = JS_GetOpaque(obj, JS_CLASS_ASYNC_GENERATOR);
- if (s) {
- js_async_generator_free(rt, s);
- }
- }
- static void js_async_generator_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func)
- {
- JSAsyncGeneratorData *s = JS_GetOpaque(val, JS_CLASS_ASYNC_GENERATOR);
- struct list_head *el;
- JSAsyncGeneratorRequest *req;
- if (s) {
- list_for_each(el, &s->queue) {
- req = list_entry(el, JSAsyncGeneratorRequest, link);
- JS_MarkValue(rt, req->result, mark_func);
- JS_MarkValue(rt, req->promise, mark_func);
- JS_MarkValue(rt, req->resolving_funcs[0], mark_func);
- JS_MarkValue(rt, req->resolving_funcs[1], mark_func);
- }
- if (s->func_state) {
- mark_func(rt, &s->func_state->header);
- }
- }
- }
- static JSValue js_async_generator_resolve_function(JSContext *ctx,
- JSValueConst this_obj,
- int argc, JSValueConst *argv,
- int magic, JSValue *func_data);
- static int js_async_generator_resolve_function_create(JSContext *ctx,
- JSValueConst generator,
- JSValue *resolving_funcs,
- BOOL is_resume_next)
- {
- int i;
- JSValue func;
- for(i = 0; i < 2; i++) {
- func = JS_NewCFunctionData(ctx, js_async_generator_resolve_function, 1,
- i + is_resume_next * 2, 1, &generator);
- if (JS_IsException(func)) {
- if (i == 1)
- JS_FreeValue(ctx, resolving_funcs[0]);
- return -1;
- }
- resolving_funcs[i] = func;
- }
- return 0;
- }
- static int js_async_generator_await(JSContext *ctx,
- JSAsyncGeneratorData *s,
- JSValueConst value)
- {
- JSValue promise, resolving_funcs[2], resolving_funcs1[2];
- int i, res;
- promise = js_promise_resolve(ctx, ctx->promise_ctor,
- 1, &value, 0);
- if (JS_IsException(promise))
- goto fail;
- if (js_async_generator_resolve_function_create(ctx, JS_MKPTR(JS_TAG_OBJECT, s->generator),
- resolving_funcs, FALSE)) {
- JS_FreeValue(ctx, promise);
- goto fail;
- }
- /* Note: no need to create 'thrownawayCapability' as in
- the spec */
- for(i = 0; i < 2; i++)
- resolving_funcs1[i] = JS_UNDEFINED;
- res = perform_promise_then(ctx, promise,
- (JSValueConst *)resolving_funcs,
- (JSValueConst *)resolving_funcs1);
- JS_FreeValue(ctx, promise);
- for(i = 0; i < 2; i++)
- JS_FreeValue(ctx, resolving_funcs[i]);
- if (res)
- goto fail;
- return 0;
- fail:
- return -1;
- }
- static void js_async_generator_resolve_or_reject(JSContext *ctx,
- JSAsyncGeneratorData *s,
- JSValueConst result,
- int is_reject)
- {
- JSAsyncGeneratorRequest *next;
- JSValue ret;
- next = list_entry(s->queue.next, JSAsyncGeneratorRequest, link);
- list_del(&next->link);
- ret = JS_Call(ctx, next->resolving_funcs[is_reject], JS_UNDEFINED, 1,
- &result);
- JS_FreeValue(ctx, ret);
- JS_FreeValue(ctx, next->result);
- JS_FreeValue(ctx, next->promise);
- JS_FreeValue(ctx, next->resolving_funcs[0]);
- JS_FreeValue(ctx, next->resolving_funcs[1]);
- js_free(ctx, next);
- }
- static void js_async_generator_resolve(JSContext *ctx,
- JSAsyncGeneratorData *s,
- JSValueConst value,
- BOOL done)
- {
- JSValue result;
- result = js_create_iterator_result(ctx, JS_DupValue(ctx, value), done);
- /* XXX: better exception handling ? */
- js_async_generator_resolve_or_reject(ctx, s, result, 0);
- JS_FreeValue(ctx, result);
- }
- static void js_async_generator_reject(JSContext *ctx,
- JSAsyncGeneratorData *s,
- JSValueConst exception)
- {
- js_async_generator_resolve_or_reject(ctx, s, exception, 1);
- }
- static void js_async_generator_complete(JSContext *ctx,
- JSAsyncGeneratorData *s)
- {
- if (s->state != JS_ASYNC_GENERATOR_STATE_COMPLETED) {
- s->state = JS_ASYNC_GENERATOR_STATE_COMPLETED;
- async_func_free(ctx->rt, s->func_state);
- s->func_state = NULL;
- }
- }
- static int js_async_generator_completed_return(JSContext *ctx,
- JSAsyncGeneratorData *s,
- JSValueConst value)
- {
- JSValue promise, resolving_funcs[2], resolving_funcs1[2];
- int res;
- // Can fail looking up JS_ATOM_constructor when is_reject==0.
- promise = js_promise_resolve(ctx, ctx->promise_ctor, 1, &value,
- /*is_reject*/0);
- // A poisoned .constructor property is observable and the resulting
- // exception should be delivered to the catch handler.
- if (JS_IsException(promise)) {
- JSValue err = JS_GetException(ctx);
- promise = js_promise_resolve(ctx, ctx->promise_ctor, 1, (JSValueConst *)&err,
- /*is_reject*/1);
- JS_FreeValue(ctx, err);
- if (JS_IsException(promise))
- return -1;
- }
- if (js_async_generator_resolve_function_create(ctx,
- JS_MKPTR(JS_TAG_OBJECT, s->generator),
- resolving_funcs1,
- TRUE)) {
- JS_FreeValue(ctx, promise);
- return -1;
- }
- resolving_funcs[0] = JS_UNDEFINED;
- resolving_funcs[1] = JS_UNDEFINED;
- res = perform_promise_then(ctx, promise,
- (JSValueConst *)resolving_funcs1,
- (JSValueConst *)resolving_funcs);
- JS_FreeValue(ctx, resolving_funcs1[0]);
- JS_FreeValue(ctx, resolving_funcs1[1]);
- JS_FreeValue(ctx, promise);
- return res;
- }
- static void js_async_generator_resume_next(JSContext *ctx,
- JSAsyncGeneratorData *s)
- {
- JSAsyncGeneratorRequest *next;
- JSValue func_ret, value;
- for(;;) {
- if (list_empty(&s->queue))
- break;
- next = list_entry(s->queue.next, JSAsyncGeneratorRequest, link);
- switch(s->state) {
- case JS_ASYNC_GENERATOR_STATE_EXECUTING:
- /* only happens when restarting execution after await() */
- goto resume_exec;
- case JS_ASYNC_GENERATOR_STATE_AWAITING_RETURN:
- goto done;
- case JS_ASYNC_GENERATOR_STATE_SUSPENDED_START:
- if (next->completion_type == GEN_MAGIC_NEXT) {
- goto exec_no_arg;
- } else {
- js_async_generator_complete(ctx, s);
- }
- break;
- case JS_ASYNC_GENERATOR_STATE_COMPLETED:
- if (next->completion_type == GEN_MAGIC_NEXT) {
- js_async_generator_resolve(ctx, s, JS_UNDEFINED, TRUE);
- } else if (next->completion_type == GEN_MAGIC_RETURN) {
- s->state = JS_ASYNC_GENERATOR_STATE_AWAITING_RETURN;
- js_async_generator_completed_return(ctx, s, next->result);
- } else {
- js_async_generator_reject(ctx, s, next->result);
- }
- goto done;
- case JS_ASYNC_GENERATOR_STATE_SUSPENDED_YIELD:
- case JS_ASYNC_GENERATOR_STATE_SUSPENDED_YIELD_STAR:
- value = JS_DupValue(ctx, next->result);
- if (next->completion_type == GEN_MAGIC_THROW &&
- s->state == JS_ASYNC_GENERATOR_STATE_SUSPENDED_YIELD) {
- JS_Throw(ctx, value);
- s->func_state->throw_flag = TRUE;
- } else {
- /* 'yield' returns a value. 'yield *' also returns a value
- in case the 'throw' method is called */
- s->func_state->frame.cur_sp[-1] = value;
- s->func_state->frame.cur_sp[0] =
- JS_NewInt32(ctx, next->completion_type);
- s->func_state->frame.cur_sp++;
- exec_no_arg:
- s->func_state->throw_flag = FALSE;
- }
- s->state = JS_ASYNC_GENERATOR_STATE_EXECUTING;
- resume_exec:
- func_ret = async_func_resume(ctx, s->func_state);
- if (s->func_state->is_completed) {
- if (JS_IsException(func_ret)) {
- value = JS_GetException(ctx);
- js_async_generator_complete(ctx, s);
- js_async_generator_reject(ctx, s, value);
- JS_FreeValue(ctx, value);
- } else {
- /* end of function */
- js_async_generator_complete(ctx, s);
- js_async_generator_resolve(ctx, s, func_ret, TRUE);
- JS_FreeValue(ctx, func_ret);
- }
- } else {
- int func_ret_code, ret;
- assert(JS_VALUE_GET_TAG(func_ret) == JS_TAG_INT);
- func_ret_code = JS_VALUE_GET_INT(func_ret);
- value = s->func_state->frame.cur_sp[-1];
- s->func_state->frame.cur_sp[-1] = JS_UNDEFINED;
- switch(func_ret_code) {
- case FUNC_RET_YIELD:
- case FUNC_RET_YIELD_STAR:
- if (func_ret_code == FUNC_RET_YIELD_STAR)
- s->state = JS_ASYNC_GENERATOR_STATE_SUSPENDED_YIELD_STAR;
- else
- s->state = JS_ASYNC_GENERATOR_STATE_SUSPENDED_YIELD;
- js_async_generator_resolve(ctx, s, value, FALSE);
- JS_FreeValue(ctx, value);
- break;
- case FUNC_RET_AWAIT:
- ret = js_async_generator_await(ctx, s, value);
- JS_FreeValue(ctx, value);
- if (ret < 0) {
- /* exception: throw it */
- s->func_state->throw_flag = TRUE;
- goto resume_exec;
- }
- goto done;
- default:
- abort();
- }
- }
- break;
- default:
- abort();
- }
- }
- done: ;
- }
- static JSValue js_async_generator_resolve_function(JSContext *ctx,
- JSValueConst this_obj,
- int argc, JSValueConst *argv,
- int magic, JSValue *func_data)
- {
- BOOL is_reject = magic & 1;
- JSAsyncGeneratorData *s = JS_GetOpaque(func_data[0], JS_CLASS_ASYNC_GENERATOR);
- JSValueConst arg = argv[0];
- /* XXX: what if s == NULL */
- if (magic >= 2) {
- /* resume next case in AWAITING_RETURN state */
- assert(s->state == JS_ASYNC_GENERATOR_STATE_AWAITING_RETURN ||
- s->state == JS_ASYNC_GENERATOR_STATE_COMPLETED);
- s->state = JS_ASYNC_GENERATOR_STATE_COMPLETED;
- if (is_reject) {
- js_async_generator_reject(ctx, s, arg);
- } else {
- js_async_generator_resolve(ctx, s, arg, TRUE);
- }
- } else if (s->state == JS_ASYNC_GENERATOR_STATE_EXECUTING) {
- /* restart function execution after await() */
- s->func_state->throw_flag = is_reject;
- if (is_reject) {
- JS_Throw(ctx, JS_DupValue(ctx, arg));
- } else {
- /* return value of await */
- s->func_state->frame.cur_sp[-1] = JS_DupValue(ctx, arg);
- }
- js_async_generator_resume_next(ctx, s);
- }
- return JS_UNDEFINED;
- }
- /* magic = GEN_MAGIC_x */
- static JSValue js_async_generator_next(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv,
- int magic)
- {
- JSAsyncGeneratorData *s = JS_GetOpaque(this_val, JS_CLASS_ASYNC_GENERATOR);
- JSValue promise, resolving_funcs[2];
- JSAsyncGeneratorRequest *req;
- promise = JS_NewPromiseCapability(ctx, resolving_funcs);
- if (JS_IsException(promise))
- return JS_EXCEPTION;
- if (!s) {
- JSValue err, res2;
- JS_ThrowTypeError(ctx, "not an AsyncGenerator object");
- err = JS_GetException(ctx);
- res2 = JS_Call(ctx, resolving_funcs[1], JS_UNDEFINED,
- 1, (JSValueConst *)&err);
- JS_FreeValue(ctx, err);
- JS_FreeValue(ctx, res2);
- JS_FreeValue(ctx, resolving_funcs[0]);
- JS_FreeValue(ctx, resolving_funcs[1]);
- return promise;
- }
- req = js_mallocz(ctx, sizeof(*req));
- if (!req)
- goto fail;
- req->completion_type = magic;
- req->result = JS_DupValue(ctx, argv[0]);
- req->promise = JS_DupValue(ctx, promise);
- req->resolving_funcs[0] = resolving_funcs[0];
- req->resolving_funcs[1] = resolving_funcs[1];
- list_add_tail(&req->link, &s->queue);
- if (s->state != JS_ASYNC_GENERATOR_STATE_EXECUTING) {
- js_async_generator_resume_next(ctx, s);
- }
- return promise;
- fail:
- JS_FreeValue(ctx, resolving_funcs[0]);
- JS_FreeValue(ctx, resolving_funcs[1]);
- JS_FreeValue(ctx, promise);
- return JS_EXCEPTION;
- }
- static JSValue js_async_generator_function_call(JSContext *ctx, JSValueConst func_obj,
- JSValueConst this_obj,
- int argc, JSValueConst *argv,
- int flags)
- {
- JSValue obj, func_ret;
- JSAsyncGeneratorData *s;
- s = js_mallocz(ctx, sizeof(*s));
- if (!s)
- return JS_EXCEPTION;
- s->state = JS_ASYNC_GENERATOR_STATE_SUSPENDED_START;
- init_list_head(&s->queue);
- s->func_state = async_func_init(ctx, func_obj, this_obj, argc, argv);
- if (!s->func_state)
- goto fail;
- /* execute the function up to 'OP_initial_yield' (no yield nor
- await are possible) */
- func_ret = async_func_resume(ctx, s->func_state);
- if (JS_IsException(func_ret))
- goto fail;
- JS_FreeValue(ctx, func_ret);
- obj = js_create_from_ctor(ctx, func_obj, JS_CLASS_ASYNC_GENERATOR);
- if (JS_IsException(obj))
- goto fail;
- s->generator = JS_VALUE_GET_OBJ(obj);
- JS_SetOpaque(obj, s);
- return obj;
- fail:
- js_async_generator_free(ctx->rt, s);
- return JS_EXCEPTION;
- }
- /* JS parser */
- enum {
- TOK_NUMBER = -128,
- TOK_STRING,
- TOK_TEMPLATE,
- TOK_IDENT,
- TOK_REGEXP,
- /* warning: order matters (see js_parse_assign_expr) */
- TOK_MUL_ASSIGN,
- TOK_DIV_ASSIGN,
- TOK_MOD_ASSIGN,
- TOK_PLUS_ASSIGN,
- TOK_MINUS_ASSIGN,
- TOK_SHL_ASSIGN,
- TOK_SAR_ASSIGN,
- TOK_SHR_ASSIGN,
- TOK_AND_ASSIGN,
- TOK_XOR_ASSIGN,
- TOK_OR_ASSIGN,
- TOK_POW_ASSIGN,
- TOK_LAND_ASSIGN,
- TOK_LOR_ASSIGN,
- TOK_DOUBLE_QUESTION_MARK_ASSIGN,
- TOK_DEC,
- TOK_INC,
- TOK_SHL,
- TOK_SAR,
- TOK_SHR,
- TOK_LT,
- TOK_LTE,
- TOK_GT,
- TOK_GTE,
- TOK_EQ,
- TOK_STRICT_EQ,
- TOK_NEQ,
- TOK_STRICT_NEQ,
- TOK_LAND,
- TOK_LOR,
- TOK_POW,
- TOK_ARROW,
- TOK_ELLIPSIS,
- TOK_DOUBLE_QUESTION_MARK,
- TOK_QUESTION_MARK_DOT,
- TOK_ERROR,
- TOK_PRIVATE_NAME,
- TOK_EOF,
- /* keywords: WARNING: same order as atoms */
- TOK_NULL, /* must be first */
- TOK_FALSE,
- TOK_TRUE,
- TOK_IF,
- TOK_ELSE,
- TOK_RETURN,
- TOK_VAR,
- TOK_THIS,
- TOK_DELETE,
- TOK_VOID,
- TOK_TYPEOF,
- TOK_NEW,
- TOK_IN,
- TOK_INSTANCEOF,
- TOK_DO,
- TOK_WHILE,
- TOK_FOR,
- TOK_BREAK,
- TOK_CONTINUE,
- TOK_SWITCH,
- TOK_CASE,
- TOK_DEFAULT,
- TOK_THROW,
- TOK_TRY,
- TOK_CATCH,
- TOK_FINALLY,
- TOK_FUNCTION,
- TOK_DEBUGGER,
- TOK_WITH,
- /* FutureReservedWord */
- TOK_CLASS,
- TOK_CONST,
- TOK_ENUM,
- TOK_EXPORT,
- TOK_EXTENDS,
- TOK_IMPORT,
- TOK_SUPER,
- /* FutureReservedWords when parsing strict mode code */
- TOK_IMPLEMENTS,
- TOK_INTERFACE,
- TOK_LET,
- TOK_PACKAGE,
- TOK_PRIVATE,
- TOK_PROTECTED,
- TOK_PUBLIC,
- TOK_STATIC,
- TOK_YIELD,
- TOK_AWAIT, /* must be last */
- TOK_OF, /* only used for js_parse_skip_parens_token() */
- };
- #define TOK_FIRST_KEYWORD TOK_NULL
- #define TOK_LAST_KEYWORD TOK_AWAIT
- /* unicode code points */
- #define CP_NBSP 0x00a0
- #define CP_BOM 0xfeff
- #define CP_LS 0x2028
- #define CP_PS 0x2029
- typedef struct BlockEnv {
- struct BlockEnv *prev;
- JSAtom label_name; /* JS_ATOM_NULL if none */
- int label_break; /* -1 if none */
- int label_cont; /* -1 if none */
- int drop_count; /* number of stack elements to drop */
- int label_finally; /* -1 if none */
- int scope_level;
- uint8_t has_iterator : 1;
- uint8_t is_regular_stmt : 1; /* i.e. not a loop statement */
- } BlockEnv;
- typedef struct JSGlobalVar {
- int cpool_idx; /* if >= 0, index in the constant pool for hoisted
- function defintion*/
- uint8_t force_init : 1; /* force initialization to undefined */
- uint8_t is_lexical : 1; /* global let/const definition */
- uint8_t is_const : 1; /* const definition */
- int scope_level; /* scope of definition */
- JSAtom var_name; /* variable name */
- } JSGlobalVar;
- typedef struct RelocEntry {
- struct RelocEntry *next;
- uint32_t addr; /* address to patch */
- int size; /* address size: 1, 2 or 4 bytes */
- } RelocEntry;
- typedef struct JumpSlot {
- int op;
- int size;
- int pos;
- int label;
- } JumpSlot;
- typedef struct LabelSlot {
- int ref_count;
- int pos; /* phase 1 address, -1 means not resolved yet */
- int pos2; /* phase 2 address, -1 means not resolved yet */
- int addr; /* phase 3 address, -1 means not resolved yet */
- RelocEntry *first_reloc;
- } LabelSlot;
- typedef struct LineNumberSlot {
- uint32_t pc;
- uint32_t source_pos;
- } LineNumberSlot;
- typedef struct {
- /* last source position */
- const uint8_t *ptr;
- int line_num;
- int col_num;
- const uint8_t *buf_start;
- } GetLineColCache;
- typedef enum JSParseFunctionEnum {
- JS_PARSE_FUNC_STATEMENT,
- JS_PARSE_FUNC_VAR,
- JS_PARSE_FUNC_EXPR,
- JS_PARSE_FUNC_ARROW,
- JS_PARSE_FUNC_GETTER,
- JS_PARSE_FUNC_SETTER,
- JS_PARSE_FUNC_METHOD,
- JS_PARSE_FUNC_CLASS_STATIC_INIT,
- JS_PARSE_FUNC_CLASS_CONSTRUCTOR,
- JS_PARSE_FUNC_DERIVED_CLASS_CONSTRUCTOR,
- } JSParseFunctionEnum;
- typedef enum JSParseExportEnum {
- JS_PARSE_EXPORT_NONE,
- JS_PARSE_EXPORT_NAMED,
- JS_PARSE_EXPORT_DEFAULT,
- } JSParseExportEnum;
- typedef struct JSVarScope {
- int parent; /* index into fd->scopes of the enclosing scope */
- int first; /* index into fd->vars of the last variable in this scope */
- } JSVarScope;
- typedef struct JSVarDef {
- JSAtom var_name;
- /* index into fd->scopes of this variable lexical scope */
- int scope_level;
- /* - if scope_level = 0: scope in which the variable is defined
- - if scope_level != 0: index into fd->vars of the next
- variable in the same or enclosing lexical scope
- */
- int scope_next;
- uint8_t is_const : 1;
- uint8_t is_lexical : 1;
- uint8_t is_captured : 1; /* XXX: could remove and use a var_ref_idx value */
- uint8_t is_static_private : 1; /* only used during private class field parsing */
- uint8_t var_kind : 4; /* see JSVarKindEnum */
- /* if is_captured = TRUE, provides, the index of the corresponding
- JSVarRef on stack */
- uint16_t var_ref_idx;
- /* function pool index for lexical variables with var_kind =
- JS_VAR_FUNCTION_DECL/JS_VAR_NEW_FUNCTION_DECL or scope level of
- the definition of the 'var' variables (they have scope_level =
- 0) */
- int func_pool_idx;
- } JSVarDef;
- typedef struct JSFunctionDef {
- JSContext *ctx;
- struct JSFunctionDef *parent;
- int parent_cpool_idx; /* index in the constant pool of the parent
- or -1 if none */
- int parent_scope_level; /* scope level in parent at point of definition */
- struct list_head child_list; /* list of JSFunctionDef.link */
- struct list_head link;
- BOOL is_eval; /* TRUE if eval code */
- int eval_type; /* only valid if is_eval = TRUE */
- BOOL is_global_var; /* TRUE if variables are not defined locally:
- eval global, eval module or non strict eval */
- BOOL is_func_expr; /* TRUE if function expression */
- BOOL has_home_object; /* TRUE if the home object is available */
- BOOL has_prototype; /* true if a prototype field is necessary */
- BOOL has_simple_parameter_list;
- BOOL has_parameter_expressions; /* if true, an argument scope is created */
- BOOL has_use_strict; /* to reject directive in special cases */
- BOOL has_eval_call; /* true if the function contains a call to eval() */
- BOOL has_arguments_binding; /* true if the 'arguments' binding is
- available in the function */
- BOOL has_this_binding; /* true if the 'this' and new.target binding are
- available in the function */
- BOOL new_target_allowed; /* true if the 'new.target' does not
- throw a syntax error */
- BOOL super_call_allowed; /* true if super() is allowed */
- BOOL super_allowed; /* true if super. or super[] is allowed */
- BOOL arguments_allowed; /* true if the 'arguments' identifier is allowed */
- BOOL is_derived_class_constructor;
- BOOL in_function_body;
- JSFunctionKindEnum func_kind : 8;
- JSParseFunctionEnum func_type : 8;
- uint8_t js_mode; /* bitmap of JS_MODE_x */
- JSAtom func_name; /* JS_ATOM_NULL if no name */
- JSVarDef *vars;
- int var_size; /* allocated size for vars[] */
- int var_count;
- JSVarDef *args;
- int arg_size; /* allocated size for args[] */
- int arg_count; /* number of arguments */
- int defined_arg_count;
- int var_ref_count; /* number of local/arg variable references */
- int var_object_idx; /* -1 if none */
- int arg_var_object_idx; /* -1 if none (var object for the argument scope) */
- int arguments_var_idx; /* -1 if none */
- int arguments_arg_idx; /* argument variable definition in argument scope,
- -1 if none */
- int func_var_idx; /* variable containing the current function (-1
- if none, only used if is_func_expr is true) */
- int eval_ret_idx; /* variable containing the return value of the eval, -1 if none */
- int this_var_idx; /* variable containg the 'this' value, -1 if none */
- int new_target_var_idx; /* variable containg the 'new.target' value, -1 if none */
- int this_active_func_var_idx; /* variable containg the 'this.active_func' value, -1 if none */
- int home_object_var_idx;
- BOOL need_home_object;
- int scope_level; /* index into fd->scopes if the current lexical scope */
- int scope_first; /* index into vd->vars of first lexically scoped variable */
- int scope_size; /* allocated size of fd->scopes array */
- int scope_count; /* number of entries used in the fd->scopes array */
- JSVarScope *scopes;
- JSVarScope def_scope_array[4];
- int body_scope; /* scope of the body of the function or eval */
- int global_var_count;
- int global_var_size;
- JSGlobalVar *global_vars;
- DynBuf byte_code;
- int last_opcode_pos; /* -1 if no last opcode */
- const uint8_t *last_opcode_source_ptr;
- BOOL use_short_opcodes; /* true if short opcodes are used in byte_code */
- LabelSlot *label_slots;
- int label_size; /* allocated size for label_slots[] */
- int label_count;
- BlockEnv *top_break; /* break/continue label stack */
- /* constant pool (strings, functions, numbers) */
- JSValue *cpool;
- int cpool_count;
- int cpool_size;
- /* list of variables in the closure */
- int closure_var_count;
- int closure_var_size;
- JSClosureVar *closure_var;
- JumpSlot *jump_slots;
- int jump_size;
- int jump_count;
- LineNumberSlot *line_number_slots;
- int line_number_size;
- int line_number_count;
- int line_number_last;
- int line_number_last_pc;
- /* pc2line table */
- BOOL strip_debug : 1; /* strip all debug info (implies strip_source = TRUE) */
- BOOL strip_source : 1; /* strip only source code */
- JSAtom filename;
- uint32_t source_pos; /* pointer in the eval() source */
- GetLineColCache *get_line_col_cache; /* XXX: could remove to save memory */
- DynBuf pc2line;
- char *source; /* raw source, utf-8 encoded */
- int source_len;
- JSModuleDef *module; /* != NULL when parsing a module */
- BOOL has_await; /* TRUE if await is used (used in module eval) */
- } JSFunctionDef;
- typedef struct JSToken {
- int val;
- const uint8_t *ptr; /* position in the source */
- union {
- struct {
- JSValue str;
- int sep;
- } str;
- struct {
- JSValue val;
- } num;
- struct {
- JSAtom atom;
- BOOL has_escape;
- BOOL is_reserved;
- } ident;
- struct {
- JSValue body;
- JSValue flags;
- } regexp;
- } u;
- } JSToken;
- typedef struct JSParseState {
- JSContext *ctx;
- const char *filename;
- JSToken token;
- BOOL got_lf; /* true if got line feed before the current token */
- const uint8_t *last_ptr;
- const uint8_t *buf_start;
- const uint8_t *buf_ptr;
- const uint8_t *buf_end;
- /* current function code */
- JSFunctionDef *cur_func;
- BOOL is_module; /* parsing a module */
- BOOL allow_html_comments;
- BOOL ext_json; /* JSON parsing: true if accepting JSON superset */
- GetLineColCache get_line_col_cache;
- } JSParseState;
- typedef struct JSOpCode {
- #ifdef DUMP_BYTECODE
- const char *name;
- #endif
- uint8_t size; /* in bytes */
- /* the opcodes remove n_pop items from the top of the stack, then
- pushes n_push items */
- uint8_t n_pop;
- uint8_t n_push;
- uint8_t fmt;
- } JSOpCode;
- static const JSOpCode opcode_info[OP_COUNT + (OP_TEMP_END - OP_TEMP_START)] = {
- #define FMT(f)
- #ifdef DUMP_BYTECODE
- #define DEF(id, size, n_pop, n_push, f) { #id, size, n_pop, n_push, OP_FMT_ ## f },
- #else
- #define DEF(id, size, n_pop, n_push, f) { size, n_pop, n_push, OP_FMT_ ## f },
- #endif
- #include "quickjs-opcode.h"
- #undef DEF
- #undef FMT
- };
- #if SHORT_OPCODES
- /* After the final compilation pass, short opcodes are used. Their
- opcodes overlap with the temporary opcodes which cannot appear in
- the final bytecode. Their description is after the temporary
- opcodes in opcode_info[]. */
- #define short_opcode_info(op) \
- opcode_info[(op) >= OP_TEMP_START ? \
- (op) + (OP_TEMP_END - OP_TEMP_START) : (op)]
- #else
- #define short_opcode_info(op) opcode_info[op]
- #endif
- static __exception int next_token(JSParseState *s);
- static void free_token(JSParseState *s, JSToken *token)
- {
- switch(token->val) {
- case TOK_NUMBER:
- JS_FreeValue(s->ctx, token->u.num.val);
- break;
- case TOK_STRING:
- case TOK_TEMPLATE:
- JS_FreeValue(s->ctx, token->u.str.str);
- break;
- case TOK_REGEXP:
- JS_FreeValue(s->ctx, token->u.regexp.body);
- JS_FreeValue(s->ctx, token->u.regexp.flags);
- break;
- case TOK_IDENT:
- case TOK_PRIVATE_NAME:
- JS_FreeAtom(s->ctx, token->u.ident.atom);
- break;
- default:
- if (token->val >= TOK_FIRST_KEYWORD &&
- token->val <= TOK_LAST_KEYWORD) {
- JS_FreeAtom(s->ctx, token->u.ident.atom);
- }
- break;
- }
- }
- static void __attribute((unused)) dump_token(JSParseState *s,
- const JSToken *token)
- {
- switch(token->val) {
- case TOK_NUMBER:
- {
- double d;
- JS_ToFloat64(s->ctx, &d, token->u.num.val); /* no exception possible */
- printf("number: %.14g\n", d);
- }
- break;
- case TOK_IDENT:
- dump_atom:
- {
- char buf[ATOM_GET_STR_BUF_SIZE];
- printf("ident: '%s'\n",
- JS_AtomGetStr(s->ctx, buf, sizeof(buf), token->u.ident.atom));
- }
- break;
- case TOK_STRING:
- {
- const char *str;
- /* XXX: quote the string */
- str = JS_ToCString(s->ctx, token->u.str.str);
- printf("string: '%s'\n", str);
- JS_FreeCString(s->ctx, str);
- }
- break;
- case TOK_TEMPLATE:
- {
- const char *str;
- str = JS_ToCString(s->ctx, token->u.str.str);
- printf("template: `%s`\n", str);
- JS_FreeCString(s->ctx, str);
- }
- break;
- case TOK_REGEXP:
- {
- const char *str, *str2;
- str = JS_ToCString(s->ctx, token->u.regexp.body);
- str2 = JS_ToCString(s->ctx, token->u.regexp.flags);
- printf("regexp: '%s' '%s'\n", str, str2);
- JS_FreeCString(s->ctx, str);
- JS_FreeCString(s->ctx, str2);
- }
- break;
- case TOK_EOF:
- printf("eof\n");
- break;
- default:
- if (s->token.val >= TOK_NULL && s->token.val <= TOK_LAST_KEYWORD) {
- goto dump_atom;
- } else if (s->token.val >= 256) {
- printf("token: %d\n", token->val);
- } else {
- printf("token: '%c'\n", token->val);
- }
- break;
- }
- }
- /* return the zero based line and column number in the source. */
- /* Note: we no longer support '\r' as line terminator */
- static int get_line_col(int *pcol_num, const uint8_t *buf, size_t len)
- {
- int line_num, col_num, c;
- size_t i;
-
- line_num = 0;
- col_num = 0;
- for(i = 0; i < len; i++) {
- c = buf[i];
- if (c == '\n') {
- line_num++;
- col_num = 0;
- } else if (c < 0x80 || c >= 0xc0) {
- col_num++;
- }
- }
- *pcol_num = col_num;
- return line_num;
- }
- static int get_line_col_cached(GetLineColCache *s, int *pcol_num, const uint8_t *ptr)
- {
- int line_num, col_num;
- if (ptr >= s->ptr) {
- line_num = get_line_col(&col_num, s->ptr, ptr - s->ptr);
- if (line_num == 0) {
- s->col_num += col_num;
- } else {
- s->line_num += line_num;
- s->col_num = col_num;
- }
- } else {
- line_num = get_line_col(&col_num, ptr, s->ptr - ptr);
- if (line_num == 0) {
- s->col_num -= col_num;
- } else {
- const uint8_t *p;
- s->line_num -= line_num;
- /* find the absolute column position */
- col_num = 0;
- for(p = ptr - 1; p >= s->buf_start; p--) {
- if (*p == '\n') {
- break;
- } else if (*p < 0x80 || *p >= 0xc0) {
- col_num++;
- }
- }
- s->col_num = col_num;
- }
- }
- s->ptr = ptr;
- *pcol_num = s->col_num;
- return s->line_num;
- }
- /* 'ptr' is the position of the error in the source */
- static int js_parse_error_v(JSParseState *s, const uint8_t *ptr, const char *fmt, va_list ap)
- {
- JSContext *ctx = s->ctx;
- int line_num, col_num;
- line_num = get_line_col(&col_num, s->buf_start, ptr - s->buf_start);
- JS_ThrowError2(ctx, JS_SYNTAX_ERROR, fmt, ap, FALSE);
- build_backtrace(ctx, ctx->rt->current_exception, s->filename,
- line_num + 1, col_num + 1, 0);
- return -1;
- }
- static __attribute__((format(printf, 3, 4))) int js_parse_error_pos(JSParseState *s, const uint8_t *ptr, const char *fmt, ...)
- {
- va_list ap;
- int ret;
-
- va_start(ap, fmt);
- ret = js_parse_error_v(s, ptr, fmt, ap);
- va_end(ap);
- return ret;
- }
- static __attribute__((format(printf, 2, 3))) int js_parse_error(JSParseState *s, const char *fmt, ...)
- {
- va_list ap;
- int ret;
-
- va_start(ap, fmt);
- ret = js_parse_error_v(s, s->token.ptr, fmt, ap);
- va_end(ap);
- return ret;
- }
- static int js_parse_expect(JSParseState *s, int tok)
- {
- if (s->token.val != tok) {
- /* XXX: dump token correctly in all cases */
- return js_parse_error(s, "expecting '%c'", tok);
- }
- return next_token(s);
- }
- static int js_parse_expect_semi(JSParseState *s)
- {
- if (s->token.val != ';') {
- /* automatic insertion of ';' */
- if (s->token.val == TOK_EOF || s->token.val == '}' || s->got_lf) {
- return 0;
- }
- return js_parse_error(s, "expecting '%c'", ';');
- }
- return next_token(s);
- }
- static int js_parse_error_reserved_identifier(JSParseState *s)
- {
- char buf1[ATOM_GET_STR_BUF_SIZE];
- return js_parse_error(s, "'%s' is a reserved identifier",
- JS_AtomGetStr(s->ctx, buf1, sizeof(buf1),
- s->token.u.ident.atom));
- }
- static __exception int js_parse_template_part(JSParseState *s, const uint8_t *p)
- {
- uint32_t c;
- StringBuffer b_s, *b = &b_s;
- JSValue str;
- /* p points to the first byte of the template part */
- if (string_buffer_init(s->ctx, b, 32))
- goto fail;
- for(;;) {
- if (p >= s->buf_end)
- goto unexpected_eof;
- c = *p++;
- if (c == '`') {
- /* template end part */
- break;
- }
- if (c == '$' && *p == '{') {
- /* template start or middle part */
- p++;
- break;
- }
- if (c == '\\') {
- if (string_buffer_putc8(b, c))
- goto fail;
- if (p >= s->buf_end)
- goto unexpected_eof;
- c = *p++;
- }
- /* newline sequences are normalized as single '\n' bytes */
- if (c == '\r') {
- if (*p == '\n')
- p++;
- c = '\n';
- }
- if (c >= 0x80) {
- const uint8_t *p_next;
- c = unicode_from_utf8(p - 1, UTF8_CHAR_LEN_MAX, &p_next);
- if (c > 0x10FFFF) {
- js_parse_error_pos(s, p - 1, "invalid UTF-8 sequence");
- goto fail;
- }
- p = p_next;
- }
- if (string_buffer_putc(b, c))
- goto fail;
- }
- str = string_buffer_end(b);
- if (JS_IsException(str))
- return -1;
- s->token.val = TOK_TEMPLATE;
- s->token.u.str.sep = c;
- s->token.u.str.str = str;
- s->buf_ptr = p;
- return 0;
- unexpected_eof:
- js_parse_error(s, "unexpected end of string");
- fail:
- string_buffer_free(b);
- return -1;
- }
- static __exception int js_parse_string(JSParseState *s, int sep,
- BOOL do_throw, const uint8_t *p,
- JSToken *token, const uint8_t **pp)
- {
- int ret;
- uint32_t c;
- StringBuffer b_s, *b = &b_s;
- const uint8_t *p_escape;
- JSValue str;
- /* string */
- if (string_buffer_init(s->ctx, b, 32))
- goto fail;
- for(;;) {
- if (p >= s->buf_end)
- goto invalid_char;
- c = *p;
- if (c < 0x20) {
- if (sep == '`') {
- if (c == '\r') {
- if (p[1] == '\n')
- p++;
- c = '\n';
- }
- /* do not update s->line_num */
- } else if (c == '\n' || c == '\r')
- goto invalid_char;
- }
- p++;
- if (c == sep)
- break;
- if (c == '$' && *p == '{' && sep == '`') {
- /* template start or middle part */
- p++;
- break;
- }
- if (c == '\\') {
- p_escape = p - 1;
- c = *p;
- /* XXX: need a specific JSON case to avoid
- accepting invalid escapes */
- switch(c) {
- case '\0':
- if (p >= s->buf_end)
- goto invalid_char;
- p++;
- break;
- case '\'':
- case '\"':
- case '\\':
- p++;
- break;
- case '\r': /* accept DOS and MAC newline sequences */
- if (p[1] == '\n') {
- p++;
- }
- /* fall thru */
- case '\n':
- /* ignore escaped newline sequence */
- p++;
- continue;
- default:
- if (c >= '0' && c <= '9') {
- if (!(s->cur_func->js_mode & JS_MODE_STRICT) && sep != '`')
- goto parse_escape;
- if (c == '0' && !(p[1] >= '0' && p[1] <= '9')) {
- p++;
- c = '\0';
- } else {
- if (c >= '8' || sep == '`') {
- /* Note: according to ES2021, \8 and \9 are not
- accepted in strict mode or in templates. */
- goto invalid_escape;
- } else {
- if (do_throw)
- js_parse_error_pos(s, p_escape, "octal escape sequences are not allowed in strict mode");
- }
- goto fail;
- }
- } else if (c >= 0x80) {
- const uint8_t *p_next;
- c = unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p_next);
- if (c > 0x10FFFF) {
- goto invalid_utf8;
- }
- p = p_next;
- /* LS or PS are skipped */
- if (c == CP_LS || c == CP_PS)
- continue;
- } else {
- parse_escape:
- ret = lre_parse_escape(&p, TRUE);
- if (ret == -1) {
- invalid_escape:
- if (do_throw)
- js_parse_error_pos(s, p_escape, "malformed escape sequence in string literal");
- goto fail;
- } else if (ret < 0) {
- /* ignore the '\' (could output a warning) */
- p++;
- } else {
- c = ret;
- }
- }
- break;
- }
- } else if (c >= 0x80) {
- const uint8_t *p_next;
- c = unicode_from_utf8(p - 1, UTF8_CHAR_LEN_MAX, &p_next);
- if (c > 0x10FFFF)
- goto invalid_utf8;
- p = p_next;
- }
- if (string_buffer_putc(b, c))
- goto fail;
- }
- str = string_buffer_end(b);
- if (JS_IsException(str))
- return -1;
- token->val = TOK_STRING;
- token->u.str.sep = c;
- token->u.str.str = str;
- *pp = p;
- return 0;
- invalid_utf8:
- if (do_throw)
- js_parse_error(s, "invalid UTF-8 sequence");
- goto fail;
- invalid_char:
- if (do_throw)
- js_parse_error(s, "unexpected end of string");
- fail:
- string_buffer_free(b);
- return -1;
- }
- static inline BOOL token_is_pseudo_keyword(JSParseState *s, JSAtom atom) {
- return s->token.val == TOK_IDENT && s->token.u.ident.atom == atom &&
- !s->token.u.ident.has_escape;
- }
- static __exception int js_parse_regexp(JSParseState *s)
- {
- const uint8_t *p;
- BOOL in_class;
- StringBuffer b_s, *b = &b_s;
- StringBuffer b2_s, *b2 = &b2_s;
- uint32_t c;
- JSValue body_str, flags_str;
- p = s->buf_ptr;
- p++;
- in_class = FALSE;
- if (string_buffer_init(s->ctx, b, 32))
- return -1;
- if (string_buffer_init(s->ctx, b2, 1))
- goto fail;
- for(;;) {
- if (p >= s->buf_end) {
- eof_error:
- js_parse_error(s, "unexpected end of regexp");
- goto fail;
- }
- c = *p++;
- if (c == '\n' || c == '\r') {
- goto eol_error;
- } else if (c == '/') {
- if (!in_class)
- break;
- } else if (c == '[') {
- in_class = TRUE;
- } else if (c == ']') {
- /* XXX: incorrect as the first character in a class */
- in_class = FALSE;
- } else if (c == '\\') {
- if (string_buffer_putc8(b, c))
- goto fail;
- c = *p++;
- if (c == '\n' || c == '\r')
- goto eol_error;
- else if (c == '\0' && p >= s->buf_end)
- goto eof_error;
- else if (c >= 0x80) {
- const uint8_t *p_next;
- c = unicode_from_utf8(p - 1, UTF8_CHAR_LEN_MAX, &p_next);
- if (c > 0x10FFFF) {
- goto invalid_utf8;
- }
- p = p_next;
- if (c == CP_LS || c == CP_PS)
- goto eol_error;
- }
- } else if (c >= 0x80) {
- const uint8_t *p_next;
- c = unicode_from_utf8(p - 1, UTF8_CHAR_LEN_MAX, &p_next);
- if (c > 0x10FFFF) {
- invalid_utf8:
- js_parse_error_pos(s, p - 1, "invalid UTF-8 sequence");
- goto fail;
- }
- /* LS or PS are considered as line terminator */
- if (c == CP_LS || c == CP_PS) {
- eol_error:
- js_parse_error_pos(s, p - 1, "unexpected line terminator in regexp");
- goto fail;
- }
- p = p_next;
- }
- if (string_buffer_putc(b, c))
- goto fail;
- }
- /* flags */
- for(;;) {
- const uint8_t *p_next = p;
- c = *p_next++;
- if (c >= 0x80) {
- c = unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p_next);
- if (c > 0x10FFFF) {
- p++;
- goto invalid_utf8;
- }
- }
- if (!lre_js_is_ident_next(c))
- break;
- if (string_buffer_putc(b2, c))
- goto fail;
- p = p_next;
- }
- body_str = string_buffer_end(b);
- flags_str = string_buffer_end(b2);
- if (JS_IsException(body_str) ||
- JS_IsException(flags_str)) {
- JS_FreeValue(s->ctx, body_str);
- JS_FreeValue(s->ctx, flags_str);
- return -1;
- }
- s->token.val = TOK_REGEXP;
- s->token.u.regexp.body = body_str;
- s->token.u.regexp.flags = flags_str;
- s->buf_ptr = p;
- return 0;
- fail:
- string_buffer_free(b);
- string_buffer_free(b2);
- return -1;
- }
- static __exception int ident_realloc(JSContext *ctx, char **pbuf, size_t *psize,
- char *static_buf)
- {
- char *buf, *new_buf;
- size_t size, new_size;
- buf = *pbuf;
- size = *psize;
- if (size >= (SIZE_MAX / 3) * 2)
- new_size = SIZE_MAX;
- else
- new_size = size + (size >> 1);
- if (buf == static_buf) {
- new_buf = js_malloc(ctx, new_size);
- if (!new_buf)
- return -1;
- memcpy(new_buf, buf, size);
- } else {
- new_buf = js_realloc(ctx, buf, new_size);
- if (!new_buf)
- return -1;
- }
- *pbuf = new_buf;
- *psize = new_size;
- return 0;
- }
- /* convert a TOK_IDENT to a keyword when needed */
- static void update_token_ident(JSParseState *s)
- {
- if (s->token.u.ident.atom <= JS_ATOM_LAST_KEYWORD ||
- (s->token.u.ident.atom <= JS_ATOM_LAST_STRICT_KEYWORD &&
- (s->cur_func->js_mode & JS_MODE_STRICT)) ||
- (s->token.u.ident.atom == JS_ATOM_yield &&
- ((s->cur_func->func_kind & JS_FUNC_GENERATOR) ||
- (s->cur_func->func_type == JS_PARSE_FUNC_ARROW &&
- !s->cur_func->in_function_body && s->cur_func->parent &&
- (s->cur_func->parent->func_kind & JS_FUNC_GENERATOR)))) ||
- (s->token.u.ident.atom == JS_ATOM_await &&
- (s->is_module ||
- (s->cur_func->func_kind & JS_FUNC_ASYNC) ||
- s->cur_func->func_type == JS_PARSE_FUNC_CLASS_STATIC_INIT ||
- (s->cur_func->func_type == JS_PARSE_FUNC_ARROW &&
- !s->cur_func->in_function_body && s->cur_func->parent &&
- ((s->cur_func->parent->func_kind & JS_FUNC_ASYNC) ||
- s->cur_func->parent->func_type == JS_PARSE_FUNC_CLASS_STATIC_INIT))))) {
- if (s->token.u.ident.has_escape) {
- s->token.u.ident.is_reserved = TRUE;
- s->token.val = TOK_IDENT;
- } else {
- /* The keywords atoms are pre allocated */
- s->token.val = s->token.u.ident.atom - 1 + TOK_FIRST_KEYWORD;
- }
- }
- }
- /* if the current token is an identifier or keyword, reparse it
- according to the current function type */
- static void reparse_ident_token(JSParseState *s)
- {
- if (s->token.val == TOK_IDENT ||
- (s->token.val >= TOK_FIRST_KEYWORD &&
- s->token.val <= TOK_LAST_KEYWORD)) {
- s->token.val = TOK_IDENT;
- s->token.u.ident.is_reserved = FALSE;
- update_token_ident(s);
- }
- }
- /* 'c' is the first character. Return JS_ATOM_NULL in case of error */
- static JSAtom parse_ident(JSParseState *s, const uint8_t **pp,
- BOOL *pident_has_escape, int c, BOOL is_private)
- {
- const uint8_t *p, *p1;
- char ident_buf[128], *buf;
- size_t ident_size, ident_pos;
- JSAtom atom;
- p = *pp;
- buf = ident_buf;
- ident_size = sizeof(ident_buf);
- ident_pos = 0;
- if (is_private)
- buf[ident_pos++] = '#';
- for(;;) {
- p1 = p;
- if (c < 128) {
- buf[ident_pos++] = c;
- } else {
- ident_pos += unicode_to_utf8((uint8_t*)buf + ident_pos, c);
- }
- c = *p1++;
- if (c == '\\' && *p1 == 'u') {
- c = lre_parse_escape(&p1, TRUE);
- *pident_has_escape = TRUE;
- } else if (c >= 128) {
- c = unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p1);
- }
- if (!lre_js_is_ident_next(c))
- break;
- p = p1;
- if (unlikely(ident_pos >= ident_size - UTF8_CHAR_LEN_MAX)) {
- if (ident_realloc(s->ctx, &buf, &ident_size, ident_buf)) {
- atom = JS_ATOM_NULL;
- goto done;
- }
- }
- }
- atom = JS_NewAtomLen(s->ctx, buf, ident_pos);
- done:
- if (unlikely(buf != ident_buf))
- js_free(s->ctx, buf);
- *pp = p;
- return atom;
- }
- static __exception int next_token(JSParseState *s)
- {
- const uint8_t *p;
- int c;
- BOOL ident_has_escape;
- JSAtom atom;
- if (js_check_stack_overflow(s->ctx->rt, 0)) {
- return js_parse_error(s, "stack overflow");
- }
- free_token(s, &s->token);
- p = s->last_ptr = s->buf_ptr;
- s->got_lf = FALSE;
- redo:
- s->token.ptr = p;
- c = *p;
- switch(c) {
- case 0:
- if (p >= s->buf_end) {
- s->token.val = TOK_EOF;
- } else {
- goto def_token;
- }
- break;
- case '`':
- if (js_parse_template_part(s, p + 1))
- goto fail;
- p = s->buf_ptr;
- break;
- case '\'':
- case '\"':
- if (js_parse_string(s, c, TRUE, p + 1, &s->token, &p))
- goto fail;
- break;
- case '\r': /* accept DOS and MAC newline sequences */
- if (p[1] == '\n') {
- p++;
- }
- /* fall thru */
- case '\n':
- p++;
- line_terminator:
- s->got_lf = TRUE;
- goto redo;
- case '\f':
- case '\v':
- case ' ':
- case '\t':
- p++;
- goto redo;
- case '/':
- if (p[1] == '*') {
- /* comment */
- p += 2;
- for(;;) {
- if (*p == '\0' && p >= s->buf_end) {
- js_parse_error(s, "unexpected end of comment");
- goto fail;
- }
- if (p[0] == '*' && p[1] == '/') {
- p += 2;
- break;
- }
- if (*p == '\n' || *p == '\r') {
- s->got_lf = TRUE; /* considered as LF for ASI */
- p++;
- } else if (*p >= 0x80) {
- c = unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p);
- if (c == CP_LS || c == CP_PS) {
- s->got_lf = TRUE; /* considered as LF for ASI */
- } else if (c == -1) {
- p++; /* skip invalid UTF-8 */
- }
- } else {
- p++;
- }
- }
- goto redo;
- } else if (p[1] == '/') {
- /* line comment */
- p += 2;
- skip_line_comment:
- for(;;) {
- if (*p == '\0' && p >= s->buf_end)
- break;
- if (*p == '\r' || *p == '\n')
- break;
- if (*p >= 0x80) {
- c = unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p);
- /* LS or PS are considered as line terminator */
- if (c == CP_LS || c == CP_PS) {
- break;
- } else if (c == -1) {
- p++; /* skip invalid UTF-8 */
- }
- } else {
- p++;
- }
- }
- goto redo;
- } else if (p[1] == '=') {
- p += 2;
- s->token.val = TOK_DIV_ASSIGN;
- } else {
- p++;
- s->token.val = c;
- }
- break;
- case '\\':
- if (p[1] == 'u') {
- const uint8_t *p1 = p + 1;
- int c1 = lre_parse_escape(&p1, TRUE);
- if (c1 >= 0 && lre_js_is_ident_first(c1)) {
- c = c1;
- p = p1;
- ident_has_escape = TRUE;
- goto has_ident;
- } else {
- /* XXX: syntax error? */
- }
- }
- goto def_token;
- case 'a': case 'b': case 'c': case 'd':
- case 'e': case 'f': case 'g': case 'h':
- case 'i': case 'j': case 'k': case 'l':
- case 'm': case 'n': case 'o': case 'p':
- case 'q': case 'r': case 's': case 't':
- case 'u': case 'v': case 'w': case 'x':
- case 'y': case 'z':
- case 'A': case 'B': case 'C': case 'D':
- case 'E': case 'F': case 'G': case 'H':
- case 'I': case 'J': case 'K': case 'L':
- case 'M': case 'N': case 'O': case 'P':
- case 'Q': case 'R': case 'S': case 'T':
- case 'U': case 'V': case 'W': case 'X':
- case 'Y': case 'Z':
- case '_':
- case '$':
- /* identifier */
- p++;
- ident_has_escape = FALSE;
- has_ident:
- atom = parse_ident(s, &p, &ident_has_escape, c, FALSE);
- if (atom == JS_ATOM_NULL)
- goto fail;
- s->token.u.ident.atom = atom;
- s->token.u.ident.has_escape = ident_has_escape;
- s->token.u.ident.is_reserved = FALSE;
- s->token.val = TOK_IDENT;
- update_token_ident(s);
- break;
- case '#':
- /* private name */
- {
- const uint8_t *p1;
- p++;
- p1 = p;
- c = *p1++;
- if (c == '\\' && *p1 == 'u') {
- c = lre_parse_escape(&p1, TRUE);
- } else if (c >= 128) {
- c = unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p1);
- }
- if (!lre_js_is_ident_first(c)) {
- js_parse_error(s, "invalid first character of private name");
- goto fail;
- }
- p = p1;
- ident_has_escape = FALSE; /* not used */
- atom = parse_ident(s, &p, &ident_has_escape, c, TRUE);
- if (atom == JS_ATOM_NULL)
- goto fail;
- s->token.u.ident.atom = atom;
- s->token.val = TOK_PRIVATE_NAME;
- }
- break;
- case '.':
- if (p[1] == '.' && p[2] == '.') {
- p += 3;
- s->token.val = TOK_ELLIPSIS;
- break;
- }
- if (p[1] >= '0' && p[1] <= '9') {
- goto parse_number;
- } else {
- goto def_token;
- }
- break;
- case '0':
- /* in strict mode, octal literals are not accepted */
- if (is_digit(p[1]) && (s->cur_func->js_mode & JS_MODE_STRICT)) {
- js_parse_error(s, "octal literals are deprecated in strict mode");
- goto fail;
- }
- goto parse_number;
- case '1': case '2': case '3': case '4':
- case '5': case '6': case '7': case '8':
- case '9':
- /* number */
- parse_number:
- {
- JSValue ret;
- const uint8_t *p1;
- int flags;
- flags = ATOD_ACCEPT_BIN_OCT | ATOD_ACCEPT_LEGACY_OCTAL |
- ATOD_ACCEPT_UNDERSCORES | ATOD_ACCEPT_SUFFIX;
- ret = js_atof(s->ctx, (const char *)p, (const char **)&p, 0,
- flags);
- if (JS_IsException(ret))
- goto fail;
- /* reject `10instanceof Number` */
- if (JS_VALUE_IS_NAN(ret) ||
- lre_js_is_ident_next(unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p1))) {
- JS_FreeValue(s->ctx, ret);
- js_parse_error(s, "invalid number literal");
- goto fail;
- }
- s->token.val = TOK_NUMBER;
- s->token.u.num.val = ret;
- }
- break;
- case '*':
- if (p[1] == '=') {
- p += 2;
- s->token.val = TOK_MUL_ASSIGN;
- } else if (p[1] == '*') {
- if (p[2] == '=') {
- p += 3;
- s->token.val = TOK_POW_ASSIGN;
- } else {
- p += 2;
- s->token.val = TOK_POW;
- }
- } else {
- goto def_token;
- }
- break;
- case '%':
- if (p[1] == '=') {
- p += 2;
- s->token.val = TOK_MOD_ASSIGN;
- } else {
- goto def_token;
- }
- break;
- case '+':
- if (p[1] == '=') {
- p += 2;
- s->token.val = TOK_PLUS_ASSIGN;
- } else if (p[1] == '+') {
- p += 2;
- s->token.val = TOK_INC;
- } else {
- goto def_token;
- }
- break;
- case '-':
- if (p[1] == '=') {
- p += 2;
- s->token.val = TOK_MINUS_ASSIGN;
- } else if (p[1] == '-') {
- if (s->allow_html_comments && p[2] == '>' &&
- (s->got_lf || s->last_ptr == s->buf_start)) {
- /* Annex B: `-->` at beginning of line is an html comment end.
- It extends to the end of the line.
- */
- goto skip_line_comment;
- }
- p += 2;
- s->token.val = TOK_DEC;
- } else {
- goto def_token;
- }
- break;
- case '<':
- if (p[1] == '=') {
- p += 2;
- s->token.val = TOK_LTE;
- } else if (p[1] == '<') {
- if (p[2] == '=') {
- p += 3;
- s->token.val = TOK_SHL_ASSIGN;
- } else {
- p += 2;
- s->token.val = TOK_SHL;
- }
- } else if (s->allow_html_comments &&
- p[1] == '!' && p[2] == '-' && p[3] == '-') {
- /* Annex B: handle `<!--` single line html comments */
- goto skip_line_comment;
- } else {
- goto def_token;
- }
- break;
- case '>':
- if (p[1] == '=') {
- p += 2;
- s->token.val = TOK_GTE;
- } else if (p[1] == '>') {
- if (p[2] == '>') {
- if (p[3] == '=') {
- p += 4;
- s->token.val = TOK_SHR_ASSIGN;
- } else {
- p += 3;
- s->token.val = TOK_SHR;
- }
- } else if (p[2] == '=') {
- p += 3;
- s->token.val = TOK_SAR_ASSIGN;
- } else {
- p += 2;
- s->token.val = TOK_SAR;
- }
- } else {
- goto def_token;
- }
- break;
- case '=':
- if (p[1] == '=') {
- if (p[2] == '=') {
- p += 3;
- s->token.val = TOK_STRICT_EQ;
- } else {
- p += 2;
- s->token.val = TOK_EQ;
- }
- } else if (p[1] == '>') {
- p += 2;
- s->token.val = TOK_ARROW;
- } else {
- goto def_token;
- }
- break;
- case '!':
- if (p[1] == '=') {
- if (p[2] == '=') {
- p += 3;
- s->token.val = TOK_STRICT_NEQ;
- } else {
- p += 2;
- s->token.val = TOK_NEQ;
- }
- } else {
- goto def_token;
- }
- break;
- case '&':
- if (p[1] == '=') {
- p += 2;
- s->token.val = TOK_AND_ASSIGN;
- } else if (p[1] == '&') {
- if (p[2] == '=') {
- p += 3;
- s->token.val = TOK_LAND_ASSIGN;
- } else {
- p += 2;
- s->token.val = TOK_LAND;
- }
- } else {
- goto def_token;
- }
- break;
- case '^':
- if (p[1] == '=') {
- p += 2;
- s->token.val = TOK_XOR_ASSIGN;
- } else {
- goto def_token;
- }
- break;
- case '|':
- if (p[1] == '=') {
- p += 2;
- s->token.val = TOK_OR_ASSIGN;
- } else if (p[1] == '|') {
- if (p[2] == '=') {
- p += 3;
- s->token.val = TOK_LOR_ASSIGN;
- } else {
- p += 2;
- s->token.val = TOK_LOR;
- }
- } else {
- goto def_token;
- }
- break;
- case '?':
- if (p[1] == '?') {
- if (p[2] == '=') {
- p += 3;
- s->token.val = TOK_DOUBLE_QUESTION_MARK_ASSIGN;
- } else {
- p += 2;
- s->token.val = TOK_DOUBLE_QUESTION_MARK;
- }
- } else if (p[1] == '.' && !(p[2] >= '0' && p[2] <= '9')) {
- p += 2;
- s->token.val = TOK_QUESTION_MARK_DOT;
- } else {
- goto def_token;
- }
- break;
- default:
- if (c >= 128) {
- /* unicode value */
- c = unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p);
- switch(c) {
- case CP_PS:
- case CP_LS:
- /* XXX: should avoid incrementing line_number, but
- needed to handle HTML comments */
- goto line_terminator;
- default:
- if (lre_is_space(c)) {
- goto redo;
- } else if (lre_js_is_ident_first(c)) {
- ident_has_escape = FALSE;
- goto has_ident;
- } else {
- js_parse_error(s, "unexpected character");
- goto fail;
- }
- }
- }
- def_token:
- s->token.val = c;
- p++;
- break;
- }
- s->buf_ptr = p;
- // dump_token(s, &s->token);
- return 0;
- fail:
- s->token.val = TOK_ERROR;
- return -1;
- }
- /* 'c' is the first character. Return JS_ATOM_NULL in case of error */
- /* XXX: accept unicode identifiers as JSON5 ? */
- static JSAtom json_parse_ident(JSParseState *s, const uint8_t **pp, int c)
- {
- const uint8_t *p;
- char ident_buf[128], *buf;
- size_t ident_size, ident_pos;
- JSAtom atom;
- p = *pp;
- buf = ident_buf;
- ident_size = sizeof(ident_buf);
- ident_pos = 0;
- for(;;) {
- buf[ident_pos++] = c;
- c = *p;
- if (c >= 128 || !lre_is_id_continue_byte(c))
- break;
- p++;
- if (unlikely(ident_pos >= ident_size - UTF8_CHAR_LEN_MAX)) {
- if (ident_realloc(s->ctx, &buf, &ident_size, ident_buf)) {
- atom = JS_ATOM_NULL;
- goto done;
- }
- }
- }
- atom = JS_NewAtomLen(s->ctx, buf, ident_pos);
- done:
- if (unlikely(buf != ident_buf))
- js_free(s->ctx, buf);
- *pp = p;
- return atom;
- }
- static int json_parse_string(JSParseState *s, const uint8_t **pp, int sep)
- {
- const uint8_t *p, *p_next;
- int i;
- uint32_t c;
- StringBuffer b_s, *b = &b_s;
- if (string_buffer_init(s->ctx, b, 32))
- goto fail;
- p = *pp;
- for(;;) {
- if (p >= s->buf_end) {
- goto end_of_input;
- }
- c = *p++;
- if (c == sep)
- break;
- if (c < 0x20) {
- js_parse_error_pos(s, p - 1, "Bad control character in string literal");
- goto fail;
- }
- if (c == '\\') {
- c = *p++;
- switch(c) {
- case 'b': c = '\b'; break;
- case 'f': c = '\f'; break;
- case 'n': c = '\n'; break;
- case 'r': c = '\r'; break;
- case 't': c = '\t'; break;
- case '\\': break;
- case '/': break;
- case 'u':
- c = 0;
- for(i = 0; i < 4; i++) {
- int h = from_hex(*p++);
- if (h < 0) {
- js_parse_error_pos(s, p - 1, "Bad Unicode escape");
- goto fail;
- }
- c = (c << 4) | h;
- }
- break;
- case '\n':
- if (s->ext_json)
- continue;
- goto bad_escape;
- case 'v':
- if (s->ext_json) {
- c = '\v';
- break;
- }
- goto bad_escape;
- default:
- if (c == sep)
- break;
- if (p > s->buf_end)
- goto end_of_input;
- bad_escape:
- js_parse_error_pos(s, p - 1, "Bad escaped character");
- goto fail;
- }
- } else
- if (c >= 0x80) {
- c = unicode_from_utf8(p - 1, UTF8_CHAR_LEN_MAX, &p_next);
- if (c > 0x10FFFF) {
- js_parse_error_pos(s, p - 1, "Bad UTF-8 sequence");
- goto fail;
- }
- p = p_next;
- }
- if (string_buffer_putc(b, c))
- goto fail;
- }
- s->token.val = TOK_STRING;
- s->token.u.str.sep = sep;
- s->token.u.str.str = string_buffer_end(b);
- *pp = p;
- return 0;
- end_of_input:
- js_parse_error(s, "Unexpected end of JSON input");
- fail:
- string_buffer_free(b);
- return -1;
- }
- static int json_parse_number(JSParseState *s, const uint8_t **pp)
- {
- const uint8_t *p = *pp;
- const uint8_t *p_start = p;
- int radix;
- double d;
- JSATODTempMem atod_mem;
-
- if (*p == '+' || *p == '-')
- p++;
- if (!is_digit(*p)) {
- if (s->ext_json) {
- if (strstart((const char *)p, "Infinity", (const char **)&p)) {
- d = 1.0 / 0.0;
- if (*p_start == '-')
- d = -d;
- goto done;
- } else if (strstart((const char *)p, "NaN", (const char **)&p)) {
- d = NAN;
- goto done;
- } else if (*p != '.') {
- goto unexpected_token;
- }
- } else {
- goto unexpected_token;
- }
- }
- if (p[0] == '0') {
- if (s->ext_json) {
- /* also accepts base 16, 8 and 2 prefix for integers */
- radix = 10;
- if (p[1] == 'x' || p[1] == 'X') {
- p += 2;
- radix = 16;
- } else if ((p[1] == 'o' || p[1] == 'O')) {
- p += 2;
- radix = 8;
- } else if ((p[1] == 'b' || p[1] == 'B')) {
- p += 2;
- radix = 2;
- }
- if (radix != 10) {
- /* prefix is present */
- if (to_digit(*p) >= radix) {
- unexpected_token:
- return js_parse_error_pos(s, p, "Unexpected token '%c'", *p);
- }
- d = js_atod((const char *)p_start, (const char **)&p, 0,
- JS_ATOD_INT_ONLY | JS_ATOD_ACCEPT_BIN_OCT, &atod_mem);
- goto done;
- }
- }
- if (is_digit(p[1]))
- return js_parse_error_pos(s, p, "Unexpected number");
- }
- while (is_digit(*p))
- p++;
- if (*p == '.') {
- p++;
- if (!is_digit(*p))
- return js_parse_error_pos(s, p, "Unterminated fractional number");
- while (is_digit(*p))
- p++;
- }
- if (*p == 'e' || *p == 'E') {
- p++;
- if (*p == '+' || *p == '-')
- p++;
- if (!is_digit(*p))
- return js_parse_error_pos(s, p, "Exponent part is missing a number");
- while (is_digit(*p))
- p++;
- }
- d = js_atod((const char *)p_start, NULL, 10, 0, &atod_mem);
- done:
- s->token.val = TOK_NUMBER;
- s->token.u.num.val = JS_NewFloat64(s->ctx, d);
- *pp = p;
- return 0;
- }
- static __exception int json_next_token(JSParseState *s)
- {
- const uint8_t *p;
- int c;
- JSAtom atom;
- if (js_check_stack_overflow(s->ctx->rt, 0)) {
- return js_parse_error(s, "stack overflow");
- }
- free_token(s, &s->token);
- p = s->last_ptr = s->buf_ptr;
- redo:
- s->token.ptr = p;
- c = *p;
- switch(c) {
- case 0:
- if (p >= s->buf_end) {
- s->token.val = TOK_EOF;
- } else {
- goto def_token;
- }
- break;
- case '\'':
- if (!s->ext_json) {
- /* JSON does not accept single quoted strings */
- goto def_token;
- }
- /* fall through */
- case '\"':
- p++;
- if (json_parse_string(s, &p, c))
- goto fail;
- break;
- case '\r': /* accept DOS and MAC newline sequences */
- if (p[1] == '\n') {
- p++;
- }
- /* fall thru */
- case '\n':
- p++;
- goto redo;
- case '\f':
- case '\v':
- if (!s->ext_json) {
- /* JSONWhitespace does not match <VT>, nor <FF> */
- goto def_token;
- }
- /* fall through */
- case ' ':
- case '\t':
- p++;
- goto redo;
- case '/':
- if (!s->ext_json) {
- /* JSON does not accept comments */
- goto def_token;
- }
- if (p[1] == '*') {
- /* comment */
- p += 2;
- for(;;) {
- if (*p == '\0' && p >= s->buf_end) {
- js_parse_error(s, "unexpected end of comment");
- goto fail;
- }
- if (p[0] == '*' && p[1] == '/') {
- p += 2;
- break;
- }
- if (*p >= 0x80) {
- c = unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p);
- if (c == -1) {
- p++; /* skip invalid UTF-8 */
- }
- } else {
- p++;
- }
- }
- goto redo;
- } else if (p[1] == '/') {
- /* line comment */
- p += 2;
- for(;;) {
- if (*p == '\0' && p >= s->buf_end)
- break;
- if (*p == '\r' || *p == '\n')
- break;
- if (*p >= 0x80) {
- c = unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p);
- /* LS or PS are considered as line terminator */
- if (c == CP_LS || c == CP_PS) {
- break;
- } else if (c == -1) {
- p++; /* skip invalid UTF-8 */
- }
- } else {
- p++;
- }
- }
- goto redo;
- } else {
- goto def_token;
- }
- break;
- case 'a': case 'b': case 'c': case 'd':
- case 'e': case 'f': case 'g': case 'h':
- case 'i': case 'j': case 'k': case 'l':
- case 'm': case 'n': case 'o': case 'p':
- case 'q': case 'r': case 's': case 't':
- case 'u': case 'v': case 'w': case 'x':
- case 'y': case 'z':
- case 'A': case 'B': case 'C': case 'D':
- case 'E': case 'F': case 'G': case 'H':
- case 'I': case 'J': case 'K': case 'L':
- case 'M': case 'N': case 'O': case 'P':
- case 'Q': case 'R': case 'S': case 'T':
- case 'U': case 'V': case 'W': case 'X':
- case 'Y': case 'Z':
- case '_':
- case '$':
- p++;
- atom = json_parse_ident(s, &p, c);
- if (atom == JS_ATOM_NULL)
- goto fail;
- s->token.u.ident.atom = atom;
- s->token.u.ident.has_escape = FALSE;
- s->token.u.ident.is_reserved = FALSE;
- s->token.val = TOK_IDENT;
- break;
- case '+':
- if (!s->ext_json)
- goto def_token;
- goto parse_number;
- case '.':
- if (s->ext_json && is_digit(p[1]))
- goto parse_number;
- else
- goto def_token;
- case '-':
- case '0':
- case '1': case '2': case '3': case '4':
- case '5': case '6': case '7': case '8':
- case '9':
- /* number */
- parse_number:
- if (json_parse_number(s, &p))
- goto fail;
- break;
- default:
- if (c >= 128) {
- js_parse_error(s, "unexpected character");
- goto fail;
- }
- def_token:
- s->token.val = c;
- p++;
- break;
- }
- s->buf_ptr = p;
- // dump_token(s, &s->token);
- return 0;
- fail:
- s->token.val = TOK_ERROR;
- return -1;
- }
- static int match_identifier(const uint8_t *p, const char *s) {
- uint32_t c;
- while (*s) {
- if ((uint8_t)*s++ != *p++)
- return 0;
- }
- c = *p;
- if (c >= 128)
- c = unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p);
- return !lre_js_is_ident_next(c);
- }
- /* simple_next_token() is used to check for the next token in simple cases.
- It is only used for ':' and '=>', 'let' or 'function' look-ahead.
- (*pp) is only set if TOK_IMPORT is returned for JS_DetectModule()
- Whitespace and comments are skipped correctly.
- Then the next token is analyzed, only for specific words.
- Return values:
- - '\n' if !no_line_terminator
- - TOK_ARROW, TOK_IN, TOK_IMPORT, TOK_OF, TOK_EXPORT, TOK_FUNCTION
- - TOK_IDENT is returned for other identifiers and keywords
- - otherwise the next character or unicode codepoint is returned.
- */
- static int simple_next_token(const uint8_t **pp, BOOL no_line_terminator)
- {
- const uint8_t *p;
- uint32_t c;
- /* skip spaces and comments */
- p = *pp;
- for (;;) {
- switch(c = *p++) {
- case '\r':
- case '\n':
- if (no_line_terminator)
- return '\n';
- continue;
- case ' ':
- case '\t':
- case '\v':
- case '\f':
- continue;
- case '/':
- if (*p == '/') {
- if (no_line_terminator)
- return '\n';
- while (*p && *p != '\r' && *p != '\n')
- p++;
- continue;
- }
- if (*p == '*') {
- while (*++p) {
- if ((*p == '\r' || *p == '\n') && no_line_terminator)
- return '\n';
- if (*p == '*' && p[1] == '/') {
- p += 2;
- break;
- }
- }
- continue;
- }
- break;
- case '=':
- if (*p == '>')
- return TOK_ARROW;
- break;
- case 'i':
- if (match_identifier(p, "n"))
- return TOK_IN;
- if (match_identifier(p, "mport")) {
- *pp = p + 5;
- return TOK_IMPORT;
- }
- return TOK_IDENT;
- case 'o':
- if (match_identifier(p, "f"))
- return TOK_OF;
- return TOK_IDENT;
- case 'e':
- if (match_identifier(p, "xport"))
- return TOK_EXPORT;
- return TOK_IDENT;
- case 'f':
- if (match_identifier(p, "unction"))
- return TOK_FUNCTION;
- return TOK_IDENT;
- case '\\':
- if (*p == 'u') {
- if (lre_js_is_ident_first(lre_parse_escape(&p, TRUE)))
- return TOK_IDENT;
- }
- break;
- default:
- if (c >= 128) {
- c = unicode_from_utf8(p - 1, UTF8_CHAR_LEN_MAX, &p);
- if (no_line_terminator && (c == CP_PS || c == CP_LS))
- return '\n';
- }
- if (lre_is_space(c))
- continue;
- if (lre_js_is_ident_first(c))
- return TOK_IDENT;
- break;
- }
- return c;
- }
- }
- static int peek_token(JSParseState *s, BOOL no_line_terminator)
- {
- const uint8_t *p = s->buf_ptr;
- return simple_next_token(&p, no_line_terminator);
- }
- static void skip_shebang(const uint8_t **pp, const uint8_t *buf_end)
- {
- const uint8_t *p = *pp;
- int c;
- if (p[0] == '#' && p[1] == '!') {
- p += 2;
- while (p < buf_end) {
- if (*p == '\n' || *p == '\r') {
- break;
- } else if (*p >= 0x80) {
- c = unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p);
- if (c == CP_LS || c == CP_PS) {
- break;
- } else if (c == -1) {
- p++; /* skip invalid UTF-8 */
- }
- } else {
- p++;
- }
- }
- *pp = p;
- }
- }
- /* return true if 'input' contains the source of a module
- (heuristic). 'input' must be a zero terminated.
- Heuristic: skip comments and expect 'import' keyword not followed
- by '(' or '.' or export keyword.
- */
- BOOL JS_DetectModule(const char *input, size_t input_len)
- {
- const uint8_t *p = (const uint8_t *)input;
- int tok;
- skip_shebang(&p, p + input_len);
- switch(simple_next_token(&p, FALSE)) {
- case TOK_IMPORT:
- tok = simple_next_token(&p, FALSE);
- return (tok != '.' && tok != '(');
- case TOK_EXPORT:
- return TRUE;
- default:
- return FALSE;
- }
- }
- static inline int get_prev_opcode(JSFunctionDef *fd) {
- if (fd->last_opcode_pos < 0 || dbuf_error(&fd->byte_code))
- return OP_invalid;
- else
- return fd->byte_code.buf[fd->last_opcode_pos];
- }
- static BOOL js_is_live_code(JSParseState *s) {
- switch (get_prev_opcode(s->cur_func)) {
- case OP_tail_call:
- case OP_tail_call_method:
- case OP_return:
- case OP_return_undef:
- case OP_return_async:
- case OP_throw:
- case OP_throw_error:
- case OP_goto:
- #if SHORT_OPCODES
- case OP_goto8:
- case OP_goto16:
- #endif
- case OP_ret:
- return FALSE;
- default:
- return TRUE;
- }
- }
- static void emit_u8(JSParseState *s, uint8_t val)
- {
- dbuf_putc(&s->cur_func->byte_code, val);
- }
- static void emit_u16(JSParseState *s, uint16_t val)
- {
- dbuf_put_u16(&s->cur_func->byte_code, val);
- }
- static void emit_u32(JSParseState *s, uint32_t val)
- {
- dbuf_put_u32(&s->cur_func->byte_code, val);
- }
- static void emit_source_pos(JSParseState *s, const uint8_t *source_ptr)
- {
- JSFunctionDef *fd = s->cur_func;
- DynBuf *bc = &fd->byte_code;
- if (unlikely(fd->last_opcode_source_ptr != source_ptr)) {
- dbuf_putc(bc, OP_line_num);
- dbuf_put_u32(bc, source_ptr - s->buf_start);
- fd->last_opcode_source_ptr = source_ptr;
- }
- }
- static void emit_op(JSParseState *s, uint8_t val)
- {
- JSFunctionDef *fd = s->cur_func;
- DynBuf *bc = &fd->byte_code;
- fd->last_opcode_pos = bc->size;
- dbuf_putc(bc, val);
- }
- static void emit_atom(JSParseState *s, JSAtom name)
- {
- DynBuf *bc = &s->cur_func->byte_code;
- if (dbuf_claim(bc, 4))
- return; /* not enough memory : don't duplicate the atom */
- put_u32(bc->buf + bc->size, JS_DupAtom(s->ctx, name));
- bc->size += 4;
- }
- static int update_label(JSFunctionDef *s, int label, int delta)
- {
- LabelSlot *ls;
- assert(label >= 0 && label < s->label_count);
- ls = &s->label_slots[label];
- ls->ref_count += delta;
- assert(ls->ref_count >= 0);
- return ls->ref_count;
- }
- static int new_label_fd(JSFunctionDef *fd)
- {
- int label;
- LabelSlot *ls;
- if (js_resize_array(fd->ctx, (void *)&fd->label_slots,
- sizeof(fd->label_slots[0]),
- &fd->label_size, fd->label_count + 1))
- return -1;
- label = fd->label_count++;
- ls = &fd->label_slots[label];
- ls->ref_count = 0;
- ls->pos = -1;
- ls->pos2 = -1;
- ls->addr = -1;
- ls->first_reloc = NULL;
- return label;
- }
- static int new_label(JSParseState *s)
- {
- int label;
- label = new_label_fd(s->cur_func);
- if (unlikely(label < 0)) {
- dbuf_set_error(&s->cur_func->byte_code);
- }
- return label;
- }
- /* don't update the last opcode and don't emit line number info */
- static void emit_label_raw(JSParseState *s, int label)
- {
- emit_u8(s, OP_label);
- emit_u32(s, label);
- s->cur_func->label_slots[label].pos = s->cur_func->byte_code.size;
- }
- /* return the label ID offset */
- static int emit_label(JSParseState *s, int label)
- {
- if (label >= 0) {
- emit_op(s, OP_label);
- emit_u32(s, label);
- s->cur_func->label_slots[label].pos = s->cur_func->byte_code.size;
- return s->cur_func->byte_code.size - 4;
- } else {
- return -1;
- }
- }
- /* return label or -1 if dead code */
- static int emit_goto(JSParseState *s, int opcode, int label)
- {
- if (js_is_live_code(s)) {
- if (label < 0) {
- label = new_label(s);
- if (label < 0)
- return -1;
- }
- emit_op(s, opcode);
- emit_u32(s, label);
- s->cur_func->label_slots[label].ref_count++;
- return label;
- }
- return -1;
- }
- /* return the constant pool index. 'val' is not duplicated. */
- static int cpool_add(JSParseState *s, JSValue val)
- {
- JSFunctionDef *fd = s->cur_func;
- if (js_resize_array(s->ctx, (void *)&fd->cpool, sizeof(fd->cpool[0]),
- &fd->cpool_size, fd->cpool_count + 1)) {
- JS_FreeValue(s->ctx, val);
- return -1;
- }
- fd->cpool[fd->cpool_count++] = val;
- return fd->cpool_count - 1;
- }
- static __exception int emit_push_const(JSParseState *s, JSValueConst val,
- BOOL as_atom)
- {
- int idx;
- if (JS_VALUE_GET_TAG(val) == JS_TAG_STRING && as_atom) {
- JSAtom atom;
- /* warning: JS_NewAtomStr frees the string value */
- JS_DupValue(s->ctx, val);
- atom = JS_NewAtomStr(s->ctx, JS_VALUE_GET_STRING(val));
- if (atom != JS_ATOM_NULL && !__JS_AtomIsTaggedInt(atom)) {
- emit_op(s, OP_push_atom_value);
- emit_u32(s, atom);
- return 0;
- }
- }
- idx = cpool_add(s, JS_DupValue(s->ctx, val));
- if (idx < 0)
- return -1;
- emit_op(s, OP_push_const);
- emit_u32(s, idx);
- return 0;
- }
- /* return the variable index or -1 if not found,
- add ARGUMENT_VAR_OFFSET for argument variables */
- static int find_arg(JSContext *ctx, JSFunctionDef *fd, JSAtom name)
- {
- int i;
- for(i = fd->arg_count; i-- > 0;) {
- if (fd->args[i].var_name == name)
- return i | ARGUMENT_VAR_OFFSET;
- }
- return -1;
- }
- static int find_var(JSContext *ctx, JSFunctionDef *fd, JSAtom name)
- {
- int i;
- for(i = fd->var_count; i-- > 0;) {
- if (fd->vars[i].var_name == name && fd->vars[i].scope_level == 0)
- return i;
- }
- return find_arg(ctx, fd, name);
- }
- /* find a variable declaration in a given scope */
- static int find_var_in_scope(JSContext *ctx, JSFunctionDef *fd,
- JSAtom name, int scope_level)
- {
- int scope_idx;
- for(scope_idx = fd->scopes[scope_level].first; scope_idx >= 0;
- scope_idx = fd->vars[scope_idx].scope_next) {
- if (fd->vars[scope_idx].scope_level != scope_level)
- break;
- if (fd->vars[scope_idx].var_name == name)
- return scope_idx;
- }
- return -1;
- }
- /* return true if scope == parent_scope or if scope is a child of
- parent_scope */
- static BOOL is_child_scope(JSContext *ctx, JSFunctionDef *fd,
- int scope, int parent_scope)
- {
- while (scope >= 0) {
- if (scope == parent_scope)
- return TRUE;
- scope = fd->scopes[scope].parent;
- }
- return FALSE;
- }
- /* find a 'var' declaration in the same scope or a child scope */
- static int find_var_in_child_scope(JSContext *ctx, JSFunctionDef *fd,
- JSAtom name, int scope_level)
- {
- int i;
- for(i = 0; i < fd->var_count; i++) {
- JSVarDef *vd = &fd->vars[i];
- if (vd->var_name == name && vd->scope_level == 0) {
- if (is_child_scope(ctx, fd, vd->scope_next,
- scope_level))
- return i;
- }
- }
- return -1;
- }
- static JSGlobalVar *find_global_var(JSFunctionDef *fd, JSAtom name)
- {
- int i;
- for(i = 0; i < fd->global_var_count; i++) {
- JSGlobalVar *hf = &fd->global_vars[i];
- if (hf->var_name == name)
- return hf;
- }
- return NULL;
- }
- static JSGlobalVar *find_lexical_global_var(JSFunctionDef *fd, JSAtom name)
- {
- JSGlobalVar *hf = find_global_var(fd, name);
- if (hf && hf->is_lexical)
- return hf;
- else
- return NULL;
- }
- static int find_lexical_decl(JSContext *ctx, JSFunctionDef *fd, JSAtom name,
- int scope_idx, BOOL check_catch_var)
- {
- while (scope_idx >= 0) {
- JSVarDef *vd = &fd->vars[scope_idx];
- if (vd->var_name == name &&
- (vd->is_lexical || (vd->var_kind == JS_VAR_CATCH &&
- check_catch_var)))
- return scope_idx;
- scope_idx = vd->scope_next;
- }
- if (fd->is_eval && fd->eval_type == JS_EVAL_TYPE_GLOBAL) {
- if (find_lexical_global_var(fd, name))
- return GLOBAL_VAR_OFFSET;
- }
- return -1;
- }
- static int push_scope(JSParseState *s) {
- if (s->cur_func) {
- JSFunctionDef *fd = s->cur_func;
- int scope = fd->scope_count;
- /* XXX: should check for scope overflow */
- if ((fd->scope_count + 1) > fd->scope_size) {
- int new_size;
- size_t slack;
- JSVarScope *new_buf;
- /* XXX: potential arithmetic overflow */
- new_size = max_int(fd->scope_count + 1, fd->scope_size * 3 / 2);
- if (fd->scopes == fd->def_scope_array) {
- new_buf = js_realloc2(s->ctx, NULL, new_size * sizeof(*fd->scopes), &slack);
- if (!new_buf)
- return -1;
- memcpy(new_buf, fd->scopes, fd->scope_count * sizeof(*fd->scopes));
- } else {
- new_buf = js_realloc2(s->ctx, fd->scopes, new_size * sizeof(*fd->scopes), &slack);
- if (!new_buf)
- return -1;
- }
- new_size += slack / sizeof(*new_buf);
- fd->scopes = new_buf;
- fd->scope_size = new_size;
- }
- fd->scope_count++;
- fd->scopes[scope].parent = fd->scope_level;
- fd->scopes[scope].first = fd->scope_first;
- emit_op(s, OP_enter_scope);
- emit_u16(s, scope);
- return fd->scope_level = scope;
- }
- return 0;
- }
- static int get_first_lexical_var(JSFunctionDef *fd, int scope)
- {
- while (scope >= 0) {
- int scope_idx = fd->scopes[scope].first;
- if (scope_idx >= 0)
- return scope_idx;
- scope = fd->scopes[scope].parent;
- }
- return -1;
- }
- static void pop_scope(JSParseState *s) {
- if (s->cur_func) {
- /* disable scoped variables */
- JSFunctionDef *fd = s->cur_func;
- int scope = fd->scope_level;
- emit_op(s, OP_leave_scope);
- emit_u16(s, scope);
- fd->scope_level = fd->scopes[scope].parent;
- fd->scope_first = get_first_lexical_var(fd, fd->scope_level);
- }
- }
- static void close_scopes(JSParseState *s, int scope, int scope_stop)
- {
- while (scope > scope_stop) {
- emit_op(s, OP_leave_scope);
- emit_u16(s, scope);
- scope = s->cur_func->scopes[scope].parent;
- }
- }
- /* return the variable index or -1 if error */
- static int add_var(JSContext *ctx, JSFunctionDef *fd, JSAtom name)
- {
- JSVarDef *vd;
- /* the local variable indexes are currently stored on 16 bits */
- if (fd->var_count >= JS_MAX_LOCAL_VARS) {
- JS_ThrowInternalError(ctx, "too many local variables");
- return -1;
- }
- if (js_resize_array(ctx, (void **)&fd->vars, sizeof(fd->vars[0]),
- &fd->var_size, fd->var_count + 1))
- return -1;
- vd = &fd->vars[fd->var_count++];
- memset(vd, 0, sizeof(*vd));
- vd->var_name = JS_DupAtom(ctx, name);
- vd->func_pool_idx = -1;
- return fd->var_count - 1;
- }
- static int add_scope_var(JSContext *ctx, JSFunctionDef *fd, JSAtom name,
- JSVarKindEnum var_kind)
- {
- int idx = add_var(ctx, fd, name);
- if (idx >= 0) {
- JSVarDef *vd = &fd->vars[idx];
- vd->var_kind = var_kind;
- vd->scope_level = fd->scope_level;
- vd->scope_next = fd->scope_first;
- fd->scopes[fd->scope_level].first = idx;
- fd->scope_first = idx;
- }
- return idx;
- }
- static int add_func_var(JSContext *ctx, JSFunctionDef *fd, JSAtom name)
- {
- int idx = fd->func_var_idx;
- if (idx < 0 && (idx = add_var(ctx, fd, name)) >= 0) {
- fd->func_var_idx = idx;
- fd->vars[idx].var_kind = JS_VAR_FUNCTION_NAME;
- if (fd->js_mode & JS_MODE_STRICT)
- fd->vars[idx].is_const = TRUE;
- }
- return idx;
- }
- static int add_arguments_var(JSContext *ctx, JSFunctionDef *fd)
- {
- int idx = fd->arguments_var_idx;
- if (idx < 0 && (idx = add_var(ctx, fd, JS_ATOM_arguments)) >= 0) {
- fd->arguments_var_idx = idx;
- }
- return idx;
- }
- /* add an argument definition in the argument scope. Only needed when
- "eval()" may be called in the argument scope. Return 0 if OK. */
- static int add_arguments_arg(JSContext *ctx, JSFunctionDef *fd)
- {
- int idx;
- if (fd->arguments_arg_idx < 0) {
- idx = find_var_in_scope(ctx, fd, JS_ATOM_arguments, ARG_SCOPE_INDEX);
- if (idx < 0) {
- /* XXX: the scope links are not fully updated. May be an
- issue if there are child scopes of the argument
- scope */
- idx = add_var(ctx, fd, JS_ATOM_arguments);
- if (idx < 0)
- return -1;
- fd->vars[idx].scope_next = fd->scopes[ARG_SCOPE_INDEX].first;
- fd->scopes[ARG_SCOPE_INDEX].first = idx;
- fd->vars[idx].scope_level = ARG_SCOPE_INDEX;
- fd->vars[idx].is_lexical = TRUE;
- fd->arguments_arg_idx = idx;
- }
- }
- return 0;
- }
- static int add_arg(JSContext *ctx, JSFunctionDef *fd, JSAtom name)
- {
- JSVarDef *vd;
- /* the local variable indexes are currently stored on 16 bits */
- if (fd->arg_count >= JS_MAX_LOCAL_VARS) {
- JS_ThrowInternalError(ctx, "too many arguments");
- return -1;
- }
- if (js_resize_array(ctx, (void **)&fd->args, sizeof(fd->args[0]),
- &fd->arg_size, fd->arg_count + 1))
- return -1;
- vd = &fd->args[fd->arg_count++];
- memset(vd, 0, sizeof(*vd));
- vd->var_name = JS_DupAtom(ctx, name);
- vd->func_pool_idx = -1;
- return fd->arg_count - 1;
- }
- /* add a global variable definition */
- static JSGlobalVar *add_global_var(JSContext *ctx, JSFunctionDef *s,
- JSAtom name)
- {
- JSGlobalVar *hf;
- if (js_resize_array(ctx, (void **)&s->global_vars,
- sizeof(s->global_vars[0]),
- &s->global_var_size, s->global_var_count + 1))
- return NULL;
- hf = &s->global_vars[s->global_var_count++];
- hf->cpool_idx = -1;
- hf->force_init = FALSE;
- hf->is_lexical = FALSE;
- hf->is_const = FALSE;
- hf->scope_level = s->scope_level;
- hf->var_name = JS_DupAtom(ctx, name);
- return hf;
- }
- typedef enum {
- JS_VAR_DEF_WITH,
- JS_VAR_DEF_LET,
- JS_VAR_DEF_CONST,
- JS_VAR_DEF_FUNCTION_DECL, /* function declaration */
- JS_VAR_DEF_NEW_FUNCTION_DECL, /* async/generator function declaration */
- JS_VAR_DEF_CATCH,
- JS_VAR_DEF_VAR,
- } JSVarDefEnum;
- static int define_var(JSParseState *s, JSFunctionDef *fd, JSAtom name,
- JSVarDefEnum var_def_type)
- {
- JSContext *ctx = s->ctx;
- JSVarDef *vd;
- int idx;
- switch (var_def_type) {
- case JS_VAR_DEF_WITH:
- idx = add_scope_var(ctx, fd, name, JS_VAR_NORMAL);
- break;
- case JS_VAR_DEF_LET:
- case JS_VAR_DEF_CONST:
- case JS_VAR_DEF_FUNCTION_DECL:
- case JS_VAR_DEF_NEW_FUNCTION_DECL:
- idx = find_lexical_decl(ctx, fd, name, fd->scope_first, TRUE);
- if (idx >= 0) {
- if (idx < GLOBAL_VAR_OFFSET) {
- if (fd->vars[idx].scope_level == fd->scope_level) {
- /* same scope: in non strict mode, functions
- can be redefined (annex B.3.3.4). */
- if (!(!(fd->js_mode & JS_MODE_STRICT) &&
- var_def_type == JS_VAR_DEF_FUNCTION_DECL &&
- fd->vars[idx].var_kind == JS_VAR_FUNCTION_DECL)) {
- goto redef_lex_error;
- }
- } else if (fd->vars[idx].var_kind == JS_VAR_CATCH && (fd->vars[idx].scope_level + 2) == fd->scope_level) {
- goto redef_lex_error;
- }
- } else {
- if (fd->scope_level == fd->body_scope) {
- redef_lex_error:
- /* redefining a scoped var in the same scope: error */
- return js_parse_error(s, "invalid redefinition of lexical identifier");
- }
- }
- }
- if (var_def_type != JS_VAR_DEF_FUNCTION_DECL &&
- var_def_type != JS_VAR_DEF_NEW_FUNCTION_DECL &&
- fd->scope_level == fd->body_scope &&
- find_arg(ctx, fd, name) >= 0) {
- /* lexical variable redefines a parameter name */
- return js_parse_error(s, "invalid redefinition of parameter name");
- }
- if (find_var_in_child_scope(ctx, fd, name, fd->scope_level) >= 0) {
- return js_parse_error(s, "invalid redefinition of a variable");
- }
- if (fd->is_global_var) {
- JSGlobalVar *hf;
- hf = find_global_var(fd, name);
- if (hf && is_child_scope(ctx, fd, hf->scope_level,
- fd->scope_level)) {
- return js_parse_error(s, "invalid redefinition of global identifier");
- }
- }
- if (fd->is_eval &&
- (fd->eval_type == JS_EVAL_TYPE_GLOBAL ||
- fd->eval_type == JS_EVAL_TYPE_MODULE) &&
- fd->scope_level == fd->body_scope) {
- JSGlobalVar *hf;
- hf = add_global_var(s->ctx, fd, name);
- if (!hf)
- return -1;
- hf->is_lexical = TRUE;
- hf->is_const = (var_def_type == JS_VAR_DEF_CONST);
- idx = GLOBAL_VAR_OFFSET;
- } else {
- JSVarKindEnum var_kind;
- if (var_def_type == JS_VAR_DEF_FUNCTION_DECL)
- var_kind = JS_VAR_FUNCTION_DECL;
- else if (var_def_type == JS_VAR_DEF_NEW_FUNCTION_DECL)
- var_kind = JS_VAR_NEW_FUNCTION_DECL;
- else
- var_kind = JS_VAR_NORMAL;
- idx = add_scope_var(ctx, fd, name, var_kind);
- if (idx >= 0) {
- vd = &fd->vars[idx];
- vd->is_lexical = 1;
- vd->is_const = (var_def_type == JS_VAR_DEF_CONST);
- }
- }
- break;
- case JS_VAR_DEF_CATCH:
- idx = add_scope_var(ctx, fd, name, JS_VAR_CATCH);
- break;
- case JS_VAR_DEF_VAR:
- if (find_lexical_decl(ctx, fd, name, fd->scope_first,
- FALSE) >= 0) {
- invalid_lexical_redefinition:
- /* error to redefine a var that inside a lexical scope */
- return js_parse_error(s, "invalid redefinition of lexical identifier");
- }
- if (fd->is_global_var) {
- JSGlobalVar *hf;
- hf = find_global_var(fd, name);
- if (hf && hf->is_lexical && hf->scope_level == fd->scope_level &&
- fd->eval_type == JS_EVAL_TYPE_MODULE) {
- goto invalid_lexical_redefinition;
- }
- hf = add_global_var(s->ctx, fd, name);
- if (!hf)
- return -1;
- idx = GLOBAL_VAR_OFFSET;
- } else {
- /* if the variable already exists, don't add it again */
- idx = find_var(ctx, fd, name);
- if (idx >= 0)
- break;
- idx = add_var(ctx, fd, name);
- if (idx >= 0) {
- if (name == JS_ATOM_arguments && fd->has_arguments_binding)
- fd->arguments_var_idx = idx;
- fd->vars[idx].scope_next = fd->scope_level;
- }
- }
- break;
- default:
- abort();
- }
- return idx;
- }
- /* add a private field variable in the current scope */
- static int add_private_class_field(JSParseState *s, JSFunctionDef *fd,
- JSAtom name, JSVarKindEnum var_kind, BOOL is_static)
- {
- JSContext *ctx = s->ctx;
- JSVarDef *vd;
- int idx;
- idx = add_scope_var(ctx, fd, name, var_kind);
- if (idx < 0)
- return idx;
- vd = &fd->vars[idx];
- vd->is_lexical = 1;
- vd->is_const = 1;
- vd->is_static_private = is_static;
- return idx;
- }
- static __exception int js_parse_expr(JSParseState *s);
- static __exception int js_parse_function_decl(JSParseState *s,
- JSParseFunctionEnum func_type,
- JSFunctionKindEnum func_kind,
- JSAtom func_name, const uint8_t *ptr);
- static JSFunctionDef *js_parse_function_class_fields_init(JSParseState *s);
- static __exception int js_parse_function_decl2(JSParseState *s,
- JSParseFunctionEnum func_type,
- JSFunctionKindEnum func_kind,
- JSAtom func_name,
- const uint8_t *ptr,
- JSParseExportEnum export_flag,
- JSFunctionDef **pfd);
- static __exception int js_parse_assign_expr2(JSParseState *s, int parse_flags);
- static __exception int js_parse_assign_expr(JSParseState *s);
- static __exception int js_parse_unary(JSParseState *s, int parse_flags);
- static void push_break_entry(JSFunctionDef *fd, BlockEnv *be,
- JSAtom label_name,
- int label_break, int label_cont,
- int drop_count);
- static void pop_break_entry(JSFunctionDef *fd);
- static JSExportEntry *add_export_entry(JSParseState *s, JSModuleDef *m,
- JSAtom local_name, JSAtom export_name,
- JSExportTypeEnum export_type);
- /* Note: all the fields are already sealed except length */
- static int seal_template_obj(JSContext *ctx, JSValueConst obj)
- {
- JSObject *p;
- JSShapeProperty *prs;
- p = JS_VALUE_GET_OBJ(obj);
- prs = find_own_property1(p, JS_ATOM_length);
- if (prs) {
- if (js_update_property_flags(ctx, p, &prs,
- prs->flags & ~(JS_PROP_CONFIGURABLE | JS_PROP_WRITABLE)))
- return -1;
- }
- p->extensible = FALSE;
- return 0;
- }
- static __exception int js_parse_template(JSParseState *s, int call, int *argc)
- {
- JSContext *ctx = s->ctx;
- JSValue raw_array, template_object;
- JSToken cooked;
- int depth, ret;
- raw_array = JS_UNDEFINED; /* avoid warning */
- template_object = JS_UNDEFINED; /* avoid warning */
- if (call) {
- /* Create a template object: an array of cooked strings */
- /* Create an array of raw strings and store it to the raw property */
- template_object = JS_NewArray(ctx);
- if (JS_IsException(template_object))
- return -1;
- // pool_idx = s->cur_func->cpool_count;
- ret = emit_push_const(s, template_object, 0);
- JS_FreeValue(ctx, template_object);
- if (ret)
- return -1;
- raw_array = JS_NewArray(ctx);
- if (JS_IsException(raw_array))
- return -1;
- if (JS_DefinePropertyValue(ctx, template_object, JS_ATOM_raw,
- raw_array, JS_PROP_THROW) < 0) {
- return -1;
- }
- }
- depth = 0;
- while (s->token.val == TOK_TEMPLATE) {
- const uint8_t *p = s->token.ptr + 1;
- cooked = s->token;
- if (call) {
- if (JS_DefinePropertyValueUint32(ctx, raw_array, depth,
- JS_DupValue(ctx, s->token.u.str.str),
- JS_PROP_ENUMERABLE | JS_PROP_THROW) < 0) {
- return -1;
- }
- /* re-parse the string with escape sequences but do not throw a
- syntax error if it contains invalid sequences
- */
- if (js_parse_string(s, '`', FALSE, p, &cooked, &p)) {
- cooked.u.str.str = JS_UNDEFINED;
- }
- if (JS_DefinePropertyValueUint32(ctx, template_object, depth,
- cooked.u.str.str,
- JS_PROP_ENUMERABLE | JS_PROP_THROW) < 0) {
- return -1;
- }
- } else {
- JSString *str;
- /* re-parse the string with escape sequences and throw a
- syntax error if it contains invalid sequences
- */
- JS_FreeValue(ctx, s->token.u.str.str);
- s->token.u.str.str = JS_UNDEFINED;
- if (js_parse_string(s, '`', TRUE, p, &cooked, &p))
- return -1;
- str = JS_VALUE_GET_STRING(cooked.u.str.str);
- if (str->len != 0 || depth == 0) {
- ret = emit_push_const(s, cooked.u.str.str, 1);
- JS_FreeValue(s->ctx, cooked.u.str.str);
- if (ret)
- return -1;
- if (depth == 0) {
- if (s->token.u.str.sep == '`')
- goto done1;
- emit_op(s, OP_get_field2);
- emit_atom(s, JS_ATOM_concat);
- }
- depth++;
- } else {
- JS_FreeValue(s->ctx, cooked.u.str.str);
- }
- }
- if (s->token.u.str.sep == '`')
- goto done;
- if (next_token(s))
- return -1;
- if (js_parse_expr(s))
- return -1;
- depth++;
- if (s->token.val != '}') {
- return js_parse_error(s, "expected '}' after template expression");
- }
- /* XXX: should convert to string at this stage? */
- free_token(s, &s->token);
- /* Resume TOK_TEMPLATE parsing (s->token.line_num and
- * s->token.ptr are OK) */
- s->got_lf = FALSE;
- if (js_parse_template_part(s, s->buf_ptr))
- return -1;
- }
- return js_parse_expect(s, TOK_TEMPLATE);
- done:
- if (call) {
- /* Seal the objects */
- seal_template_obj(ctx, raw_array);
- seal_template_obj(ctx, template_object);
- *argc = depth + 1;
- } else {
- emit_op(s, OP_call_method);
- emit_u16(s, depth - 1);
- }
- done1:
- return next_token(s);
- }
- #define PROP_TYPE_IDENT 0
- #define PROP_TYPE_VAR 1
- #define PROP_TYPE_GET 2
- #define PROP_TYPE_SET 3
- #define PROP_TYPE_STAR 4
- #define PROP_TYPE_ASYNC 5
- #define PROP_TYPE_ASYNC_STAR 6
- #define PROP_TYPE_PRIVATE (1 << 4)
- static BOOL token_is_ident(int tok)
- {
- /* Accept keywords and reserved words as property names */
- return (tok == TOK_IDENT ||
- (tok >= TOK_FIRST_KEYWORD &&
- tok <= TOK_LAST_KEYWORD));
- }
- /* if the property is an expression, name = JS_ATOM_NULL */
- static int __exception js_parse_property_name(JSParseState *s,
- JSAtom *pname,
- BOOL allow_method, BOOL allow_var,
- BOOL allow_private)
- {
- int is_private = 0;
- BOOL is_non_reserved_ident;
- JSAtom name;
- int prop_type;
- prop_type = PROP_TYPE_IDENT;
- if (allow_method) {
- /* if allow_private is true (for class field parsing) and
- get/set is following by ';' (or LF with ASI), then it
- is a field name */
- if ((token_is_pseudo_keyword(s, JS_ATOM_get) ||
- token_is_pseudo_keyword(s, JS_ATOM_set)) &&
- (!allow_private || peek_token(s, TRUE) != '\n')) {
- /* get x(), set x() */
- name = JS_DupAtom(s->ctx, s->token.u.ident.atom);
- if (next_token(s))
- goto fail1;
- if (s->token.val == ':' || s->token.val == ',' ||
- s->token.val == '}' || s->token.val == '(' ||
- s->token.val == '=' ||
- (s->token.val == ';' && allow_private)) {
- is_non_reserved_ident = TRUE;
- goto ident_found;
- }
- prop_type = PROP_TYPE_GET + (name == JS_ATOM_set);
- JS_FreeAtom(s->ctx, name);
- } else if (s->token.val == '*') {
- if (next_token(s))
- goto fail;
- prop_type = PROP_TYPE_STAR;
- } else if (token_is_pseudo_keyword(s, JS_ATOM_async) &&
- peek_token(s, TRUE) != '\n') {
- name = JS_DupAtom(s->ctx, s->token.u.ident.atom);
- if (next_token(s))
- goto fail1;
- if (s->token.val == ':' || s->token.val == ',' ||
- s->token.val == '}' || s->token.val == '(' ||
- s->token.val == '=') {
- is_non_reserved_ident = TRUE;
- goto ident_found;
- }
- JS_FreeAtom(s->ctx, name);
- if (s->token.val == '*') {
- if (next_token(s))
- goto fail;
- prop_type = PROP_TYPE_ASYNC_STAR;
- } else {
- prop_type = PROP_TYPE_ASYNC;
- }
- }
- }
- if (token_is_ident(s->token.val)) {
- /* variable can only be a non-reserved identifier */
- is_non_reserved_ident =
- (s->token.val == TOK_IDENT && !s->token.u.ident.is_reserved);
- /* keywords and reserved words have a valid atom */
- name = JS_DupAtom(s->ctx, s->token.u.ident.atom);
- if (next_token(s))
- goto fail1;
- ident_found:
- if (is_non_reserved_ident &&
- prop_type == PROP_TYPE_IDENT && allow_var) {
- if (!(s->token.val == ':' ||
- (s->token.val == '(' && allow_method))) {
- prop_type = PROP_TYPE_VAR;
- }
- }
- } else if (s->token.val == TOK_STRING) {
- name = JS_ValueToAtom(s->ctx, s->token.u.str.str);
- if (name == JS_ATOM_NULL)
- goto fail;
- if (next_token(s))
- goto fail1;
- } else if (s->token.val == TOK_NUMBER) {
- JSValue val;
- val = s->token.u.num.val;
- name = JS_ValueToAtom(s->ctx, val);
- if (name == JS_ATOM_NULL)
- goto fail;
- if (next_token(s))
- goto fail1;
- } else if (s->token.val == '[') {
- if (next_token(s))
- goto fail;
- if (js_parse_assign_expr(s))
- goto fail;
- if (js_parse_expect(s, ']'))
- goto fail;
- name = JS_ATOM_NULL;
- } else if (s->token.val == TOK_PRIVATE_NAME && allow_private) {
- name = JS_DupAtom(s->ctx, s->token.u.ident.atom);
- if (next_token(s))
- goto fail1;
- is_private = PROP_TYPE_PRIVATE;
- } else {
- goto invalid_prop;
- }
- if (prop_type != PROP_TYPE_IDENT && prop_type != PROP_TYPE_VAR &&
- s->token.val != '(') {
- JS_FreeAtom(s->ctx, name);
- invalid_prop:
- js_parse_error(s, "invalid property name");
- goto fail;
- }
- *pname = name;
- return prop_type | is_private;
- fail1:
- JS_FreeAtom(s->ctx, name);
- fail:
- *pname = JS_ATOM_NULL;
- return -1;
- }
- typedef struct JSParsePos {
- BOOL got_lf;
- const uint8_t *ptr;
- } JSParsePos;
- static int js_parse_get_pos(JSParseState *s, JSParsePos *sp)
- {
- sp->ptr = s->token.ptr;
- sp->got_lf = s->got_lf;
- return 0;
- }
- static __exception int js_parse_seek_token(JSParseState *s, const JSParsePos *sp)
- {
- s->buf_ptr = sp->ptr;
- s->got_lf = sp->got_lf;
- return next_token(s);
- }
- /* return TRUE if a regexp literal is allowed after this token */
- static BOOL is_regexp_allowed(int tok)
- {
- switch (tok) {
- case TOK_NUMBER:
- case TOK_STRING:
- case TOK_REGEXP:
- case TOK_DEC:
- case TOK_INC:
- case TOK_NULL:
- case TOK_FALSE:
- case TOK_TRUE:
- case TOK_THIS:
- case ')':
- case ']':
- case '}': /* XXX: regexp may occur after */
- case TOK_IDENT:
- return FALSE;
- default:
- return TRUE;
- }
- }
- #define SKIP_HAS_SEMI (1 << 0)
- #define SKIP_HAS_ELLIPSIS (1 << 1)
- #define SKIP_HAS_ASSIGNMENT (1 << 2)
- static BOOL has_lf_in_range(const uint8_t *p1, const uint8_t *p2)
- {
- const uint8_t *tmp;
- if (p1 > p2) {
- tmp = p1;
- p1 = p2;
- p2 = tmp;
- }
- return (memchr(p1, '\n', p2 - p1) != NULL);
- }
- /* XXX: improve speed with early bailout */
- /* XXX: no longer works if regexps are present. Could use previous
- regexp parsing heuristics to handle most cases */
- static int js_parse_skip_parens_token(JSParseState *s, int *pbits, BOOL no_line_terminator)
- {
- char state[256];
- size_t level = 0;
- JSParsePos pos;
- int last_tok, tok = TOK_EOF;
- int c, tok_len, bits = 0;
- const uint8_t *last_token_ptr;
-
- /* protect from underflow */
- state[level++] = 0;
- js_parse_get_pos(s, &pos);
- last_tok = 0;
- for (;;) {
- switch(s->token.val) {
- case '(':
- case '[':
- case '{':
- if (level >= sizeof(state))
- goto done;
- state[level++] = s->token.val;
- break;
- case ')':
- if (state[--level] != '(')
- goto done;
- break;
- case ']':
- if (state[--level] != '[')
- goto done;
- break;
- case '}':
- c = state[--level];
- if (c == '`') {
- /* continue the parsing of the template */
- free_token(s, &s->token);
- /* Resume TOK_TEMPLATE parsing (s->token.line_num and
- * s->token.ptr are OK) */
- s->got_lf = FALSE;
- if (js_parse_template_part(s, s->buf_ptr))
- goto done;
- goto handle_template;
- } else if (c != '{') {
- goto done;
- }
- break;
- case TOK_TEMPLATE:
- handle_template:
- if (s->token.u.str.sep != '`') {
- /* '${' inside the template : closing '}' and continue
- parsing the template */
- if (level >= sizeof(state))
- goto done;
- state[level++] = '`';
- }
- break;
- case TOK_EOF:
- goto done;
- case ';':
- if (level == 2) {
- bits |= SKIP_HAS_SEMI;
- }
- break;
- case TOK_ELLIPSIS:
- if (level == 2) {
- bits |= SKIP_HAS_ELLIPSIS;
- }
- break;
- case '=':
- bits |= SKIP_HAS_ASSIGNMENT;
- break;
- case TOK_DIV_ASSIGN:
- tok_len = 2;
- goto parse_regexp;
- case '/':
- tok_len = 1;
- parse_regexp:
- if (is_regexp_allowed(last_tok)) {
- s->buf_ptr -= tok_len;
- if (js_parse_regexp(s)) {
- /* XXX: should clear the exception */
- goto done;
- }
- }
- break;
- }
- /* last_tok is only used to recognize regexps */
- if (s->token.val == TOK_IDENT &&
- (token_is_pseudo_keyword(s, JS_ATOM_of) ||
- token_is_pseudo_keyword(s, JS_ATOM_yield))) {
- last_tok = TOK_OF;
- } else {
- last_tok = s->token.val;
- }
- last_token_ptr = s->token.ptr;
- if (next_token(s)) {
- /* XXX: should clear the exception generated by next_token() */
- break;
- }
- if (level <= 1) {
- tok = s->token.val;
- if (token_is_pseudo_keyword(s, JS_ATOM_of))
- tok = TOK_OF;
- if (no_line_terminator && has_lf_in_range(last_token_ptr, s->token.ptr))
- tok = '\n';
- break;
- }
- }
- done:
- if (pbits) {
- *pbits = bits;
- }
- if (js_parse_seek_token(s, &pos))
- return -1;
- return tok;
- }
- static void set_object_name(JSParseState *s, JSAtom name)
- {
- JSFunctionDef *fd = s->cur_func;
- int opcode;
- opcode = get_prev_opcode(fd);
- if (opcode == OP_set_name) {
- /* XXX: should free atom after OP_set_name? */
- fd->byte_code.size = fd->last_opcode_pos;
- fd->last_opcode_pos = -1;
- emit_op(s, OP_set_name);
- emit_atom(s, name);
- } else if (opcode == OP_set_class_name) {
- int define_class_pos;
- JSAtom atom;
- define_class_pos = fd->last_opcode_pos + 1 -
- get_u32(fd->byte_code.buf + fd->last_opcode_pos + 1);
- assert(fd->byte_code.buf[define_class_pos] == OP_define_class);
- /* for consistency we free the previous atom which is
- JS_ATOM_empty_string */
- atom = get_u32(fd->byte_code.buf + define_class_pos + 1);
- JS_FreeAtom(s->ctx, atom);
- put_u32(fd->byte_code.buf + define_class_pos + 1,
- JS_DupAtom(s->ctx, name));
- fd->last_opcode_pos = -1;
- }
- }
- static void set_object_name_computed(JSParseState *s)
- {
- JSFunctionDef *fd = s->cur_func;
- int opcode;
- opcode = get_prev_opcode(fd);
- if (opcode == OP_set_name) {
- /* XXX: should free atom after OP_set_name? */
- fd->byte_code.size = fd->last_opcode_pos;
- fd->last_opcode_pos = -1;
- emit_op(s, OP_set_name_computed);
- } else if (opcode == OP_set_class_name) {
- int define_class_pos;
- define_class_pos = fd->last_opcode_pos + 1 -
- get_u32(fd->byte_code.buf + fd->last_opcode_pos + 1);
- assert(fd->byte_code.buf[define_class_pos] == OP_define_class);
- fd->byte_code.buf[define_class_pos] = OP_define_class_computed;
- fd->last_opcode_pos = -1;
- }
- }
- static __exception int js_parse_object_literal(JSParseState *s)
- {
- JSAtom name = JS_ATOM_NULL;
- const uint8_t *start_ptr;
- int prop_type;
- BOOL has_proto;
- if (next_token(s))
- goto fail;
- /* XXX: add an initial length that will be patched back */
- emit_op(s, OP_object);
- has_proto = FALSE;
- while (s->token.val != '}') {
- /* specific case for getter/setter */
- start_ptr = s->token.ptr;
- if (s->token.val == TOK_ELLIPSIS) {
- if (next_token(s))
- return -1;
- if (js_parse_assign_expr(s))
- return -1;
- emit_op(s, OP_null); /* dummy excludeList */
- emit_op(s, OP_copy_data_properties);
- emit_u8(s, 2 | (1 << 2) | (0 << 5));
- emit_op(s, OP_drop); /* pop excludeList */
- emit_op(s, OP_drop); /* pop src object */
- goto next;
- }
- prop_type = js_parse_property_name(s, &name, TRUE, TRUE, FALSE);
- if (prop_type < 0)
- goto fail;
- if (prop_type == PROP_TYPE_VAR) {
- /* shortcut for x: x */
- emit_op(s, OP_scope_get_var);
- emit_atom(s, name);
- emit_u16(s, s->cur_func->scope_level);
- emit_op(s, OP_define_field);
- emit_atom(s, name);
- } else if (s->token.val == '(') {
- BOOL is_getset = (prop_type == PROP_TYPE_GET ||
- prop_type == PROP_TYPE_SET);
- JSParseFunctionEnum func_type;
- JSFunctionKindEnum func_kind;
- int op_flags;
- func_kind = JS_FUNC_NORMAL;
- if (is_getset) {
- func_type = JS_PARSE_FUNC_GETTER + prop_type - PROP_TYPE_GET;
- } else {
- func_type = JS_PARSE_FUNC_METHOD;
- if (prop_type == PROP_TYPE_STAR)
- func_kind = JS_FUNC_GENERATOR;
- else if (prop_type == PROP_TYPE_ASYNC)
- func_kind = JS_FUNC_ASYNC;
- else if (prop_type == PROP_TYPE_ASYNC_STAR)
- func_kind = JS_FUNC_ASYNC_GENERATOR;
- }
- if (js_parse_function_decl(s, func_type, func_kind, JS_ATOM_NULL,
- start_ptr))
- goto fail;
- if (name == JS_ATOM_NULL) {
- emit_op(s, OP_define_method_computed);
- } else {
- emit_op(s, OP_define_method);
- emit_atom(s, name);
- }
- if (is_getset) {
- op_flags = OP_DEFINE_METHOD_GETTER +
- prop_type - PROP_TYPE_GET;
- } else {
- op_flags = OP_DEFINE_METHOD_METHOD;
- }
- emit_u8(s, op_flags | OP_DEFINE_METHOD_ENUMERABLE);
- } else {
- if (name == JS_ATOM_NULL) {
- /* must be done before evaluating expr */
- emit_op(s, OP_to_propkey);
- }
- if (js_parse_expect(s, ':'))
- goto fail;
- if (js_parse_assign_expr(s))
- goto fail;
- if (name == JS_ATOM_NULL) {
- set_object_name_computed(s);
- emit_op(s, OP_define_array_el);
- emit_op(s, OP_drop);
- } else if (name == JS_ATOM___proto__) {
- if (has_proto) {
- js_parse_error(s, "duplicate __proto__ property name");
- goto fail;
- }
- emit_op(s, OP_set_proto);
- has_proto = TRUE;
- } else {
- set_object_name(s, name);
- emit_op(s, OP_define_field);
- emit_atom(s, name);
- }
- }
- JS_FreeAtom(s->ctx, name);
- next:
- name = JS_ATOM_NULL;
- if (s->token.val != ',')
- break;
- if (next_token(s))
- goto fail;
- }
- if (js_parse_expect(s, '}'))
- goto fail;
- return 0;
- fail:
- JS_FreeAtom(s->ctx, name);
- return -1;
- }
- /* allow the 'in' binary operator */
- #define PF_IN_ACCEPTED (1 << 0)
- /* allow function calls parsing in js_parse_postfix_expr() */
- #define PF_POSTFIX_CALL (1 << 1)
- /* allow the exponentiation operator in js_parse_unary() */
- #define PF_POW_ALLOWED (1 << 2)
- /* forbid the exponentiation operator in js_parse_unary() */
- #define PF_POW_FORBIDDEN (1 << 3)
- static __exception int js_parse_postfix_expr(JSParseState *s, int parse_flags);
- static void emit_class_field_init(JSParseState *s);
- static JSFunctionDef *js_new_function_def(JSContext *ctx,
- JSFunctionDef *parent,
- BOOL is_eval,
- BOOL is_func_expr,
- const char *filename,
- const uint8_t *source_ptr,
- GetLineColCache *get_line_col_cache);
- static void emit_return(JSParseState *s, BOOL hasval);
- static __exception int js_parse_left_hand_side_expr(JSParseState *s)
- {
- return js_parse_postfix_expr(s, PF_POSTFIX_CALL);
- }
- static __exception int js_parse_class_default_ctor(JSParseState *s,
- BOOL has_super,
- JSFunctionDef **pfd)
- {
- JSParseFunctionEnum func_type;
- JSFunctionDef *fd = s->cur_func;
- int idx;
- fd = js_new_function_def(s->ctx, fd, FALSE, FALSE, s->filename,
- s->token.ptr, &s->get_line_col_cache);
- if (!fd)
- return -1;
- s->cur_func = fd;
- fd->has_home_object = TRUE;
- fd->super_allowed = TRUE;
- fd->has_prototype = FALSE;
- fd->has_this_binding = TRUE;
- fd->new_target_allowed = TRUE;
- push_scope(s); /* enter body scope */
- fd->body_scope = fd->scope_level;
- if (has_super) {
- fd->is_derived_class_constructor = TRUE;
- fd->super_call_allowed = TRUE;
- fd->arguments_allowed = TRUE;
- fd->has_arguments_binding = TRUE;
- func_type = JS_PARSE_FUNC_DERIVED_CLASS_CONSTRUCTOR;
- emit_op(s, OP_init_ctor);
- // TODO(bnoordhuis) roll into OP_init_ctor
- emit_op(s, OP_scope_put_var_init);
- emit_atom(s, JS_ATOM_this);
- emit_u16(s, 0);
- emit_class_field_init(s);
- } else {
- func_type = JS_PARSE_FUNC_CLASS_CONSTRUCTOR;
- /* error if not invoked as a constructor */
- emit_op(s, OP_check_ctor);
- emit_class_field_init(s);
- }
- fd->func_kind = JS_FUNC_NORMAL;
- fd->func_type = func_type;
- emit_return(s, FALSE);
- s->cur_func = fd->parent;
- if (pfd)
- *pfd = fd;
- /* the real object will be set at the end of the compilation */
- idx = cpool_add(s, JS_NULL);
- fd->parent_cpool_idx = idx;
- return 0;
- }
- /* find field in the current scope */
- static int find_private_class_field(JSContext *ctx, JSFunctionDef *fd,
- JSAtom name, int scope_level)
- {
- int idx;
- idx = fd->scopes[scope_level].first;
- while (idx != -1) {
- if (fd->vars[idx].scope_level != scope_level)
- break;
- if (fd->vars[idx].var_name == name)
- return idx;
- idx = fd->vars[idx].scope_next;
- }
- return -1;
- }
- /* initialize the class fields, called by the constructor. Note:
- super() can be called in an arrow function, so <this> and
- <class_fields_init> can be variable references */
- static void emit_class_field_init(JSParseState *s)
- {
- int label_next;
- emit_op(s, OP_scope_get_var);
- emit_atom(s, JS_ATOM_class_fields_init);
- emit_u16(s, s->cur_func->scope_level);
- /* no need to call the class field initializer if not defined */
- emit_op(s, OP_dup);
- label_next = emit_goto(s, OP_if_false, -1);
- emit_op(s, OP_scope_get_var);
- emit_atom(s, JS_ATOM_this);
- emit_u16(s, 0);
- emit_op(s, OP_swap);
- emit_op(s, OP_call_method);
- emit_u16(s, 0);
- emit_label(s, label_next);
- emit_op(s, OP_drop);
- }
- /* build a private setter function name from the private getter name */
- static JSAtom get_private_setter_name(JSContext *ctx, JSAtom name)
- {
- return js_atom_concat_str(ctx, name, "<set>");
- }
- typedef struct {
- JSFunctionDef *fields_init_fd;
- int computed_fields_count;
- BOOL need_brand;
- int brand_push_pos;
- BOOL is_static;
- } ClassFieldsDef;
- static __exception int emit_class_init_start(JSParseState *s,
- ClassFieldsDef *cf)
- {
- int label_add_brand;
- cf->fields_init_fd = js_parse_function_class_fields_init(s);
- if (!cf->fields_init_fd)
- return -1;
- s->cur_func = cf->fields_init_fd;
- if (!cf->is_static) {
- /* add the brand to the newly created instance */
- /* XXX: would be better to add the code only if needed, maybe in a
- later pass */
- emit_op(s, OP_push_false); /* will be patched later */
- cf->brand_push_pos = cf->fields_init_fd->last_opcode_pos;
- label_add_brand = emit_goto(s, OP_if_false, -1);
- emit_op(s, OP_scope_get_var);
- emit_atom(s, JS_ATOM_this);
- emit_u16(s, 0);
- emit_op(s, OP_scope_get_var);
- emit_atom(s, JS_ATOM_home_object);
- emit_u16(s, 0);
- emit_op(s, OP_add_brand);
- emit_label(s, label_add_brand);
- }
- s->cur_func = s->cur_func->parent;
- return 0;
- }
- static void emit_class_init_end(JSParseState *s, ClassFieldsDef *cf)
- {
- int cpool_idx;
- s->cur_func = cf->fields_init_fd;
- emit_op(s, OP_return_undef);
- s->cur_func = s->cur_func->parent;
- cpool_idx = cpool_add(s, JS_NULL);
- cf->fields_init_fd->parent_cpool_idx = cpool_idx;
- emit_op(s, OP_fclosure);
- emit_u32(s, cpool_idx);
- emit_op(s, OP_set_home_object);
- }
- static __exception int js_parse_class(JSParseState *s, BOOL is_class_expr,
- JSParseExportEnum export_flag)
- {
- JSContext *ctx = s->ctx;
- JSFunctionDef *fd = s->cur_func;
- JSAtom name = JS_ATOM_NULL, class_name = JS_ATOM_NULL, class_name1;
- JSAtom class_var_name = JS_ATOM_NULL;
- JSFunctionDef *method_fd, *ctor_fd;
- int saved_js_mode, class_name_var_idx, prop_type, ctor_cpool_offset;
- int class_flags = 0, i, define_class_offset;
- BOOL is_static, is_private;
- const uint8_t *class_start_ptr = s->token.ptr;
- const uint8_t *start_ptr;
- ClassFieldsDef class_fields[2];
- /* classes are parsed and executed in strict mode */
- saved_js_mode = fd->js_mode;
- fd->js_mode |= JS_MODE_STRICT;
- if (next_token(s))
- goto fail;
- if (s->token.val == TOK_IDENT) {
- if (s->token.u.ident.is_reserved) {
- js_parse_error_reserved_identifier(s);
- goto fail;
- }
- class_name = JS_DupAtom(ctx, s->token.u.ident.atom);
- if (next_token(s))
- goto fail;
- } else if (!is_class_expr && export_flag != JS_PARSE_EXPORT_DEFAULT) {
- js_parse_error(s, "class statement requires a name");
- goto fail;
- }
- if (!is_class_expr) {
- if (class_name == JS_ATOM_NULL)
- class_var_name = JS_ATOM__default_; /* export default */
- else
- class_var_name = class_name;
- class_var_name = JS_DupAtom(ctx, class_var_name);
- }
- push_scope(s);
- if (s->token.val == TOK_EXTENDS) {
- class_flags = JS_DEFINE_CLASS_HAS_HERITAGE;
- if (next_token(s))
- goto fail;
- if (js_parse_left_hand_side_expr(s))
- goto fail;
- } else {
- emit_op(s, OP_undefined);
- }
- /* add a 'const' definition for the class name */
- if (class_name != JS_ATOM_NULL) {
- class_name_var_idx = define_var(s, fd, class_name, JS_VAR_DEF_CONST);
- if (class_name_var_idx < 0)
- goto fail;
- }
- if (js_parse_expect(s, '{'))
- goto fail;
- /* this scope contains the private fields */
- push_scope(s);
- emit_op(s, OP_push_const);
- ctor_cpool_offset = fd->byte_code.size;
- emit_u32(s, 0); /* will be patched at the end of the class parsing */
- if (class_name == JS_ATOM_NULL) {
- if (class_var_name != JS_ATOM_NULL)
- class_name1 = JS_ATOM_default;
- else
- class_name1 = JS_ATOM_empty_string;
- } else {
- class_name1 = class_name;
- }
- emit_op(s, OP_define_class);
- emit_atom(s, class_name1);
- emit_u8(s, class_flags);
- define_class_offset = fd->last_opcode_pos;
- for(i = 0; i < 2; i++) {
- ClassFieldsDef *cf = &class_fields[i];
- cf->fields_init_fd = NULL;
- cf->computed_fields_count = 0;
- cf->need_brand = FALSE;
- cf->is_static = i;
- }
- ctor_fd = NULL;
- while (s->token.val != '}') {
- if (s->token.val == ';') {
- if (next_token(s))
- goto fail;
- continue;
- }
- is_static = FALSE;
- if (s->token.val == TOK_STATIC) {
- int next = peek_token(s, TRUE);
- if (!(next == ';' || next == '}' || next == '(' || next == '='))
- is_static = TRUE;
- }
- prop_type = -1;
- if (is_static) {
- if (next_token(s))
- goto fail;
- if (s->token.val == '{') {
- ClassFieldsDef *cf = &class_fields[is_static];
- JSFunctionDef *init;
- if (!cf->fields_init_fd) {
- if (emit_class_init_start(s, cf))
- goto fail;
- }
- s->cur_func = cf->fields_init_fd;
- /* XXX: could try to avoid creating a new function and
- reuse 'fields_init_fd' with a specific 'var'
- scope */
- // stack is now: <empty>
- if (js_parse_function_decl2(s, JS_PARSE_FUNC_CLASS_STATIC_INIT,
- JS_FUNC_NORMAL, JS_ATOM_NULL,
- s->token.ptr,
- JS_PARSE_EXPORT_NONE, &init) < 0) {
- goto fail;
- }
- // stack is now: fclosure
- push_scope(s);
- emit_op(s, OP_scope_get_var);
- emit_atom(s, JS_ATOM_this);
- emit_u16(s, 0);
- // stack is now: fclosure this
- emit_op(s, OP_swap);
- // stack is now: this fclosure
- emit_op(s, OP_call_method);
- emit_u16(s, 0);
- // stack is now: returnvalue
- emit_op(s, OP_drop);
- // stack is now: <empty>
- pop_scope(s);
- s->cur_func = s->cur_func->parent;
- continue;
- }
- /* allow "static" field name */
- if (s->token.val == ';' || s->token.val == '=') {
- is_static = FALSE;
- name = JS_DupAtom(ctx, JS_ATOM_static);
- prop_type = PROP_TYPE_IDENT;
- }
- }
- if (is_static)
- emit_op(s, OP_swap);
- start_ptr = s->token.ptr;
- if (prop_type < 0) {
- prop_type = js_parse_property_name(s, &name, TRUE, FALSE, TRUE);
- if (prop_type < 0)
- goto fail;
- }
- is_private = prop_type & PROP_TYPE_PRIVATE;
- prop_type &= ~PROP_TYPE_PRIVATE;
- if ((name == JS_ATOM_constructor && !is_static &&
- prop_type != PROP_TYPE_IDENT) ||
- (name == JS_ATOM_prototype && is_static) ||
- name == JS_ATOM_hash_constructor) {
- js_parse_error(s, "invalid method name");
- goto fail;
- }
- if (prop_type == PROP_TYPE_GET || prop_type == PROP_TYPE_SET) {
- BOOL is_set = prop_type - PROP_TYPE_GET;
- JSFunctionDef *method_fd;
- if (is_private) {
- int idx, var_kind, is_static1;
- idx = find_private_class_field(ctx, fd, name, fd->scope_level);
- if (idx >= 0) {
- var_kind = fd->vars[idx].var_kind;
- is_static1 = fd->vars[idx].is_static_private;
- if (var_kind == JS_VAR_PRIVATE_FIELD ||
- var_kind == JS_VAR_PRIVATE_METHOD ||
- var_kind == JS_VAR_PRIVATE_GETTER_SETTER ||
- var_kind == (JS_VAR_PRIVATE_GETTER + is_set) ||
- (var_kind == (JS_VAR_PRIVATE_GETTER + 1 - is_set) &&
- is_static != is_static1)) {
- goto private_field_already_defined;
- }
- fd->vars[idx].var_kind = JS_VAR_PRIVATE_GETTER_SETTER;
- } else {
- if (add_private_class_field(s, fd, name,
- JS_VAR_PRIVATE_GETTER + is_set, is_static) < 0)
- goto fail;
- }
- class_fields[is_static].need_brand = TRUE;
- }
- if (js_parse_function_decl2(s, JS_PARSE_FUNC_GETTER + is_set,
- JS_FUNC_NORMAL, JS_ATOM_NULL,
- start_ptr,
- JS_PARSE_EXPORT_NONE, &method_fd))
- goto fail;
- if (is_private) {
- method_fd->need_home_object = TRUE; /* needed for brand check */
- emit_op(s, OP_set_home_object);
- /* XXX: missing function name */
- emit_op(s, OP_scope_put_var_init);
- if (is_set) {
- JSAtom setter_name;
- int ret;
- setter_name = get_private_setter_name(ctx, name);
- if (setter_name == JS_ATOM_NULL)
- goto fail;
- emit_atom(s, setter_name);
- ret = add_private_class_field(s, fd, setter_name,
- JS_VAR_PRIVATE_SETTER, is_static);
- JS_FreeAtom(ctx, setter_name);
- if (ret < 0)
- goto fail;
- } else {
- emit_atom(s, name);
- }
- emit_u16(s, s->cur_func->scope_level);
- } else {
- if (name == JS_ATOM_NULL) {
- emit_op(s, OP_define_method_computed);
- } else {
- emit_op(s, OP_define_method);
- emit_atom(s, name);
- }
- emit_u8(s, OP_DEFINE_METHOD_GETTER + is_set);
- }
- } else if (prop_type == PROP_TYPE_IDENT && s->token.val != '(') {
- ClassFieldsDef *cf = &class_fields[is_static];
- JSAtom field_var_name = JS_ATOM_NULL;
- /* class field */
- /* XXX: spec: not consistent with method name checks */
- if (name == JS_ATOM_constructor || name == JS_ATOM_prototype) {
- js_parse_error(s, "invalid field name");
- goto fail;
- }
- if (is_private) {
- if (find_private_class_field(ctx, fd, name,
- fd->scope_level) >= 0) {
- goto private_field_already_defined;
- }
- if (add_private_class_field(s, fd, name,
- JS_VAR_PRIVATE_FIELD, is_static) < 0)
- goto fail;
- emit_op(s, OP_private_symbol);
- emit_atom(s, name);
- emit_op(s, OP_scope_put_var_init);
- emit_atom(s, name);
- emit_u16(s, s->cur_func->scope_level);
- }
- if (!cf->fields_init_fd) {
- if (emit_class_init_start(s, cf))
- goto fail;
- }
- if (name == JS_ATOM_NULL ) {
- /* save the computed field name into a variable */
- field_var_name = js_atom_concat_num(ctx, JS_ATOM_computed_field + is_static, cf->computed_fields_count);
- if (field_var_name == JS_ATOM_NULL)
- goto fail;
- if (define_var(s, fd, field_var_name, JS_VAR_DEF_CONST) < 0) {
- JS_FreeAtom(ctx, field_var_name);
- goto fail;
- }
- emit_op(s, OP_to_propkey);
- emit_op(s, OP_scope_put_var_init);
- emit_atom(s, field_var_name);
- emit_u16(s, s->cur_func->scope_level);
- }
- s->cur_func = cf->fields_init_fd;
- emit_op(s, OP_scope_get_var);
- emit_atom(s, JS_ATOM_this);
- emit_u16(s, 0);
- if (name == JS_ATOM_NULL) {
- emit_op(s, OP_scope_get_var);
- emit_atom(s, field_var_name);
- emit_u16(s, s->cur_func->scope_level);
- cf->computed_fields_count++;
- JS_FreeAtom(ctx, field_var_name);
- } else if (is_private) {
- emit_op(s, OP_scope_get_var);
- emit_atom(s, name);
- emit_u16(s, s->cur_func->scope_level);
- }
- if (s->token.val == '=') {
- if (next_token(s))
- goto fail;
- if (js_parse_assign_expr(s))
- goto fail;
- } else {
- emit_op(s, OP_undefined);
- }
- if (is_private) {
- set_object_name_computed(s);
- emit_op(s, OP_define_private_field);
- } else if (name == JS_ATOM_NULL) {
- set_object_name_computed(s);
- emit_op(s, OP_define_array_el);
- emit_op(s, OP_drop);
- } else {
- set_object_name(s, name);
- emit_op(s, OP_define_field);
- emit_atom(s, name);
- }
- s->cur_func = s->cur_func->parent;
- if (js_parse_expect_semi(s))
- goto fail;
- } else {
- JSParseFunctionEnum func_type;
- JSFunctionKindEnum func_kind;
- func_type = JS_PARSE_FUNC_METHOD;
- func_kind = JS_FUNC_NORMAL;
- if (prop_type == PROP_TYPE_STAR) {
- func_kind = JS_FUNC_GENERATOR;
- } else if (prop_type == PROP_TYPE_ASYNC) {
- func_kind = JS_FUNC_ASYNC;
- } else if (prop_type == PROP_TYPE_ASYNC_STAR) {
- func_kind = JS_FUNC_ASYNC_GENERATOR;
- } else if (name == JS_ATOM_constructor && !is_static) {
- if (ctor_fd) {
- js_parse_error(s, "property constructor appears more than once");
- goto fail;
- }
- if (class_flags & JS_DEFINE_CLASS_HAS_HERITAGE)
- func_type = JS_PARSE_FUNC_DERIVED_CLASS_CONSTRUCTOR;
- else
- func_type = JS_PARSE_FUNC_CLASS_CONSTRUCTOR;
- }
- if (is_private) {
- class_fields[is_static].need_brand = TRUE;
- }
- if (js_parse_function_decl2(s, func_type, func_kind, JS_ATOM_NULL, start_ptr, JS_PARSE_EXPORT_NONE, &method_fd))
- goto fail;
- if (func_type == JS_PARSE_FUNC_DERIVED_CLASS_CONSTRUCTOR ||
- func_type == JS_PARSE_FUNC_CLASS_CONSTRUCTOR) {
- ctor_fd = method_fd;
- } else if (is_private) {
- method_fd->need_home_object = TRUE; /* needed for brand check */
- if (find_private_class_field(ctx, fd, name,
- fd->scope_level) >= 0) {
- private_field_already_defined:
- js_parse_error(s, "private class field is already defined");
- goto fail;
- }
- if (add_private_class_field(s, fd, name,
- JS_VAR_PRIVATE_METHOD, is_static) < 0)
- goto fail;
- emit_op(s, OP_set_home_object);
- emit_op(s, OP_set_name);
- emit_atom(s, name);
- emit_op(s, OP_scope_put_var_init);
- emit_atom(s, name);
- emit_u16(s, s->cur_func->scope_level);
- } else {
- if (name == JS_ATOM_NULL) {
- emit_op(s, OP_define_method_computed);
- } else {
- emit_op(s, OP_define_method);
- emit_atom(s, name);
- }
- emit_u8(s, OP_DEFINE_METHOD_METHOD);
- }
- }
- if (is_static)
- emit_op(s, OP_swap);
- JS_FreeAtom(ctx, name);
- name = JS_ATOM_NULL;
- }
- if (s->token.val != '}') {
- js_parse_error(s, "expecting '%c'", '}');
- goto fail;
- }
- if (!ctor_fd) {
- if (js_parse_class_default_ctor(s, class_flags & JS_DEFINE_CLASS_HAS_HERITAGE, &ctor_fd))
- goto fail;
- }
- /* patch the constant pool index for the constructor */
- put_u32(fd->byte_code.buf + ctor_cpool_offset, ctor_fd->parent_cpool_idx);
- /* store the class source code in the constructor. */
- if (!fd->strip_source) {
- js_free(ctx, ctor_fd->source);
- ctor_fd->source_len = s->buf_ptr - class_start_ptr;
- ctor_fd->source = js_strndup(ctx, (const char *)class_start_ptr,
- ctor_fd->source_len);
- if (!ctor_fd->source)
- goto fail;
- }
- /* consume the '}' */
- if (next_token(s))
- goto fail;
- {
- ClassFieldsDef *cf = &class_fields[0];
- int var_idx;
- if (cf->need_brand) {
- /* add a private brand to the prototype */
- emit_op(s, OP_dup);
- emit_op(s, OP_null);
- emit_op(s, OP_swap);
- emit_op(s, OP_add_brand);
- /* define the brand field in 'this' of the initializer */
- if (!cf->fields_init_fd) {
- if (emit_class_init_start(s, cf))
- goto fail;
- }
- /* patch the start of the function to enable the
- OP_add_brand_instance code */
- cf->fields_init_fd->byte_code.buf[cf->brand_push_pos] = OP_push_true;
- }
- /* store the function to initialize the fields to that it can be
- referenced by the constructor */
- var_idx = define_var(s, fd, JS_ATOM_class_fields_init,
- JS_VAR_DEF_CONST);
- if (var_idx < 0)
- goto fail;
- if (cf->fields_init_fd) {
- emit_class_init_end(s, cf);
- } else {
- emit_op(s, OP_undefined);
- }
- emit_op(s, OP_scope_put_var_init);
- emit_atom(s, JS_ATOM_class_fields_init);
- emit_u16(s, s->cur_func->scope_level);
- }
- /* drop the prototype */
- emit_op(s, OP_drop);
- if (class_fields[1].need_brand) {
- /* add a private brand to the class */
- emit_op(s, OP_dup);
- emit_op(s, OP_dup);
- emit_op(s, OP_add_brand);
- }
- if (class_name != JS_ATOM_NULL) {
- /* store the class name in the scoped class name variable (it
- is independent from the class statement variable
- definition) */
- emit_op(s, OP_dup);
- emit_op(s, OP_scope_put_var_init);
- emit_atom(s, class_name);
- emit_u16(s, fd->scope_level);
- }
- /* initialize the static fields */
- if (class_fields[1].fields_init_fd != NULL) {
- ClassFieldsDef *cf = &class_fields[1];
- emit_op(s, OP_dup);
- emit_class_init_end(s, cf);
- emit_op(s, OP_call_method);
- emit_u16(s, 0);
- emit_op(s, OP_drop);
- }
- pop_scope(s);
- pop_scope(s);
- /* the class statements have a block level scope */
- if (class_var_name != JS_ATOM_NULL) {
- if (define_var(s, fd, class_var_name, JS_VAR_DEF_LET) < 0)
- goto fail;
- emit_op(s, OP_scope_put_var_init);
- emit_atom(s, class_var_name);
- emit_u16(s, fd->scope_level);
- } else {
- if (class_name == JS_ATOM_NULL) {
- /* cannot use OP_set_name because the name of the class
- must be defined before the static initializers are
- executed */
- emit_op(s, OP_set_class_name);
- emit_u32(s, fd->last_opcode_pos + 1 - define_class_offset);
- }
- }
- if (export_flag != JS_PARSE_EXPORT_NONE) {
- if (!add_export_entry(s, fd->module,
- class_var_name,
- export_flag == JS_PARSE_EXPORT_NAMED ? class_var_name : JS_ATOM_default,
- JS_EXPORT_TYPE_LOCAL))
- goto fail;
- }
- JS_FreeAtom(ctx, class_name);
- JS_FreeAtom(ctx, class_var_name);
- fd->js_mode = saved_js_mode;
- return 0;
- fail:
- JS_FreeAtom(ctx, name);
- JS_FreeAtom(ctx, class_name);
- JS_FreeAtom(ctx, class_var_name);
- fd->js_mode = saved_js_mode;
- return -1;
- }
- static __exception int js_parse_array_literal(JSParseState *s)
- {
- uint32_t idx;
- BOOL need_length;
- if (next_token(s))
- return -1;
- /* small regular arrays are created on the stack */
- idx = 0;
- while (s->token.val != ']' && idx < 32) {
- if (s->token.val == ',' || s->token.val == TOK_ELLIPSIS)
- break;
- if (js_parse_assign_expr(s))
- return -1;
- idx++;
- /* accept trailing comma */
- if (s->token.val == ',') {
- if (next_token(s))
- return -1;
- } else
- if (s->token.val != ']')
- goto done;
- }
- emit_op(s, OP_array_from);
- emit_u16(s, idx);
- /* larger arrays and holes are handled with explicit indices */
- need_length = FALSE;
- while (s->token.val != ']' && idx < 0x7fffffff) {
- if (s->token.val == TOK_ELLIPSIS)
- break;
- need_length = TRUE;
- if (s->token.val != ',') {
- if (js_parse_assign_expr(s))
- return -1;
- emit_op(s, OP_define_field);
- emit_u32(s, __JS_AtomFromUInt32(idx));
- need_length = FALSE;
- }
- idx++;
- /* accept trailing comma */
- if (s->token.val == ',') {
- if (next_token(s))
- return -1;
- }
- }
- if (s->token.val == ']') {
- if (need_length) {
- /* Set the length: Cannot use OP_define_field because
- length is not configurable */
- emit_op(s, OP_dup);
- emit_op(s, OP_push_i32);
- emit_u32(s, idx);
- emit_op(s, OP_put_field);
- emit_atom(s, JS_ATOM_length);
- }
- goto done;
- }
- /* huge arrays and spread elements require a dynamic index on the stack */
- emit_op(s, OP_push_i32);
- emit_u32(s, idx);
- /* stack has array, index */
- while (s->token.val != ']') {
- if (s->token.val == TOK_ELLIPSIS) {
- if (next_token(s))
- return -1;
- if (js_parse_assign_expr(s))
- return -1;
- #if 1
- emit_op(s, OP_append);
- #else
- int label_next, label_done;
- label_next = new_label(s);
- label_done = new_label(s);
- /* enumerate object */
- emit_op(s, OP_for_of_start);
- emit_op(s, OP_rot5l);
- emit_op(s, OP_rot5l);
- emit_label(s, label_next);
- /* on stack: enum_rec array idx */
- emit_op(s, OP_for_of_next);
- emit_u8(s, 2);
- emit_goto(s, OP_if_true, label_done);
- /* append element */
- /* enum_rec array idx val -> enum_rec array new_idx */
- emit_op(s, OP_define_array_el);
- emit_op(s, OP_inc);
- emit_goto(s, OP_goto, label_next);
- emit_label(s, label_done);
- /* close enumeration */
- emit_op(s, OP_drop); /* drop undef val */
- emit_op(s, OP_nip1); /* drop enum_rec */
- emit_op(s, OP_nip1);
- emit_op(s, OP_nip1);
- #endif
- } else {
- need_length = TRUE;
- if (s->token.val != ',') {
- if (js_parse_assign_expr(s))
- return -1;
- /* a idx val */
- emit_op(s, OP_define_array_el);
- need_length = FALSE;
- }
- emit_op(s, OP_inc);
- }
- if (s->token.val != ',')
- break;
- if (next_token(s))
- return -1;
- }
- if (need_length) {
- /* Set the length: cannot use OP_define_field because
- length is not configurable */
- emit_op(s, OP_dup1); /* array length - array array length */
- emit_op(s, OP_put_field);
- emit_atom(s, JS_ATOM_length);
- } else {
- emit_op(s, OP_drop); /* array length - array */
- }
- done:
- return js_parse_expect(s, ']');
- }
- /* check if scope chain contains a with statement */
- static BOOL has_with_scope(JSFunctionDef *s, int scope_level)
- {
- while (s) {
- /* no with in strict mode */
- if (!(s->js_mode & JS_MODE_STRICT)) {
- int scope_idx = s->scopes[scope_level].first;
- while (scope_idx >= 0) {
- JSVarDef *vd = &s->vars[scope_idx];
- if (vd->var_name == JS_ATOM__with_)
- return TRUE;
- scope_idx = vd->scope_next;
- }
- }
- /* check parent scopes */
- scope_level = s->parent_scope_level;
- s = s->parent;
- }
- return FALSE;
- }
- static __exception int get_lvalue(JSParseState *s, int *popcode, int *pscope,
- JSAtom *pname, int *plabel, int *pdepth, BOOL keep,
- int tok)
- {
- JSFunctionDef *fd;
- int opcode, scope, label, depth;
- JSAtom name;
- /* we check the last opcode to get the lvalue type */
- fd = s->cur_func;
- scope = 0;
- name = JS_ATOM_NULL;
- label = -1;
- depth = 0;
- switch(opcode = get_prev_opcode(fd)) {
- case OP_scope_get_var:
- name = get_u32(fd->byte_code.buf + fd->last_opcode_pos + 1);
- scope = get_u16(fd->byte_code.buf + fd->last_opcode_pos + 5);
- if ((name == JS_ATOM_arguments || name == JS_ATOM_eval) &&
- (fd->js_mode & JS_MODE_STRICT)) {
- return js_parse_error(s, "invalid lvalue in strict mode");
- }
- if (name == JS_ATOM_this || name == JS_ATOM_new_target)
- goto invalid_lvalue;
- if (has_with_scope(fd, scope)) {
- depth = 2; /* will generate OP_get_ref_value */
- } else {
- depth = 0;
- }
- break;
- case OP_get_field:
- name = get_u32(fd->byte_code.buf + fd->last_opcode_pos + 1);
- depth = 1;
- break;
- case OP_scope_get_private_field:
- name = get_u32(fd->byte_code.buf + fd->last_opcode_pos + 1);
- scope = get_u16(fd->byte_code.buf + fd->last_opcode_pos + 5);
- depth = 1;
- break;
- case OP_get_array_el:
- depth = 2;
- break;
- case OP_get_super_value:
- depth = 3;
- break;
- default:
- invalid_lvalue:
- if (tok == TOK_FOR) {
- return js_parse_error(s, "invalid for in/of left hand-side");
- } else if (tok == TOK_INC || tok == TOK_DEC) {
- return js_parse_error(s, "invalid increment/decrement operand");
- } else if (tok == '[' || tok == '{') {
- return js_parse_error(s, "invalid destructuring target");
- } else {
- return js_parse_error(s, "invalid assignment left-hand side");
- }
- }
- /* remove the last opcode */
- fd->byte_code.size = fd->last_opcode_pos;
- fd->last_opcode_pos = -1;
- if (keep) {
- /* get the value but keep the object/fields on the stack */
- switch(opcode) {
- case OP_scope_get_var:
- if (depth != 0) {
- label = new_label(s);
- if (label < 0)
- return -1;
- emit_op(s, OP_scope_make_ref);
- emit_atom(s, name);
- emit_u32(s, label);
- emit_u16(s, scope);
- update_label(fd, label, 1);
- emit_op(s, OP_get_ref_value);
- opcode = OP_get_ref_value;
- } else {
- emit_op(s, OP_scope_get_var);
- emit_atom(s, name);
- emit_u16(s, scope);
- }
- break;
- case OP_get_field:
- emit_op(s, OP_get_field2);
- emit_atom(s, name);
- break;
- case OP_scope_get_private_field:
- emit_op(s, OP_scope_get_private_field2);
- emit_atom(s, name);
- emit_u16(s, scope);
- break;
- case OP_get_array_el:
- emit_op(s, OP_get_array_el3);
- break;
- case OP_get_super_value:
- emit_op(s, OP_to_propkey);
- emit_op(s, OP_dup3);
- emit_op(s, OP_get_super_value);
- break;
- default:
- abort();
- }
- } else {
- switch(opcode) {
- case OP_scope_get_var:
- if (depth != 0) {
- label = new_label(s);
- if (label < 0)
- return -1;
- emit_op(s, OP_scope_make_ref);
- emit_atom(s, name);
- emit_u32(s, label);
- emit_u16(s, scope);
- update_label(fd, label, 1);
- opcode = OP_get_ref_value;
- }
- break;
- default:
- break;
- }
- }
- *popcode = opcode;
- *pscope = scope;
- /* name has refcount for OP_get_field and OP_get_ref_value,
- and JS_ATOM_NULL for other opcodes */
- *pname = name;
- *plabel = label;
- if (pdepth)
- *pdepth = depth;
- return 0;
- }
- typedef enum {
- PUT_LVALUE_NOKEEP, /* [depth] v -> */
- PUT_LVALUE_NOKEEP_DEPTH, /* [depth] v -> , keep depth (currently
- just disable optimizations) */
- PUT_LVALUE_KEEP_TOP, /* [depth] v -> v */
- PUT_LVALUE_KEEP_SECOND, /* [depth] v0 v -> v0 */
- PUT_LVALUE_NOKEEP_BOTTOM, /* v [depth] -> */
- } PutLValueEnum;
- /* name has a live reference. 'is_let' is only used with opcode =
- OP_scope_get_var which is never generated by get_lvalue(). */
- static void put_lvalue(JSParseState *s, int opcode, int scope,
- JSAtom name, int label, PutLValueEnum special,
- BOOL is_let)
- {
- switch(opcode) {
- case OP_scope_get_var:
- /* depth = 0 */
- switch(special) {
- case PUT_LVALUE_NOKEEP:
- case PUT_LVALUE_NOKEEP_DEPTH:
- case PUT_LVALUE_KEEP_SECOND:
- case PUT_LVALUE_NOKEEP_BOTTOM:
- break;
- case PUT_LVALUE_KEEP_TOP:
- emit_op(s, OP_dup);
- break;
- default:
- abort();
- }
- break;
- case OP_get_field:
- case OP_scope_get_private_field:
- /* depth = 1 */
- switch(special) {
- case PUT_LVALUE_NOKEEP:
- case PUT_LVALUE_NOKEEP_DEPTH:
- break;
- case PUT_LVALUE_KEEP_TOP:
- emit_op(s, OP_insert2); /* obj v -> v obj v */
- break;
- case PUT_LVALUE_KEEP_SECOND:
- emit_op(s, OP_perm3); /* obj v0 v -> v0 obj v */
- break;
- case PUT_LVALUE_NOKEEP_BOTTOM:
- emit_op(s, OP_swap);
- break;
- default:
- abort();
- }
- break;
- case OP_get_array_el:
- case OP_get_ref_value:
- /* depth = 2 */
- if (opcode == OP_get_ref_value) {
- JS_FreeAtom(s->ctx, name);
- emit_label(s, label);
- }
- switch(special) {
- case PUT_LVALUE_NOKEEP:
- emit_op(s, OP_nop); /* will trigger optimization */
- break;
- case PUT_LVALUE_NOKEEP_DEPTH:
- break;
- case PUT_LVALUE_KEEP_TOP:
- emit_op(s, OP_insert3); /* obj prop v -> v obj prop v */
- break;
- case PUT_LVALUE_KEEP_SECOND:
- emit_op(s, OP_perm4); /* obj prop v0 v -> v0 obj prop v */
- break;
- case PUT_LVALUE_NOKEEP_BOTTOM:
- emit_op(s, OP_rot3l);
- break;
- default:
- abort();
- }
- break;
- case OP_get_super_value:
- /* depth = 3 */
- switch(special) {
- case PUT_LVALUE_NOKEEP:
- case PUT_LVALUE_NOKEEP_DEPTH:
- break;
- case PUT_LVALUE_KEEP_TOP:
- emit_op(s, OP_insert4); /* this obj prop v -> v this obj prop v */
- break;
- case PUT_LVALUE_KEEP_SECOND:
- emit_op(s, OP_perm5); /* this obj prop v0 v -> v0 this obj prop v */
- break;
- case PUT_LVALUE_NOKEEP_BOTTOM:
- emit_op(s, OP_rot4l);
- break;
- default:
- abort();
- }
- break;
- default:
- break;
- }
- switch(opcode) {
- case OP_scope_get_var: /* val -- */
- emit_op(s, is_let ? OP_scope_put_var_init : OP_scope_put_var);
- emit_u32(s, name); /* has refcount */
- emit_u16(s, scope);
- break;
- case OP_get_field:
- emit_op(s, OP_put_field);
- emit_u32(s, name); /* name has refcount */
- break;
- case OP_scope_get_private_field:
- emit_op(s, OP_scope_put_private_field);
- emit_u32(s, name); /* name has refcount */
- emit_u16(s, scope);
- break;
- case OP_get_array_el:
- emit_op(s, OP_put_array_el);
- break;
- case OP_get_ref_value:
- emit_op(s, OP_put_ref_value);
- break;
- case OP_get_super_value:
- emit_op(s, OP_put_super_value);
- break;
- default:
- abort();
- }
- }
- static __exception int js_parse_expr_paren(JSParseState *s)
- {
- if (js_parse_expect(s, '('))
- return -1;
- if (js_parse_expr(s))
- return -1;
- if (js_parse_expect(s, ')'))
- return -1;
- return 0;
- }
- static int js_unsupported_keyword(JSParseState *s, JSAtom atom)
- {
- char buf[ATOM_GET_STR_BUF_SIZE];
- return js_parse_error(s, "unsupported keyword: %s",
- JS_AtomGetStr(s->ctx, buf, sizeof(buf), atom));
- }
- static __exception int js_define_var(JSParseState *s, JSAtom name, int tok)
- {
- JSFunctionDef *fd = s->cur_func;
- JSVarDefEnum var_def_type;
- if (name == JS_ATOM_yield && fd->func_kind == JS_FUNC_GENERATOR) {
- return js_parse_error(s, "yield is a reserved identifier");
- }
- if ((name == JS_ATOM_arguments || name == JS_ATOM_eval)
- && (fd->js_mode & JS_MODE_STRICT)) {
- return js_parse_error(s, "invalid variable name in strict mode");
- }
- if (name == JS_ATOM_let
- && (tok == TOK_LET || tok == TOK_CONST)) {
- return js_parse_error(s, "invalid lexical variable name");
- }
- switch(tok) {
- case TOK_LET:
- var_def_type = JS_VAR_DEF_LET;
- break;
- case TOK_CONST:
- var_def_type = JS_VAR_DEF_CONST;
- break;
- case TOK_VAR:
- var_def_type = JS_VAR_DEF_VAR;
- break;
- case TOK_CATCH:
- var_def_type = JS_VAR_DEF_CATCH;
- break;
- default:
- abort();
- }
- if (define_var(s, fd, name, var_def_type) < 0)
- return -1;
- return 0;
- }
- static void js_emit_spread_code(JSParseState *s, int depth)
- {
- int label_rest_next, label_rest_done;
- /* XXX: could check if enum object is an actual array and optimize
- slice extraction. enumeration record and target array are in a
- different order from OP_append case. */
- /* enum_rec xxx -- enum_rec xxx array 0 */
- emit_op(s, OP_array_from);
- emit_u16(s, 0);
- emit_op(s, OP_push_i32);
- emit_u32(s, 0);
- emit_label(s, label_rest_next = new_label(s));
- emit_op(s, OP_for_of_next);
- emit_u8(s, 2 + depth);
- label_rest_done = emit_goto(s, OP_if_true, -1);
- /* array idx val -- array idx */
- emit_op(s, OP_define_array_el);
- emit_op(s, OP_inc);
- emit_goto(s, OP_goto, label_rest_next);
- emit_label(s, label_rest_done);
- /* enum_rec xxx array idx undef -- enum_rec xxx array */
- emit_op(s, OP_drop);
- emit_op(s, OP_drop);
- }
- static int js_parse_check_duplicate_parameter(JSParseState *s, JSAtom name)
- {
- /* Check for duplicate parameter names */
- JSFunctionDef *fd = s->cur_func;
- int i;
- for (i = 0; i < fd->arg_count; i++) {
- if (fd->args[i].var_name == name)
- goto duplicate;
- }
- for (i = 0; i < fd->var_count; i++) {
- if (fd->vars[i].var_name == name)
- goto duplicate;
- }
- return 0;
- duplicate:
- return js_parse_error(s, "duplicate parameter names not allowed in this context");
- }
- /* tok = TOK_VAR, TOK_LET or TOK_CONST. Return whether a reference
- must be taken to the variable for proper 'with' or global variable
- evaluation */
- /* Note: this function is needed only because variable references are
- not yet optimized in destructuring */
- static BOOL need_var_reference(JSParseState *s, int tok)
- {
- JSFunctionDef *fd = s->cur_func;
- if (tok != TOK_VAR)
- return FALSE; /* no reference for let/const */
- if (fd->js_mode & JS_MODE_STRICT) {
- if (!fd->is_global_var)
- return FALSE; /* local definitions in strict mode in function or direct eval */
- if (s->is_module)
- return FALSE; /* in a module global variables are like closure variables */
- }
- return TRUE;
- }
- static JSAtom js_parse_destructuring_var(JSParseState *s, int tok, int is_arg)
- {
- JSAtom name;
- if (!(s->token.val == TOK_IDENT && !s->token.u.ident.is_reserved)
- || ((s->cur_func->js_mode & JS_MODE_STRICT) &&
- (s->token.u.ident.atom == JS_ATOM_eval || s->token.u.ident.atom == JS_ATOM_arguments))) {
- js_parse_error(s, "invalid destructuring target");
- return JS_ATOM_NULL;
- }
- name = JS_DupAtom(s->ctx, s->token.u.ident.atom);
- if (is_arg && js_parse_check_duplicate_parameter(s, name))
- goto fail;
- if (next_token(s))
- goto fail;
- return name;
- fail:
- JS_FreeAtom(s->ctx, name);
- return JS_ATOM_NULL;
- }
- /* Return -1 if error, 0 if no initializer, 1 if an initializer is
- present at the top level. */
- static int js_parse_destructuring_element(JSParseState *s, int tok, int is_arg,
- int hasval, int has_ellipsis,
- BOOL allow_initializer, BOOL export_flag)
- {
- int label_parse, label_assign, label_done, label_lvalue, depth_lvalue;
- int start_addr, assign_addr;
- JSAtom prop_name, var_name;
- int opcode, scope, tok1, skip_bits;
- BOOL has_initializer;
- if (has_ellipsis < 0) {
- /* pre-parse destructuration target for spread detection */
- js_parse_skip_parens_token(s, &skip_bits, FALSE);
- has_ellipsis = skip_bits & SKIP_HAS_ELLIPSIS;
- }
- label_parse = new_label(s);
- label_assign = new_label(s);
- start_addr = s->cur_func->byte_code.size;
- if (hasval) {
- /* consume value from the stack */
- emit_op(s, OP_dup);
- emit_op(s, OP_undefined);
- emit_op(s, OP_strict_eq);
- emit_goto(s, OP_if_true, label_parse);
- emit_label(s, label_assign);
- } else {
- emit_goto(s, OP_goto, label_parse);
- emit_label(s, label_assign);
- /* leave value on the stack */
- emit_op(s, OP_dup);
- }
- assign_addr = s->cur_func->byte_code.size;
- if (s->token.val == '{') {
- if (next_token(s))
- return -1;
- /* throw an exception if the value cannot be converted to an object */
- emit_op(s, OP_to_object);
- if (has_ellipsis) {
- /* add excludeList on stack just below src object */
- emit_op(s, OP_object);
- emit_op(s, OP_swap);
- }
- while (s->token.val != '}') {
- int prop_type;
- if (s->token.val == TOK_ELLIPSIS) {
- if (!has_ellipsis) {
- JS_ThrowInternalError(s->ctx, "unexpected ellipsis token");
- return -1;
- }
- if (next_token(s))
- return -1;
- if (tok) {
- var_name = js_parse_destructuring_var(s, tok, is_arg);
- if (var_name == JS_ATOM_NULL)
- return -1;
- if (need_var_reference(s, tok)) {
- /* Must make a reference for proper `with` semantics */
- emit_op(s, OP_scope_get_var);
- emit_atom(s, var_name);
- emit_u16(s, s->cur_func->scope_level);
- JS_FreeAtom(s->ctx, var_name);
- goto lvalue0;
- } else {
- opcode = OP_scope_get_var;
- scope = s->cur_func->scope_level;
- label_lvalue = -1;
- depth_lvalue = 0;
- }
- } else {
- if (js_parse_left_hand_side_expr(s))
- return -1;
- lvalue0:
- if (get_lvalue(s, &opcode, &scope, &var_name,
- &label_lvalue, &depth_lvalue, FALSE, '{'))
- return -1;
- }
- if (s->token.val != '}') {
- js_parse_error(s, "assignment rest property must be last");
- goto var_error;
- }
- emit_op(s, OP_object); /* target */
- emit_op(s, OP_copy_data_properties);
- emit_u8(s, 0 | ((depth_lvalue + 1) << 2) | ((depth_lvalue + 2) << 5));
- goto set_val;
- }
- prop_type = js_parse_property_name(s, &prop_name, FALSE, TRUE, FALSE);
- if (prop_type < 0)
- return -1;
- var_name = JS_ATOM_NULL;
- if (prop_type == PROP_TYPE_IDENT) {
- if (next_token(s))
- goto prop_error;
- if ((s->token.val == '[' || s->token.val == '{')
- && ((tok1 = js_parse_skip_parens_token(s, &skip_bits, FALSE)) == ',' ||
- tok1 == '=' || tok1 == '}')) {
- if (prop_name == JS_ATOM_NULL) {
- /* computed property name on stack */
- if (has_ellipsis) {
- /* define the property in excludeList */
- emit_op(s, OP_to_propkey); /* avoid calling ToString twice */
- emit_op(s, OP_perm3); /* TOS: src excludeList prop */
- emit_op(s, OP_null); /* TOS: src excludeList prop null */
- emit_op(s, OP_define_array_el); /* TOS: src excludeList prop */
- emit_op(s, OP_perm3); /* TOS: excludeList src prop */
- }
- /* get the computed property from the source object */
- emit_op(s, OP_get_array_el2);
- } else {
- /* named property */
- if (has_ellipsis) {
- /* define the property in excludeList */
- emit_op(s, OP_swap); /* TOS: src excludeList */
- emit_op(s, OP_null); /* TOS: src excludeList null */
- emit_op(s, OP_define_field); /* TOS: src excludeList */
- emit_atom(s, prop_name);
- emit_op(s, OP_swap); /* TOS: excludeList src */
- }
- /* get the named property from the source object */
- emit_op(s, OP_get_field2);
- emit_u32(s, prop_name);
- }
- if (js_parse_destructuring_element(s, tok, is_arg, TRUE, -1, TRUE, export_flag) < 0)
- return -1;
- if (s->token.val == '}')
- break;
- /* accept a trailing comma before the '}' */
- if (js_parse_expect(s, ','))
- return -1;
- continue;
- }
- if (prop_name == JS_ATOM_NULL) {
- emit_op(s, OP_to_propkey);
- if (has_ellipsis) {
- /* define the property in excludeList */
- emit_op(s, OP_perm3);
- emit_op(s, OP_null);
- emit_op(s, OP_define_array_el);
- emit_op(s, OP_perm3);
- }
- /* source prop -- source source prop */
- emit_op(s, OP_dup1);
- } else {
- if (has_ellipsis) {
- /* define the property in excludeList */
- emit_op(s, OP_swap);
- emit_op(s, OP_null);
- emit_op(s, OP_define_field);
- emit_atom(s, prop_name);
- emit_op(s, OP_swap);
- }
- /* source -- source source */
- emit_op(s, OP_dup);
- }
- if (tok) {
- var_name = js_parse_destructuring_var(s, tok, is_arg);
- if (var_name == JS_ATOM_NULL)
- goto prop_error;
- if (need_var_reference(s, tok)) {
- /* Must make a reference for proper `with` semantics */
- emit_op(s, OP_scope_get_var);
- emit_atom(s, var_name);
- emit_u16(s, s->cur_func->scope_level);
- JS_FreeAtom(s->ctx, var_name);
- goto lvalue1;
- } else {
- /* no need to make a reference for let/const */
- opcode = OP_scope_get_var;
- scope = s->cur_func->scope_level;
- label_lvalue = -1;
- depth_lvalue = 0;
- }
- } else {
- if (js_parse_left_hand_side_expr(s))
- goto prop_error;
- lvalue1:
- if (get_lvalue(s, &opcode, &scope, &var_name,
- &label_lvalue, &depth_lvalue, FALSE, '{'))
- goto prop_error;
- /* swap ref and lvalue object if any */
- if (prop_name == JS_ATOM_NULL) {
- switch(depth_lvalue) {
- case 0:
- break;
- case 1:
- /* source prop x -> x source prop */
- emit_op(s, OP_rot3r);
- break;
- case 2:
- /* source prop x y -> x y source prop */
- emit_op(s, OP_swap2); /* t p2 s p1 */
- break;
- case 3:
- /* source prop x y z -> x y z source prop */
- emit_op(s, OP_rot5l);
- emit_op(s, OP_rot5l);
- break;
- default:
- abort();
- }
- } else {
- switch(depth_lvalue) {
- case 0:
- break;
- case 1:
- /* source x -> x source */
- emit_op(s, OP_swap);
- break;
- case 2:
- /* source x y -> x y source */
- emit_op(s, OP_rot3l);
- break;
- case 3:
- /* source x y z -> x y z source */
- emit_op(s, OP_rot4l);
- break;
- default:
- abort();
- }
- }
- }
- if (prop_name == JS_ATOM_NULL) {
- /* computed property name on stack */
- /* XXX: should have OP_get_array_el2x with depth */
- /* source prop -- val */
- emit_op(s, OP_get_array_el);
- } else {
- /* named property */
- /* XXX: should have OP_get_field2x with depth */
- /* source -- val */
- emit_op(s, OP_get_field);
- emit_u32(s, prop_name);
- }
- } else {
- /* prop_type = PROP_TYPE_VAR, cannot be a computed property */
- if (is_arg && js_parse_check_duplicate_parameter(s, prop_name))
- goto prop_error;
- if ((s->cur_func->js_mode & JS_MODE_STRICT) &&
- (prop_name == JS_ATOM_eval || prop_name == JS_ATOM_arguments)) {
- js_parse_error(s, "invalid destructuring target");
- goto prop_error;
- }
- if (has_ellipsis) {
- /* define the property in excludeList */
- emit_op(s, OP_swap);
- emit_op(s, OP_null);
- emit_op(s, OP_define_field);
- emit_atom(s, prop_name);
- emit_op(s, OP_swap);
- }
- if (!tok || need_var_reference(s, tok)) {
- /* generate reference */
- /* source -- source source */
- emit_op(s, OP_dup);
- emit_op(s, OP_scope_get_var);
- emit_atom(s, prop_name);
- emit_u16(s, s->cur_func->scope_level);
- goto lvalue1;
- } else {
- /* no need to make a reference for let/const */
- var_name = JS_DupAtom(s->ctx, prop_name);
- opcode = OP_scope_get_var;
- scope = s->cur_func->scope_level;
- label_lvalue = -1;
- depth_lvalue = 0;
-
- /* source -- source val */
- emit_op(s, OP_get_field2);
- emit_u32(s, prop_name);
- }
- }
- set_val:
- if (tok) {
- if (js_define_var(s, var_name, tok))
- goto var_error;
- if (export_flag) {
- if (!add_export_entry(s, s->cur_func->module, var_name, var_name,
- JS_EXPORT_TYPE_LOCAL))
- goto var_error;
- }
- scope = s->cur_func->scope_level; /* XXX: check */
- }
- if (s->token.val == '=') { /* handle optional default value */
- int label_hasval;
- emit_op(s, OP_dup);
- emit_op(s, OP_undefined);
- emit_op(s, OP_strict_eq);
- label_hasval = emit_goto(s, OP_if_false, -1);
- if (next_token(s))
- goto var_error;
- emit_op(s, OP_drop);
- if (js_parse_assign_expr(s))
- goto var_error;
- if (opcode == OP_scope_get_var || opcode == OP_get_ref_value)
- set_object_name(s, var_name);
- emit_label(s, label_hasval);
- }
- /* store value into lvalue object */
- put_lvalue(s, opcode, scope, var_name, label_lvalue,
- PUT_LVALUE_NOKEEP_DEPTH,
- (tok == TOK_CONST || tok == TOK_LET));
- if (s->token.val == '}')
- break;
- /* accept a trailing comma before the '}' */
- if (js_parse_expect(s, ','))
- return -1;
- }
- /* drop the source object */
- emit_op(s, OP_drop);
- if (has_ellipsis) {
- emit_op(s, OP_drop); /* pop excludeList */
- }
- if (next_token(s))
- return -1;
- } else if (s->token.val == '[') {
- BOOL has_spread;
- int enum_depth;
- BlockEnv block_env;
- if (next_token(s))
- return -1;
- /* the block environment is only needed in generators in case
- 'yield' triggers a 'return' */
- push_break_entry(s->cur_func, &block_env,
- JS_ATOM_NULL, -1, -1, 2);
- block_env.has_iterator = TRUE;
- emit_op(s, OP_for_of_start);
- has_spread = FALSE;
- while (s->token.val != ']') {
- /* get the next value */
- if (s->token.val == TOK_ELLIPSIS) {
- if (next_token(s))
- return -1;
- if (s->token.val == ',' || s->token.val == ']')
- return js_parse_error(s, "missing binding pattern...");
- has_spread = TRUE;
- }
- if (s->token.val == ',') {
- /* do nothing, skip the value, has_spread is false */
- emit_op(s, OP_for_of_next);
- emit_u8(s, 0);
- emit_op(s, OP_drop);
- emit_op(s, OP_drop);
- } else if ((s->token.val == '[' || s->token.val == '{')
- && ((tok1 = js_parse_skip_parens_token(s, &skip_bits, FALSE)) == ',' ||
- tok1 == '=' || tok1 == ']')) {
- if (has_spread) {
- if (tok1 == '=')
- return js_parse_error(s, "rest element cannot have a default value");
- js_emit_spread_code(s, 0);
- } else {
- emit_op(s, OP_for_of_next);
- emit_u8(s, 0);
- emit_op(s, OP_drop);
- }
- if (js_parse_destructuring_element(s, tok, is_arg, TRUE, skip_bits & SKIP_HAS_ELLIPSIS, TRUE, export_flag) < 0)
- return -1;
- } else {
- var_name = JS_ATOM_NULL;
- if (tok) {
- var_name = js_parse_destructuring_var(s, tok, is_arg);
- if (var_name == JS_ATOM_NULL)
- goto var_error;
- if (js_define_var(s, var_name, tok))
- goto var_error;
- if (need_var_reference(s, tok)) {
- /* Must make a reference for proper `with` semantics */
- emit_op(s, OP_scope_get_var);
- emit_atom(s, var_name);
- emit_u16(s, s->cur_func->scope_level);
- JS_FreeAtom(s->ctx, var_name);
- goto lvalue2;
- } else {
- /* no need to make a reference for let/const */
- opcode = OP_scope_get_var;
- scope = s->cur_func->scope_level;
- label_lvalue = -1;
- enum_depth = 0;
- }
- } else {
- if (js_parse_left_hand_side_expr(s))
- return -1;
- lvalue2:
- if (get_lvalue(s, &opcode, &scope, &var_name,
- &label_lvalue, &enum_depth, FALSE, '[')) {
- return -1;
- }
- }
- if (has_spread) {
- js_emit_spread_code(s, enum_depth);
- } else {
- emit_op(s, OP_for_of_next);
- emit_u8(s, enum_depth);
- emit_op(s, OP_drop);
- }
- if (s->token.val == '=' && !has_spread) {
- /* handle optional default value */
- int label_hasval;
- emit_op(s, OP_dup);
- emit_op(s, OP_undefined);
- emit_op(s, OP_strict_eq);
- label_hasval = emit_goto(s, OP_if_false, -1);
- if (next_token(s))
- goto var_error;
- emit_op(s, OP_drop);
- if (js_parse_assign_expr(s))
- goto var_error;
- if (opcode == OP_scope_get_var || opcode == OP_get_ref_value)
- set_object_name(s, var_name);
- emit_label(s, label_hasval);
- }
- /* store value into lvalue object */
- put_lvalue(s, opcode, scope, var_name,
- label_lvalue, PUT_LVALUE_NOKEEP_DEPTH,
- (tok == TOK_CONST || tok == TOK_LET));
- }
- if (s->token.val == ']')
- break;
- if (has_spread)
- return js_parse_error(s, "rest element must be the last one");
- /* accept a trailing comma before the ']' */
- if (js_parse_expect(s, ','))
- return -1;
- }
- /* close iterator object:
- if completed, enum_obj has been replaced by undefined */
- emit_op(s, OP_iterator_close);
- pop_break_entry(s->cur_func);
- if (next_token(s))
- return -1;
- } else {
- return js_parse_error(s, "invalid assignment syntax");
- }
- if (s->token.val == '=' && allow_initializer) {
- label_done = emit_goto(s, OP_goto, -1);
- if (next_token(s))
- return -1;
- emit_label(s, label_parse);
- if (hasval)
- emit_op(s, OP_drop);
- if (js_parse_assign_expr(s))
- return -1;
- emit_goto(s, OP_goto, label_assign);
- emit_label(s, label_done);
- has_initializer = TRUE;
- } else {
- /* normally hasval is true except if
- js_parse_skip_parens_token() was wrong in the parsing */
- // assert(hasval);
- if (!hasval) {
- js_parse_error(s, "too complicated destructuring expression");
- return -1;
- }
- /* remove test and decrement label ref count */
- memset(s->cur_func->byte_code.buf + start_addr, OP_nop,
- assign_addr - start_addr);
- s->cur_func->label_slots[label_parse].ref_count--;
- has_initializer = FALSE;
- }
- return has_initializer;
- prop_error:
- JS_FreeAtom(s->ctx, prop_name);
- var_error:
- JS_FreeAtom(s->ctx, var_name);
- return -1;
- }
- typedef enum FuncCallType {
- FUNC_CALL_NORMAL,
- FUNC_CALL_NEW,
- FUNC_CALL_SUPER_CTOR,
- FUNC_CALL_TEMPLATE,
- } FuncCallType;
- static void optional_chain_test(JSParseState *s, int *poptional_chaining_label,
- int drop_count)
- {
- int label_next, i;
- if (*poptional_chaining_label < 0)
- *poptional_chaining_label = new_label(s);
- /* XXX: could be more efficient with a specific opcode */
- emit_op(s, OP_dup);
- emit_op(s, OP_is_undefined_or_null);
- label_next = emit_goto(s, OP_if_false, -1);
- for(i = 0; i < drop_count; i++)
- emit_op(s, OP_drop);
- emit_op(s, OP_undefined);
- emit_goto(s, OP_goto, *poptional_chaining_label);
- emit_label(s, label_next);
- }
- /* allowed parse_flags: PF_POSTFIX_CALL */
- static __exception int js_parse_postfix_expr(JSParseState *s, int parse_flags)
- {
- FuncCallType call_type;
- int optional_chaining_label;
- BOOL accept_lparen = (parse_flags & PF_POSTFIX_CALL) != 0;
- const uint8_t *op_token_ptr;
-
- call_type = FUNC_CALL_NORMAL;
- switch(s->token.val) {
- case TOK_NUMBER:
- {
- JSValue val;
- val = s->token.u.num.val;
- if (JS_VALUE_GET_TAG(val) == JS_TAG_INT) {
- emit_op(s, OP_push_i32);
- emit_u32(s, JS_VALUE_GET_INT(val));
- } else if (JS_VALUE_GET_TAG(val) == JS_TAG_SHORT_BIG_INT) {
- int64_t v;
- v = JS_VALUE_GET_SHORT_BIG_INT(val);
- if (v >= INT32_MIN && v <= INT32_MAX) {
- emit_op(s, OP_push_bigint_i32);
- emit_u32(s, v);
- } else {
- goto large_number;
- }
- } else {
- large_number:
- if (emit_push_const(s, val, 0) < 0)
- return -1;
- }
- }
- if (next_token(s))
- return -1;
- break;
- case TOK_TEMPLATE:
- if (js_parse_template(s, 0, NULL))
- return -1;
- break;
- case TOK_STRING:
- if (emit_push_const(s, s->token.u.str.str, 1))
- return -1;
- if (next_token(s))
- return -1;
- break;
- case TOK_DIV_ASSIGN:
- s->buf_ptr -= 2;
- goto parse_regexp;
- case '/':
- s->buf_ptr--;
- parse_regexp:
- {
- JSValue str;
- int ret;
- if (!s->ctx->compile_regexp)
- return js_parse_error(s, "RegExp are not supported");
- /* the previous token is '/' or '/=', so no need to free */
- if (js_parse_regexp(s))
- return -1;
- ret = emit_push_const(s, s->token.u.regexp.body, 0);
- str = s->ctx->compile_regexp(s->ctx, s->token.u.regexp.body,
- s->token.u.regexp.flags);
- if (JS_IsException(str)) {
- /* add the line number info */
- int line_num, col_num;
- line_num = get_line_col(&col_num, s->buf_start, s->token.ptr - s->buf_start);
- build_backtrace(s->ctx, s->ctx->rt->current_exception,
- s->filename, line_num + 1, col_num + 1, 0);
- return -1;
- }
- ret = emit_push_const(s, str, 0);
- JS_FreeValue(s->ctx, str);
- if (ret)
- return -1;
- /* we use a specific opcode to be sure the correct
- function is called (otherwise the bytecode would have
- to be verified by the RegExp constructor) */
- emit_op(s, OP_regexp);
- if (next_token(s))
- return -1;
- }
- break;
- case '(':
- if (js_parse_expr_paren(s))
- return -1;
- break;
- case TOK_FUNCTION:
- if (js_parse_function_decl(s, JS_PARSE_FUNC_EXPR,
- JS_FUNC_NORMAL, JS_ATOM_NULL,
- s->token.ptr))
- return -1;
- break;
- case TOK_CLASS:
- if (js_parse_class(s, TRUE, JS_PARSE_EXPORT_NONE))
- return -1;
- break;
- case TOK_NULL:
- if (next_token(s))
- return -1;
- emit_op(s, OP_null);
- break;
- case TOK_THIS:
- if (next_token(s))
- return -1;
- emit_op(s, OP_scope_get_var);
- emit_atom(s, JS_ATOM_this);
- emit_u16(s, 0);
- break;
- case TOK_FALSE:
- if (next_token(s))
- return -1;
- emit_op(s, OP_push_false);
- break;
- case TOK_TRUE:
- if (next_token(s))
- return -1;
- emit_op(s, OP_push_true);
- break;
- case TOK_IDENT:
- {
- JSAtom name;
- const uint8_t *source_ptr;
- if (s->token.u.ident.is_reserved) {
- return js_parse_error_reserved_identifier(s);
- }
- source_ptr = s->token.ptr;
- if (token_is_pseudo_keyword(s, JS_ATOM_async) &&
- peek_token(s, TRUE) != '\n') {
- if (next_token(s))
- return -1;
- if (s->token.val == TOK_FUNCTION) {
- if (js_parse_function_decl(s, JS_PARSE_FUNC_EXPR,
- JS_FUNC_ASYNC, JS_ATOM_NULL,
- source_ptr))
- return -1;
- } else {
- name = JS_DupAtom(s->ctx, JS_ATOM_async);
- goto do_get_var;
- }
- } else {
- if (s->token.u.ident.atom == JS_ATOM_arguments &&
- !s->cur_func->arguments_allowed) {
- js_parse_error(s, "'arguments' identifier is not allowed in class field initializer");
- return -1;
- }
- name = JS_DupAtom(s->ctx, s->token.u.ident.atom);
- if (next_token(s)) {
- JS_FreeAtom(s->ctx, name);
- return -1;
- }
- do_get_var:
- emit_source_pos(s, source_ptr);
- emit_op(s, OP_scope_get_var);
- emit_u32(s, name);
- emit_u16(s, s->cur_func->scope_level);
- }
- }
- break;
- case '{':
- case '[':
- if (s->token.val == '{') {
- if (js_parse_object_literal(s))
- return -1;
- } else {
- if (js_parse_array_literal(s))
- return -1;
- }
- break;
- case TOK_NEW:
- if (next_token(s))
- return -1;
- if (s->token.val == '.') {
- if (next_token(s))
- return -1;
- if (!token_is_pseudo_keyword(s, JS_ATOM_target))
- return js_parse_error(s, "expecting target");
- if (!s->cur_func->new_target_allowed)
- return js_parse_error(s, "new.target only allowed within functions");
- if (next_token(s))
- return -1;
- emit_op(s, OP_scope_get_var);
- emit_atom(s, JS_ATOM_new_target);
- emit_u16(s, 0);
- } else {
- if (js_parse_postfix_expr(s, 0))
- return -1;
- accept_lparen = TRUE;
- if (s->token.val != '(') {
- /* new operator on an object */
- emit_source_pos(s, s->token.ptr);
- emit_op(s, OP_dup);
- emit_op(s, OP_call_constructor);
- emit_u16(s, 0);
- } else {
- call_type = FUNC_CALL_NEW;
- }
- }
- break;
- case TOK_SUPER:
- if (next_token(s))
- return -1;
- if (s->token.val == '(') {
- if (!s->cur_func->super_call_allowed)
- return js_parse_error(s, "super() is only valid in a derived class constructor");
- call_type = FUNC_CALL_SUPER_CTOR;
- } else if (s->token.val == '.' || s->token.val == '[') {
- if (!s->cur_func->super_allowed)
- return js_parse_error(s, "'super' is only valid in a method");
- emit_op(s, OP_scope_get_var);
- emit_atom(s, JS_ATOM_this);
- emit_u16(s, 0);
- emit_op(s, OP_scope_get_var);
- emit_atom(s, JS_ATOM_home_object);
- emit_u16(s, 0);
- emit_op(s, OP_get_super);
- } else {
- return js_parse_error(s, "invalid use of 'super'");
- }
- break;
- case TOK_IMPORT:
- if (next_token(s))
- return -1;
- if (s->token.val == '.') {
- if (next_token(s))
- return -1;
- if (!token_is_pseudo_keyword(s, JS_ATOM_meta))
- return js_parse_error(s, "meta expected");
- if (!s->is_module)
- return js_parse_error(s, "import.meta only valid in module code");
- if (next_token(s))
- return -1;
- emit_op(s, OP_special_object);
- emit_u8(s, OP_SPECIAL_OBJECT_IMPORT_META);
- } else {
- if (js_parse_expect(s, '('))
- return -1;
- if (!accept_lparen)
- return js_parse_error(s, "invalid use of 'import()'");
- if (js_parse_assign_expr(s))
- return -1;
- if (s->token.val == ',') {
- if (next_token(s))
- return -1;
- if (s->token.val != ')') {
- if (js_parse_assign_expr(s))
- return -1;
- /* accept a trailing comma */
- if (s->token.val == ',') {
- if (next_token(s))
- return -1;
- }
- } else {
- emit_op(s, OP_undefined);
- }
- } else {
- emit_op(s, OP_undefined);
- }
- if (js_parse_expect(s, ')'))
- return -1;
- emit_op(s, OP_import);
- }
- break;
- default:
- return js_parse_error(s, "unexpected token in expression: '%.*s'",
- (int)(s->buf_ptr - s->token.ptr), s->token.ptr);
- }
- optional_chaining_label = -1;
- for(;;) {
- JSFunctionDef *fd = s->cur_func;
- BOOL has_optional_chain = FALSE;
- if (s->token.val == TOK_QUESTION_MARK_DOT) {
- if ((parse_flags & PF_POSTFIX_CALL) == 0)
- return js_parse_error(s, "new keyword cannot be used with an optional chain");
- op_token_ptr = s->token.ptr;
- /* optional chaining */
- if (next_token(s))
- return -1;
- has_optional_chain = TRUE;
- if (s->token.val == '(' && accept_lparen) {
- goto parse_func_call;
- } else if (s->token.val == '[') {
- goto parse_array_access;
- } else {
- goto parse_property;
- }
- } else if (s->token.val == TOK_TEMPLATE &&
- call_type == FUNC_CALL_NORMAL) {
- if (optional_chaining_label >= 0) {
- return js_parse_error(s, "template literal cannot appear in an optional chain");
- }
- call_type = FUNC_CALL_TEMPLATE;
- op_token_ptr = s->token.ptr; /* XXX: check if right position */
- goto parse_func_call2;
- } else if (s->token.val == '(' && accept_lparen) {
- int opcode, arg_count, drop_count;
- /* function call */
- parse_func_call:
- op_token_ptr = s->token.ptr;
- if (next_token(s))
- return -1;
- if (call_type == FUNC_CALL_NORMAL) {
- parse_func_call2:
- switch(opcode = get_prev_opcode(fd)) {
- case OP_get_field:
- /* keep the object on the stack */
- fd->byte_code.buf[fd->last_opcode_pos] = OP_get_field2;
- drop_count = 2;
- break;
- case OP_get_field_opt_chain:
- {
- int opt_chain_label, next_label;
- opt_chain_label = get_u32(fd->byte_code.buf +
- fd->last_opcode_pos + 1 + 4 + 1);
- /* keep the object on the stack */
- fd->byte_code.buf[fd->last_opcode_pos] = OP_get_field2;
- fd->byte_code.size = fd->last_opcode_pos + 1 + 4;
- next_label = emit_goto(s, OP_goto, -1);
- emit_label(s, opt_chain_label);
- /* need an additional undefined value for the
- case where the optional field does not
- exists */
- emit_op(s, OP_undefined);
- emit_label(s, next_label);
- drop_count = 2;
- opcode = OP_get_field;
- }
- break;
- case OP_scope_get_private_field:
- /* keep the object on the stack */
- fd->byte_code.buf[fd->last_opcode_pos] = OP_scope_get_private_field2;
- drop_count = 2;
- break;
- case OP_get_array_el:
- /* keep the object on the stack */
- fd->byte_code.buf[fd->last_opcode_pos] = OP_get_array_el2;
- drop_count = 2;
- break;
- case OP_get_array_el_opt_chain:
- {
- int opt_chain_label, next_label;
- opt_chain_label = get_u32(fd->byte_code.buf +
- fd->last_opcode_pos + 1 + 1);
- /* keep the object on the stack */
- fd->byte_code.buf[fd->last_opcode_pos] = OP_get_array_el2;
- fd->byte_code.size = fd->last_opcode_pos + 1;
- next_label = emit_goto(s, OP_goto, -1);
- emit_label(s, opt_chain_label);
- /* need an additional undefined value for the
- case where the optional field does not
- exists */
- emit_op(s, OP_undefined);
- emit_label(s, next_label);
- drop_count = 2;
- opcode = OP_get_array_el;
- }
- break;
- case OP_scope_get_var:
- {
- JSAtom name;
- int scope;
- name = get_u32(fd->byte_code.buf + fd->last_opcode_pos + 1);
- scope = get_u16(fd->byte_code.buf + fd->last_opcode_pos + 5);
- if (name == JS_ATOM_eval && call_type == FUNC_CALL_NORMAL && !has_optional_chain) {
- /* direct 'eval' */
- opcode = OP_eval;
- } else {
- /* verify if function name resolves to a simple
- get_loc/get_arg: a function call inside a `with`
- statement can resolve to a method call of the
- `with` context object
- */
- /* XXX: always generate the OP_scope_get_ref
- and remove it in variable resolution
- pass ? */
- if (has_with_scope(fd, scope)) {
- opcode = OP_scope_get_ref;
- fd->byte_code.buf[fd->last_opcode_pos] = opcode;
- }
- }
- drop_count = 1;
- }
- break;
- case OP_get_super_value:
- fd->byte_code.buf[fd->last_opcode_pos] = OP_get_array_el;
- /* on stack: this func_obj */
- opcode = OP_get_array_el;
- drop_count = 2;
- break;
- default:
- opcode = OP_invalid;
- drop_count = 1;
- break;
- }
- if (has_optional_chain) {
- optional_chain_test(s, &optional_chaining_label,
- drop_count);
- }
- } else {
- opcode = OP_invalid;
- }
- if (call_type == FUNC_CALL_TEMPLATE) {
- if (js_parse_template(s, 1, &arg_count))
- return -1;
- goto emit_func_call;
- } else if (call_type == FUNC_CALL_SUPER_CTOR) {
- emit_op(s, OP_scope_get_var);
- emit_atom(s, JS_ATOM_this_active_func);
- emit_u16(s, 0);
- emit_op(s, OP_get_super);
- emit_op(s, OP_scope_get_var);
- emit_atom(s, JS_ATOM_new_target);
- emit_u16(s, 0);
- } else if (call_type == FUNC_CALL_NEW) {
- emit_op(s, OP_dup); /* new.target = function */
- }
- /* parse arguments */
- arg_count = 0;
- while (s->token.val != ')') {
- if (arg_count >= 65535) {
- return js_parse_error(s, "Too many call arguments");
- }
- if (s->token.val == TOK_ELLIPSIS)
- break;
- if (js_parse_assign_expr(s))
- return -1;
- arg_count++;
- if (s->token.val == ')')
- break;
- /* accept a trailing comma before the ')' */
- if (js_parse_expect(s, ','))
- return -1;
- }
- if (s->token.val == TOK_ELLIPSIS) {
- emit_op(s, OP_array_from);
- emit_u16(s, arg_count);
- emit_op(s, OP_push_i32);
- emit_u32(s, arg_count);
- /* on stack: array idx */
- while (s->token.val != ')') {
- if (s->token.val == TOK_ELLIPSIS) {
- if (next_token(s))
- return -1;
- if (js_parse_assign_expr(s))
- return -1;
- #if 1
- /* XXX: could pass is_last indicator? */
- emit_op(s, OP_append);
- #else
- int label_next, label_done;
- label_next = new_label(s);
- label_done = new_label(s);
- /* push enumerate object below array/idx pair */
- emit_op(s, OP_for_of_start);
- emit_op(s, OP_rot5l);
- emit_op(s, OP_rot5l);
- emit_label(s, label_next);
- /* on stack: enum_rec array idx */
- emit_op(s, OP_for_of_next);
- emit_u8(s, 2);
- emit_goto(s, OP_if_true, label_done);
- /* append element */
- /* enum_rec array idx val -> enum_rec array new_idx */
- emit_op(s, OP_define_array_el);
- emit_op(s, OP_inc);
- emit_goto(s, OP_goto, label_next);
- emit_label(s, label_done);
- /* close enumeration, drop enum_rec and idx */
- emit_op(s, OP_drop); /* drop undef */
- emit_op(s, OP_nip1); /* drop enum_rec */
- emit_op(s, OP_nip1);
- emit_op(s, OP_nip1);
- #endif
- } else {
- if (js_parse_assign_expr(s))
- return -1;
- /* array idx val */
- emit_op(s, OP_define_array_el);
- emit_op(s, OP_inc);
- }
- if (s->token.val == ')')
- break;
- /* accept a trailing comma before the ')' */
- if (js_parse_expect(s, ','))
- return -1;
- }
- if (next_token(s))
- return -1;
- /* drop the index */
- emit_op(s, OP_drop);
- emit_source_pos(s, op_token_ptr);
- /* apply function call */
- switch(opcode) {
- case OP_get_field:
- case OP_scope_get_private_field:
- case OP_get_array_el:
- case OP_scope_get_ref:
- /* obj func array -> func obj array */
- emit_op(s, OP_perm3);
- emit_op(s, OP_apply);
- emit_u16(s, call_type == FUNC_CALL_NEW);
- break;
- case OP_eval:
- emit_op(s, OP_apply_eval);
- emit_u16(s, fd->scope_level);
- fd->has_eval_call = TRUE;
- break;
- default:
- if (call_type == FUNC_CALL_SUPER_CTOR) {
- emit_op(s, OP_apply);
- emit_u16(s, 1);
- /* set the 'this' value */
- emit_op(s, OP_dup);
- emit_op(s, OP_scope_put_var_init);
- emit_atom(s, JS_ATOM_this);
- emit_u16(s, 0);
- emit_class_field_init(s);
- } else if (call_type == FUNC_CALL_NEW) {
- /* obj func array -> func obj array */
- emit_op(s, OP_perm3);
- emit_op(s, OP_apply);
- emit_u16(s, 1);
- } else {
- /* func array -> func undef array */
- emit_op(s, OP_undefined);
- emit_op(s, OP_swap);
- emit_op(s, OP_apply);
- emit_u16(s, 0);
- }
- break;
- }
- } else {
- if (next_token(s))
- return -1;
- emit_func_call:
- emit_source_pos(s, op_token_ptr);
- switch(opcode) {
- case OP_get_field:
- case OP_scope_get_private_field:
- case OP_get_array_el:
- case OP_scope_get_ref:
- emit_op(s, OP_call_method);
- emit_u16(s, arg_count);
- break;
- case OP_eval:
- emit_op(s, OP_eval);
- emit_u16(s, arg_count);
- emit_u16(s, fd->scope_level);
- fd->has_eval_call = TRUE;
- break;
- default:
- if (call_type == FUNC_CALL_SUPER_CTOR) {
- emit_op(s, OP_call_constructor);
- emit_u16(s, arg_count);
- /* set the 'this' value */
- emit_op(s, OP_dup);
- emit_op(s, OP_scope_put_var_init);
- emit_atom(s, JS_ATOM_this);
- emit_u16(s, 0);
- emit_class_field_init(s);
- } else if (call_type == FUNC_CALL_NEW) {
- emit_op(s, OP_call_constructor);
- emit_u16(s, arg_count);
- } else {
- emit_op(s, OP_call);
- emit_u16(s, arg_count);
- }
- break;
- }
- }
- call_type = FUNC_CALL_NORMAL;
- } else if (s->token.val == '.') {
- op_token_ptr = s->token.ptr;
- if (next_token(s))
- return -1;
- parse_property:
- emit_source_pos(s, op_token_ptr);
- if (s->token.val == TOK_PRIVATE_NAME) {
- /* private class field */
- if (get_prev_opcode(fd) == OP_get_super) {
- return js_parse_error(s, "private class field forbidden after super");
- }
- if (has_optional_chain) {
- optional_chain_test(s, &optional_chaining_label, 1);
- }
- emit_op(s, OP_scope_get_private_field);
- emit_atom(s, s->token.u.ident.atom);
- emit_u16(s, s->cur_func->scope_level);
- } else {
- if (!token_is_ident(s->token.val)) {
- return js_parse_error(s, "expecting field name");
- }
- if (get_prev_opcode(fd) == OP_get_super) {
- JSValue val;
- int ret;
- val = JS_AtomToValue(s->ctx, s->token.u.ident.atom);
- ret = emit_push_const(s, val, 1);
- JS_FreeValue(s->ctx, val);
- if (ret)
- return -1;
- emit_op(s, OP_get_super_value);
- } else {
- if (has_optional_chain) {
- optional_chain_test(s, &optional_chaining_label, 1);
- }
- emit_op(s, OP_get_field);
- emit_atom(s, s->token.u.ident.atom);
- }
- }
- if (next_token(s))
- return -1;
- } else if (s->token.val == '[') {
- int prev_op;
- op_token_ptr = s->token.ptr;
- parse_array_access:
- prev_op = get_prev_opcode(fd);
- if (has_optional_chain) {
- optional_chain_test(s, &optional_chaining_label, 1);
- }
- if (next_token(s))
- return -1;
- if (js_parse_expr(s))
- return -1;
- if (js_parse_expect(s, ']'))
- return -1;
- emit_source_pos(s, op_token_ptr);
- if (prev_op == OP_get_super) {
- emit_op(s, OP_get_super_value);
- } else {
- emit_op(s, OP_get_array_el);
- }
- } else {
- break;
- }
- }
- if (optional_chaining_label >= 0) {
- JSFunctionDef *fd = s->cur_func;
- int opcode;
- emit_label_raw(s, optional_chaining_label);
- /* modify the last opcode so that it is an indicator of an
- optional chain */
- opcode = get_prev_opcode(fd);
- if (opcode == OP_get_field || opcode == OP_get_array_el) {
- if (opcode == OP_get_field)
- opcode = OP_get_field_opt_chain;
- else
- opcode = OP_get_array_el_opt_chain;
- fd->byte_code.buf[fd->last_opcode_pos] = opcode;
- } else {
- fd->last_opcode_pos = -1;
- }
- }
- return 0;
- }
- static __exception int js_parse_delete(JSParseState *s)
- {
- JSFunctionDef *fd = s->cur_func;
- JSAtom name;
- int opcode;
- if (next_token(s))
- return -1;
- if (js_parse_unary(s, PF_POW_FORBIDDEN))
- return -1;
- switch(opcode = get_prev_opcode(fd)) {
- case OP_get_field:
- case OP_get_field_opt_chain:
- {
- JSValue val;
- int ret, opt_chain_label, next_label;
- if (opcode == OP_get_field_opt_chain) {
- opt_chain_label = get_u32(fd->byte_code.buf +
- fd->last_opcode_pos + 1 + 4 + 1);
- } else {
- opt_chain_label = -1;
- }
- name = get_u32(fd->byte_code.buf + fd->last_opcode_pos + 1);
- fd->byte_code.size = fd->last_opcode_pos;
- val = JS_AtomToValue(s->ctx, name);
- ret = emit_push_const(s, val, 1);
- JS_FreeValue(s->ctx, val);
- JS_FreeAtom(s->ctx, name);
- if (ret)
- return ret;
- emit_op(s, OP_delete);
- if (opt_chain_label >= 0) {
- next_label = emit_goto(s, OP_goto, -1);
- emit_label(s, opt_chain_label);
- /* if the optional chain is not taken, return 'true' */
- emit_op(s, OP_drop);
- emit_op(s, OP_push_true);
- emit_label(s, next_label);
- }
- fd->last_opcode_pos = -1;
- }
- break;
- case OP_get_array_el:
- fd->byte_code.size = fd->last_opcode_pos;
- fd->last_opcode_pos = -1;
- emit_op(s, OP_delete);
- break;
- case OP_get_array_el_opt_chain:
- {
- int opt_chain_label, next_label;
- opt_chain_label = get_u32(fd->byte_code.buf +
- fd->last_opcode_pos + 1 + 1);
- fd->byte_code.size = fd->last_opcode_pos;
- emit_op(s, OP_delete);
- next_label = emit_goto(s, OP_goto, -1);
- emit_label(s, opt_chain_label);
- /* if the optional chain is not taken, return 'true' */
- emit_op(s, OP_drop);
- emit_op(s, OP_push_true);
- emit_label(s, next_label);
- fd->last_opcode_pos = -1;
- }
- break;
- case OP_scope_get_var:
- /* 'delete this': this is not a reference */
- name = get_u32(fd->byte_code.buf + fd->last_opcode_pos + 1);
- if (name == JS_ATOM_this || name == JS_ATOM_new_target)
- goto ret_true;
- if (fd->js_mode & JS_MODE_STRICT) {
- return js_parse_error(s, "cannot delete a direct reference in strict mode");
- } else {
- fd->byte_code.buf[fd->last_opcode_pos] = OP_scope_delete_var;
- }
- break;
- case OP_scope_get_private_field:
- return js_parse_error(s, "cannot delete a private class field");
- case OP_get_super_value:
- fd->byte_code.size = fd->last_opcode_pos;
- fd->last_opcode_pos = -1;
- emit_op(s, OP_throw_error);
- emit_atom(s, JS_ATOM_NULL);
- emit_u8(s, JS_THROW_ERROR_DELETE_SUPER);
- break;
- default:
- ret_true:
- emit_op(s, OP_drop);
- emit_op(s, OP_push_true);
- break;
- }
- return 0;
- }
- /* allowed parse_flags: PF_POW_ALLOWED, PF_POW_FORBIDDEN */
- static __exception int js_parse_unary(JSParseState *s, int parse_flags)
- {
- int op;
- const uint8_t *op_token_ptr;
- switch(s->token.val) {
- case '+':
- case '-':
- case '!':
- case '~':
- case TOK_VOID:
- op_token_ptr = s->token.ptr;
- op = s->token.val;
- if (next_token(s))
- return -1;
- if (js_parse_unary(s, PF_POW_FORBIDDEN))
- return -1;
- switch(op) {
- case '-':
- emit_source_pos(s, op_token_ptr);
- emit_op(s, OP_neg);
- break;
- case '+':
- emit_source_pos(s, op_token_ptr);
- emit_op(s, OP_plus);
- break;
- case '!':
- emit_op(s, OP_lnot);
- break;
- case '~':
- emit_source_pos(s, op_token_ptr);
- emit_op(s, OP_not);
- break;
- case TOK_VOID:
- emit_op(s, OP_drop);
- emit_op(s, OP_undefined);
- break;
- default:
- abort();
- }
- parse_flags = 0;
- break;
- case TOK_DEC:
- case TOK_INC:
- {
- int opcode, op, scope, label;
- JSAtom name;
- op = s->token.val;
- op_token_ptr = s->token.ptr;
- if (next_token(s))
- return -1;
- if (js_parse_unary(s, 0))
- return -1;
- if (get_lvalue(s, &opcode, &scope, &name, &label, NULL, TRUE, op))
- return -1;
- emit_source_pos(s, op_token_ptr);
- emit_op(s, OP_dec + op - TOK_DEC);
- put_lvalue(s, opcode, scope, name, label, PUT_LVALUE_KEEP_TOP,
- FALSE);
- }
- break;
- case TOK_TYPEOF:
- {
- JSFunctionDef *fd;
- if (next_token(s))
- return -1;
- if (js_parse_unary(s, PF_POW_FORBIDDEN))
- return -1;
- /* reference access should not return an exception, so we
- patch the get_var */
- fd = s->cur_func;
- if (get_prev_opcode(fd) == OP_scope_get_var) {
- fd->byte_code.buf[fd->last_opcode_pos] = OP_scope_get_var_undef;
- }
- emit_op(s, OP_typeof);
- parse_flags = 0;
- }
- break;
- case TOK_DELETE:
- if (js_parse_delete(s))
- return -1;
- parse_flags = 0;
- break;
- case TOK_AWAIT:
- if (!(s->cur_func->func_kind & JS_FUNC_ASYNC))
- return js_parse_error(s, "unexpected 'await' keyword");
- if (!s->cur_func->in_function_body)
- return js_parse_error(s, "await in default expression");
- if (next_token(s))
- return -1;
- if (js_parse_unary(s, PF_POW_FORBIDDEN))
- return -1;
- s->cur_func->has_await = TRUE;
- emit_op(s, OP_await);
- parse_flags = 0;
- break;
- default:
- if (js_parse_postfix_expr(s, PF_POSTFIX_CALL))
- return -1;
- if (!s->got_lf &&
- (s->token.val == TOK_DEC || s->token.val == TOK_INC)) {
- int opcode, op, scope, label;
- JSAtom name;
- op = s->token.val;
- op_token_ptr = s->token.ptr;
- if (get_lvalue(s, &opcode, &scope, &name, &label, NULL, TRUE, op))
- return -1;
- emit_source_pos(s, op_token_ptr);
- emit_op(s, OP_post_dec + op - TOK_DEC);
- put_lvalue(s, opcode, scope, name, label, PUT_LVALUE_KEEP_SECOND,
- FALSE);
- if (next_token(s))
- return -1;
- }
- break;
- }
- if (parse_flags & (PF_POW_ALLOWED | PF_POW_FORBIDDEN)) {
- if (s->token.val == TOK_POW) {
- /* Strict ES7 exponentiation syntax rules: To solve
- conficting semantics between different implementations
- regarding the precedence of prefix operators and the
- postifx exponential, ES7 specifies that -2**2 is a
- syntax error. */
- if (parse_flags & PF_POW_FORBIDDEN) {
- JS_ThrowSyntaxError(s->ctx, "unparenthesized unary expression can't appear on the left-hand side of '**'");
- return -1;
- }
- op_token_ptr = s->token.ptr;
- if (next_token(s))
- return -1;
- if (js_parse_unary(s, PF_POW_ALLOWED))
- return -1;
- emit_source_pos(s, op_token_ptr);
- emit_op(s, OP_pow);
- }
- }
- return 0;
- }
- /* allowed parse_flags: PF_IN_ACCEPTED */
- static __exception int js_parse_expr_binary(JSParseState *s, int level,
- int parse_flags)
- {
- int op, opcode;
- const uint8_t *op_token_ptr;
-
- if (level == 0) {
- return js_parse_unary(s, PF_POW_ALLOWED);
- } else if (s->token.val == TOK_PRIVATE_NAME &&
- (parse_flags & PF_IN_ACCEPTED) && level == 4 &&
- peek_token(s, FALSE) == TOK_IN) {
- JSAtom atom;
- atom = JS_DupAtom(s->ctx, s->token.u.ident.atom);
- if (next_token(s))
- goto fail_private_in;
- if (s->token.val != TOK_IN)
- goto fail_private_in;
- if (next_token(s))
- goto fail_private_in;
- if (js_parse_expr_binary(s, level - 1, parse_flags)) {
- fail_private_in:
- JS_FreeAtom(s->ctx, atom);
- return -1;
- }
- emit_op(s, OP_scope_in_private_field);
- emit_atom(s, atom);
- emit_u16(s, s->cur_func->scope_level);
- JS_FreeAtom(s->ctx, atom);
- return 0;
- } else {
- if (js_parse_expr_binary(s, level - 1, parse_flags))
- return -1;
- }
- for(;;) {
- op = s->token.val;
- op_token_ptr = s->token.ptr;
- switch(level) {
- case 1:
- switch(op) {
- case '*':
- opcode = OP_mul;
- break;
- case '/':
- opcode = OP_div;
- break;
- case '%':
- opcode = OP_mod;
- break;
- default:
- return 0;
- }
- break;
- case 2:
- switch(op) {
- case '+':
- opcode = OP_add;
- break;
- case '-':
- opcode = OP_sub;
- break;
- default:
- return 0;
- }
- break;
- case 3:
- switch(op) {
- case TOK_SHL:
- opcode = OP_shl;
- break;
- case TOK_SAR:
- opcode = OP_sar;
- break;
- case TOK_SHR:
- opcode = OP_shr;
- break;
- default:
- return 0;
- }
- break;
- case 4:
- switch(op) {
- case '<':
- opcode = OP_lt;
- break;
- case '>':
- opcode = OP_gt;
- break;
- case TOK_LTE:
- opcode = OP_lte;
- break;
- case TOK_GTE:
- opcode = OP_gte;
- break;
- case TOK_INSTANCEOF:
- opcode = OP_instanceof;
- break;
- case TOK_IN:
- if (parse_flags & PF_IN_ACCEPTED) {
- opcode = OP_in;
- } else {
- return 0;
- }
- break;
- default:
- return 0;
- }
- break;
- case 5:
- switch(op) {
- case TOK_EQ:
- opcode = OP_eq;
- break;
- case TOK_NEQ:
- opcode = OP_neq;
- break;
- case TOK_STRICT_EQ:
- opcode = OP_strict_eq;
- break;
- case TOK_STRICT_NEQ:
- opcode = OP_strict_neq;
- break;
- default:
- return 0;
- }
- break;
- case 6:
- switch(op) {
- case '&':
- opcode = OP_and;
- break;
- default:
- return 0;
- }
- break;
- case 7:
- switch(op) {
- case '^':
- opcode = OP_xor;
- break;
- default:
- return 0;
- }
- break;
- case 8:
- switch(op) {
- case '|':
- opcode = OP_or;
- break;
- default:
- return 0;
- }
- break;
- default:
- abort();
- }
- if (next_token(s))
- return -1;
- if (js_parse_expr_binary(s, level - 1, parse_flags))
- return -1;
- emit_source_pos(s, op_token_ptr);
- emit_op(s, opcode);
- }
- return 0;
- }
- /* allowed parse_flags: PF_IN_ACCEPTED */
- static __exception int js_parse_logical_and_or(JSParseState *s, int op,
- int parse_flags)
- {
- int label1;
- if (op == TOK_LAND) {
- if (js_parse_expr_binary(s, 8, parse_flags))
- return -1;
- } else {
- if (js_parse_logical_and_or(s, TOK_LAND, parse_flags))
- return -1;
- }
- if (s->token.val == op) {
- label1 = new_label(s);
- for(;;) {
- if (next_token(s))
- return -1;
- emit_op(s, OP_dup);
- emit_goto(s, op == TOK_LAND ? OP_if_false : OP_if_true, label1);
- emit_op(s, OP_drop);
- if (op == TOK_LAND) {
- if (js_parse_expr_binary(s, 8, parse_flags))
- return -1;
- } else {
- if (js_parse_logical_and_or(s, TOK_LAND,
- parse_flags))
- return -1;
- }
- if (s->token.val != op) {
- if (s->token.val == TOK_DOUBLE_QUESTION_MARK)
- return js_parse_error(s, "cannot mix ?? with && or ||");
- break;
- }
- }
- emit_label(s, label1);
- }
- return 0;
- }
- static __exception int js_parse_coalesce_expr(JSParseState *s, int parse_flags)
- {
- int label1;
- if (js_parse_logical_and_or(s, TOK_LOR, parse_flags))
- return -1;
- if (s->token.val == TOK_DOUBLE_QUESTION_MARK) {
- label1 = new_label(s);
- for(;;) {
- if (next_token(s))
- return -1;
- emit_op(s, OP_dup);
- emit_op(s, OP_is_undefined_or_null);
- emit_goto(s, OP_if_false, label1);
- emit_op(s, OP_drop);
- if (js_parse_expr_binary(s, 8, parse_flags))
- return -1;
- if (s->token.val != TOK_DOUBLE_QUESTION_MARK)
- break;
- }
- emit_label(s, label1);
- }
- return 0;
- }
- /* allowed parse_flags: PF_IN_ACCEPTED */
- static __exception int js_parse_cond_expr(JSParseState *s, int parse_flags)
- {
- int label1, label2;
- if (js_parse_coalesce_expr(s, parse_flags))
- return -1;
- if (s->token.val == '?') {
- if (next_token(s))
- return -1;
- label1 = emit_goto(s, OP_if_false, -1);
- if (js_parse_assign_expr(s))
- return -1;
- if (js_parse_expect(s, ':'))
- return -1;
- label2 = emit_goto(s, OP_goto, -1);
- emit_label(s, label1);
- if (js_parse_assign_expr2(s, parse_flags & PF_IN_ACCEPTED))
- return -1;
- emit_label(s, label2);
- }
- return 0;
- }
- /* allowed parse_flags: PF_IN_ACCEPTED */
- static __exception int js_parse_assign_expr2(JSParseState *s, int parse_flags)
- {
- int opcode, op, scope, skip_bits;
- JSAtom name0 = JS_ATOM_NULL;
- JSAtom name;
- if (s->token.val == TOK_YIELD) {
- BOOL is_star = FALSE, is_async;
- if (!(s->cur_func->func_kind & JS_FUNC_GENERATOR))
- return js_parse_error(s, "unexpected 'yield' keyword");
- if (!s->cur_func->in_function_body)
- return js_parse_error(s, "yield in default expression");
- if (next_token(s))
- return -1;
- /* XXX: is there a better method to detect 'yield' without
- parameters ? */
- if (s->token.val != ';' && s->token.val != ')' &&
- s->token.val != ']' && s->token.val != '}' &&
- s->token.val != ',' && s->token.val != ':' && !s->got_lf) {
- if (s->token.val == '*') {
- is_star = TRUE;
- if (next_token(s))
- return -1;
- }
- if (js_parse_assign_expr2(s, parse_flags))
- return -1;
- } else {
- emit_op(s, OP_undefined);
- }
- is_async = (s->cur_func->func_kind == JS_FUNC_ASYNC_GENERATOR);
- if (is_star) {
- int label_loop, label_return, label_next;
- int label_return1, label_yield, label_throw, label_throw1;
- int label_throw2;
- label_loop = new_label(s);
- label_yield = new_label(s);
- emit_op(s, is_async ? OP_for_await_of_start : OP_for_of_start);
- /* remove the catch offset (XXX: could avoid pushing back
- undefined) */
- emit_op(s, OP_drop);
- emit_op(s, OP_undefined);
- emit_op(s, OP_undefined); /* initial value */
- emit_label(s, label_loop);
- emit_op(s, OP_iterator_next);
- if (is_async)
- emit_op(s, OP_await);
- emit_op(s, OP_iterator_check_object);
- emit_op(s, OP_get_field2);
- emit_atom(s, JS_ATOM_done);
- label_next = emit_goto(s, OP_if_true, -1); /* end of loop */
- emit_label(s, label_yield);
- if (is_async) {
- /* OP_async_yield_star takes the value as parameter */
- emit_op(s, OP_get_field);
- emit_atom(s, JS_ATOM_value);
- emit_op(s, OP_async_yield_star);
- } else {
- /* OP_yield_star takes (value, done) as parameter */
- emit_op(s, OP_yield_star);
- }
- emit_op(s, OP_dup);
- label_return = emit_goto(s, OP_if_true, -1);
- emit_op(s, OP_drop);
- emit_goto(s, OP_goto, label_loop);
- emit_label(s, label_return);
- emit_op(s, OP_push_i32);
- emit_u32(s, 2);
- emit_op(s, OP_strict_eq);
- label_throw = emit_goto(s, OP_if_true, -1);
- /* return handling */
- if (is_async)
- emit_op(s, OP_await);
- emit_op(s, OP_iterator_call);
- emit_u8(s, 0);
- label_return1 = emit_goto(s, OP_if_true, -1);
- if (is_async)
- emit_op(s, OP_await);
- emit_op(s, OP_iterator_check_object);
- emit_op(s, OP_get_field2);
- emit_atom(s, JS_ATOM_done);
- emit_goto(s, OP_if_false, label_yield);
- emit_op(s, OP_get_field);
- emit_atom(s, JS_ATOM_value);
- emit_label(s, label_return1);
- emit_op(s, OP_nip);
- emit_op(s, OP_nip);
- emit_op(s, OP_nip);
- emit_return(s, TRUE);
- /* throw handling */
- emit_label(s, label_throw);
- emit_op(s, OP_iterator_call);
- emit_u8(s, 1);
- label_throw1 = emit_goto(s, OP_if_true, -1);
- if (is_async)
- emit_op(s, OP_await);
- emit_op(s, OP_iterator_check_object);
- emit_op(s, OP_get_field2);
- emit_atom(s, JS_ATOM_done);
- emit_goto(s, OP_if_false, label_yield);
- emit_goto(s, OP_goto, label_next);
- /* close the iterator and throw a type error exception */
- emit_label(s, label_throw1);
- emit_op(s, OP_iterator_call);
- emit_u8(s, 2);
- label_throw2 = emit_goto(s, OP_if_true, -1);
- if (is_async)
- emit_op(s, OP_await);
- emit_label(s, label_throw2);
- emit_op(s, OP_throw_error);
- emit_atom(s, JS_ATOM_NULL);
- emit_u8(s, JS_THROW_ERROR_ITERATOR_THROW);
- emit_label(s, label_next);
- emit_op(s, OP_get_field);
- emit_atom(s, JS_ATOM_value);
- emit_op(s, OP_nip); /* keep the value associated with
- done = true */
- emit_op(s, OP_nip);
- emit_op(s, OP_nip);
- } else {
- int label_next;
- if (is_async)
- emit_op(s, OP_await);
- emit_op(s, OP_yield);
- label_next = emit_goto(s, OP_if_false, -1);
- emit_return(s, TRUE);
- emit_label(s, label_next);
- }
- return 0;
- } else if (s->token.val == '(' &&
- js_parse_skip_parens_token(s, NULL, TRUE) == TOK_ARROW) {
- return js_parse_function_decl(s, JS_PARSE_FUNC_ARROW,
- JS_FUNC_NORMAL, JS_ATOM_NULL,
- s->token.ptr);
- } else if (token_is_pseudo_keyword(s, JS_ATOM_async)) {
- const uint8_t *source_ptr;
- int tok;
- JSParsePos pos;
- /* fast test */
- tok = peek_token(s, TRUE);
- if (tok == TOK_FUNCTION || tok == '\n')
- goto next;
- source_ptr = s->token.ptr;
- js_parse_get_pos(s, &pos);
- if (next_token(s))
- return -1;
- if ((s->token.val == '(' &&
- js_parse_skip_parens_token(s, NULL, TRUE) == TOK_ARROW) ||
- (s->token.val == TOK_IDENT && !s->token.u.ident.is_reserved &&
- peek_token(s, TRUE) == TOK_ARROW)) {
- return js_parse_function_decl(s, JS_PARSE_FUNC_ARROW,
- JS_FUNC_ASYNC, JS_ATOM_NULL,
- source_ptr);
- } else {
- /* undo the token parsing */
- if (js_parse_seek_token(s, &pos))
- return -1;
- }
- } else if (s->token.val == TOK_IDENT &&
- peek_token(s, TRUE) == TOK_ARROW) {
- return js_parse_function_decl(s, JS_PARSE_FUNC_ARROW,
- JS_FUNC_NORMAL, JS_ATOM_NULL,
- s->token.ptr);
- } else if ((s->token.val == '{' || s->token.val == '[') &&
- js_parse_skip_parens_token(s, &skip_bits, FALSE) == '=') {
- if (js_parse_destructuring_element(s, 0, 0, FALSE, skip_bits & SKIP_HAS_ELLIPSIS, TRUE, FALSE) < 0)
- return -1;
- return 0;
- }
- next:
- if (s->token.val == TOK_IDENT) {
- /* name0 is used to check for OP_set_name pattern, not duplicated */
- name0 = s->token.u.ident.atom;
- }
- if (js_parse_cond_expr(s, parse_flags))
- return -1;
- op = s->token.val;
- if (op == '=' || (op >= TOK_MUL_ASSIGN && op <= TOK_POW_ASSIGN)) {
- int label;
- const uint8_t *op_token_ptr;
- op_token_ptr = s->token.ptr;
- if (next_token(s))
- return -1;
- if (get_lvalue(s, &opcode, &scope, &name, &label, NULL, (op != '='), op) < 0)
- return -1;
- if (js_parse_assign_expr2(s, parse_flags)) {
- JS_FreeAtom(s->ctx, name);
- return -1;
- }
- if (op == '=') {
- if ((opcode == OP_get_ref_value || opcode == OP_scope_get_var) && name == name0) {
- set_object_name(s, name);
- }
- } else {
- static const uint8_t assign_opcodes[] = {
- OP_mul, OP_div, OP_mod, OP_add, OP_sub,
- OP_shl, OP_sar, OP_shr, OP_and, OP_xor, OP_or,
- OP_pow,
- };
- op = assign_opcodes[op - TOK_MUL_ASSIGN];
- emit_source_pos(s, op_token_ptr);
- emit_op(s, op);
- }
- put_lvalue(s, opcode, scope, name, label, PUT_LVALUE_KEEP_TOP, FALSE);
- } else if (op >= TOK_LAND_ASSIGN && op <= TOK_DOUBLE_QUESTION_MARK_ASSIGN) {
- int label, label1, depth_lvalue, label2;
- if (next_token(s))
- return -1;
- if (get_lvalue(s, &opcode, &scope, &name, &label,
- &depth_lvalue, TRUE, op) < 0)
- return -1;
- emit_op(s, OP_dup);
- if (op == TOK_DOUBLE_QUESTION_MARK_ASSIGN)
- emit_op(s, OP_is_undefined_or_null);
- label1 = emit_goto(s, op == TOK_LOR_ASSIGN ? OP_if_true : OP_if_false,
- -1);
- emit_op(s, OP_drop);
- if (js_parse_assign_expr2(s, parse_flags)) {
- JS_FreeAtom(s->ctx, name);
- return -1;
- }
- if ((opcode == OP_get_ref_value || opcode == OP_scope_get_var) && name == name0) {
- set_object_name(s, name);
- }
- switch(depth_lvalue) {
- case 0:
- emit_op(s, OP_dup);
- break;
- case 1:
- emit_op(s, OP_insert2);
- break;
- case 2:
- emit_op(s, OP_insert3);
- break;
- case 3:
- emit_op(s, OP_insert4);
- break;
- default:
- abort();
- }
- /* XXX: we disable the OP_put_ref_value optimization by not
- using put_lvalue() otherwise depth_lvalue is not correct */
- put_lvalue(s, opcode, scope, name, label, PUT_LVALUE_NOKEEP_DEPTH,
- FALSE);
- label2 = emit_goto(s, OP_goto, -1);
- emit_label(s, label1);
- /* remove the lvalue stack entries */
- while (depth_lvalue != 0) {
- emit_op(s, OP_nip);
- depth_lvalue--;
- }
- emit_label(s, label2);
- }
- return 0;
- }
- static __exception int js_parse_assign_expr(JSParseState *s)
- {
- return js_parse_assign_expr2(s, PF_IN_ACCEPTED);
- }
- /* allowed parse_flags: PF_IN_ACCEPTED */
- static __exception int js_parse_expr2(JSParseState *s, int parse_flags)
- {
- BOOL comma = FALSE;
- for(;;) {
- if (js_parse_assign_expr2(s, parse_flags))
- return -1;
- if (comma) {
- /* prevent get_lvalue from using the last expression
- as an lvalue. This also prevents the conversion of
- of get_var to get_ref for method lookup in function
- call inside `with` statement.
- */
- s->cur_func->last_opcode_pos = -1;
- }
- if (s->token.val != ',')
- break;
- comma = TRUE;
- if (next_token(s))
- return -1;
- emit_op(s, OP_drop);
- }
- return 0;
- }
- static __exception int js_parse_expr(JSParseState *s)
- {
- return js_parse_expr2(s, PF_IN_ACCEPTED);
- }
- static void push_break_entry(JSFunctionDef *fd, BlockEnv *be,
- JSAtom label_name,
- int label_break, int label_cont,
- int drop_count)
- {
- be->prev = fd->top_break;
- fd->top_break = be;
- be->label_name = label_name;
- be->label_break = label_break;
- be->label_cont = label_cont;
- be->drop_count = drop_count;
- be->label_finally = -1;
- be->scope_level = fd->scope_level;
- be->has_iterator = FALSE;
- be->is_regular_stmt = FALSE;
- }
- static void pop_break_entry(JSFunctionDef *fd)
- {
- BlockEnv *be;
- be = fd->top_break;
- fd->top_break = be->prev;
- }
- static __exception int emit_break(JSParseState *s, JSAtom name, int is_cont)
- {
- BlockEnv *top;
- int i, scope_level;
- scope_level = s->cur_func->scope_level;
- top = s->cur_func->top_break;
- while (top != NULL) {
- close_scopes(s, scope_level, top->scope_level);
- scope_level = top->scope_level;
- if (is_cont &&
- top->label_cont != -1 &&
- (name == JS_ATOM_NULL || top->label_name == name)) {
- /* continue stays inside the same block */
- emit_goto(s, OP_goto, top->label_cont);
- return 0;
- }
- if (!is_cont &&
- top->label_break != -1 &&
- ((name == JS_ATOM_NULL && !top->is_regular_stmt) ||
- top->label_name == name)) {
- emit_goto(s, OP_goto, top->label_break);
- return 0;
- }
- i = 0;
- if (top->has_iterator) {
- emit_op(s, OP_iterator_close);
- i += 3;
- }
- for(; i < top->drop_count; i++)
- emit_op(s, OP_drop);
- if (top->label_finally != -1) {
- /* must push dummy value to keep same stack depth */
- emit_op(s, OP_undefined);
- emit_goto(s, OP_gosub, top->label_finally);
- emit_op(s, OP_drop);
- }
- top = top->prev;
- }
- if (name == JS_ATOM_NULL) {
- if (is_cont)
- return js_parse_error(s, "continue must be inside loop");
- else
- return js_parse_error(s, "break must be inside loop or switch");
- } else {
- return js_parse_error(s, "break/continue label not found");
- }
- }
- /* execute the finally blocks before return */
- static void emit_return(JSParseState *s, BOOL hasval)
- {
- BlockEnv *top;
- if (s->cur_func->func_kind != JS_FUNC_NORMAL) {
- if (!hasval) {
- /* no value: direct return in case of async generator */
- emit_op(s, OP_undefined);
- hasval = TRUE;
- } else if (s->cur_func->func_kind == JS_FUNC_ASYNC_GENERATOR) {
- /* the await must be done before handling the "finally" in
- case it raises an exception */
- emit_op(s, OP_await);
- }
- }
- top = s->cur_func->top_break;
- while (top != NULL) {
- if (top->has_iterator || top->label_finally != -1) {
- if (!hasval) {
- emit_op(s, OP_undefined);
- hasval = TRUE;
- }
- /* Remove the stack elements up to and including the catch
- offset. When 'yield' is used in an expression we have
- no easy way to count them, so we use this specific
- instruction instead. */
- emit_op(s, OP_nip_catch);
- /* stack: iter_obj next ret_val */
- if (top->has_iterator) {
- if (s->cur_func->func_kind == JS_FUNC_ASYNC_GENERATOR) {
- int label_next, label_next2;
- emit_op(s, OP_nip); /* next */
- emit_op(s, OP_swap);
- emit_op(s, OP_get_field2);
- emit_atom(s, JS_ATOM_return);
- /* stack: iter_obj return_func */
- emit_op(s, OP_dup);
- emit_op(s, OP_is_undefined_or_null);
- label_next = emit_goto(s, OP_if_true, -1);
- emit_op(s, OP_call_method);
- emit_u16(s, 0);
- emit_op(s, OP_iterator_check_object);
- emit_op(s, OP_await);
- label_next2 = emit_goto(s, OP_goto, -1);
- emit_label(s, label_next);
- emit_op(s, OP_drop);
- emit_label(s, label_next2);
- emit_op(s, OP_drop);
- } else {
- emit_op(s, OP_rot3r);
- emit_op(s, OP_undefined); /* dummy catch offset */
- emit_op(s, OP_iterator_close);
- }
- } else {
- /* execute the "finally" block */
- emit_goto(s, OP_gosub, top->label_finally);
- }
- }
- top = top->prev;
- }
- if (s->cur_func->is_derived_class_constructor) {
- int label_return;
- /* 'this' can be uninitialized, so it may be accessed only if
- the derived class constructor does not return an object */
- if (hasval) {
- emit_op(s, OP_check_ctor_return);
- label_return = emit_goto(s, OP_if_false, -1);
- emit_op(s, OP_drop);
- } else {
- label_return = -1;
- }
- /* The error should be raised in the caller context, so we use
- a specific opcode */
- emit_op(s, OP_scope_get_var_checkthis);
- emit_atom(s, JS_ATOM_this);
- emit_u16(s, 0);
- emit_label(s, label_return);
- emit_op(s, OP_return);
- } else if (s->cur_func->func_kind != JS_FUNC_NORMAL) {
- emit_op(s, OP_return_async);
- } else {
- emit_op(s, hasval ? OP_return : OP_return_undef);
- }
- }
- #define DECL_MASK_FUNC (1 << 0) /* allow normal function declaration */
- /* ored with DECL_MASK_FUNC if function declarations are allowed with a label */
- #define DECL_MASK_FUNC_WITH_LABEL (1 << 1)
- #define DECL_MASK_OTHER (1 << 2) /* all other declarations */
- #define DECL_MASK_ALL (DECL_MASK_FUNC | DECL_MASK_FUNC_WITH_LABEL | DECL_MASK_OTHER)
- static __exception int js_parse_statement_or_decl(JSParseState *s,
- int decl_mask);
- static __exception int js_parse_statement(JSParseState *s)
- {
- return js_parse_statement_or_decl(s, 0);
- }
- static __exception int js_parse_block(JSParseState *s)
- {
- if (js_parse_expect(s, '{'))
- return -1;
- if (s->token.val != '}') {
- push_scope(s);
- for(;;) {
- if (js_parse_statement_or_decl(s, DECL_MASK_ALL))
- return -1;
- if (s->token.val == '}')
- break;
- }
- pop_scope(s);
- }
- if (next_token(s))
- return -1;
- return 0;
- }
- /* allowed parse_flags: PF_IN_ACCEPTED */
- static __exception int js_parse_var(JSParseState *s, int parse_flags, int tok,
- BOOL export_flag)
- {
- JSContext *ctx = s->ctx;
- JSFunctionDef *fd = s->cur_func;
- JSAtom name = JS_ATOM_NULL;
- for (;;) {
- if (s->token.val == TOK_IDENT) {
- if (s->token.u.ident.is_reserved) {
- return js_parse_error_reserved_identifier(s);
- }
- name = JS_DupAtom(ctx, s->token.u.ident.atom);
- if (name == JS_ATOM_let && (tok == TOK_LET || tok == TOK_CONST)) {
- js_parse_error(s, "'let' is not a valid lexical identifier");
- goto var_error;
- }
- if (next_token(s))
- goto var_error;
- if (js_define_var(s, name, tok))
- goto var_error;
- if (export_flag) {
- if (!add_export_entry(s, s->cur_func->module, name, name,
- JS_EXPORT_TYPE_LOCAL))
- goto var_error;
- }
- if (s->token.val == '=') {
- const uint8_t *source_ptr = s->token.ptr;
- if (next_token(s))
- goto var_error;
- if (need_var_reference(s, tok)) {
- /* Must make a reference for proper `with` semantics */
- int opcode, scope, label;
- JSAtom name1;
- emit_op(s, OP_scope_get_var);
- emit_atom(s, name);
- emit_u16(s, fd->scope_level);
- if (get_lvalue(s, &opcode, &scope, &name1, &label, NULL, FALSE, '=') < 0)
- goto var_error;
- if (js_parse_assign_expr2(s, parse_flags)) {
- JS_FreeAtom(ctx, name1);
- goto var_error;
- }
- set_object_name(s, name);
- emit_source_pos(s, source_ptr);
- put_lvalue(s, opcode, scope, name1, label,
- PUT_LVALUE_NOKEEP, FALSE);
- } else {
- if (js_parse_assign_expr2(s, parse_flags))
- goto var_error;
- set_object_name(s, name);
- emit_source_pos(s, source_ptr);
- emit_op(s, (tok == TOK_CONST || tok == TOK_LET) ?
- OP_scope_put_var_init : OP_scope_put_var);
- emit_atom(s, name);
- emit_u16(s, fd->scope_level);
- }
- } else {
- if (tok == TOK_CONST) {
- js_parse_error(s, "missing initializer for const variable");
- goto var_error;
- }
- if (tok == TOK_LET) {
- /* initialize lexical variable upon entering its scope */
- emit_op(s, OP_undefined);
- emit_op(s, OP_scope_put_var_init);
- emit_atom(s, name);
- emit_u16(s, fd->scope_level);
- }
- }
- JS_FreeAtom(ctx, name);
- } else {
- int skip_bits;
- if ((s->token.val == '[' || s->token.val == '{')
- && js_parse_skip_parens_token(s, &skip_bits, FALSE) == '=') {
- emit_op(s, OP_undefined);
- if (js_parse_destructuring_element(s, tok, 0, TRUE, skip_bits & SKIP_HAS_ELLIPSIS, TRUE, export_flag) < 0)
- return -1;
- } else {
- return js_parse_error(s, "variable name expected");
- }
- }
- if (s->token.val != ',')
- break;
- if (next_token(s))
- return -1;
- }
- return 0;
- var_error:
- JS_FreeAtom(ctx, name);
- return -1;
- }
- /* test if the current token is a label. Use simplistic look-ahead scanner */
- static BOOL is_label(JSParseState *s)
- {
- return (s->token.val == TOK_IDENT && !s->token.u.ident.is_reserved &&
- peek_token(s, FALSE) == ':');
- }
- /* test if the current token is a let keyword. Use simplistic look-ahead scanner */
- static int is_let(JSParseState *s, int decl_mask)
- {
- int res = FALSE;
- const uint8_t *last_token_ptr;
-
- if (token_is_pseudo_keyword(s, JS_ATOM_let)) {
- JSParsePos pos;
- js_parse_get_pos(s, &pos);
- for (;;) {
- last_token_ptr = s->token.ptr;
- if (next_token(s)) {
- res = -1;
- break;
- }
- if (s->token.val == '[') {
- /* let [ is a syntax restriction:
- it never introduces an ExpressionStatement */
- res = TRUE;
- break;
- }
- if (s->token.val == '{' ||
- (s->token.val == TOK_IDENT && !s->token.u.ident.is_reserved) ||
- s->token.val == TOK_LET ||
- s->token.val == TOK_YIELD ||
- s->token.val == TOK_AWAIT) {
- /* Check for possible ASI if not scanning for Declaration */
- /* XXX: should also check that `{` introduces a BindingPattern,
- but Firefox does not and rejects eval("let=1;let\n{if(1)2;}") */
- if (!has_lf_in_range(last_token_ptr, s->token.ptr) ||
- (decl_mask & DECL_MASK_OTHER)) {
- res = TRUE;
- break;
- }
- break;
- }
- break;
- }
- if (js_parse_seek_token(s, &pos)) {
- res = -1;
- }
- }
- return res;
- }
- /* XXX: handle IteratorClose when exiting the loop before the
- enumeration is done */
- static __exception int js_parse_for_in_of(JSParseState *s, int label_name,
- BOOL is_async)
- {
- JSContext *ctx = s->ctx;
- JSFunctionDef *fd = s->cur_func;
- JSAtom var_name;
- BOOL has_initializer, is_for_of, has_destructuring;
- int tok, tok1, opcode, scope, block_scope_level;
- int label_next, label_expr, label_cont, label_body, label_break;
- int pos_next, pos_expr;
- BlockEnv break_entry;
- has_initializer = FALSE;
- has_destructuring = FALSE;
- is_for_of = FALSE;
- block_scope_level = fd->scope_level;
- label_cont = new_label(s);
- label_body = new_label(s);
- label_break = new_label(s);
- label_next = new_label(s);
- /* create scope for the lexical variables declared in the enumeration
- expressions. XXX: Not completely correct because of weird capturing
- semantics in `for (i of o) a.push(function(){return i})` */
- push_scope(s);
- /* local for_in scope starts here so individual elements
- can be closed in statement. */
- push_break_entry(s->cur_func, &break_entry,
- label_name, label_break, label_cont, 1);
- break_entry.scope_level = block_scope_level;
- label_expr = emit_goto(s, OP_goto, -1);
- pos_next = s->cur_func->byte_code.size;
- emit_label(s, label_next);
- tok = s->token.val;
- switch (is_let(s, DECL_MASK_OTHER)) {
- case TRUE:
- tok = TOK_LET;
- break;
- case FALSE:
- break;
- default:
- return -1;
- }
- if (tok == TOK_VAR || tok == TOK_LET || tok == TOK_CONST) {
- if (next_token(s))
- return -1;
- if (!(s->token.val == TOK_IDENT && !s->token.u.ident.is_reserved)) {
- if (s->token.val == '[' || s->token.val == '{') {
- if (js_parse_destructuring_element(s, tok, 0, TRUE, -1, FALSE, FALSE) < 0)
- return -1;
- has_destructuring = TRUE;
- } else {
- return js_parse_error(s, "variable name expected");
- }
- var_name = JS_ATOM_NULL;
- } else {
- var_name = JS_DupAtom(ctx, s->token.u.ident.atom);
- if (next_token(s)) {
- JS_FreeAtom(s->ctx, var_name);
- return -1;
- }
- if (js_define_var(s, var_name, tok)) {
- JS_FreeAtom(s->ctx, var_name);
- return -1;
- }
- emit_op(s, (tok == TOK_CONST || tok == TOK_LET) ?
- OP_scope_put_var_init : OP_scope_put_var);
- emit_atom(s, var_name);
- emit_u16(s, fd->scope_level);
- }
- } else if (!is_async && token_is_pseudo_keyword(s, JS_ATOM_async) &&
- peek_token(s, FALSE) == TOK_OF) {
- return js_parse_error(s, "'for of' expression cannot start with 'async'");
- } else {
- int skip_bits;
- if ((s->token.val == '[' || s->token.val == '{')
- && ((tok1 = js_parse_skip_parens_token(s, &skip_bits, FALSE)) == TOK_IN || tok1 == TOK_OF)) {
- if (js_parse_destructuring_element(s, 0, 0, TRUE, skip_bits & SKIP_HAS_ELLIPSIS, TRUE, FALSE) < 0)
- return -1;
- } else {
- int lvalue_label;
- if (js_parse_left_hand_side_expr(s))
- return -1;
- if (get_lvalue(s, &opcode, &scope, &var_name, &lvalue_label,
- NULL, FALSE, TOK_FOR))
- return -1;
- put_lvalue(s, opcode, scope, var_name, lvalue_label,
- PUT_LVALUE_NOKEEP_BOTTOM, FALSE);
- }
- var_name = JS_ATOM_NULL;
- }
- emit_goto(s, OP_goto, label_body);
- pos_expr = s->cur_func->byte_code.size;
- emit_label(s, label_expr);
- if (s->token.val == '=') {
- /* XXX: potential scoping issue if inside `with` statement */
- has_initializer = TRUE;
- /* parse and evaluate initializer prior to evaluating the
- object (only used with "for in" with a non lexical variable
- in non strict mode */
- if (next_token(s) || js_parse_assign_expr2(s, 0)) {
- JS_FreeAtom(ctx, var_name);
- return -1;
- }
- if (var_name != JS_ATOM_NULL) {
- emit_op(s, OP_scope_put_var);
- emit_atom(s, var_name);
- emit_u16(s, fd->scope_level);
- }
- }
- JS_FreeAtom(ctx, var_name);
- if (token_is_pseudo_keyword(s, JS_ATOM_of)) {
- is_for_of = TRUE;
- if (has_initializer)
- goto initializer_error;
- } else if (s->token.val == TOK_IN) {
- if (is_async)
- return js_parse_error(s, "'for await' loop should be used with 'of'");
- if (has_initializer &&
- (tok != TOK_VAR || (fd->js_mode & JS_MODE_STRICT) ||
- has_destructuring)) {
- initializer_error:
- return js_parse_error(s, "a declaration in the head of a for-%s loop can't have an initializer",
- is_for_of ? "of" : "in");
- }
- } else {
- return js_parse_error(s, "expected 'of' or 'in' in for control expression");
- }
- if (next_token(s))
- return -1;
- if (is_for_of) {
- if (js_parse_assign_expr(s))
- return -1;
- } else {
- if (js_parse_expr(s))
- return -1;
- }
- /* close the scope after having evaluated the expression so that
- the TDZ values are in the closures */
- close_scopes(s, s->cur_func->scope_level, block_scope_level);
- if (is_for_of) {
- /* set has_iterator after the iterable expression is parsed so
- that a yield in the expression does not try to close a
- not-yet-created iterator */
- break_entry.has_iterator = TRUE;
- break_entry.drop_count += 2;
- if (is_async)
- emit_op(s, OP_for_await_of_start);
- else
- emit_op(s, OP_for_of_start);
- /* on stack: enum_rec */
- } else {
- emit_op(s, OP_for_in_start);
- /* on stack: enum_obj */
- }
- emit_goto(s, OP_goto, label_cont);
- if (js_parse_expect(s, ')'))
- return -1;
- if (OPTIMIZE) {
- /* move the `next` code here */
- DynBuf *bc = &s->cur_func->byte_code;
- int chunk_size = pos_expr - pos_next;
- int offset = bc->size - pos_next;
- int i;
- if (dbuf_claim(bc, chunk_size))
- return -1;
- dbuf_put(bc, bc->buf + pos_next, chunk_size);
- memset(bc->buf + pos_next, OP_nop, chunk_size);
- /* `next` part ends with a goto */
- s->cur_func->last_opcode_pos = bc->size - 5;
- /* relocate labels */
- for (i = label_cont; i < s->cur_func->label_count; i++) {
- LabelSlot *ls = &s->cur_func->label_slots[i];
- if (ls->pos >= pos_next && ls->pos < pos_expr)
- ls->pos += offset;
- }
- }
- emit_label(s, label_body);
- if (js_parse_statement(s))
- return -1;
- close_scopes(s, s->cur_func->scope_level, block_scope_level);
- emit_label(s, label_cont);
- if (is_for_of) {
- if (is_async) {
- /* stack: iter_obj next catch_offset */
- /* call the next method */
- emit_op(s, OP_for_await_of_next);
- /* get the result of the promise */
- emit_op(s, OP_await);
- /* unwrap the value and done values */
- emit_op(s, OP_iterator_get_value_done);
- } else {
- emit_op(s, OP_for_of_next);
- emit_u8(s, 0);
- }
- } else {
- emit_op(s, OP_for_in_next);
- }
- /* on stack: enum_rec / enum_obj value bool */
- emit_goto(s, OP_if_false, label_next);
- /* drop the undefined value from for_xx_next */
- emit_op(s, OP_drop);
- emit_label(s, label_break);
- if (is_for_of) {
- /* close and drop enum_rec */
- emit_op(s, OP_iterator_close);
- } else {
- emit_op(s, OP_drop);
- }
- pop_break_entry(s->cur_func);
- pop_scope(s);
- return 0;
- }
- static void set_eval_ret_undefined(JSParseState *s)
- {
- if (s->cur_func->eval_ret_idx >= 0) {
- emit_op(s, OP_undefined);
- emit_op(s, OP_put_loc);
- emit_u16(s, s->cur_func->eval_ret_idx);
- }
- }
- static __exception int js_parse_statement_or_decl(JSParseState *s,
- int decl_mask)
- {
- JSContext *ctx = s->ctx;
- JSAtom label_name;
- int tok;
- /* specific label handling */
- /* XXX: support multiple labels on loop statements */
- label_name = JS_ATOM_NULL;
- if (is_label(s)) {
- BlockEnv *be;
- label_name = JS_DupAtom(ctx, s->token.u.ident.atom);
- for (be = s->cur_func->top_break; be; be = be->prev) {
- if (be->label_name == label_name) {
- js_parse_error(s, "duplicate label name");
- goto fail;
- }
- }
- if (next_token(s))
- goto fail;
- if (js_parse_expect(s, ':'))
- goto fail;
- if (s->token.val != TOK_FOR
- && s->token.val != TOK_DO
- && s->token.val != TOK_WHILE) {
- /* labelled regular statement */
- int label_break, mask;
- BlockEnv break_entry;
- label_break = new_label(s);
- push_break_entry(s->cur_func, &break_entry,
- label_name, label_break, -1, 0);
- break_entry.is_regular_stmt = TRUE;
- if (!(s->cur_func->js_mode & JS_MODE_STRICT) &&
- (decl_mask & DECL_MASK_FUNC_WITH_LABEL)) {
- mask = DECL_MASK_FUNC | DECL_MASK_FUNC_WITH_LABEL;
- } else {
- mask = 0;
- }
- if (js_parse_statement_or_decl(s, mask))
- goto fail;
- emit_label(s, label_break);
- pop_break_entry(s->cur_func);
- goto done;
- }
- }
- switch(tok = s->token.val) {
- case '{':
- if (js_parse_block(s))
- goto fail;
- break;
- case TOK_RETURN:
- {
- const uint8_t *op_token_ptr;
- if (s->cur_func->is_eval) {
- js_parse_error(s, "return not in a function");
- goto fail;
- }
- if (s->cur_func->func_type == JS_PARSE_FUNC_CLASS_STATIC_INIT) {
- js_parse_error(s, "return in a static initializer block");
- goto fail;
- }
- op_token_ptr = s->token.ptr;
- if (next_token(s))
- goto fail;
- if (s->token.val != ';' && s->token.val != '}' && !s->got_lf) {
- if (js_parse_expr(s))
- goto fail;
- emit_source_pos(s, op_token_ptr);
- emit_return(s, TRUE);
- } else {
- emit_source_pos(s, op_token_ptr);
- emit_return(s, FALSE);
- }
- if (js_parse_expect_semi(s))
- goto fail;
- }
- break;
- case TOK_THROW:
- {
- const uint8_t *op_token_ptr;
- op_token_ptr = s->token.ptr;
- if (next_token(s))
- goto fail;
- if (s->got_lf) {
- js_parse_error(s, "line terminator not allowed after throw");
- goto fail;
- }
- if (js_parse_expr(s))
- goto fail;
- emit_source_pos(s, op_token_ptr);
- emit_op(s, OP_throw);
- if (js_parse_expect_semi(s))
- goto fail;
- }
- break;
- case TOK_LET:
- case TOK_CONST:
- haslet:
- if (!(decl_mask & DECL_MASK_OTHER)) {
- js_parse_error(s, "lexical declarations can't appear in single-statement context");
- goto fail;
- }
- /* fall thru */
- case TOK_VAR:
- if (next_token(s))
- goto fail;
- if (js_parse_var(s, TRUE, tok, FALSE))
- goto fail;
- if (js_parse_expect_semi(s))
- goto fail;
- break;
- case TOK_IF:
- {
- int label1, label2, mask;
- if (next_token(s))
- goto fail;
- /* create a new scope for `let f;if(1) function f(){}` */
- push_scope(s);
- set_eval_ret_undefined(s);
- if (js_parse_expr_paren(s))
- goto fail;
- label1 = emit_goto(s, OP_if_false, -1);
- if (s->cur_func->js_mode & JS_MODE_STRICT)
- mask = 0;
- else
- mask = DECL_MASK_FUNC; /* Annex B.3.4 */
- if (js_parse_statement_or_decl(s, mask))
- goto fail;
- if (s->token.val == TOK_ELSE) {
- label2 = emit_goto(s, OP_goto, -1);
- if (next_token(s))
- goto fail;
- emit_label(s, label1);
- if (js_parse_statement_or_decl(s, mask))
- goto fail;
- label1 = label2;
- }
- emit_label(s, label1);
- pop_scope(s);
- }
- break;
- case TOK_WHILE:
- {
- int label_cont, label_break;
- BlockEnv break_entry;
- label_cont = new_label(s);
- label_break = new_label(s);
- push_break_entry(s->cur_func, &break_entry,
- label_name, label_break, label_cont, 0);
- if (next_token(s))
- goto fail;
- set_eval_ret_undefined(s);
- emit_label(s, label_cont);
- if (js_parse_expr_paren(s))
- goto fail;
- emit_goto(s, OP_if_false, label_break);
- if (js_parse_statement(s))
- goto fail;
- emit_goto(s, OP_goto, label_cont);
- emit_label(s, label_break);
- pop_break_entry(s->cur_func);
- }
- break;
- case TOK_DO:
- {
- int label_cont, label_break, label1;
- BlockEnv break_entry;
- label_cont = new_label(s);
- label_break = new_label(s);
- label1 = new_label(s);
- push_break_entry(s->cur_func, &break_entry,
- label_name, label_break, label_cont, 0);
- if (next_token(s))
- goto fail;
- emit_label(s, label1);
- set_eval_ret_undefined(s);
- if (js_parse_statement(s))
- goto fail;
- emit_label(s, label_cont);
- if (js_parse_expect(s, TOK_WHILE))
- goto fail;
- if (js_parse_expr_paren(s))
- goto fail;
- /* Insert semicolon if missing */
- if (s->token.val == ';') {
- if (next_token(s))
- goto fail;
- }
- emit_goto(s, OP_if_true, label1);
- emit_label(s, label_break);
- pop_break_entry(s->cur_func);
- }
- break;
- case TOK_FOR:
- {
- int label_cont, label_break, label_body, label_test;
- int pos_cont, pos_body, block_scope_level;
- BlockEnv break_entry;
- int tok, bits;
- BOOL is_async;
- if (next_token(s))
- goto fail;
- set_eval_ret_undefined(s);
- bits = 0;
- is_async = FALSE;
- if (s->token.val == '(') {
- js_parse_skip_parens_token(s, &bits, FALSE);
- } else if (s->token.val == TOK_AWAIT) {
- if (!(s->cur_func->func_kind & JS_FUNC_ASYNC)) {
- js_parse_error(s, "for await is only valid in asynchronous functions");
- goto fail;
- }
- is_async = TRUE;
- if (next_token(s))
- goto fail;
- s->cur_func->has_await = TRUE;
- }
- if (js_parse_expect(s, '('))
- goto fail;
- if (!(bits & SKIP_HAS_SEMI)) {
- /* parse for/in or for/of */
- if (js_parse_for_in_of(s, label_name, is_async))
- goto fail;
- break;
- }
- block_scope_level = s->cur_func->scope_level;
- /* create scope for the lexical variables declared in the initial,
- test and increment expressions */
- push_scope(s);
- /* initial expression */
- tok = s->token.val;
- if (tok != ';') {
- switch (is_let(s, DECL_MASK_OTHER)) {
- case TRUE:
- tok = TOK_LET;
- break;
- case FALSE:
- break;
- default:
- goto fail;
- }
- if (tok == TOK_VAR || tok == TOK_LET || tok == TOK_CONST) {
- if (next_token(s))
- goto fail;
- if (js_parse_var(s, FALSE, tok, FALSE))
- goto fail;
- } else {
- if (js_parse_expr2(s, FALSE))
- goto fail;
- emit_op(s, OP_drop);
- }
- /* close the closures before the first iteration */
- close_scopes(s, s->cur_func->scope_level, block_scope_level);
- }
- if (js_parse_expect(s, ';'))
- goto fail;
- label_test = new_label(s);
- label_cont = new_label(s);
- label_body = new_label(s);
- label_break = new_label(s);
- push_break_entry(s->cur_func, &break_entry,
- label_name, label_break, label_cont, 0);
- /* test expression */
- if (s->token.val == ';') {
- /* no test expression */
- label_test = label_body;
- } else {
- emit_label(s, label_test);
- if (js_parse_expr(s))
- goto fail;
- emit_goto(s, OP_if_false, label_break);
- }
- if (js_parse_expect(s, ';'))
- goto fail;
- if (s->token.val == ')') {
- /* no end expression */
- break_entry.label_cont = label_cont = label_test;
- pos_cont = 0; /* avoid warning */
- } else {
- /* skip the end expression */
- emit_goto(s, OP_goto, label_body);
- pos_cont = s->cur_func->byte_code.size;
- emit_label(s, label_cont);
- if (js_parse_expr(s))
- goto fail;
- emit_op(s, OP_drop);
- if (label_test != label_body)
- emit_goto(s, OP_goto, label_test);
- }
- if (js_parse_expect(s, ')'))
- goto fail;
- pos_body = s->cur_func->byte_code.size;
- emit_label(s, label_body);
- if (js_parse_statement(s))
- goto fail;
- /* close the closures before the next iteration */
- /* XXX: check continue case */
- close_scopes(s, s->cur_func->scope_level, block_scope_level);
- if (OPTIMIZE && label_test != label_body && label_cont != label_test) {
- /* move the increment code here */
- DynBuf *bc = &s->cur_func->byte_code;
- int chunk_size = pos_body - pos_cont;
- int offset = bc->size - pos_cont;
- int i;
- if (dbuf_claim(bc, chunk_size))
- goto fail;
- dbuf_put(bc, bc->buf + pos_cont, chunk_size);
- memset(bc->buf + pos_cont, OP_nop, chunk_size);
- /* increment part ends with a goto */
- s->cur_func->last_opcode_pos = bc->size - 5;
- /* relocate labels */
- for (i = label_cont; i < s->cur_func->label_count; i++) {
- LabelSlot *ls = &s->cur_func->label_slots[i];
- if (ls->pos >= pos_cont && ls->pos < pos_body)
- ls->pos += offset;
- }
- } else {
- emit_goto(s, OP_goto, label_cont);
- }
- emit_label(s, label_break);
- pop_break_entry(s->cur_func);
- pop_scope(s);
- }
- break;
- case TOK_BREAK:
- case TOK_CONTINUE:
- {
- int is_cont = s->token.val - TOK_BREAK;
- int label;
- if (next_token(s))
- goto fail;
- if (!s->got_lf && s->token.val == TOK_IDENT && !s->token.u.ident.is_reserved)
- label = s->token.u.ident.atom;
- else
- label = JS_ATOM_NULL;
- if (emit_break(s, label, is_cont))
- goto fail;
- if (label != JS_ATOM_NULL) {
- if (next_token(s))
- goto fail;
- }
- if (js_parse_expect_semi(s))
- goto fail;
- }
- break;
- case TOK_SWITCH:
- {
- int label_case, label_break, label1;
- int default_label_pos;
- BlockEnv break_entry;
- if (next_token(s))
- goto fail;
- set_eval_ret_undefined(s);
- if (js_parse_expr_paren(s))
- goto fail;
- push_scope(s);
- label_break = new_label(s);
- push_break_entry(s->cur_func, &break_entry,
- label_name, label_break, -1, 1);
- if (js_parse_expect(s, '{'))
- goto fail;
- default_label_pos = -1;
- label_case = -1;
- while (s->token.val != '}') {
- if (s->token.val == TOK_CASE) {
- label1 = -1;
- if (label_case >= 0) {
- /* skip the case if needed */
- label1 = emit_goto(s, OP_goto, -1);
- }
- emit_label(s, label_case);
- label_case = -1;
- for (;;) {
- /* parse a sequence of case clauses */
- if (next_token(s))
- goto fail;
- emit_op(s, OP_dup);
- if (js_parse_expr(s))
- goto fail;
- if (js_parse_expect(s, ':'))
- goto fail;
- emit_op(s, OP_strict_eq);
- if (s->token.val == TOK_CASE) {
- label1 = emit_goto(s, OP_if_true, label1);
- } else {
- label_case = emit_goto(s, OP_if_false, -1);
- emit_label(s, label1);
- break;
- }
- }
- } else if (s->token.val == TOK_DEFAULT) {
- if (next_token(s))
- goto fail;
- if (js_parse_expect(s, ':'))
- goto fail;
- if (default_label_pos >= 0) {
- js_parse_error(s, "duplicate default");
- goto fail;
- }
- if (label_case < 0) {
- /* falling thru direct from switch expression */
- label_case = emit_goto(s, OP_goto, -1);
- }
- /* Emit a dummy label opcode. Label will be patched after
- the end of the switch body. Do not use emit_label(s, 0)
- because it would clobber label 0 address, preventing
- proper optimizer operation.
- */
- emit_op(s, OP_label);
- emit_u32(s, 0);
- default_label_pos = s->cur_func->byte_code.size - 4;
- } else {
- if (label_case < 0) {
- /* falling thru direct from switch expression */
- js_parse_error(s, "invalid switch statement");
- goto fail;
- }
- if (js_parse_statement_or_decl(s, DECL_MASK_ALL))
- goto fail;
- }
- }
- if (js_parse_expect(s, '}'))
- goto fail;
- if (default_label_pos >= 0) {
- /* Ugly patch for the `default` label, shameful and risky */
- put_u32(s->cur_func->byte_code.buf + default_label_pos,
- label_case);
- s->cur_func->label_slots[label_case].pos = default_label_pos + 4;
- } else {
- emit_label(s, label_case);
- }
- emit_label(s, label_break);
- emit_op(s, OP_drop); /* drop the switch expression */
- pop_break_entry(s->cur_func);
- pop_scope(s);
- }
- break;
- case TOK_TRY:
- {
- int label_catch, label_catch2, label_finally, label_end;
- JSAtom name;
- BlockEnv block_env;
- set_eval_ret_undefined(s);
- if (next_token(s))
- goto fail;
- label_catch = new_label(s);
- label_catch2 = new_label(s);
- label_finally = new_label(s);
- label_end = new_label(s);
- emit_goto(s, OP_catch, label_catch);
- push_break_entry(s->cur_func, &block_env,
- JS_ATOM_NULL, -1, -1, 1);
- block_env.label_finally = label_finally;
- if (js_parse_block(s))
- goto fail;
- pop_break_entry(s->cur_func);
- if (js_is_live_code(s)) {
- /* drop the catch offset */
- emit_op(s, OP_drop);
- /* must push dummy value to keep same stack size */
- emit_op(s, OP_undefined);
- emit_goto(s, OP_gosub, label_finally);
- emit_op(s, OP_drop);
- emit_goto(s, OP_goto, label_end);
- }
- if (s->token.val == TOK_CATCH) {
- if (next_token(s))
- goto fail;
- push_scope(s); /* catch variable */
- emit_label(s, label_catch);
- if (s->token.val == '{') {
- /* support optional-catch-binding feature */
- emit_op(s, OP_drop); /* pop the exception object */
- } else {
- if (js_parse_expect(s, '('))
- goto fail;
- if (!(s->token.val == TOK_IDENT && !s->token.u.ident.is_reserved)) {
- if (s->token.val == '[' || s->token.val == '{') {
- /* XXX: TOK_LET is not completely correct */
- if (js_parse_destructuring_element(s, TOK_LET, 0, TRUE, -1, TRUE, FALSE) < 0)
- goto fail;
- } else {
- js_parse_error(s, "identifier expected");
- goto fail;
- }
- } else {
- name = JS_DupAtom(ctx, s->token.u.ident.atom);
- if (next_token(s)
- || js_define_var(s, name, TOK_CATCH) < 0) {
- JS_FreeAtom(ctx, name);
- goto fail;
- }
- /* store the exception value in the catch variable */
- emit_op(s, OP_scope_put_var);
- emit_u32(s, name);
- emit_u16(s, s->cur_func->scope_level);
- }
- if (js_parse_expect(s, ')'))
- goto fail;
- }
- /* XXX: should keep the address to nop it out if there is no finally block */
- emit_goto(s, OP_catch, label_catch2);
- push_scope(s); /* catch block */
- push_break_entry(s->cur_func, &block_env, JS_ATOM_NULL,
- -1, -1, 1);
- block_env.label_finally = label_finally;
- if (js_parse_block(s))
- goto fail;
- pop_break_entry(s->cur_func);
- pop_scope(s); /* catch block */
- pop_scope(s); /* catch variable */
- if (js_is_live_code(s)) {
- /* drop the catch2 offset */
- emit_op(s, OP_drop);
- /* XXX: should keep the address to nop it out if there is no finally block */
- /* must push dummy value to keep same stack size */
- emit_op(s, OP_undefined);
- emit_goto(s, OP_gosub, label_finally);
- emit_op(s, OP_drop);
- emit_goto(s, OP_goto, label_end);
- }
- /* catch exceptions thrown in the catch block to execute the
- * finally clause and rethrow the exception */
- emit_label(s, label_catch2);
- /* catch value is at TOS, no need to push undefined */
- emit_goto(s, OP_gosub, label_finally);
- emit_op(s, OP_throw);
- } else if (s->token.val == TOK_FINALLY) {
- /* finally without catch : execute the finally clause
- * and rethrow the exception */
- emit_label(s, label_catch);
- /* catch value is at TOS, no need to push undefined */
- emit_goto(s, OP_gosub, label_finally);
- emit_op(s, OP_throw);
- } else {
- js_parse_error(s, "expecting catch or finally");
- goto fail;
- }
- emit_label(s, label_finally);
- if (s->token.val == TOK_FINALLY) {
- int saved_eval_ret_idx = 0; /* avoid warning */
- if (next_token(s))
- goto fail;
- /* on the stack: ret_value gosub_ret_value */
- push_break_entry(s->cur_func, &block_env, JS_ATOM_NULL,
- -1, -1, 2);
- if (s->cur_func->eval_ret_idx >= 0) {
- /* 'finally' updates eval_ret only if not a normal
- termination */
- saved_eval_ret_idx =
- add_var(s->ctx, s->cur_func, JS_ATOM__ret_);
- if (saved_eval_ret_idx < 0)
- goto fail;
- emit_op(s, OP_get_loc);
- emit_u16(s, s->cur_func->eval_ret_idx);
- emit_op(s, OP_put_loc);
- emit_u16(s, saved_eval_ret_idx);
- set_eval_ret_undefined(s);
- }
- if (js_parse_block(s))
- goto fail;
- if (s->cur_func->eval_ret_idx >= 0) {
- emit_op(s, OP_get_loc);
- emit_u16(s, saved_eval_ret_idx);
- emit_op(s, OP_put_loc);
- emit_u16(s, s->cur_func->eval_ret_idx);
- }
- pop_break_entry(s->cur_func);
- }
- emit_op(s, OP_ret);
- emit_label(s, label_end);
- }
- break;
- case ';':
- /* empty statement */
- if (next_token(s))
- goto fail;
- break;
- case TOK_WITH:
- if (s->cur_func->js_mode & JS_MODE_STRICT) {
- js_parse_error(s, "invalid keyword: with");
- goto fail;
- } else {
- int with_idx;
- if (next_token(s))
- goto fail;
- if (js_parse_expr_paren(s))
- goto fail;
- push_scope(s);
- with_idx = define_var(s, s->cur_func, JS_ATOM__with_,
- JS_VAR_DEF_WITH);
- if (with_idx < 0)
- goto fail;
- emit_op(s, OP_to_object);
- emit_op(s, OP_put_loc);
- emit_u16(s, with_idx);
- set_eval_ret_undefined(s);
- if (js_parse_statement(s))
- goto fail;
- /* Popping scope drops lexical context for the with object variable */
- pop_scope(s);
- }
- break;
- case TOK_FUNCTION:
- /* ES6 Annex B.3.2 and B.3.3 semantics */
- if (!(decl_mask & DECL_MASK_FUNC))
- goto func_decl_error;
- if (!(decl_mask & DECL_MASK_OTHER) && peek_token(s, FALSE) == '*')
- goto func_decl_error;
- goto parse_func_var;
- case TOK_IDENT:
- if (s->token.u.ident.is_reserved) {
- js_parse_error_reserved_identifier(s);
- goto fail;
- }
- /* Determine if `let` introduces a Declaration or an ExpressionStatement */
- switch (is_let(s, decl_mask)) {
- case TRUE:
- tok = TOK_LET;
- goto haslet;
- case FALSE:
- break;
- default:
- goto fail;
- }
- if (token_is_pseudo_keyword(s, JS_ATOM_async) &&
- peek_token(s, TRUE) == TOK_FUNCTION) {
- if (!(decl_mask & DECL_MASK_OTHER)) {
- func_decl_error:
- js_parse_error(s, "function declarations can't appear in single-statement context");
- goto fail;
- }
- parse_func_var:
- if (js_parse_function_decl(s, JS_PARSE_FUNC_VAR,
- JS_FUNC_NORMAL, JS_ATOM_NULL,
- s->token.ptr))
- goto fail;
- break;
- }
- goto hasexpr;
- case TOK_CLASS:
- if (!(decl_mask & DECL_MASK_OTHER)) {
- js_parse_error(s, "class declarations can't appear in single-statement context");
- goto fail;
- }
- if (js_parse_class(s, FALSE, JS_PARSE_EXPORT_NONE))
- return -1;
- break;
- case TOK_DEBUGGER:
- /* currently no debugger, so just skip the keyword */
- if (next_token(s))
- goto fail;
- if (js_parse_expect_semi(s))
- goto fail;
- break;
- case TOK_ENUM:
- case TOK_EXPORT:
- case TOK_EXTENDS:
- js_unsupported_keyword(s, s->token.u.ident.atom);
- goto fail;
- default:
- hasexpr:
- emit_source_pos(s, s->token.ptr);
- if (js_parse_expr(s))
- goto fail;
- if (s->cur_func->eval_ret_idx >= 0) {
- /* store the expression value so that it can be returned
- by eval() */
- emit_op(s, OP_put_loc);
- emit_u16(s, s->cur_func->eval_ret_idx);
- } else {
- emit_op(s, OP_drop); /* drop the result */
- }
- if (js_parse_expect_semi(s))
- goto fail;
- break;
- }
- done:
- JS_FreeAtom(ctx, label_name);
- return 0;
- fail:
- JS_FreeAtom(ctx, label_name);
- return -1;
- }
- /* 'name' is freed. The module is referenced by 'ctx->loaded_modules' */
- static JSModuleDef *js_new_module_def(JSContext *ctx, JSAtom name)
- {
- JSModuleDef *m;
- m = js_mallocz(ctx, sizeof(*m));
- if (!m) {
- JS_FreeAtom(ctx, name);
- return NULL;
- }
- js_rc(m)->ref_count = 1;
- add_gc_object(ctx->rt, &m->header, JS_GC_OBJ_TYPE_MODULE);
- m->module_name = name;
- m->module_ns = JS_UNDEFINED;
- m->func_obj = JS_UNDEFINED;
- m->eval_exception = JS_UNDEFINED;
- m->meta_obj = JS_UNDEFINED;
- m->promise = JS_UNDEFINED;
- m->resolving_funcs[0] = JS_UNDEFINED;
- m->resolving_funcs[1] = JS_UNDEFINED;
- m->private_value = JS_UNDEFINED;
- list_add_tail(&m->link, &ctx->loaded_modules);
- return m;
- }
- static void js_mark_module_def(JSRuntime *rt, JSModuleDef *m,
- JS_MarkFunc *mark_func)
- {
- int i;
- for(i = 0; i < m->req_module_entries_count; i++) {
- JSReqModuleEntry *rme = &m->req_module_entries[i];
- JS_MarkValue(rt, rme->attributes, mark_func);
- }
-
- for(i = 0; i < m->export_entries_count; i++) {
- JSExportEntry *me = &m->export_entries[i];
- if (me->export_type == JS_EXPORT_TYPE_LOCAL &&
- me->u.local.var_ref) {
- mark_func(rt, &me->u.local.var_ref->header);
- }
- }
- JS_MarkValue(rt, m->module_ns, mark_func);
- JS_MarkValue(rt, m->func_obj, mark_func);
- JS_MarkValue(rt, m->eval_exception, mark_func);
- JS_MarkValue(rt, m->meta_obj, mark_func);
- JS_MarkValue(rt, m->promise, mark_func);
- JS_MarkValue(rt, m->resolving_funcs[0], mark_func);
- JS_MarkValue(rt, m->resolving_funcs[1], mark_func);
- JS_MarkValue(rt, m->private_value, mark_func);
- }
- static void js_free_module_def(JSRuntime *rt, JSModuleDef *m)
- {
- int i;
- JS_FreeAtomRT(rt, m->module_name);
- for(i = 0; i < m->req_module_entries_count; i++) {
- JSReqModuleEntry *rme = &m->req_module_entries[i];
- JS_FreeAtomRT(rt, rme->module_name);
- JS_FreeValueRT(rt, rme->attributes);
- }
- js_free_rt(rt, m->req_module_entries);
- for(i = 0; i < m->export_entries_count; i++) {
- JSExportEntry *me = &m->export_entries[i];
- if (me->export_type == JS_EXPORT_TYPE_LOCAL)
- free_var_ref(rt, me->u.local.var_ref);
- JS_FreeAtomRT(rt, me->export_name);
- JS_FreeAtomRT(rt, me->local_name);
- }
- js_free_rt(rt, m->export_entries);
- js_free_rt(rt, m->star_export_entries);
- for(i = 0; i < m->import_entries_count; i++) {
- JSImportEntry *mi = &m->import_entries[i];
- JS_FreeAtomRT(rt, mi->import_name);
- }
- js_free_rt(rt, m->import_entries);
- js_free_rt(rt, m->async_parent_modules);
- JS_FreeValueRT(rt, m->module_ns);
- JS_FreeValueRT(rt, m->func_obj);
- JS_FreeValueRT(rt, m->eval_exception);
- JS_FreeValueRT(rt, m->meta_obj);
- JS_FreeValueRT(rt, m->promise);
- JS_FreeValueRT(rt, m->resolving_funcs[0]);
- JS_FreeValueRT(rt, m->resolving_funcs[1]);
- JS_FreeValueRT(rt, m->private_value);
- /* during the GC the finalizers are called in an arbitrary
- order so the module may no longer be referenced by the JSContext list */
- if (m->link.next) {
- list_del(&m->link);
- }
- remove_gc_object(&m->header);
- if (rt->gc_phase == JS_GC_PHASE_REMOVE_CYCLES && js_rc(m)->ref_count != 0) {
- list_add_tail(&m->header.link, &rt->gc_zero_ref_count_list);
- } else {
- js_free_rt(rt, m);
- }
- }
- static int add_req_module_entry(JSContext *ctx, JSModuleDef *m,
- JSAtom module_name)
- {
- JSReqModuleEntry *rme;
- if (js_resize_array(ctx, (void **)&m->req_module_entries,
- sizeof(JSReqModuleEntry),
- &m->req_module_entries_size,
- m->req_module_entries_count + 1))
- return -1;
- rme = &m->req_module_entries[m->req_module_entries_count++];
- rme->module_name = JS_DupAtom(ctx, module_name);
- rme->module = NULL;
- rme->attributes = JS_UNDEFINED;
- return m->req_module_entries_count - 1;
- }
- static JSExportEntry *find_export_entry(JSContext *ctx, JSModuleDef *m,
- JSAtom export_name)
- {
- JSExportEntry *me;
- int i;
- for(i = 0; i < m->export_entries_count; i++) {
- me = &m->export_entries[i];
- if (me->export_name == export_name)
- return me;
- }
- return NULL;
- }
- static JSExportEntry *add_export_entry2(JSContext *ctx,
- JSParseState *s, JSModuleDef *m,
- JSAtom local_name, JSAtom export_name,
- JSExportTypeEnum export_type)
- {
- JSExportEntry *me;
- if (find_export_entry(ctx, m, export_name)) {
- char buf1[ATOM_GET_STR_BUF_SIZE];
- if (s) {
- js_parse_error(s, "duplicate exported name '%s'",
- JS_AtomGetStr(ctx, buf1, sizeof(buf1), export_name));
- } else {
- JS_ThrowSyntaxErrorAtom(ctx, "duplicate exported name '%s'", export_name);
- }
- return NULL;
- }
- if (js_resize_array(ctx, (void **)&m->export_entries,
- sizeof(JSExportEntry),
- &m->export_entries_size,
- m->export_entries_count + 1))
- return NULL;
- me = &m->export_entries[m->export_entries_count++];
- memset(me, 0, sizeof(*me));
- me->local_name = JS_DupAtom(ctx, local_name);
- me->export_name = JS_DupAtom(ctx, export_name);
- me->export_type = export_type;
- return me;
- }
- static JSExportEntry *add_export_entry(JSParseState *s, JSModuleDef *m,
- JSAtom local_name, JSAtom export_name,
- JSExportTypeEnum export_type)
- {
- return add_export_entry2(s->ctx, s, m, local_name, export_name,
- export_type);
- }
- static int add_star_export_entry(JSContext *ctx, JSModuleDef *m,
- int req_module_idx)
- {
- JSStarExportEntry *se;
- if (js_resize_array(ctx, (void **)&m->star_export_entries,
- sizeof(JSStarExportEntry),
- &m->star_export_entries_size,
- m->star_export_entries_count + 1))
- return -1;
- se = &m->star_export_entries[m->star_export_entries_count++];
- se->req_module_idx = req_module_idx;
- return 0;
- }
- /* create a C module */
- JSModuleDef *JS_NewCModule(JSContext *ctx, const char *name_str,
- JSModuleInitFunc *func)
- {
- JSModuleDef *m;
- JSAtom name;
- name = JS_NewAtom(ctx, name_str);
- if (name == JS_ATOM_NULL)
- return NULL;
- m = js_new_module_def(ctx, name);
- if (!m)
- return NULL;
- m->init_func = func;
- return m;
- }
- int JS_AddModuleExport(JSContext *ctx, JSModuleDef *m, const char *export_name)
- {
- JSExportEntry *me;
- JSAtom name;
- name = JS_NewAtom(ctx, export_name);
- if (name == JS_ATOM_NULL)
- return -1;
- me = add_export_entry2(ctx, NULL, m, JS_ATOM_NULL, name,
- JS_EXPORT_TYPE_LOCAL);
- JS_FreeAtom(ctx, name);
- if (!me)
- return -1;
- else
- return 0;
- }
- int JS_SetModuleExport(JSContext *ctx, JSModuleDef *m, const char *export_name,
- JSValue val)
- {
- JSExportEntry *me;
- JSAtom name;
- name = JS_NewAtom(ctx, export_name);
- if (name == JS_ATOM_NULL)
- goto fail;
- me = find_export_entry(ctx, m, name);
- JS_FreeAtom(ctx, name);
- if (!me)
- goto fail;
- set_value(ctx, me->u.local.var_ref->pvalue, val);
- return 0;
- fail:
- JS_FreeValue(ctx, val);
- return -1;
- }
- int JS_SetModulePrivateValue(JSContext *ctx, JSModuleDef *m, JSValue val)
- {
- set_value(ctx, &m->private_value, val);
- return 0;
- }
- JSValue JS_GetModulePrivateValue(JSContext *ctx, JSModuleDef *m)
- {
- return JS_DupValue(ctx, m->private_value);
- }
- void JS_SetModuleLoaderFunc(JSRuntime *rt,
- JSModuleNormalizeFunc *module_normalize,
- JSModuleLoaderFunc *module_loader, void *opaque)
- {
- rt->module_normalize_func = module_normalize;
- rt->module_loader_has_attr = FALSE;
- rt->u.module_loader_func = module_loader;
- rt->module_check_attrs = NULL;
- rt->module_loader_opaque = opaque;
- }
- void JS_SetModuleLoaderFunc2(JSRuntime *rt,
- JSModuleNormalizeFunc *module_normalize,
- JSModuleLoaderFunc2 *module_loader,
- JSModuleCheckSupportedImportAttributes *module_check_attrs,
- void *opaque)
- {
- rt->module_normalize_func = module_normalize;
- rt->module_loader_has_attr = TRUE;
- rt->u.module_loader_func2 = module_loader;
- rt->module_check_attrs = module_check_attrs;
- rt->module_loader_opaque = opaque;
- }
- /* default module filename normalizer */
- static char *js_default_module_normalize_name(JSContext *ctx,
- const char *base_name,
- const char *name)
- {
- char *filename, *p;
- const char *r;
- int cap;
- int len;
- if (name[0] != '.') {
- /* if no initial dot, the module name is not modified */
- return js_strdup(ctx, name);
- }
- p = strrchr(base_name, '/');
- if (p)
- len = p - base_name;
- else
- len = 0;
- cap = len + strlen(name) + 1 + 1;
- filename = js_malloc(ctx, cap);
- if (!filename)
- return NULL;
- memcpy(filename, base_name, len);
- filename[len] = '\0';
- /* we only normalize the leading '..' or '.' */
- r = name;
- for(;;) {
- if (r[0] == '.' && r[1] == '/') {
- r += 2;
- } else if (r[0] == '.' && r[1] == '.' && r[2] == '/') {
- /* remove the last path element of filename, except if "."
- or ".." */
- if (filename[0] == '\0')
- break;
- p = strrchr(filename, '/');
- if (!p)
- p = filename;
- else
- p++;
- if (!strcmp(p, ".") || !strcmp(p, ".."))
- break;
- if (p > filename)
- p--;
- *p = '\0';
- r += 3;
- } else {
- break;
- }
- }
- if (filename[0] != '\0')
- pstrcat(filename, cap, "/");
- pstrcat(filename, cap, r);
- // printf("normalize: %s %s -> %s\n", base_name, name, filename);
- return filename;
- }
- static JSModuleDef *js_find_loaded_module(JSContext *ctx, JSAtom name)
- {
- struct list_head *el;
- JSModuleDef *m;
- /* first look at the loaded modules */
- list_for_each(el, &ctx->loaded_modules) {
- m = list_entry(el, JSModuleDef, link);
- if (m->module_name == name)
- return m;
- }
- return NULL;
- }
- /* return NULL in case of exception (e.g. module could not be loaded) */
- static JSModuleDef *js_host_resolve_imported_module(JSContext *ctx,
- const char *base_cname,
- const char *cname1,
- JSValueConst attributes)
- {
- JSRuntime *rt = ctx->rt;
- JSModuleDef *m;
- char *cname;
- JSAtom module_name;
- if (!rt->module_normalize_func) {
- cname = js_default_module_normalize_name(ctx, base_cname, cname1);
- } else {
- cname = rt->module_normalize_func(ctx, base_cname, cname1,
- rt->module_loader_opaque);
- }
- if (!cname)
- return NULL;
- module_name = JS_NewAtom(ctx, cname);
- if (module_name == JS_ATOM_NULL) {
- js_free(ctx, cname);
- return NULL;
- }
- /* first look at the loaded modules */
- m = js_find_loaded_module(ctx, module_name);
- if (m) {
- js_free(ctx, cname);
- JS_FreeAtom(ctx, module_name);
- return m;
- }
- JS_FreeAtom(ctx, module_name);
- /* load the module */
- if (!rt->u.module_loader_func) {
- /* XXX: use a syntax error ? */
- JS_ThrowReferenceError(ctx, "could not load module '%s'",
- cname);
- js_free(ctx, cname);
- return NULL;
- }
- if (rt->module_loader_has_attr) {
- m = rt->u.module_loader_func2(ctx, cname, rt->module_loader_opaque, attributes);
- } else {
- m = rt->u.module_loader_func(ctx, cname, rt->module_loader_opaque);
- }
- js_free(ctx, cname);
- return m;
- }
- static JSModuleDef *js_host_resolve_imported_module_atom(JSContext *ctx,
- JSAtom base_module_name,
- JSAtom module_name1,
- JSValueConst attributes)
- {
- const char *base_cname, *cname;
- JSModuleDef *m;
- base_cname = JS_AtomToCString(ctx, base_module_name);
- if (!base_cname)
- return NULL;
- cname = JS_AtomToCString(ctx, module_name1);
- if (!cname) {
- JS_FreeCString(ctx, base_cname);
- return NULL;
- }
- m = js_host_resolve_imported_module(ctx, base_cname, cname, attributes);
- JS_FreeCString(ctx, base_cname);
- JS_FreeCString(ctx, cname);
- return m;
- }
- typedef struct JSResolveEntry {
- JSModuleDef *module;
- JSAtom name;
- } JSResolveEntry;
- typedef struct JSResolveState {
- JSResolveEntry *array;
- int size;
- int count;
- } JSResolveState;
- static int find_resolve_entry(JSResolveState *s,
- JSModuleDef *m, JSAtom name)
- {
- int i;
- for(i = 0; i < s->count; i++) {
- JSResolveEntry *re = &s->array[i];
- if (re->module == m && re->name == name)
- return i;
- }
- return -1;
- }
- static int add_resolve_entry(JSContext *ctx, JSResolveState *s,
- JSModuleDef *m, JSAtom name)
- {
- JSResolveEntry *re;
- if (js_resize_array(ctx, (void **)&s->array,
- sizeof(JSResolveEntry),
- &s->size, s->count + 1))
- return -1;
- re = &s->array[s->count++];
- re->module = m;
- re->name = JS_DupAtom(ctx, name);
- return 0;
- }
- typedef enum JSResolveResultEnum {
- JS_RESOLVE_RES_EXCEPTION = -1, /* memory alloc error */
- JS_RESOLVE_RES_FOUND = 0,
- JS_RESOLVE_RES_NOT_FOUND,
- JS_RESOLVE_RES_CIRCULAR,
- JS_RESOLVE_RES_AMBIGUOUS,
- } JSResolveResultEnum;
- static JSResolveResultEnum js_resolve_export1(JSContext *ctx,
- JSModuleDef **pmodule,
- JSExportEntry **pme,
- JSModuleDef *m,
- JSAtom export_name,
- JSResolveState *s)
- {
- JSExportEntry *me;
- *pmodule = NULL;
- *pme = NULL;
- if (find_resolve_entry(s, m, export_name) >= 0)
- return JS_RESOLVE_RES_CIRCULAR;
- if (add_resolve_entry(ctx, s, m, export_name) < 0)
- return JS_RESOLVE_RES_EXCEPTION;
- me = find_export_entry(ctx, m, export_name);
- if (me) {
- if (me->export_type == JS_EXPORT_TYPE_LOCAL) {
- /* local export */
- *pmodule = m;
- *pme = me;
- return JS_RESOLVE_RES_FOUND;
- } else {
- /* indirect export */
- JSModuleDef *m1;
- m1 = m->req_module_entries[me->u.req_module_idx].module;
- if (me->local_name == JS_ATOM__star_) {
- /* export ns from */
- *pmodule = m;
- *pme = me;
- return JS_RESOLVE_RES_FOUND;
- } else {
- return js_resolve_export1(ctx, pmodule, pme, m1,
- me->local_name, s);
- }
- }
- } else {
- if (export_name != JS_ATOM_default) {
- /* not found in direct or indirect exports: try star exports */
- int i;
- for(i = 0; i < m->star_export_entries_count; i++) {
- JSStarExportEntry *se = &m->star_export_entries[i];
- JSModuleDef *m1, *res_m;
- JSExportEntry *res_me;
- JSResolveResultEnum ret;
- m1 = m->req_module_entries[se->req_module_idx].module;
- ret = js_resolve_export1(ctx, &res_m, &res_me, m1,
- export_name, s);
- if (ret == JS_RESOLVE_RES_AMBIGUOUS ||
- ret == JS_RESOLVE_RES_EXCEPTION) {
- return ret;
- } else if (ret == JS_RESOLVE_RES_FOUND) {
- if (*pme != NULL) {
- if (*pmodule != res_m ||
- res_me->local_name != (*pme)->local_name) {
- *pmodule = NULL;
- *pme = NULL;
- return JS_RESOLVE_RES_AMBIGUOUS;
- }
- } else {
- *pmodule = res_m;
- *pme = res_me;
- }
- }
- }
- if (*pme != NULL)
- return JS_RESOLVE_RES_FOUND;
- }
- return JS_RESOLVE_RES_NOT_FOUND;
- }
- }
- /* If the return value is JS_RESOLVE_RES_FOUND, return the module
- (*pmodule) and the corresponding local export entry
- (*pme). Otherwise return (NULL, NULL) */
- static JSResolveResultEnum js_resolve_export(JSContext *ctx,
- JSModuleDef **pmodule,
- JSExportEntry **pme,
- JSModuleDef *m,
- JSAtom export_name)
- {
- JSResolveState ss, *s = &ss;
- int i;
- JSResolveResultEnum ret;
- s->array = NULL;
- s->size = 0;
- s->count = 0;
- ret = js_resolve_export1(ctx, pmodule, pme, m, export_name, s);
- for(i = 0; i < s->count; i++)
- JS_FreeAtom(ctx, s->array[i].name);
- js_free(ctx, s->array);
- return ret;
- }
- static void js_resolve_export_throw_error(JSContext *ctx,
- JSResolveResultEnum res,
- JSModuleDef *m, JSAtom export_name)
- {
- char buf1[ATOM_GET_STR_BUF_SIZE];
- char buf2[ATOM_GET_STR_BUF_SIZE];
- switch(res) {
- case JS_RESOLVE_RES_EXCEPTION:
- break;
- default:
- case JS_RESOLVE_RES_NOT_FOUND:
- JS_ThrowSyntaxError(ctx, "Could not find export '%s' in module '%s'",
- JS_AtomGetStr(ctx, buf1, sizeof(buf1), export_name),
- JS_AtomGetStr(ctx, buf2, sizeof(buf2), m->module_name));
- break;
- case JS_RESOLVE_RES_CIRCULAR:
- JS_ThrowSyntaxError(ctx, "circular reference when looking for export '%s' in module '%s'",
- JS_AtomGetStr(ctx, buf1, sizeof(buf1), export_name),
- JS_AtomGetStr(ctx, buf2, sizeof(buf2), m->module_name));
- break;
- case JS_RESOLVE_RES_AMBIGUOUS:
- JS_ThrowSyntaxError(ctx, "export '%s' in module '%s' is ambiguous",
- JS_AtomGetStr(ctx, buf1, sizeof(buf1), export_name),
- JS_AtomGetStr(ctx, buf2, sizeof(buf2), m->module_name));
- break;
- }
- }
- typedef enum {
- EXPORTED_NAME_AMBIGUOUS,
- EXPORTED_NAME_NORMAL,
- EXPORTED_NAME_DELAYED,
- } ExportedNameEntryEnum;
- typedef struct ExportedNameEntry {
- JSAtom export_name;
- ExportedNameEntryEnum export_type;
- union {
- JSExportEntry *me; /* using when the list is built */
- JSVarRef *var_ref; /* EXPORTED_NAME_NORMAL */
- } u;
- } ExportedNameEntry;
- typedef struct GetExportNamesState {
- JSModuleDef **modules;
- int modules_size;
- int modules_count;
- ExportedNameEntry *exported_names;
- int exported_names_size;
- int exported_names_count;
- } GetExportNamesState;
- static int find_exported_name(GetExportNamesState *s, JSAtom name)
- {
- int i;
- for(i = 0; i < s->exported_names_count; i++) {
- if (s->exported_names[i].export_name == name)
- return i;
- }
- return -1;
- }
- static __exception int get_exported_names(JSContext *ctx,
- GetExportNamesState *s,
- JSModuleDef *m, BOOL from_star)
- {
- ExportedNameEntry *en;
- int i, j;
- /* check circular reference */
- for(i = 0; i < s->modules_count; i++) {
- if (s->modules[i] == m)
- return 0;
- }
- if (js_resize_array(ctx, (void **)&s->modules, sizeof(s->modules[0]),
- &s->modules_size, s->modules_count + 1))
- return -1;
- s->modules[s->modules_count++] = m;
- for(i = 0; i < m->export_entries_count; i++) {
- JSExportEntry *me = &m->export_entries[i];
- if (from_star && me->export_name == JS_ATOM_default)
- continue;
- j = find_exported_name(s, me->export_name);
- if (j < 0) {
- if (js_resize_array(ctx, (void **)&s->exported_names, sizeof(s->exported_names[0]),
- &s->exported_names_size,
- s->exported_names_count + 1))
- return -1;
- en = &s->exported_names[s->exported_names_count++];
- en->export_name = me->export_name;
- /* avoid a second lookup for simple module exports */
- if (from_star || me->export_type != JS_EXPORT_TYPE_LOCAL)
- en->u.me = NULL;
- else
- en->u.me = me;
- } else {
- en = &s->exported_names[j];
- en->u.me = NULL;
- }
- }
- for(i = 0; i < m->star_export_entries_count; i++) {
- JSStarExportEntry *se = &m->star_export_entries[i];
- JSModuleDef *m1;
- m1 = m->req_module_entries[se->req_module_idx].module;
- if (get_exported_names(ctx, s, m1, TRUE))
- return -1;
- }
- return 0;
- }
- /* Unfortunately, the spec gives a different behavior from GetOwnProperty ! */
- static int js_module_ns_has(JSContext *ctx, JSValueConst obj, JSAtom atom)
- {
- return (find_own_property1(JS_VALUE_GET_OBJ(obj), atom) != NULL);
- }
- static const JSClassExoticMethods js_module_ns_exotic_methods = {
- .has_property = js_module_ns_has,
- };
- static int exported_names_cmp(const void *p1, const void *p2, void *opaque)
- {
- JSContext *ctx = opaque;
- const ExportedNameEntry *me1 = p1;
- const ExportedNameEntry *me2 = p2;
- JSValue str1, str2;
- int ret;
- /* XXX: should avoid allocation memory in atom comparison */
- str1 = JS_AtomToString(ctx, me1->export_name);
- str2 = JS_AtomToString(ctx, me2->export_name);
- if (JS_IsException(str1) || JS_IsException(str2)) {
- /* XXX: raise an error ? */
- ret = 0;
- } else {
- ret = js_string_compare(ctx, JS_VALUE_GET_STRING(str1),
- JS_VALUE_GET_STRING(str2));
- }
- JS_FreeValue(ctx, str1);
- JS_FreeValue(ctx, str2);
- return ret;
- }
- static JSValue js_module_ns_autoinit(JSContext *ctx, JSObject *p, JSAtom atom,
- void *opaque)
- {
- JSModuleDef *m = opaque;
- JSResolveResultEnum res;
- JSExportEntry *res_me;
- JSModuleDef *res_m;
- JSVarRef *var_ref;
- res = js_resolve_export(ctx, &res_m, &res_me, m, atom);
- if (res != JS_RESOLVE_RES_FOUND) {
- /* fail safe: normally no error should happen here except for memory */
- js_resolve_export_throw_error(ctx, res, m, atom);
- return JS_EXCEPTION;
- }
- if (res_me->local_name == JS_ATOM__star_) {
- return JS_GetModuleNamespace(ctx, res_m->req_module_entries[res_me->u.req_module_idx].module);
- } else {
- if (res_me->u.local.var_ref) {
- var_ref = res_me->u.local.var_ref;
- } else {
- JSObject *p1 = JS_VALUE_GET_OBJ(res_m->func_obj);
- var_ref = p1->u.func.var_refs[res_me->u.local.var_idx];
- }
- /* WARNING: a varref is returned as a string ! */
- return JS_MKPTR(JS_TAG_STRING, var_ref);
- }
- }
- static JSValue js_build_module_ns(JSContext *ctx, JSModuleDef *m)
- {
- JSValue obj;
- JSObject *p;
- GetExportNamesState s_s, *s = &s_s;
- int i, ret;
- JSProperty *pr;
- obj = JS_NewObjectClass(ctx, JS_CLASS_MODULE_NS);
- if (JS_IsException(obj))
- return obj;
- p = JS_VALUE_GET_OBJ(obj);
- memset(s, 0, sizeof(*s));
- ret = get_exported_names(ctx, s, m, FALSE);
- js_free(ctx, s->modules);
- if (ret)
- goto fail;
- /* Resolve the exported names. The ambiguous exports are removed */
- for(i = 0; i < s->exported_names_count; i++) {
- ExportedNameEntry *en = &s->exported_names[i];
- JSResolveResultEnum res;
- JSExportEntry *res_me;
- JSModuleDef *res_m;
- if (en->u.me) {
- res_me = en->u.me; /* fast case: no resolution needed */
- res_m = m;
- res = JS_RESOLVE_RES_FOUND;
- } else {
- res = js_resolve_export(ctx, &res_m, &res_me, m,
- en->export_name);
- }
- if (res != JS_RESOLVE_RES_FOUND) {
- if (res != JS_RESOLVE_RES_AMBIGUOUS) {
- js_resolve_export_throw_error(ctx, res, m, en->export_name);
- goto fail;
- }
- en->export_type = EXPORTED_NAME_AMBIGUOUS;
- } else {
- if (res_me->local_name == JS_ATOM__star_) {
- en->export_type = EXPORTED_NAME_DELAYED;
- } else {
- if (res_me->u.local.var_ref) {
- en->u.var_ref = res_me->u.local.var_ref;
- } else {
- JSObject *p1 = JS_VALUE_GET_OBJ(res_m->func_obj);
- en->u.var_ref = p1->u.func.var_refs[res_me->u.local.var_idx];
- }
- if (en->u.var_ref == NULL)
- en->export_type = EXPORTED_NAME_DELAYED;
- else
- en->export_type = EXPORTED_NAME_NORMAL;
- }
- }
- }
- /* sort the exported names */
- rqsort(s->exported_names, s->exported_names_count,
- sizeof(s->exported_names[0]), exported_names_cmp, ctx);
- for(i = 0; i < s->exported_names_count; i++) {
- ExportedNameEntry *en = &s->exported_names[i];
- switch(en->export_type) {
- case EXPORTED_NAME_NORMAL:
- {
- JSVarRef *var_ref = en->u.var_ref;
- pr = add_property(ctx, p, en->export_name,
- JS_PROP_ENUMERABLE | JS_PROP_WRITABLE |
- JS_PROP_VARREF);
- if (!pr)
- goto fail;
- js_rc(var_ref)->ref_count++;
- pr->u.var_ref = var_ref;
- }
- break;
- case EXPORTED_NAME_DELAYED:
- /* the exported namespace or reference may depend on
- circular references, so we resolve it lazily */
- if (JS_DefineAutoInitProperty(ctx, obj,
- en->export_name,
- JS_AUTOINIT_ID_MODULE_NS,
- m, JS_PROP_ENUMERABLE | JS_PROP_WRITABLE) < 0)
- goto fail;
- break;
- default:
- break;
- }
- }
- js_free(ctx, s->exported_names);
- JS_DefinePropertyValue(ctx, obj, JS_ATOM_Symbol_toStringTag,
- JS_AtomToString(ctx, JS_ATOM_Module),
- 0);
- p->extensible = FALSE;
- return obj;
- fail:
- js_free(ctx, s->exported_names);
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- JSValue JS_GetModuleNamespace(JSContext *ctx, JSModuleDef *m)
- {
- if (JS_IsUndefined(m->module_ns)) {
- JSValue val;
- val = js_build_module_ns(ctx, m);
- if (JS_IsException(val))
- return JS_EXCEPTION;
- m->module_ns = val;
- }
- return JS_DupValue(ctx, m->module_ns);
- }
- /* Load all the required modules for module 'm' */
- static int js_resolve_module(JSContext *ctx, JSModuleDef *m)
- {
- int i;
- JSModuleDef *m1;
- if (m->resolved)
- return 0;
- #ifdef DUMP_MODULE_RESOLVE
- {
- char buf1[ATOM_GET_STR_BUF_SIZE];
- printf("resolving module '%s':\n", JS_AtomGetStr(ctx, buf1, sizeof(buf1), m->module_name));
- }
- #endif
- m->resolved = TRUE;
- /* resolve each requested module */
- for(i = 0; i < m->req_module_entries_count; i++) {
- JSReqModuleEntry *rme = &m->req_module_entries[i];
- m1 = js_host_resolve_imported_module_atom(ctx, m->module_name,
- rme->module_name,
- rme->attributes);
- if (!m1)
- return -1;
- rme->module = m1;
- /* already done in js_host_resolve_imported_module() except if
- the module was loaded with JS_EvalBinary() */
- if (js_resolve_module(ctx, m1) < 0)
- return -1;
- }
- return 0;
- }
- /* Create the <eval> function associated with the module */
- static int js_create_module_bytecode_function(JSContext *ctx, JSModuleDef *m)
- {
- JSFunctionBytecode *b;
- JSValue func_obj, bfunc;
- bfunc = m->func_obj;
- func_obj = JS_NewObjectProtoClass(ctx, ctx->function_proto,
- JS_CLASS_BYTECODE_FUNCTION);
- if (JS_IsException(func_obj))
- return -1;
- m->func_obj = func_obj;
- b = JS_VALUE_GET_PTR(bfunc);
- func_obj = js_closure2(ctx, func_obj, b, NULL, NULL, TRUE, m);
- if (JS_IsException(func_obj)) {
- m->func_obj = JS_UNDEFINED; /* XXX: keep it ? */
- JS_FreeValue(ctx, func_obj);
- return -1;
- }
- return 0;
- }
- /* must be done before js_link_module() because of cyclic references */
- static int js_create_module_function(JSContext *ctx, JSModuleDef *m)
- {
- BOOL is_c_module;
- int i;
- JSVarRef *var_ref;
- if (m->func_created)
- return 0;
- is_c_module = (m->init_func != NULL);
- if (is_c_module) {
- /* initialize the exported variables */
- for(i = 0; i < m->export_entries_count; i++) {
- JSExportEntry *me = &m->export_entries[i];
- if (me->export_type == JS_EXPORT_TYPE_LOCAL) {
- var_ref = js_create_var_ref(ctx, FALSE);
- if (!var_ref)
- return -1;
- me->u.local.var_ref = var_ref;
- }
- }
- } else {
- if (js_create_module_bytecode_function(ctx, m))
- return -1;
- }
- m->func_created = TRUE;
- /* do it on the dependencies */
- for(i = 0; i < m->req_module_entries_count; i++) {
- JSReqModuleEntry *rme = &m->req_module_entries[i];
- if (js_create_module_function(ctx, rme->module) < 0)
- return -1;
- }
- return 0;
- }
- /* Prepare a module to be executed by resolving all the imported
- variables. */
- static int js_inner_module_linking(JSContext *ctx, JSModuleDef *m,
- JSModuleDef **pstack_top, int index)
- {
- int i;
- JSImportEntry *mi;
- JSModuleDef *m1;
- JSVarRef **var_refs, *var_ref;
- JSObject *p;
- BOOL is_c_module;
- JSValue ret_val;
- if (js_check_stack_overflow(ctx->rt, 0)) {
- JS_ThrowStackOverflow(ctx);
- return -1;
- }
- #ifdef DUMP_MODULE_RESOLVE
- {
- char buf1[ATOM_GET_STR_BUF_SIZE];
- printf("js_inner_module_linking '%s':\n", JS_AtomGetStr(ctx, buf1, sizeof(buf1), m->module_name));
- }
- #endif
- if (m->status == JS_MODULE_STATUS_LINKING ||
- m->status == JS_MODULE_STATUS_LINKED ||
- m->status == JS_MODULE_STATUS_EVALUATING_ASYNC ||
- m->status == JS_MODULE_STATUS_EVALUATED)
- return index;
- assert(m->status == JS_MODULE_STATUS_UNLINKED);
- m->status = JS_MODULE_STATUS_LINKING;
- m->dfs_index = index;
- m->dfs_ancestor_index = index;
- index++;
- /* push 'm' on stack */
- m->stack_prev = *pstack_top;
- *pstack_top = m;
- for(i = 0; i < m->req_module_entries_count; i++) {
- JSReqModuleEntry *rme = &m->req_module_entries[i];
- m1 = rme->module;
- index = js_inner_module_linking(ctx, m1, pstack_top, index);
- if (index < 0)
- goto fail;
- assert(m1->status == JS_MODULE_STATUS_LINKING ||
- m1->status == JS_MODULE_STATUS_LINKED ||
- m1->status == JS_MODULE_STATUS_EVALUATING_ASYNC ||
- m1->status == JS_MODULE_STATUS_EVALUATED);
- if (m1->status == JS_MODULE_STATUS_LINKING) {
- m->dfs_ancestor_index = min_int(m->dfs_ancestor_index,
- m1->dfs_ancestor_index);
- }
- }
- #ifdef DUMP_MODULE_RESOLVE
- {
- char buf1[ATOM_GET_STR_BUF_SIZE];
- printf("instantiating module '%s':\n", JS_AtomGetStr(ctx, buf1, sizeof(buf1), m->module_name));
- }
- #endif
- /* check the indirect exports */
- for(i = 0; i < m->export_entries_count; i++) {
- JSExportEntry *me = &m->export_entries[i];
- if (me->export_type == JS_EXPORT_TYPE_INDIRECT &&
- me->local_name != JS_ATOM__star_) {
- JSResolveResultEnum ret;
- JSExportEntry *res_me;
- JSModuleDef *res_m, *m1;
- m1 = m->req_module_entries[me->u.req_module_idx].module;
- ret = js_resolve_export(ctx, &res_m, &res_me, m1, me->local_name);
- if (ret != JS_RESOLVE_RES_FOUND) {
- js_resolve_export_throw_error(ctx, ret, m, me->export_name);
- goto fail;
- }
- }
- }
- #ifdef DUMP_MODULE_RESOLVE
- {
- printf("exported bindings:\n");
- for(i = 0; i < m->export_entries_count; i++) {
- JSExportEntry *me = &m->export_entries[i];
- printf(" name="); print_atom(ctx, me->export_name);
- printf(" local="); print_atom(ctx, me->local_name);
- printf(" type=%d idx=%d\n", me->export_type, me->u.local.var_idx);
- }
- }
- #endif
- is_c_module = (m->init_func != NULL);
- if (!is_c_module) {
- p = JS_VALUE_GET_OBJ(m->func_obj);
- var_refs = p->u.func.var_refs;
- for(i = 0; i < m->import_entries_count; i++) {
- mi = &m->import_entries[i];
- #ifdef DUMP_MODULE_RESOLVE
- printf("import var_idx=%d name=", mi->var_idx);
- print_atom(ctx, mi->import_name);
- printf(": ");
- #endif
- m1 = m->req_module_entries[mi->req_module_idx].module;
- if (mi->is_star) {
- JSValue val;
- /* name space import */
- val = JS_GetModuleNamespace(ctx, m1);
- if (JS_IsException(val))
- goto fail;
- set_value(ctx, &var_refs[mi->var_idx]->value, val);
- #ifdef DUMP_MODULE_RESOLVE
- printf("namespace\n");
- #endif
- } else {
- JSResolveResultEnum ret;
- JSExportEntry *res_me;
- JSModuleDef *res_m;
- JSObject *p1;
- ret = js_resolve_export(ctx, &res_m,
- &res_me, m1, mi->import_name);
- if (ret != JS_RESOLVE_RES_FOUND) {
- js_resolve_export_throw_error(ctx, ret, m1, mi->import_name);
- goto fail;
- }
- if (res_me->local_name == JS_ATOM__star_) {
- JSValue val;
- JSModuleDef *m2;
- /* name space import from */
- m2 = res_m->req_module_entries[res_me->u.req_module_idx].module;
- val = JS_GetModuleNamespace(ctx, m2);
- if (JS_IsException(val))
- goto fail;
- var_ref = js_create_var_ref(ctx, TRUE);
- if (!var_ref) {
- JS_FreeValue(ctx, val);
- goto fail;
- }
- set_value(ctx, &var_ref->value, val);
- var_refs[mi->var_idx] = var_ref;
- #ifdef DUMP_MODULE_RESOLVE
- printf("namespace from\n");
- #endif
- } else {
- var_ref = res_me->u.local.var_ref;
- if (!var_ref) {
- p1 = JS_VALUE_GET_OBJ(res_m->func_obj);
- var_ref = p1->u.func.var_refs[res_me->u.local.var_idx];
- }
- js_rc(var_ref)->ref_count++;
- var_refs[mi->var_idx] = var_ref;
- #ifdef DUMP_MODULE_RESOLVE
- printf("local export (var_ref=%p)\n", var_ref);
- #endif
- }
- }
- }
- /* keep the exported variables in the module export entries (they
- are used when the eval function is deleted and cannot be
- initialized before in case imports are exported) */
- for(i = 0; i < m->export_entries_count; i++) {
- JSExportEntry *me = &m->export_entries[i];
- if (me->export_type == JS_EXPORT_TYPE_LOCAL) {
- var_ref = var_refs[me->u.local.var_idx];
- js_rc(var_ref)->ref_count++;
- me->u.local.var_ref = var_ref;
- }
- }
- /* initialize the global variables */
- ret_val = JS_Call(ctx, m->func_obj, JS_TRUE, 0, NULL);
- if (JS_IsException(ret_val))
- goto fail;
- JS_FreeValue(ctx, ret_val);
- }
- assert(m->dfs_ancestor_index <= m->dfs_index);
- if (m->dfs_index == m->dfs_ancestor_index) {
- for(;;) {
- /* pop m1 from stack */
- m1 = *pstack_top;
- *pstack_top = m1->stack_prev;
- m1->status = JS_MODULE_STATUS_LINKED;
- if (m1 == m)
- break;
- }
- }
- #ifdef DUMP_MODULE_RESOLVE
- printf("js_inner_module_linking done\n");
- #endif
- return index;
- fail:
- return -1;
- }
- /* Prepare a module to be executed by resolving all the imported
- variables. */
- static int js_link_module(JSContext *ctx, JSModuleDef *m)
- {
- JSModuleDef *stack_top, *m1;
- #ifdef DUMP_MODULE_RESOLVE
- {
- char buf1[ATOM_GET_STR_BUF_SIZE];
- printf("js_link_module '%s':\n", JS_AtomGetStr(ctx, buf1, sizeof(buf1), m->module_name));
- }
- #endif
- assert(m->status == JS_MODULE_STATUS_UNLINKED ||
- m->status == JS_MODULE_STATUS_LINKED ||
- m->status == JS_MODULE_STATUS_EVALUATING_ASYNC ||
- m->status == JS_MODULE_STATUS_EVALUATED);
- stack_top = NULL;
- if (js_inner_module_linking(ctx, m, &stack_top, 0) < 0) {
- while (stack_top != NULL) {
- m1 = stack_top;
- assert(m1->status == JS_MODULE_STATUS_LINKING);
- m1->status = JS_MODULE_STATUS_UNLINKED;
- stack_top = m1->stack_prev;
- }
- return -1;
- }
- assert(stack_top == NULL);
- assert(m->status == JS_MODULE_STATUS_LINKED ||
- m->status == JS_MODULE_STATUS_EVALUATING_ASYNC ||
- m->status == JS_MODULE_STATUS_EVALUATED);
- return 0;
- }
- /* return JS_ATOM_NULL if the name cannot be found. Only works with
- not striped bytecode functions. */
- JSAtom JS_GetScriptOrModuleName(JSContext *ctx, int n_stack_levels)
- {
- JSStackFrame *sf;
- JSFunctionBytecode *b;
- JSObject *p;
- /* XXX: currently we just use the filename of the englobing
- function from the debug info. May need to add a ScriptOrModule
- info in JSFunctionBytecode. */
- sf = ctx->rt->current_stack_frame;
- if (!sf)
- return JS_ATOM_NULL;
- while (n_stack_levels-- > 0) {
- sf = sf->prev_frame;
- if (!sf)
- return JS_ATOM_NULL;
- }
- for(;;) {
- if (JS_VALUE_GET_TAG(sf->cur_func) != JS_TAG_OBJECT)
- return JS_ATOM_NULL;
- p = JS_VALUE_GET_OBJ(sf->cur_func);
- if (!js_class_has_bytecode(p->class_id))
- return JS_ATOM_NULL;
- b = p->u.func.function_bytecode;
- if (!b->is_direct_or_indirect_eval) {
- if (!b->has_debug)
- return JS_ATOM_NULL;
- return JS_DupAtom(ctx, b->debug.filename);
- } else {
- sf = sf->prev_frame;
- if (!sf)
- return JS_ATOM_NULL;
- }
- }
- }
- JSAtom JS_GetModuleName(JSContext *ctx, JSModuleDef *m)
- {
- return JS_DupAtom(ctx, m->module_name);
- }
- JSValue JS_GetImportMeta(JSContext *ctx, JSModuleDef *m)
- {
- JSValue obj;
- /* allocate meta_obj only if requested to save memory */
- obj = m->meta_obj;
- if (JS_IsUndefined(obj)) {
- obj = JS_NewObjectProto(ctx, JS_NULL);
- if (JS_IsException(obj))
- return JS_EXCEPTION;
- m->meta_obj = obj;
- }
- return JS_DupValue(ctx, obj);
- }
- static JSValue js_import_meta(JSContext *ctx)
- {
- JSAtom filename;
- JSModuleDef *m;
- filename = JS_GetScriptOrModuleName(ctx, 0);
- if (filename == JS_ATOM_NULL)
- goto fail;
- /* XXX: inefficient, need to add a module or script pointer in
- JSFunctionBytecode */
- m = js_find_loaded_module(ctx, filename);
- JS_FreeAtom(ctx, filename);
- if (!m) {
- fail:
- JS_ThrowTypeError(ctx, "import.meta not supported in this context");
- return JS_EXCEPTION;
- }
- return JS_GetImportMeta(ctx, m);
- }
- static JSValue JS_NewModuleValue(JSContext *ctx, JSModuleDef *m)
- {
- return JS_DupValue(ctx, JS_MKPTR(JS_TAG_MODULE, m));
- }
- static JSValue js_load_module_rejected(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic, JSValue *func_data)
- {
- JSValueConst *resolving_funcs = (JSValueConst *)func_data;
- JSValueConst error;
- JSValue ret;
- /* XXX: check if the test is necessary */
- if (argc >= 1)
- error = argv[0];
- else
- error = JS_UNDEFINED;
- ret = JS_Call(ctx, resolving_funcs[1], JS_UNDEFINED,
- 1, &error);
- JS_FreeValue(ctx, ret);
- return JS_UNDEFINED;
- }
- static JSValue js_load_module_fulfilled(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic, JSValue *func_data)
- {
- JSValueConst *resolving_funcs = (JSValueConst *)func_data;
- JSModuleDef *m = JS_VALUE_GET_PTR(func_data[2]);
- JSValue ret, ns;
- /* return the module namespace */
- ns = JS_GetModuleNamespace(ctx, m);
- if (JS_IsException(ns)) {
- JSValue err = JS_GetException(ctx);
- js_load_module_rejected(ctx, JS_UNDEFINED, 1, (JSValueConst *)&err, 0, func_data);
- return JS_UNDEFINED;
- }
- ret = JS_Call(ctx, resolving_funcs[0], JS_UNDEFINED,
- 1, (JSValueConst *)&ns);
- JS_FreeValue(ctx, ret);
- JS_FreeValue(ctx, ns);
- return JS_UNDEFINED;
- }
- static void JS_LoadModuleInternal(JSContext *ctx, const char *basename,
- const char *filename,
- JSValueConst *resolving_funcs,
- JSValueConst attributes)
- {
- JSValue evaluate_promise;
- JSModuleDef *m;
- JSValue ret, err, func_obj, evaluate_resolving_funcs[2];
- JSValueConst func_data[3];
- m = js_host_resolve_imported_module(ctx, basename, filename, attributes);
- if (!m)
- goto fail;
- if (js_resolve_module(ctx, m) < 0) {
- js_free_modules(ctx, JS_FREE_MODULE_NOT_RESOLVED);
- goto fail;
- }
- /* Evaluate the module code */
- func_obj = JS_NewModuleValue(ctx, m);
- evaluate_promise = JS_EvalFunction(ctx, func_obj);
- if (JS_IsException(evaluate_promise)) {
- fail:
- err = JS_GetException(ctx);
- ret = JS_Call(ctx, resolving_funcs[1], JS_UNDEFINED,
- 1, (JSValueConst *)&err);
- JS_FreeValue(ctx, ret); /* XXX: what to do if exception ? */
- JS_FreeValue(ctx, err);
- return;
- }
- func_obj = JS_NewModuleValue(ctx, m);
- func_data[0] = resolving_funcs[0];
- func_data[1] = resolving_funcs[1];
- func_data[2] = func_obj;
- evaluate_resolving_funcs[0] = JS_NewCFunctionData(ctx, js_load_module_fulfilled, 0, 0, 3, func_data);
- evaluate_resolving_funcs[1] = JS_NewCFunctionData(ctx, js_load_module_rejected, 0, 0, 3, func_data);
- JS_FreeValue(ctx, func_obj);
- ret = js_promise_then(ctx, evaluate_promise, 2, (JSValueConst *)evaluate_resolving_funcs);
- JS_FreeValue(ctx, ret);
- JS_FreeValue(ctx, evaluate_resolving_funcs[0]);
- JS_FreeValue(ctx, evaluate_resolving_funcs[1]);
- JS_FreeValue(ctx, evaluate_promise);
- }
- /* Return a promise or an exception in case of memory error. Used by
- os.Worker() */
- JSValue JS_LoadModule(JSContext *ctx, const char *basename,
- const char *filename)
- {
- JSValue promise, resolving_funcs[2];
- promise = JS_NewPromiseCapability(ctx, resolving_funcs);
- if (JS_IsException(promise))
- return JS_EXCEPTION;
- JS_LoadModuleInternal(ctx, basename, filename,
- (JSValueConst *)resolving_funcs, JS_UNDEFINED);
- JS_FreeValue(ctx, resolving_funcs[0]);
- JS_FreeValue(ctx, resolving_funcs[1]);
- return promise;
- }
- static JSValue js_dynamic_import_job(JSContext *ctx,
- int argc, JSValueConst *argv)
- {
- JSValueConst *resolving_funcs = argv;
- JSValueConst basename_val = argv[2];
- JSValueConst specifier = argv[3];
- JSValueConst attributes = argv[4];
- const char *basename = NULL, *filename;
- JSValue ret, err;
- if (!JS_IsString(basename_val)) {
- JS_ThrowTypeError(ctx, "no function filename for import()");
- goto exception;
- }
- basename = JS_ToCString(ctx, basename_val);
- if (!basename)
- goto exception;
- filename = JS_ToCString(ctx, specifier);
- if (!filename)
- goto exception;
- JS_LoadModuleInternal(ctx, basename, filename,
- resolving_funcs, attributes);
- JS_FreeCString(ctx, filename);
- JS_FreeCString(ctx, basename);
- return JS_UNDEFINED;
- exception:
- err = JS_GetException(ctx);
- ret = JS_Call(ctx, resolving_funcs[1], JS_UNDEFINED,
- 1, (JSValueConst *)&err);
- JS_FreeValue(ctx, ret); /* XXX: what to do if exception ? */
- JS_FreeValue(ctx, err);
- JS_FreeCString(ctx, basename);
- return JS_UNDEFINED;
- }
- static JSValue js_dynamic_import(JSContext *ctx, JSValueConst specifier, JSValueConst options)
- {
- JSAtom basename;
- JSValue promise, resolving_funcs[2], basename_val, err, ret;
- JSValue specifier_str = JS_UNDEFINED, attributes = JS_UNDEFINED, attributes_obj = JS_UNDEFINED;
- JSValueConst args[5];
- basename = JS_GetScriptOrModuleName(ctx, 0);
- if (basename == JS_ATOM_NULL)
- basename_val = JS_NULL;
- else
- basename_val = JS_AtomToValue(ctx, basename);
- JS_FreeAtom(ctx, basename);
- if (JS_IsException(basename_val))
- return basename_val;
- promise = JS_NewPromiseCapability(ctx, resolving_funcs);
- if (JS_IsException(promise)) {
- JS_FreeValue(ctx, basename_val);
- return promise;
- }
- /* the string conversion must occur here */
- specifier_str = JS_ToString(ctx, specifier);
- if (JS_IsException(specifier_str))
- goto exception;
-
- if (!JS_IsUndefined(options)) {
- if (!JS_IsObject(options)) {
- JS_ThrowTypeError(ctx, "options must be an object");
- goto exception;
- }
- attributes_obj = JS_GetProperty(ctx, options, JS_ATOM_with);
- if (JS_IsException(attributes_obj))
- goto exception;
- if (!JS_IsUndefined(attributes_obj)) {
- JSPropertyEnum *atoms;
- uint32_t atoms_len, i;
- JSValue val;
-
- if (!JS_IsObject(attributes_obj)) {
- JS_ThrowTypeError(ctx, "options.with must be an object");
- goto exception;
- }
- attributes = JS_NewObjectProto(ctx, JS_NULL);
- if (JS_GetOwnPropertyNamesInternal(ctx, &atoms, &atoms_len, JS_VALUE_GET_OBJ(attributes_obj),
- JS_GPN_STRING_MASK | JS_GPN_ENUM_ONLY)) {
- goto exception;
- }
- for(i = 0; i < atoms_len; i++) {
- val = JS_GetProperty(ctx, attributes_obj, atoms[i].atom);
- if (JS_IsException(val))
- goto exception1;
- if (!JS_IsString(val)) {
- JS_FreeValue(ctx, val);
- JS_ThrowTypeError(ctx, "module attribute values must be strings");
- goto exception1;
- }
- if (JS_DefinePropertyValue(ctx, attributes, atoms[i].atom, val,
- JS_PROP_C_W_E) < 0) {
- exception1:
- JS_FreePropertyEnum(ctx, atoms, atoms_len);
- goto exception;
- }
- }
- JS_FreePropertyEnum(ctx, atoms, atoms_len);
- if (ctx->rt->module_check_attrs &&
- ctx->rt->module_check_attrs(ctx, ctx->rt->module_loader_opaque, attributes) < 0) {
- goto exception;
- }
- JS_FreeValue(ctx, attributes_obj);
- }
- }
- args[0] = resolving_funcs[0];
- args[1] = resolving_funcs[1];
- args[2] = basename_val;
- args[3] = specifier_str;
- args[4] = attributes;
-
- /* cannot run JS_LoadModuleInternal synchronously because it would
- cause an unexpected recursion in js_evaluate_module() */
- JS_EnqueueJob(ctx, js_dynamic_import_job, 5, args);
- done:
- JS_FreeValue(ctx, basename_val);
- JS_FreeValue(ctx, resolving_funcs[0]);
- JS_FreeValue(ctx, resolving_funcs[1]);
- JS_FreeValue(ctx, specifier_str);
- JS_FreeValue(ctx, attributes);
- return promise;
- exception:
- JS_FreeValue(ctx, attributes_obj);
- err = JS_GetException(ctx);
- ret = JS_Call(ctx, resolving_funcs[1], JS_UNDEFINED,
- 1, (JSValueConst *)&err);
- JS_FreeValue(ctx, ret);
- JS_FreeValue(ctx, err);
- goto done;
- }
- static void js_set_module_evaluated(JSContext *ctx, JSModuleDef *m)
- {
- m->status = JS_MODULE_STATUS_EVALUATED;
- if (!JS_IsUndefined(m->promise)) {
- JSValue value, ret_val;
- assert(m->cycle_root == m);
- value = JS_UNDEFINED;
- ret_val = JS_Call(ctx, m->resolving_funcs[0], JS_UNDEFINED,
- 1, (JSValueConst *)&value);
- JS_FreeValue(ctx, ret_val);
- }
- }
- typedef struct {
- JSModuleDef **tab;
- int count;
- int size;
- } ExecModuleList;
- /* XXX: slow. Could use a linked list instead of ExecModuleList */
- static BOOL find_in_exec_module_list(ExecModuleList *exec_list, JSModuleDef *m)
- {
- int i;
- for(i = 0; i < exec_list->count; i++) {
- if (exec_list->tab[i] == m)
- return TRUE;
- }
- return FALSE;
- }
- static int gather_available_ancestors(JSContext *ctx, JSModuleDef *module,
- ExecModuleList *exec_list)
- {
- int i;
- if (js_check_stack_overflow(ctx->rt, 0)) {
- JS_ThrowStackOverflow(ctx);
- return -1;
- }
- for(i = 0; i < module->async_parent_modules_count; i++) {
- JSModuleDef *m = module->async_parent_modules[i];
- if (!find_in_exec_module_list(exec_list, m) &&
- !m->cycle_root->eval_has_exception) {
- assert(m->status == JS_MODULE_STATUS_EVALUATING_ASYNC);
- assert(!m->eval_has_exception);
- assert(m->async_evaluation);
- assert(m->pending_async_dependencies > 0);
- m->pending_async_dependencies--;
- if (m->pending_async_dependencies == 0) {
- if (js_resize_array(ctx, (void **)&exec_list->tab, sizeof(exec_list->tab[0]), &exec_list->size, exec_list->count + 1)) {
- return -1;
- }
- exec_list->tab[exec_list->count++] = m;
- if (!m->has_tla) {
- if (gather_available_ancestors(ctx, m, exec_list))
- return -1;
- }
- }
- }
- }
- return 0;
- }
- static int exec_module_list_cmp(const void *p1, const void *p2, void *opaque)
- {
- JSModuleDef *m1 = *(JSModuleDef **)p1;
- JSModuleDef *m2 = *(JSModuleDef **)p2;
- return (m1->async_evaluation_timestamp > m2->async_evaluation_timestamp) -
- (m1->async_evaluation_timestamp < m2->async_evaluation_timestamp);
- }
- static int js_execute_async_module(JSContext *ctx, JSModuleDef *m);
- static int js_execute_sync_module(JSContext *ctx, JSModuleDef *m,
- JSValue *pvalue);
- #ifdef DUMP_MODULE_EXEC
- static void js_dump_module(JSContext *ctx, const char *str, JSModuleDef *m)
- {
- char buf1[ATOM_GET_STR_BUF_SIZE];
- static const char *module_status_str[] = { "unlinked", "linking", "linked", "evaluating", "evaluating_async", "evaluated" };
- printf("%s: %s status=%s\n", str, JS_AtomGetStr(ctx, buf1, sizeof(buf1), m->module_name), module_status_str[m->status]);
- }
- #endif
- static JSValue js_async_module_execution_rejected(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic, JSValue *func_data)
- {
- JSModuleDef *module = JS_VALUE_GET_PTR(func_data[0]);
- JSValueConst error = argv[0];
- int i;
- #ifdef DUMP_MODULE_EXEC
- js_dump_module(ctx, __func__, module);
- #endif
- if (js_check_stack_overflow(ctx->rt, 0))
- return JS_ThrowStackOverflow(ctx);
- if (module->status == JS_MODULE_STATUS_EVALUATED) {
- assert(module->eval_has_exception);
- return JS_UNDEFINED;
- }
- assert(module->status == JS_MODULE_STATUS_EVALUATING_ASYNC);
- assert(!module->eval_has_exception);
- assert(module->async_evaluation);
- module->eval_has_exception = TRUE;
- module->eval_exception = JS_DupValue(ctx, error);
- module->status = JS_MODULE_STATUS_EVALUATED;
- module->async_evaluation = FALSE;
- if (!JS_IsUndefined(module->promise)) {
- JSValue ret_val;
- assert(module->cycle_root == module);
- ret_val = JS_Call(ctx, module->resolving_funcs[1], JS_UNDEFINED,
- 1, &error);
- JS_FreeValue(ctx, ret_val);
- }
- for(i = 0; i < module->async_parent_modules_count; i++) {
- JSModuleDef *m = module->async_parent_modules[i];
- JSValue m_obj = JS_NewModuleValue(ctx, m);
- js_async_module_execution_rejected(ctx, JS_UNDEFINED, 1, &error, 0,
- &m_obj);
- JS_FreeValue(ctx, m_obj);
- }
- return JS_UNDEFINED;
- }
- static JSValue js_async_module_execution_fulfilled(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic, JSValue *func_data)
- {
- JSModuleDef *module = JS_VALUE_GET_PTR(func_data[0]);
- ExecModuleList exec_list_s, *exec_list = &exec_list_s;
- int i;
- #ifdef DUMP_MODULE_EXEC
- js_dump_module(ctx, __func__, module);
- #endif
- if (module->status == JS_MODULE_STATUS_EVALUATED) {
- assert(module->eval_has_exception);
- return JS_UNDEFINED;
- }
- assert(module->status == JS_MODULE_STATUS_EVALUATING_ASYNC);
- assert(!module->eval_has_exception);
- assert(module->async_evaluation);
- module->async_evaluation = FALSE;
- js_set_module_evaluated(ctx, module);
- exec_list->tab = NULL;
- exec_list->count = 0;
- exec_list->size = 0;
- if (gather_available_ancestors(ctx, module, exec_list) < 0) {
- js_free(ctx, exec_list->tab);
- return JS_EXCEPTION;
- }
- /* sort by increasing async_evaluation timestamp */
- rqsort(exec_list->tab, exec_list->count, sizeof(exec_list->tab[0]),
- exec_module_list_cmp, NULL);
- for(i = 0; i < exec_list->count; i++) {
- JSModuleDef *m = exec_list->tab[i];
- #ifdef DUMP_MODULE_EXEC
- printf(" %d/%d", i, exec_list->count); js_dump_module(ctx, "", m);
- #endif
- if (m->status == JS_MODULE_STATUS_EVALUATED) {
- assert(m->eval_has_exception);
- } else if (m->has_tla) {
- js_execute_async_module(ctx, m);
- } else {
- JSValue error;
- if (js_execute_sync_module(ctx, m, &error) < 0) {
- JSValue m_obj = JS_NewModuleValue(ctx, m);
- js_async_module_execution_rejected(ctx, JS_UNDEFINED,
- 1, (JSValueConst *)&error, 0,
- &m_obj);
- JS_FreeValue(ctx, m_obj);
- JS_FreeValue(ctx, error);
- } else {
- m->async_evaluation = FALSE;
- js_set_module_evaluated(ctx, m);
- }
- }
- }
- js_free(ctx, exec_list->tab);
- return JS_UNDEFINED;
- }
- static int js_execute_async_module(JSContext *ctx, JSModuleDef *m)
- {
- JSValue promise, m_obj;
- JSValue resolve_funcs[2], ret_val;
- #ifdef DUMP_MODULE_EXEC
- js_dump_module(ctx, __func__, m);
- #endif
- promise = js_async_function_call(ctx, m->func_obj, JS_UNDEFINED, 0, NULL, 0);
- if (JS_IsException(promise))
- return -1;
- m_obj = JS_NewModuleValue(ctx, m);
- resolve_funcs[0] = JS_NewCFunctionData(ctx, js_async_module_execution_fulfilled, 0, 0, 1, (JSValueConst *)&m_obj);
- resolve_funcs[1] = JS_NewCFunctionData(ctx, js_async_module_execution_rejected, 0, 0, 1, (JSValueConst *)&m_obj);
- ret_val = js_promise_then(ctx, promise, 2, (JSValueConst *)resolve_funcs);
- JS_FreeValue(ctx, ret_val);
- JS_FreeValue(ctx, m_obj);
- JS_FreeValue(ctx, resolve_funcs[0]);
- JS_FreeValue(ctx, resolve_funcs[1]);
- JS_FreeValue(ctx, promise);
- return 0;
- }
- /* return < 0 in case of exception. *pvalue contains the exception. */
- static int js_execute_sync_module(JSContext *ctx, JSModuleDef *m,
- JSValue *pvalue)
- {
- #ifdef DUMP_MODULE_EXEC
- js_dump_module(ctx, __func__, m);
- #endif
- if (m->init_func) {
- /* C module init : no asynchronous execution */
- if (m->init_func(ctx, m) < 0)
- goto fail;
- } else {
- JSValue promise;
- JSPromiseStateEnum state;
- promise = js_async_function_call(ctx, m->func_obj, JS_UNDEFINED, 0, NULL, 0);
- if (JS_IsException(promise))
- goto fail;
- state = JS_PromiseState(ctx, promise);
- if (state == JS_PROMISE_FULFILLED) {
- JS_FreeValue(ctx, promise);
- } else if (state == JS_PROMISE_REJECTED) {
- *pvalue = JS_PromiseResult(ctx, promise);
- JS_FreeValue(ctx, promise);
- return -1;
- } else {
- JS_FreeValue(ctx, promise);
- JS_ThrowTypeError(ctx, "promise is pending");
- fail:
- *pvalue = JS_GetException(ctx);
- return -1;
- }
- }
- *pvalue = JS_UNDEFINED;
- return 0;
- }
- /* spec: InnerModuleEvaluation. Return (index, JS_UNDEFINED) or (-1,
- exception) */
- static int js_inner_module_evaluation(JSContext *ctx, JSModuleDef *m,
- int index, JSModuleDef **pstack_top,
- JSValue *pvalue)
- {
- JSModuleDef *m1;
- int i;
- #ifdef DUMP_MODULE_EXEC
- js_dump_module(ctx, __func__, m);
- #endif
- if (js_check_stack_overflow(ctx->rt, 0)) {
- JS_ThrowStackOverflow(ctx);
- *pvalue = JS_GetException(ctx);
- return -1;
- }
- if (m->status == JS_MODULE_STATUS_EVALUATING_ASYNC ||
- m->status == JS_MODULE_STATUS_EVALUATED) {
- if (m->eval_has_exception) {
- *pvalue = JS_DupValue(ctx, m->eval_exception);
- return -1;
- } else {
- *pvalue = JS_UNDEFINED;
- return index;
- }
- }
- if (m->status == JS_MODULE_STATUS_EVALUATING) {
- *pvalue = JS_UNDEFINED;
- return index;
- }
- assert(m->status == JS_MODULE_STATUS_LINKED);
- m->status = JS_MODULE_STATUS_EVALUATING;
- m->dfs_index = index;
- m->dfs_ancestor_index = index;
- m->pending_async_dependencies = 0;
- index++;
- /* push 'm' on stack */
- m->stack_prev = *pstack_top;
- *pstack_top = m;
- for(i = 0; i < m->req_module_entries_count; i++) {
- JSReqModuleEntry *rme = &m->req_module_entries[i];
- m1 = rme->module;
- index = js_inner_module_evaluation(ctx, m1, index, pstack_top, pvalue);
- if (index < 0)
- return -1;
- assert(m1->status == JS_MODULE_STATUS_EVALUATING ||
- m1->status == JS_MODULE_STATUS_EVALUATING_ASYNC ||
- m1->status == JS_MODULE_STATUS_EVALUATED);
- if (m1->status == JS_MODULE_STATUS_EVALUATING) {
- m->dfs_ancestor_index = min_int(m->dfs_ancestor_index,
- m1->dfs_ancestor_index);
- } else {
- m1 = m1->cycle_root;
- assert(m1->status == JS_MODULE_STATUS_EVALUATING_ASYNC ||
- m1->status == JS_MODULE_STATUS_EVALUATED);
- if (m1->eval_has_exception) {
- *pvalue = JS_DupValue(ctx, m1->eval_exception);
- return -1;
- }
- }
- if (m1->async_evaluation) {
- m->pending_async_dependencies++;
- if (js_resize_array(ctx, (void **)&m1->async_parent_modules, sizeof(m1->async_parent_modules[0]), &m1->async_parent_modules_size, m1->async_parent_modules_count + 1)) {
- *pvalue = JS_GetException(ctx);
- return -1;
- }
- m1->async_parent_modules[m1->async_parent_modules_count++] = m;
- }
- }
- if (m->pending_async_dependencies > 0) {
- assert(!m->async_evaluation);
- m->async_evaluation = TRUE;
- m->async_evaluation_timestamp =
- ctx->rt->module_async_evaluation_next_timestamp++;
- } else if (m->has_tla) {
- assert(!m->async_evaluation);
- m->async_evaluation = TRUE;
- m->async_evaluation_timestamp =
- ctx->rt->module_async_evaluation_next_timestamp++;
- js_execute_async_module(ctx, m);
- } else {
- if (js_execute_sync_module(ctx, m, pvalue) < 0)
- return -1;
- }
- assert(m->dfs_ancestor_index <= m->dfs_index);
- if (m->dfs_index == m->dfs_ancestor_index) {
- for(;;) {
- /* pop m1 from stack */
- m1 = *pstack_top;
- *pstack_top = m1->stack_prev;
- if (!m1->async_evaluation) {
- m1->status = JS_MODULE_STATUS_EVALUATED;
- } else {
- m1->status = JS_MODULE_STATUS_EVALUATING_ASYNC;
- }
- /* spec bug: cycle_root must be assigned before the test */
- m1->cycle_root = m;
- if (m1 == m)
- break;
- }
- }
- *pvalue = JS_UNDEFINED;
- return index;
- }
- /* Run the <eval> function of the module and of all its requested
- modules. Return a promise or an exception. */
- static JSValue js_evaluate_module(JSContext *ctx, JSModuleDef *m)
- {
- JSModuleDef *m1, *stack_top;
- JSValue ret_val, result;
- #ifdef DUMP_MODULE_EXEC
- js_dump_module(ctx, __func__, m);
- #endif
- assert(m->status == JS_MODULE_STATUS_LINKED ||
- m->status == JS_MODULE_STATUS_EVALUATING_ASYNC ||
- m->status == JS_MODULE_STATUS_EVALUATED);
- if (m->status == JS_MODULE_STATUS_EVALUATING_ASYNC ||
- m->status == JS_MODULE_STATUS_EVALUATED) {
- m = m->cycle_root;
- }
- /* a promise may be created only on the cycle_root of a cycle */
- if (!JS_IsUndefined(m->promise))
- return JS_DupValue(ctx, m->promise);
- m->promise = JS_NewPromiseCapability(ctx, m->resolving_funcs);
- if (JS_IsException(m->promise))
- return JS_EXCEPTION;
- stack_top = NULL;
- if (js_inner_module_evaluation(ctx, m, 0, &stack_top, &result) < 0) {
- while (stack_top != NULL) {
- m1 = stack_top;
- assert(m1->status == JS_MODULE_STATUS_EVALUATING);
- m1->status = JS_MODULE_STATUS_EVALUATED;
- m1->eval_has_exception = TRUE;
- m1->eval_exception = JS_DupValue(ctx, result);
- m1->cycle_root = m; /* spec bug: should be present */
- stack_top = m1->stack_prev;
- }
- JS_FreeValue(ctx, result);
- assert(m->status == JS_MODULE_STATUS_EVALUATED);
- assert(m->eval_has_exception);
- ret_val = JS_Call(ctx, m->resolving_funcs[1], JS_UNDEFINED,
- 1, (JSValueConst *)&m->eval_exception);
- JS_FreeValue(ctx, ret_val);
- } else {
- #ifdef DUMP_MODULE_EXEC
- js_dump_module(ctx, " done", m);
- #endif
- assert(m->status == JS_MODULE_STATUS_EVALUATING_ASYNC ||
- m->status == JS_MODULE_STATUS_EVALUATED);
- assert(!m->eval_has_exception);
- if (!m->async_evaluation) {
- JSValue value;
- assert(m->status == JS_MODULE_STATUS_EVALUATED);
- value = JS_UNDEFINED;
- ret_val = JS_Call(ctx, m->resolving_funcs[0], JS_UNDEFINED,
- 1, (JSValueConst *)&value);
- JS_FreeValue(ctx, ret_val);
- }
- assert(stack_top == NULL);
- }
- return JS_DupValue(ctx, m->promise);
- }
- static __exception int js_parse_with_clause(JSParseState *s, JSReqModuleEntry *rme)
- {
- JSContext *ctx = s->ctx;
- JSAtom key;
- int ret;
- const uint8_t *key_token_ptr;
-
- if (next_token(s))
- return -1;
- if (js_parse_expect(s, '{'))
- return -1;
- while (s->token.val != '}') {
- key_token_ptr = s->token.ptr;
- if (s->token.val == TOK_STRING) {
- key = JS_ValueToAtom(ctx, s->token.u.str.str);
- if (key == JS_ATOM_NULL)
- return -1;
- } else {
- if (!token_is_ident(s->token.val)) {
- js_parse_error(s, "identifier expected");
- return -1;
- }
- key = JS_DupAtom(ctx, s->token.u.ident.atom);
- }
- if (next_token(s))
- return -1;
- if (js_parse_expect(s, ':')) {
- JS_FreeAtom(ctx, key);
- return -1;
- }
- if (s->token.val != TOK_STRING) {
- js_parse_error_pos(s, key_token_ptr, "string expected");
- return -1;
- }
- if (JS_IsUndefined(rme->attributes)) {
- JSValue attributes = JS_NewObjectProto(ctx, JS_NULL);
- if (JS_IsException(attributes)) {
- JS_FreeAtom(ctx, key);
- return -1;
- }
- rme->attributes = attributes;
- }
- ret = JS_HasProperty(ctx, rme->attributes, key);
- if (ret != 0) {
- JS_FreeAtom(ctx, key);
- if (ret < 0)
- return -1;
- else
- return js_parse_error(s, "duplicate with key");
- }
- ret = JS_DefinePropertyValue(ctx, rme->attributes, key,
- JS_DupValue(ctx, s->token.u.str.str), JS_PROP_C_W_E);
- JS_FreeAtom(ctx, key);
- if (ret < 0)
- return -1;
- if (next_token(s))
- return -1;
- if (s->token.val != ',')
- break;
- if (next_token(s))
- return -1;
- }
- if (!JS_IsUndefined(rme->attributes) &&
- ctx->rt->module_check_attrs &&
- ctx->rt->module_check_attrs(ctx, ctx->rt->module_loader_opaque, rme->attributes) < 0) {
- return -1;
- }
- return js_parse_expect(s, '}');
- }
- /* return the module index in m->req_module_entries[] or < 0 if error */
- static __exception int js_parse_from_clause(JSParseState *s, JSModuleDef *m)
- {
- JSAtom module_name;
- int idx;
- if (!token_is_pseudo_keyword(s, JS_ATOM_from)) {
- js_parse_error(s, "from clause expected");
- return -1;
- }
- if (next_token(s))
- return -1;
- if (s->token.val != TOK_STRING) {
- js_parse_error(s, "string expected");
- return -1;
- }
- module_name = JS_ValueToAtom(s->ctx, s->token.u.str.str);
- if (module_name == JS_ATOM_NULL)
- return -1;
- if (next_token(s)) {
- JS_FreeAtom(s->ctx, module_name);
- return -1;
- }
- idx = add_req_module_entry(s->ctx, m, module_name);
- JS_FreeAtom(s->ctx, module_name);
- if (idx < 0)
- return -1;
- if (s->token.val == TOK_WITH) {
- if (js_parse_with_clause(s, &m->req_module_entries[idx]))
- return -1;
- }
- return idx;
- }
- static __exception int js_parse_export(JSParseState *s)
- {
- JSContext *ctx = s->ctx;
- JSModuleDef *m = s->cur_func->module;
- JSAtom local_name, export_name;
- int first_export, idx, i, tok;
- JSExportEntry *me;
- if (next_token(s))
- return -1;
- tok = s->token.val;
- if (tok == TOK_CLASS) {
- return js_parse_class(s, FALSE, JS_PARSE_EXPORT_NAMED);
- } else if (tok == TOK_FUNCTION ||
- (token_is_pseudo_keyword(s, JS_ATOM_async) &&
- peek_token(s, TRUE) == TOK_FUNCTION)) {
- return js_parse_function_decl2(s, JS_PARSE_FUNC_STATEMENT,
- JS_FUNC_NORMAL, JS_ATOM_NULL,
- s->token.ptr,
- JS_PARSE_EXPORT_NAMED, NULL);
- }
- if (next_token(s))
- return -1;
- switch(tok) {
- case '{':
- first_export = m->export_entries_count;
- while (s->token.val != '}') {
- if (!token_is_ident(s->token.val)) {
- js_parse_error(s, "identifier expected");
- return -1;
- }
- local_name = JS_DupAtom(ctx, s->token.u.ident.atom);
- export_name = JS_ATOM_NULL;
- if (next_token(s))
- goto fail;
- if (token_is_pseudo_keyword(s, JS_ATOM_as)) {
- if (next_token(s))
- goto fail;
- if (s->token.val == TOK_STRING) {
- if (js_string_find_invalid_codepoint(JS_VALUE_GET_STRING(s->token.u.str.str)) >= 0) {
- js_parse_error(s, "contains unpaired surrogate");
- goto fail;
- }
- export_name = JS_ValueToAtom(s->ctx, s->token.u.str.str);
- if (export_name == JS_ATOM_NULL)
- goto fail;
- } else {
- if (!token_is_ident(s->token.val)) {
- js_parse_error(s, "identifier expected");
- goto fail;
- }
- export_name = JS_DupAtom(ctx, s->token.u.ident.atom);
- }
- if (next_token(s)) {
- fail:
- JS_FreeAtom(ctx, local_name);
- fail1:
- JS_FreeAtom(ctx, export_name);
- return -1;
- }
- } else {
- export_name = JS_DupAtom(ctx, local_name);
- }
- me = add_export_entry(s, m, local_name, export_name,
- JS_EXPORT_TYPE_LOCAL);
- JS_FreeAtom(ctx, local_name);
- JS_FreeAtom(ctx, export_name);
- if (!me)
- return -1;
- if (s->token.val != ',')
- break;
- if (next_token(s))
- return -1;
- }
- if (js_parse_expect(s, '}'))
- return -1;
- if (token_is_pseudo_keyword(s, JS_ATOM_from)) {
- idx = js_parse_from_clause(s, m);
- if (idx < 0)
- return -1;
- for(i = first_export; i < m->export_entries_count; i++) {
- me = &m->export_entries[i];
- me->export_type = JS_EXPORT_TYPE_INDIRECT;
- me->u.req_module_idx = idx;
- }
- }
- break;
- case '*':
- if (token_is_pseudo_keyword(s, JS_ATOM_as)) {
- /* export ns from */
- if (next_token(s))
- return -1;
- if (!token_is_ident(s->token.val)) {
- js_parse_error(s, "identifier expected");
- return -1;
- }
- export_name = JS_DupAtom(ctx, s->token.u.ident.atom);
- if (next_token(s))
- goto fail1;
- idx = js_parse_from_clause(s, m);
- if (idx < 0)
- goto fail1;
- me = add_export_entry(s, m, JS_ATOM__star_, export_name,
- JS_EXPORT_TYPE_INDIRECT);
- JS_FreeAtom(ctx, export_name);
- if (!me)
- return -1;
- me->u.req_module_idx = idx;
- } else {
- idx = js_parse_from_clause(s, m);
- if (idx < 0)
- return -1;
- if (add_star_export_entry(ctx, m, idx) < 0)
- return -1;
- }
- break;
- case TOK_DEFAULT:
- if (s->token.val == TOK_CLASS) {
- return js_parse_class(s, FALSE, JS_PARSE_EXPORT_DEFAULT);
- } else if (s->token.val == TOK_FUNCTION ||
- (token_is_pseudo_keyword(s, JS_ATOM_async) &&
- peek_token(s, TRUE) == TOK_FUNCTION)) {
- return js_parse_function_decl2(s, JS_PARSE_FUNC_STATEMENT,
- JS_FUNC_NORMAL, JS_ATOM_NULL,
- s->token.ptr,
- JS_PARSE_EXPORT_DEFAULT, NULL);
- } else {
- if (js_parse_assign_expr(s))
- return -1;
- }
- /* set the name of anonymous functions */
- set_object_name(s, JS_ATOM_default);
- /* store the value in the _default_ global variable and export
- it */
- local_name = JS_ATOM__default_;
- if (define_var(s, s->cur_func, local_name, JS_VAR_DEF_LET) < 0)
- return -1;
- emit_op(s, OP_scope_put_var_init);
- emit_atom(s, local_name);
- emit_u16(s, 0);
- if (!add_export_entry(s, m, local_name, JS_ATOM_default,
- JS_EXPORT_TYPE_LOCAL))
- return -1;
- break;
- case TOK_VAR:
- case TOK_LET:
- case TOK_CONST:
- return js_parse_var(s, TRUE, tok, TRUE);
- default:
- return js_parse_error(s, "invalid export syntax");
- }
- return js_parse_expect_semi(s);
- }
- static int add_closure_var(JSContext *ctx, JSFunctionDef *s,
- JSClosureTypeEnum closure_type,
- int var_idx, JSAtom var_name,
- BOOL is_const, BOOL is_lexical,
- JSVarKindEnum var_kind);
- static int add_import(JSParseState *s, JSModuleDef *m,
- JSAtom local_name, JSAtom import_name, BOOL is_star)
- {
- JSContext *ctx = s->ctx;
- int i, var_idx;
- JSImportEntry *mi;
- if (local_name == JS_ATOM_arguments || local_name == JS_ATOM_eval)
- return js_parse_error(s, "invalid import binding");
- if (local_name != JS_ATOM_default) {
- for (i = 0; i < s->cur_func->closure_var_count; i++) {
- if (s->cur_func->closure_var[i].var_name == local_name)
- return js_parse_error(s, "duplicate import binding");
- }
- }
- var_idx = add_closure_var(ctx, s->cur_func,
- is_star ? JS_CLOSURE_MODULE_DECL : JS_CLOSURE_MODULE_IMPORT,
- m->import_entries_count,
- local_name, TRUE, TRUE, JS_VAR_NORMAL);
- if (var_idx < 0)
- return -1;
- if (js_resize_array(ctx, (void **)&m->import_entries,
- sizeof(JSImportEntry),
- &m->import_entries_size,
- m->import_entries_count + 1))
- return -1;
- mi = &m->import_entries[m->import_entries_count++];
- mi->import_name = JS_DupAtom(ctx, import_name);
- mi->var_idx = var_idx;
- mi->is_star = is_star;
- return 0;
- }
- static __exception int js_parse_import(JSParseState *s)
- {
- JSContext *ctx = s->ctx;
- JSModuleDef *m = s->cur_func->module;
- JSAtom local_name, import_name, module_name;
- int first_import, i, idx;
- if (next_token(s))
- return -1;
- first_import = m->import_entries_count;
- if (s->token.val == TOK_STRING) {
- module_name = JS_ValueToAtom(ctx, s->token.u.str.str);
- if (module_name == JS_ATOM_NULL)
- return -1;
- if (next_token(s)) {
- JS_FreeAtom(ctx, module_name);
- return -1;
- }
- idx = add_req_module_entry(ctx, m, module_name);
- JS_FreeAtom(ctx, module_name);
- if (idx < 0)
- return -1;
- if (s->token.val == TOK_WITH) {
- if (js_parse_with_clause(s, &m->req_module_entries[idx]))
- return -1;
- }
- } else {
- if (s->token.val == TOK_IDENT) {
- if (s->token.u.ident.is_reserved) {
- return js_parse_error_reserved_identifier(s);
- }
- /* "default" import */
- local_name = JS_DupAtom(ctx, s->token.u.ident.atom);
- import_name = JS_ATOM_default;
- if (next_token(s))
- goto fail;
- if (add_import(s, m, local_name, import_name, FALSE))
- goto fail;
- JS_FreeAtom(ctx, local_name);
- if (s->token.val != ',')
- goto end_import_clause;
- if (next_token(s))
- return -1;
- }
- if (s->token.val == '*') {
- /* name space import */
- if (next_token(s))
- return -1;
- if (!token_is_pseudo_keyword(s, JS_ATOM_as))
- return js_parse_error(s, "expecting 'as'");
- if (next_token(s))
- return -1;
- if (!token_is_ident(s->token.val)) {
- js_parse_error(s, "identifier expected");
- return -1;
- }
- local_name = JS_DupAtom(ctx, s->token.u.ident.atom);
- import_name = JS_ATOM__star_;
- if (next_token(s))
- goto fail;
- if (add_import(s, m, local_name, import_name, TRUE))
- goto fail;
- JS_FreeAtom(ctx, local_name);
- } else if (s->token.val == '{') {
- if (next_token(s))
- return -1;
- while (s->token.val != '}') {
- BOOL is_string;
- if (s->token.val == TOK_STRING) {
- is_string = TRUE;
- if (js_string_find_invalid_codepoint(JS_VALUE_GET_STRING(s->token.u.str.str)) >= 0) {
- js_parse_error(s, "contains unpaired surrogate");
- return -1;
- }
- import_name = JS_ValueToAtom(s->ctx, s->token.u.str.str);
- if (import_name == JS_ATOM_NULL)
- return -1;
- } else {
- is_string = FALSE;
- if (!token_is_ident(s->token.val)) {
- js_parse_error(s, "identifier expected");
- return -1;
- }
- import_name = JS_DupAtom(ctx, s->token.u.ident.atom);
- }
- local_name = JS_ATOM_NULL;
- if (next_token(s))
- goto fail;
- if (token_is_pseudo_keyword(s, JS_ATOM_as)) {
- if (next_token(s))
- goto fail;
- if (!token_is_ident(s->token.val)) {
- js_parse_error(s, "identifier expected");
- goto fail;
- }
- local_name = JS_DupAtom(ctx, s->token.u.ident.atom);
- if (next_token(s))
- goto fail;
- } else {
- if (is_string) {
- js_parse_error(s, "expecting 'as'");
- fail:
- JS_FreeAtom(ctx, local_name);
- JS_FreeAtom(ctx, import_name);
- return -1;
- }
- local_name = JS_DupAtom(ctx, import_name);
- }
- if (add_import(s, m, local_name, import_name, FALSE))
- goto fail;
- JS_FreeAtom(ctx, local_name);
- JS_FreeAtom(ctx, import_name);
- if (s->token.val != ',')
- break;
- if (next_token(s))
- return -1;
- }
- if (js_parse_expect(s, '}'))
- return -1;
- }
- end_import_clause:
- idx = js_parse_from_clause(s, m);
- if (idx < 0)
- return -1;
- }
- for(i = first_import; i < m->import_entries_count; i++)
- m->import_entries[i].req_module_idx = idx;
- return js_parse_expect_semi(s);
- }
- static __exception int js_parse_source_element(JSParseState *s)
- {
- JSFunctionDef *fd = s->cur_func;
- int tok;
- if (s->token.val == TOK_FUNCTION ||
- (token_is_pseudo_keyword(s, JS_ATOM_async) &&
- peek_token(s, TRUE) == TOK_FUNCTION)) {
- if (js_parse_function_decl(s, JS_PARSE_FUNC_STATEMENT,
- JS_FUNC_NORMAL, JS_ATOM_NULL,
- s->token.ptr))
- return -1;
- } else if (s->token.val == TOK_EXPORT && fd->module) {
- if (js_parse_export(s))
- return -1;
- } else if (s->token.val == TOK_IMPORT && fd->module &&
- ((tok = peek_token(s, FALSE)) != '(' && tok != '.')) {
- /* the peek_token is needed to avoid confusion with ImportCall
- (dynamic import) or import.meta */
- if (js_parse_import(s))
- return -1;
- } else {
- if (js_parse_statement_or_decl(s, DECL_MASK_ALL))
- return -1;
- }
- return 0;
- }
- static JSFunctionDef *js_new_function_def(JSContext *ctx,
- JSFunctionDef *parent,
- BOOL is_eval,
- BOOL is_func_expr,
- const char *filename,
- const uint8_t *source_ptr,
- GetLineColCache *get_line_col_cache)
- {
- JSFunctionDef *fd;
- fd = js_mallocz(ctx, sizeof(*fd));
- if (!fd)
- return NULL;
- fd->ctx = ctx;
- init_list_head(&fd->child_list);
- /* insert in parent list */
- fd->parent = parent;
- fd->parent_cpool_idx = -1;
- if (parent) {
- list_add_tail(&fd->link, &parent->child_list);
- fd->js_mode = parent->js_mode;
- fd->parent_scope_level = parent->scope_level;
- }
- fd->strip_debug = ((ctx->rt->strip_flags & JS_STRIP_DEBUG) != 0);
- fd->strip_source = ((ctx->rt->strip_flags & (JS_STRIP_DEBUG | JS_STRIP_SOURCE)) != 0);
- fd->is_eval = is_eval;
- fd->is_func_expr = is_func_expr;
- js_dbuf_bytecode_init(ctx, &fd->byte_code);
- fd->last_opcode_pos = -1;
- fd->func_name = JS_ATOM_NULL;
- fd->var_object_idx = -1;
- fd->arg_var_object_idx = -1;
- fd->arguments_var_idx = -1;
- fd->arguments_arg_idx = -1;
- fd->func_var_idx = -1;
- fd->eval_ret_idx = -1;
- fd->this_var_idx = -1;
- fd->new_target_var_idx = -1;
- fd->this_active_func_var_idx = -1;
- fd->home_object_var_idx = -1;
- /* XXX: should distinguish arg, var and var object and body scopes */
- fd->scopes = fd->def_scope_array;
- fd->scope_size = countof(fd->def_scope_array);
- fd->scope_count = 1;
- fd->scopes[0].first = -1;
- fd->scopes[0].parent = -1;
- fd->scope_level = 0; /* 0: var/arg scope */
- fd->scope_first = -1;
- fd->body_scope = -1;
- fd->filename = JS_NewAtom(ctx, filename);
- fd->source_pos = source_ptr - get_line_col_cache->buf_start;
- fd->get_line_col_cache = get_line_col_cache;
-
- js_dbuf_init(ctx, &fd->pc2line);
- //fd->pc2line_last_line_num = line_num;
- //fd->pc2line_last_pc = 0;
- fd->last_opcode_source_ptr = source_ptr;
- return fd;
- }
- static void free_bytecode_atoms(JSRuntime *rt,
- const uint8_t *bc_buf, int bc_len,
- BOOL use_short_opcodes)
- {
- int pos, len, op;
- JSAtom atom;
- const JSOpCode *oi;
- pos = 0;
- while (pos < bc_len) {
- op = bc_buf[pos];
- if (use_short_opcodes)
- oi = &short_opcode_info(op);
- else
- oi = &opcode_info[op];
- len = oi->size;
- switch(oi->fmt) {
- case OP_FMT_atom:
- case OP_FMT_atom_u8:
- case OP_FMT_atom_u16:
- case OP_FMT_atom_label_u8:
- case OP_FMT_atom_label_u16:
- if ((pos + 1 + 4) > bc_len)
- break; /* may happen if there is not enough memory when emiting bytecode */
- atom = get_u32(bc_buf + pos + 1);
- JS_FreeAtomRT(rt, atom);
- break;
- default:
- break;
- }
- pos += len;
- }
- }
- static void js_free_function_def(JSContext *ctx, JSFunctionDef *fd)
- {
- int i;
- struct list_head *el, *el1;
- /* free the child functions */
- list_for_each_safe(el, el1, &fd->child_list) {
- JSFunctionDef *fd1;
- fd1 = list_entry(el, JSFunctionDef, link);
- js_free_function_def(ctx, fd1);
- }
- free_bytecode_atoms(ctx->rt, fd->byte_code.buf, fd->byte_code.size,
- fd->use_short_opcodes);
- dbuf_free(&fd->byte_code);
- js_free(ctx, fd->jump_slots);
- js_free(ctx, fd->label_slots);
- js_free(ctx, fd->line_number_slots);
- for(i = 0; i < fd->cpool_count; i++) {
- JS_FreeValue(ctx, fd->cpool[i]);
- }
- js_free(ctx, fd->cpool);
- JS_FreeAtom(ctx, fd->func_name);
- for(i = 0; i < fd->var_count; i++) {
- JS_FreeAtom(ctx, fd->vars[i].var_name);
- }
- js_free(ctx, fd->vars);
- for(i = 0; i < fd->arg_count; i++) {
- JS_FreeAtom(ctx, fd->args[i].var_name);
- }
- js_free(ctx, fd->args);
- for(i = 0; i < fd->global_var_count; i++) {
- JS_FreeAtom(ctx, fd->global_vars[i].var_name);
- }
- js_free(ctx, fd->global_vars);
- for(i = 0; i < fd->closure_var_count; i++) {
- JSClosureVar *cv = &fd->closure_var[i];
- JS_FreeAtom(ctx, cv->var_name);
- }
- js_free(ctx, fd->closure_var);
- if (fd->scopes != fd->def_scope_array)
- js_free(ctx, fd->scopes);
- JS_FreeAtom(ctx, fd->filename);
- dbuf_free(&fd->pc2line);
- js_free(ctx, fd->source);
- if (fd->parent) {
- /* remove in parent list */
- list_del(&fd->link);
- }
- js_free(ctx, fd);
- }
- #ifdef DUMP_BYTECODE
- static const char *skip_lines(const char *p, int n) {
- while (n-- > 0 && *p) {
- while (*p && *p++ != '\n')
- continue;
- }
- return p;
- }
- static void print_lines(const char *source, int line, int line1) {
- const char *s = source;
- const char *p = skip_lines(s, line);
- if (*p) {
- while (line++ < line1) {
- p = skip_lines(s = p, 1);
- printf(";; %.*s", (int)(p - s), s);
- if (!*p) {
- if (p[-1] != '\n')
- printf("\n");
- break;
- }
- }
- }
- }
- static void dump_byte_code(JSContext *ctx, int pass,
- const uint8_t *tab, int len,
- const JSBytecodeVarDef *vardefs,
- const JSVarDef *args, int arg_count,
- const JSVarDef *vars, int var_count,
- const JSClosureVar *closure_var, int closure_var_count,
- const JSValue *cpool, uint32_t cpool_count,
- const char *source,
- const LabelSlot *label_slots, JSFunctionBytecode *b)
- {
- const JSOpCode *oi;
- int pos, pos_next, op, size, idx, addr, line, line1, in_source, line_num;
- uint8_t *bits = js_mallocz(ctx, len * sizeof(*bits));
- BOOL use_short_opcodes = (b != NULL), dump_pc;
- if (b) {
- int col_num;
- line_num = find_line_num(ctx, b, -1, &col_num);
- }
-
- /* scan for jump targets */
- for (pos = 0; pos < len; pos = pos_next) {
- op = tab[pos];
- if (use_short_opcodes)
- oi = &short_opcode_info(op);
- else
- oi = &opcode_info[op];
- pos_next = pos + oi->size;
- if (op < OP_COUNT) {
- switch (oi->fmt) {
- #if SHORT_OPCODES
- case OP_FMT_label8:
- pos++;
- addr = (int8_t)tab[pos];
- goto has_addr;
- case OP_FMT_label16:
- pos++;
- addr = (int16_t)get_u16(tab + pos);
- goto has_addr;
- #endif
- case OP_FMT_atom_label_u8:
- case OP_FMT_atom_label_u16:
- pos += 4;
- /* fall thru */
- case OP_FMT_label:
- case OP_FMT_label_u16:
- pos++;
- addr = get_u32(tab + pos);
- goto has_addr;
- has_addr:
- if (pass == 1)
- addr = label_slots[addr].pos;
- if (pass == 2)
- addr = label_slots[addr].pos2;
- if (pass == 3)
- addr += pos;
- if (addr >= 0 && addr < len)
- bits[addr] |= 1;
- break;
- }
- }
- }
- in_source = 0;
- if (source) {
- /* Always print first line: needed if single line */
- print_lines(source, 0, 1);
- in_source = 1;
- }
- line1 = line = 1;
- pos = 0;
- while (pos < len) {
- op = tab[pos];
- if (source && b) {
- int col_num;
- if (b) {
- line1 = find_line_num(ctx, b, pos, &col_num) - line_num + 1;
- } else if (op == OP_line_num) {
- /* XXX: no longer works */
- line1 = get_u32(tab + pos + 1) - line_num + 1;
- }
- if (line1 > line) {
- if (!in_source)
- printf("\n");
- in_source = 1;
- print_lines(source, line, line1);
- line = line1;
- //bits[pos] |= 2;
- }
- }
- if (in_source)
- printf("\n");
- in_source = 0;
- if (op >= OP_COUNT) {
- printf("invalid opcode (0x%02x)\n", op);
- pos++;
- continue;
- }
- if (use_short_opcodes)
- oi = &short_opcode_info(op);
- else
- oi = &opcode_info[op];
- size = oi->size;
- if (pos + size > len) {
- printf("truncated opcode (0x%02x)\n", op);
- break;
- }
- #if defined(DUMP_BYTECODE) && (DUMP_BYTECODE & 16)
- {
- int i, x, x0;
- x = x0 = printf("%5d ", pos);
- for (i = 0; i < size; i++) {
- if (i == 6) {
- printf("\n%*s", x = x0, "");
- }
- x += printf(" %02X", tab[pos + i]);
- }
- printf("%*s", x0 + 20 - x, "");
- }
- #endif
- #if defined(DUMP_BYTECODE) && (DUMP_BYTECODE & 32)
- dump_pc = TRUE;
- #else
- dump_pc = bits[pos];
- #endif
- if (dump_pc) {
- printf("%5d: ", pos);
- } else {
- printf(" ");
- }
- printf("%s", oi->name);
- pos++;
- switch(oi->fmt) {
- case OP_FMT_none_int:
- printf(" %d", op - OP_push_0);
- break;
- case OP_FMT_npopx:
- printf(" %d", op - OP_call0);
- break;
- case OP_FMT_u8:
- printf(" %u", get_u8(tab + pos));
- break;
- case OP_FMT_i8:
- printf(" %d", get_i8(tab + pos));
- break;
- case OP_FMT_u16:
- case OP_FMT_npop:
- printf(" %u", get_u16(tab + pos));
- break;
- case OP_FMT_npop_u16:
- printf(" %u,%u", get_u16(tab + pos), get_u16(tab + pos + 2));
- break;
- case OP_FMT_i16:
- printf(" %d", get_i16(tab + pos));
- break;
- case OP_FMT_i32:
- printf(" %d", get_i32(tab + pos));
- break;
- case OP_FMT_u32:
- printf(" %u", get_u32(tab + pos));
- break;
- #if SHORT_OPCODES
- case OP_FMT_label8:
- addr = get_i8(tab + pos);
- goto has_addr1;
- case OP_FMT_label16:
- addr = get_i16(tab + pos);
- goto has_addr1;
- #endif
- case OP_FMT_label:
- addr = get_u32(tab + pos);
- goto has_addr1;
- has_addr1:
- if (pass == 1)
- printf(" %u:%u", addr, label_slots[addr].pos);
- if (pass == 2)
- printf(" %u:%u", addr, label_slots[addr].pos2);
- if (pass == 3)
- printf(" %u", addr + pos);
- break;
- case OP_FMT_label_u16:
- addr = get_u32(tab + pos);
- if (pass == 1)
- printf(" %u:%u", addr, label_slots[addr].pos);
- if (pass == 2)
- printf(" %u:%u", addr, label_slots[addr].pos2);
- if (pass == 3)
- printf(" %u", addr + pos);
- printf(",%u", get_u16(tab + pos + 4));
- break;
- #if SHORT_OPCODES
- case OP_FMT_const8:
- idx = get_u8(tab + pos);
- goto has_pool_idx;
- #endif
- case OP_FMT_const:
- idx = get_u32(tab + pos);
- goto has_pool_idx;
- has_pool_idx:
- printf(" %u: ", idx);
- if (idx < cpool_count) {
- JS_PrintValue(ctx, js_dump_value_write, stdout, cpool[idx], NULL);
- }
- break;
- case OP_FMT_atom:
- printf(" ");
- print_atom(ctx, get_u32(tab + pos));
- break;
- case OP_FMT_atom_u8:
- printf(" ");
- print_atom(ctx, get_u32(tab + pos));
- printf(",%d", get_u8(tab + pos + 4));
- break;
- case OP_FMT_atom_u16:
- printf(" ");
- print_atom(ctx, get_u32(tab + pos));
- printf(",%d", get_u16(tab + pos + 4));
- break;
- case OP_FMT_atom_label_u8:
- case OP_FMT_atom_label_u16:
- printf(" ");
- print_atom(ctx, get_u32(tab + pos));
- addr = get_u32(tab + pos + 4);
- if (pass == 1)
- printf(",%u:%u", addr, label_slots[addr].pos);
- if (pass == 2)
- printf(",%u:%u", addr, label_slots[addr].pos2);
- if (pass == 3)
- printf(",%u", addr + pos + 4);
- if (oi->fmt == OP_FMT_atom_label_u8)
- printf(",%u", get_u8(tab + pos + 8));
- else
- printf(",%u", get_u16(tab + pos + 8));
- break;
- case OP_FMT_none_loc:
- idx = (op - OP_get_loc0) % 4;
- goto has_loc;
- case OP_FMT_loc8:
- idx = get_u8(tab + pos);
- goto has_loc;
- case OP_FMT_loc:
- idx = get_u16(tab + pos);
- has_loc:
- printf(" %d: ", idx);
- if (idx < var_count) {
- print_atom(ctx, vars ? vars[idx].var_name : vardefs[arg_count + idx].var_name);
- }
- break;
- case OP_FMT_none_arg:
- idx = (op - OP_get_arg0) % 4;
- goto has_arg;
- case OP_FMT_arg:
- idx = get_u16(tab + pos);
- has_arg:
- printf(" %d: ", idx);
- if (idx < arg_count) {
- print_atom(ctx, args ? args[idx].var_name : vardefs[idx].var_name);
- }
- break;
- case OP_FMT_none_var_ref:
- idx = (op - OP_get_var_ref0) % 4;
- goto has_var_ref;
- case OP_FMT_var_ref:
- idx = get_u16(tab + pos);
- has_var_ref:
- printf(" %d: ", idx);
- if (idx < closure_var_count) {
- print_atom(ctx, closure_var[idx].var_name);
- }
- break;
- default:
- break;
- }
- printf("\n");
- pos += oi->size - 1;
- }
- if (source) {
- if (!in_source)
- printf("\n");
- print_lines(source, line, INT32_MAX);
- }
- js_free(ctx, bits);
- }
- static __maybe_unused void dump_pc2line(JSContext *ctx, const uint8_t *buf, int len)
- {
- const uint8_t *p_end, *p;
- int pc, v, line_num, col_num, ret;
- unsigned int op;
- uint32_t val;
-
- if (len <= 0)
- return;
- printf("%5s %5s %5s\n", "PC", "LINE", "COL");
- p = buf;
- p_end = buf + len;
-
- /* get the function line and column numbers */
- ret = get_leb128(&val, p, p_end);
- if (ret < 0)
- goto fail;
- p += ret;
- line_num = val + 1;
- ret = get_leb128(&val, p, p_end);
- if (ret < 0)
- goto fail;
- p += ret;
- col_num = val + 1;
- printf("%5s %5d %5d\n", "-", line_num, col_num);
-
- pc = 0;
- while (p < p_end) {
- op = *p++;
- if (op == 0) {
- ret = get_leb128(&val, p, p_end);
- if (ret < 0)
- goto fail;
- pc += val;
- p += ret;
- ret = get_sleb128(&v, p, p_end);
- if (ret < 0)
- goto fail;
- p += ret;
- line_num += v;
- } else {
- op -= PC2LINE_OP_FIRST;
- pc += (op / PC2LINE_RANGE);
- line_num += (op % PC2LINE_RANGE) + PC2LINE_BASE;
- }
- ret = get_sleb128(&v, p, p_end);
- if (ret < 0)
- goto fail;
- p += ret;
- col_num += v;
-
- printf("%5d %5d %5d\n", pc, line_num, col_num);
- }
- fail: ;
- }
- static __maybe_unused void js_dump_function_bytecode(JSContext *ctx, JSFunctionBytecode *b)
- {
- int i;
- char atom_buf[ATOM_GET_STR_BUF_SIZE];
- const char *str;
- if (b->has_debug && b->debug.filename != JS_ATOM_NULL) {
- int line_num, col_num;
- str = JS_AtomGetStr(ctx, atom_buf, sizeof(atom_buf), b->debug.filename);
- line_num = find_line_num(ctx, b, -1, &col_num);
- printf("%s:%d:%d: ", str, line_num, col_num);
- }
- str = JS_AtomGetStr(ctx, atom_buf, sizeof(atom_buf), b->func_name);
- printf("function: %s%s\n", &"*"[b->func_kind != JS_FUNC_GENERATOR], str);
- if (b->js_mode) {
- printf(" mode:");
- if (b->js_mode & JS_MODE_STRICT)
- printf(" strict");
- printf("\n");
- }
- if (b->arg_count && b->vardefs) {
- printf(" args:");
- for(i = 0; i < b->arg_count; i++) {
- printf(" %s", JS_AtomGetStr(ctx, atom_buf, sizeof(atom_buf),
- b->vardefs[i].var_name));
- }
- printf("\n");
- }
- if (b->var_count && b->vardefs) {
- printf(" locals:\n");
- for(i = 0; i < b->var_count; i++) {
- JSBytecodeVarDef *vd = &b->vardefs[b->arg_count + i];
- printf("%5d: %s %s", i,
- vd->var_kind == JS_VAR_CATCH ? "catch" :
- (vd->var_kind == JS_VAR_FUNCTION_DECL ||
- vd->var_kind == JS_VAR_NEW_FUNCTION_DECL) ? "function" :
- vd->is_const ? "const" :
- vd->is_lexical ? "let" : "var",
- JS_AtomGetStr(ctx, atom_buf, sizeof(atom_buf), vd->var_name));
- if (vd->has_scope)
- printf(" [next:%d]", vd->scope_next);
- printf("\n");
- }
- }
- if (b->closure_var_count) {
- printf(" closure vars:\n");
- for(i = 0; i < b->closure_var_count; i++) {
- JSClosureVar *cv = &b->closure_var[i];
- printf("%5d: %s %s", i,
- cv->is_const ? "const" :
- cv->is_lexical ? "let" : "var",
- JS_AtomGetStr(ctx, atom_buf, sizeof(atom_buf), cv->var_name));
- switch(cv->closure_type) {
- case JS_CLOSURE_LOCAL:
- printf(" [loc%d]\n", cv->var_idx);
- break;
- case JS_CLOSURE_ARG:
- printf(" [arg%d]\n", cv->var_idx);
- break;
- case JS_CLOSURE_REF:
- printf(" [ref%d]\n", cv->var_idx);
- break;
- case JS_CLOSURE_GLOBAL_REF:
- printf(" [global_ref%d]\n", cv->var_idx);
- break;
- case JS_CLOSURE_GLOBAL_DECL:
- printf(" [global_decl]\n");
- break;
- case JS_CLOSURE_GLOBAL:
- printf(" [global]\n");
- break;
- case JS_CLOSURE_MODULE_DECL:
- printf(" [module_decl]\n");
- break;
- case JS_CLOSURE_MODULE_IMPORT:
- printf(" [module_import]\n");
- break;
- default:
- printf(" [?]\n");
- break;
- }
- }
- }
- printf(" stack_size: %d\n", b->stack_size);
- printf(" var_ref_count: %d\n", b->var_ref_count);
- printf(" opcodes:\n");
- dump_byte_code(ctx, 3, b->byte_code_buf, b->byte_code_len,
- b->vardefs,
- NULL, b->arg_count,
- NULL, b->var_count,
- b->closure_var, b->closure_var_count,
- b->cpool, b->cpool_count,
- b->has_debug ? b->debug.source : NULL,
- NULL, b);
- #if defined(DUMP_BYTECODE) && (DUMP_BYTECODE & 32)
- if (b->has_debug)
- dump_pc2line(ctx, b->debug.pc2line_buf, b->debug.pc2line_len);
- #endif
- printf("\n");
- }
- #endif
- static int add_closure_var(JSContext *ctx, JSFunctionDef *s,
- JSClosureTypeEnum closure_type,
- int var_idx, JSAtom var_name,
- BOOL is_const, BOOL is_lexical,
- JSVarKindEnum var_kind)
- {
- JSClosureVar *cv;
- /* the closure variable indexes are currently stored on 16 bits */
- if (s->closure_var_count >= JS_MAX_LOCAL_VARS) {
- JS_ThrowInternalError(ctx, "too many closure variables");
- return -1;
- }
- if (js_resize_array(ctx, (void **)&s->closure_var,
- sizeof(s->closure_var[0]),
- &s->closure_var_size, s->closure_var_count + 1))
- return -1;
- cv = &s->closure_var[s->closure_var_count++];
- cv->closure_type = closure_type;
- cv->is_const = is_const;
- cv->is_lexical = is_lexical;
- cv->var_kind = var_kind;
- cv->var_idx = var_idx;
- cv->var_name = JS_DupAtom(ctx, var_name);
- return s->closure_var_count - 1;
- }
- static int find_closure_var(JSContext *ctx, JSFunctionDef *s,
- JSAtom var_name)
- {
- int i;
- for(i = 0; i < s->closure_var_count; i++) {
- JSClosureVar *cv = &s->closure_var[i];
- if (cv->var_name == var_name)
- return i;
- }
- return -1;
- }
- /* 'fd' must be a parent of 's'. Create in 's' a closure referencing
- another one in 'fd' */
- static int get_closure_var(JSContext *ctx, JSFunctionDef *s,
- JSFunctionDef *fd, JSClosureTypeEnum closure_type,
- int var_idx, JSAtom var_name,
- BOOL is_const, BOOL is_lexical,
- JSVarKindEnum var_kind)
- {
- int i;
- if (fd != s->parent) {
- var_idx = get_closure_var(ctx, s->parent, fd, closure_type,
- var_idx, var_name,
- is_const, is_lexical, var_kind);
- if (var_idx < 0)
- return -1;
- if (closure_type != JS_CLOSURE_GLOBAL_REF)
- closure_type = JS_CLOSURE_REF;
- }
- for(i = 0; i < s->closure_var_count; i++) {
- JSClosureVar *cv = &s->closure_var[i];
- if (cv->var_idx == var_idx && cv->closure_type == closure_type)
- return i;
- }
- return add_closure_var(ctx, s, closure_type, var_idx, var_name,
- is_const, is_lexical, var_kind);
- }
- static int get_with_scope_opcode(int op)
- {
- if (op == OP_scope_get_var_undef)
- return OP_with_get_var;
- else
- return OP_with_get_var + (op - OP_scope_get_var);
- }
- static BOOL can_opt_put_ref_value(const uint8_t *bc_buf, int pos)
- {
- int opcode = bc_buf[pos];
- return (bc_buf[pos + 1] == OP_put_ref_value &&
- (opcode == OP_insert3 ||
- opcode == OP_perm4 ||
- opcode == OP_nop ||
- opcode == OP_rot3l));
- }
- static BOOL can_opt_put_global_ref_value(const uint8_t *bc_buf, int pos)
- {
- int opcode = bc_buf[pos];
- return (bc_buf[pos + 1] == OP_put_ref_value &&
- (opcode == OP_insert3 ||
- opcode == OP_perm4 ||
- opcode == OP_nop ||
- opcode == OP_rot3l));
- }
- static int optimize_scope_make_ref(JSContext *ctx, JSFunctionDef *s,
- DynBuf *bc, uint8_t *bc_buf,
- LabelSlot *ls, int pos_next,
- int get_op, int var_idx)
- {
- int label_pos, end_pos, pos;
- /* XXX: should optimize `loc(a) += expr` as `expr add_loc(a)`
- but only if expr does not modify `a`.
- should scan the code between pos_next and label_pos
- for operations that can potentially change `a`:
- OP_scope_make_ref(a), function calls, jumps and gosub.
- */
- /* replace the reference get/put with normal variable
- accesses */
- if (bc_buf[pos_next] == OP_get_ref_value) {
- dbuf_putc(bc, get_op);
- dbuf_put_u16(bc, var_idx);
- pos_next++;
- }
- /* remove the OP_label to make room for replacement */
- /* label should have a refcount of 0 anyway */
- /* XXX: should avoid this patch by inserting nops in phase 1 */
- label_pos = ls->pos;
- pos = label_pos - 5;
- assert(bc_buf[pos] == OP_label);
- /* label points to an instruction pair:
- - insert3 / put_ref_value
- - perm4 / put_ref_value
- - rot3l / put_ref_value
- - nop / put_ref_value
- */
- end_pos = label_pos + 2;
- if (bc_buf[label_pos] == OP_insert3)
- bc_buf[pos++] = OP_dup;
- bc_buf[pos] = get_op + 1;
- put_u16(bc_buf + pos + 1, var_idx);
- pos += 3;
- /* pad with OP_nop */
- while (pos < end_pos)
- bc_buf[pos++] = OP_nop;
- return pos_next;
- }
- static int add_var_this(JSContext *ctx, JSFunctionDef *fd)
- {
- int idx;
- idx = add_var(ctx, fd, JS_ATOM_this);
- if (idx >= 0 && fd->is_derived_class_constructor) {
- JSVarDef *vd = &fd->vars[idx];
- /* XXX: should have is_this flag or var type */
- vd->is_lexical = 1; /* used to trigger 'uninitialized' checks
- in a derived class constructor */
- }
- return idx;
- }
- static int resolve_pseudo_var(JSContext *ctx, JSFunctionDef *s,
- JSAtom var_name)
- {
- int var_idx;
- if (!s->has_this_binding)
- return -1;
- switch(var_name) {
- case JS_ATOM_home_object:
- /* 'home_object' pseudo variable */
- if (s->home_object_var_idx < 0)
- s->home_object_var_idx = add_var(ctx, s, var_name);
- var_idx = s->home_object_var_idx;
- break;
- case JS_ATOM_this_active_func:
- /* 'this.active_func' pseudo variable */
- if (s->this_active_func_var_idx < 0)
- s->this_active_func_var_idx = add_var(ctx, s, var_name);
- var_idx = s->this_active_func_var_idx;
- break;
- case JS_ATOM_new_target:
- /* 'new.target' pseudo variable */
- if (s->new_target_var_idx < 0)
- s->new_target_var_idx = add_var(ctx, s, var_name);
- var_idx = s->new_target_var_idx;
- break;
- case JS_ATOM_this:
- /* 'this' pseudo variable */
- if (s->this_var_idx < 0)
- s->this_var_idx = add_var_this(ctx, s);
- var_idx = s->this_var_idx;
- break;
- default:
- var_idx = -1;
- break;
- }
- return var_idx;
- }
- /* test if 'var_name' is in the variable object on the stack. If is it
- the case, handle it and jump to 'label_done' */
- static void var_object_test(JSContext *ctx, JSFunctionDef *s,
- JSAtom var_name, int op, DynBuf *bc,
- int *plabel_done, BOOL is_with)
- {
- dbuf_putc(bc, get_with_scope_opcode(op));
- dbuf_put_u32(bc, JS_DupAtom(ctx, var_name));
- if (*plabel_done < 0) {
- *plabel_done = new_label_fd(s);
- if (*plabel_done < 0) {
- dbuf_set_error(bc);
- return;
- }
- }
- dbuf_put_u32(bc, *plabel_done);
- dbuf_putc(bc, is_with);
- update_label(s, *plabel_done, 1);
- s->jump_size++;
- }
- static inline void capture_var(JSFunctionDef *s, JSVarDef *vd)
- {
- if (!vd->is_captured) {
- vd->is_captured = 1;
- vd->var_ref_idx = s->var_ref_count++;
- }
- }
- /* return the position of the next opcode or -1 if error */
- static int resolve_scope_var(JSContext *ctx, JSFunctionDef *s,
- JSAtom var_name, int scope_level, int op,
- DynBuf *bc, uint8_t *bc_buf,
- LabelSlot *ls, int pos_next)
- {
- int idx, var_idx, is_put;
- int label_done;
- JSFunctionDef *fd;
- JSVarDef *vd;
- BOOL is_pseudo_var, is_arg_scope;
- label_done = -1;
- /* XXX: could be simpler to use a specific function to
- resolve the pseudo variables */
- is_pseudo_var = (var_name == JS_ATOM_home_object ||
- var_name == JS_ATOM_this_active_func ||
- var_name == JS_ATOM_new_target ||
- var_name == JS_ATOM_this);
- /* resolve local scoped variables */
- var_idx = -1;
- for (idx = s->scopes[scope_level].first; idx >= 0;) {
- vd = &s->vars[idx];
- if (vd->var_name == var_name) {
- if (op == OP_scope_put_var || op == OP_scope_make_ref) {
- if (vd->is_const) {
- dbuf_putc(bc, OP_throw_error);
- dbuf_put_u32(bc, JS_DupAtom(ctx, var_name));
- dbuf_putc(bc, JS_THROW_VAR_RO);
- goto done;
- }
- }
- var_idx = idx;
- break;
- } else
- if (vd->var_name == JS_ATOM__with_ && !is_pseudo_var) {
- dbuf_putc(bc, OP_get_loc);
- dbuf_put_u16(bc, idx);
- var_object_test(ctx, s, var_name, op, bc, &label_done, 1);
- }
- idx = vd->scope_next;
- }
- is_arg_scope = (idx == ARG_SCOPE_END);
- if (var_idx < 0) {
- /* argument scope: variables are not visible but pseudo
- variables are visible */
- if (!is_arg_scope) {
- var_idx = find_var(ctx, s, var_name);
- }
- if (var_idx < 0 && is_pseudo_var)
- var_idx = resolve_pseudo_var(ctx, s, var_name);
- if (var_idx < 0 && var_name == JS_ATOM_arguments &&
- s->has_arguments_binding) {
- /* 'arguments' pseudo variable */
- var_idx = add_arguments_var(ctx, s);
- }
- if (var_idx < 0 && s->is_func_expr && var_name == s->func_name) {
- /* add a new variable with the function name */
- var_idx = add_func_var(ctx, s, var_name);
- }
- }
- if (var_idx >= 0) {
- if ((op == OP_scope_put_var || op == OP_scope_make_ref) &&
- !(var_idx & ARGUMENT_VAR_OFFSET) &&
- s->vars[var_idx].is_const) {
- /* only happens when assigning a function expression name
- in strict mode */
- dbuf_putc(bc, OP_throw_error);
- dbuf_put_u32(bc, JS_DupAtom(ctx, var_name));
- dbuf_putc(bc, JS_THROW_VAR_RO);
- goto done;
- }
- /* OP_scope_put_var_init is only used to initialize a
- lexical variable, so it is never used in a with or var object. It
- can be used with a closure (module global variable case). */
- switch (op) {
- case OP_scope_make_ref:
- if (!(var_idx & ARGUMENT_VAR_OFFSET) &&
- s->vars[var_idx].var_kind == JS_VAR_FUNCTION_NAME) {
- /* Create a dummy object reference for the func_var */
- dbuf_putc(bc, OP_object);
- dbuf_putc(bc, OP_get_loc);
- dbuf_put_u16(bc, var_idx);
- dbuf_putc(bc, OP_define_field);
- dbuf_put_u32(bc, JS_DupAtom(ctx, var_name));
- dbuf_putc(bc, OP_push_atom_value);
- dbuf_put_u32(bc, JS_DupAtom(ctx, var_name));
- } else
- if (label_done == -1 && can_opt_put_ref_value(bc_buf, ls->pos)) {
- int get_op;
- if (var_idx & ARGUMENT_VAR_OFFSET) {
- get_op = OP_get_arg;
- var_idx -= ARGUMENT_VAR_OFFSET;
- } else {
- if (s->vars[var_idx].is_lexical)
- get_op = OP_get_loc_check;
- else
- get_op = OP_get_loc;
- }
- pos_next = optimize_scope_make_ref(ctx, s, bc, bc_buf, ls,
- pos_next, get_op, var_idx);
- } else {
- /* Create a dummy object with a named slot that is
- a reference to the local variable */
- if (var_idx & ARGUMENT_VAR_OFFSET) {
- capture_var(s, &s->args[var_idx - ARGUMENT_VAR_OFFSET]);
- dbuf_putc(bc, OP_make_arg_ref);
- dbuf_put_u32(bc, JS_DupAtom(ctx, var_name));
- dbuf_put_u16(bc, var_idx - ARGUMENT_VAR_OFFSET);
- } else {
- capture_var(s, &s->vars[var_idx]);
- dbuf_putc(bc, OP_make_loc_ref);
- dbuf_put_u32(bc, JS_DupAtom(ctx, var_name));
- dbuf_put_u16(bc, var_idx);
- }
- }
- break;
- case OP_scope_put_var:
- if (!(var_idx & ARGUMENT_VAR_OFFSET) &&
- s->vars[var_idx].var_kind == JS_VAR_FUNCTION_NAME) {
- /* in non strict mode, modifying the function name is ignored */
- dbuf_putc(bc, OP_drop);
- goto done;
- }
- goto local_scope_var;
- case OP_scope_get_ref:
- dbuf_putc(bc, OP_undefined);
- goto local_scope_var;
- case OP_scope_get_var_checkthis:
- case OP_scope_get_var_undef:
- case OP_scope_get_var:
- case OP_scope_put_var_init:
- local_scope_var:
- is_put = (op == OP_scope_put_var || op == OP_scope_put_var_init);
- if (var_idx & ARGUMENT_VAR_OFFSET) {
- dbuf_putc(bc, OP_get_arg + is_put);
- dbuf_put_u16(bc, var_idx - ARGUMENT_VAR_OFFSET);
- } else {
- if (is_put) {
- if (s->vars[var_idx].is_lexical) {
- if (op == OP_scope_put_var_init) {
- /* 'this' can only be initialized once */
- if (var_name == JS_ATOM_this)
- dbuf_putc(bc, OP_put_loc_check_init);
- else
- dbuf_putc(bc, OP_put_loc);
- } else {
- dbuf_putc(bc, OP_put_loc_check);
- }
- } else {
- dbuf_putc(bc, OP_put_loc);
- }
- } else {
- if (s->vars[var_idx].is_lexical) {
- if (op == OP_scope_get_var_checkthis) {
- /* only used for 'this' return in derived class constructors */
- dbuf_putc(bc, OP_get_loc_checkthis);
- } else {
- dbuf_putc(bc, OP_get_loc_check);
- }
- } else {
- dbuf_putc(bc, OP_get_loc);
- }
- }
- dbuf_put_u16(bc, var_idx);
- }
- break;
- case OP_scope_delete_var:
- dbuf_putc(bc, OP_push_false);
- break;
- }
- goto done;
- }
- /* check eval object */
- if (!is_arg_scope && s->var_object_idx >= 0 && !is_pseudo_var) {
- dbuf_putc(bc, OP_get_loc);
- dbuf_put_u16(bc, s->var_object_idx);
- var_object_test(ctx, s, var_name, op, bc, &label_done, 0);
- }
- /* check eval object in argument scope */
- if (s->arg_var_object_idx >= 0 && !is_pseudo_var) {
- dbuf_putc(bc, OP_get_loc);
- dbuf_put_u16(bc, s->arg_var_object_idx);
- var_object_test(ctx, s, var_name, op, bc, &label_done, 0);
- }
- /* check parent scopes */
- for (fd = s; fd->parent;) {
- scope_level = fd->parent_scope_level;
- fd = fd->parent;
- for (idx = fd->scopes[scope_level].first; idx >= 0;) {
- vd = &fd->vars[idx];
- if (vd->var_name == var_name) {
- if (op == OP_scope_put_var || op == OP_scope_make_ref) {
- if (vd->is_const) {
- dbuf_putc(bc, OP_throw_error);
- dbuf_put_u32(bc, JS_DupAtom(ctx, var_name));
- dbuf_putc(bc, JS_THROW_VAR_RO);
- goto done;
- }
- }
- var_idx = idx;
- break;
- } else if (vd->var_name == JS_ATOM__with_ && !is_pseudo_var) {
- capture_var(fd, vd);
- idx = get_closure_var(ctx, s, fd, JS_CLOSURE_LOCAL, idx, vd->var_name, FALSE, FALSE, JS_VAR_NORMAL);
- if (idx >= 0) {
- dbuf_putc(bc, OP_get_var_ref);
- dbuf_put_u16(bc, idx);
- var_object_test(ctx, s, var_name, op, bc, &label_done, 1);
- }
- }
- idx = vd->scope_next;
- }
- is_arg_scope = (idx == ARG_SCOPE_END);
- if (var_idx >= 0)
- break;
- if (!is_arg_scope) {
- var_idx = find_var(ctx, fd, var_name);
- if (var_idx >= 0)
- break;
- }
- if (is_pseudo_var) {
- var_idx = resolve_pseudo_var(ctx, fd, var_name);
- if (var_idx >= 0)
- break;
- }
- if (var_name == JS_ATOM_arguments && fd->has_arguments_binding) {
- var_idx = add_arguments_var(ctx, fd);
- break;
- }
- if (fd->is_func_expr && fd->func_name == var_name) {
- /* add a new variable with the function name */
- var_idx = add_func_var(ctx, fd, var_name);
- break;
- }
- /* check eval object */
- if (!is_arg_scope && fd->var_object_idx >= 0 && !is_pseudo_var) {
- vd = &fd->vars[fd->var_object_idx];
- capture_var(fd, vd);
- idx = get_closure_var(ctx, s, fd, JS_CLOSURE_LOCAL,
- fd->var_object_idx, vd->var_name,
- FALSE, FALSE, JS_VAR_NORMAL);
- dbuf_putc(bc, OP_get_var_ref);
- dbuf_put_u16(bc, idx);
- var_object_test(ctx, s, var_name, op, bc, &label_done, 0);
- }
- /* check eval object in argument scope */
- if (fd->arg_var_object_idx >= 0 && !is_pseudo_var) {
- vd = &fd->vars[fd->arg_var_object_idx];
- capture_var(fd, vd);
- idx = get_closure_var(ctx, s, fd, JS_CLOSURE_LOCAL,
- fd->arg_var_object_idx, vd->var_name,
- FALSE, FALSE, JS_VAR_NORMAL);
- dbuf_putc(bc, OP_get_var_ref);
- dbuf_put_u16(bc, idx);
- var_object_test(ctx, s, var_name, op, bc, &label_done, 0);
- }
- if (fd->is_eval)
- break; /* it it necessarily the top level function */
- }
- /* check direct eval scope (in the closure of the eval function
- which is necessarily at the top level) */
- if (!fd)
- fd = s;
- if (var_idx < 0 && fd->is_eval) {
- int idx1;
- for (idx1 = 0; idx1 < fd->closure_var_count; idx1++) {
- JSClosureVar *cv = &fd->closure_var[idx1];
- if (var_name == cv->var_name) {
- if (fd != s) {
- JSClosureTypeEnum closure_type;
- if (cv->closure_type == JS_CLOSURE_GLOBAL ||
- cv->closure_type == JS_CLOSURE_GLOBAL_DECL ||
- cv->closure_type == JS_CLOSURE_GLOBAL_REF)
- closure_type = JS_CLOSURE_GLOBAL_REF;
- else
- closure_type = JS_CLOSURE_REF;
- idx = get_closure_var(ctx, s, fd,
- closure_type,
- idx1,
- cv->var_name, cv->is_const,
- cv->is_lexical, cv->var_kind);
- } else {
- idx = idx1;
- }
- if (cv->closure_type == JS_CLOSURE_GLOBAL ||
- cv->closure_type == JS_CLOSURE_GLOBAL_DECL ||
- cv->closure_type == JS_CLOSURE_GLOBAL_REF)
- goto has_global_idx;
- else
- goto has_idx;
- } else if ((cv->var_name == JS_ATOM__var_ ||
- cv->var_name == JS_ATOM__arg_var_ ||
- cv->var_name == JS_ATOM__with_) && !is_pseudo_var) {
- int is_with = (cv->var_name == JS_ATOM__with_);
- if (fd != s) {
- idx = get_closure_var(ctx, s, fd,
- JS_CLOSURE_REF,
- idx1,
- cv->var_name, FALSE, FALSE,
- JS_VAR_NORMAL);
- } else {
- idx = idx1;
- }
- dbuf_putc(bc, OP_get_var_ref);
- dbuf_put_u16(bc, idx);
- var_object_test(ctx, s, var_name, op, bc, &label_done, is_with);
- }
- }
- /* not found: add a closure for a global variable access */
- idx1 = add_closure_var(ctx, fd, JS_CLOSURE_GLOBAL, 0, var_name,
- FALSE, FALSE, JS_VAR_NORMAL);
- if (idx1 < 0)
- return -1;
- if (fd != s) {
- idx = get_closure_var(ctx, s, fd,
- JS_CLOSURE_GLOBAL_REF,
- idx1,
- var_name, FALSE, FALSE,
- JS_VAR_NORMAL);
- } else {
- idx = idx1;
- }
- has_global_idx:
- /* global variable access */
- switch (op) {
- case OP_scope_make_ref:
- if (label_done == -1 && can_opt_put_global_ref_value(bc_buf, ls->pos)) {
- pos_next = optimize_scope_make_ref(ctx, s, bc, bc_buf, ls,
- pos_next,
- OP_get_var, idx);
- } else {
- dbuf_putc(bc, OP_make_var_ref);
- dbuf_put_u32(bc, JS_DupAtom(ctx, var_name));
- }
- break;
- case OP_scope_get_ref:
- /* XXX: should create a dummy object with a named slot that is
- a reference to the global variable */
- dbuf_putc(bc, OP_undefined);
- dbuf_putc(bc, OP_get_var);
- dbuf_put_u16(bc, idx);
- break;
- case OP_scope_get_var_undef:
- case OP_scope_get_var:
- case OP_scope_put_var:
- dbuf_putc(bc, OP_get_var_undef + (op - OP_scope_get_var_undef));
- dbuf_put_u16(bc, idx);
- break;
- case OP_scope_put_var_init:
- dbuf_putc(bc, OP_put_var_init);
- dbuf_put_u16(bc, idx);
- break;
- case OP_scope_delete_var:
- dbuf_putc(bc, OP_delete_var);
- dbuf_put_u32(bc, JS_DupAtom(ctx, var_name));
- break;
- }
- } else {
- /* find the corresponding closure variable */
- if (var_idx & ARGUMENT_VAR_OFFSET) {
- capture_var(fd, &fd->args[var_idx - ARGUMENT_VAR_OFFSET]);
- idx = get_closure_var(ctx, s, fd,
- JS_CLOSURE_ARG, var_idx - ARGUMENT_VAR_OFFSET,
- var_name, FALSE, FALSE, JS_VAR_NORMAL);
- } else {
- capture_var(fd, &fd->vars[var_idx]);
- idx = get_closure_var(ctx, s, fd,
- JS_CLOSURE_LOCAL, var_idx,
- var_name,
- fd->vars[var_idx].is_const,
- fd->vars[var_idx].is_lexical,
- fd->vars[var_idx].var_kind);
- }
- if (idx >= 0) {
- has_idx:
- if ((op == OP_scope_put_var || op == OP_scope_make_ref) &&
- s->closure_var[idx].is_const) {
- dbuf_putc(bc, OP_throw_error);
- dbuf_put_u32(bc, JS_DupAtom(ctx, var_name));
- dbuf_putc(bc, JS_THROW_VAR_RO);
- goto done;
- }
- switch (op) {
- case OP_scope_make_ref:
- if (s->closure_var[idx].var_kind == JS_VAR_FUNCTION_NAME) {
- /* Create a dummy object reference for the func_var */
- dbuf_putc(bc, OP_object);
- dbuf_putc(bc, OP_get_var_ref);
- dbuf_put_u16(bc, idx);
- dbuf_putc(bc, OP_define_field);
- dbuf_put_u32(bc, JS_DupAtom(ctx, var_name));
- dbuf_putc(bc, OP_push_atom_value);
- dbuf_put_u32(bc, JS_DupAtom(ctx, var_name));
- } else
- if (label_done == -1 &&
- can_opt_put_ref_value(bc_buf, ls->pos)) {
- int get_op;
- if (s->closure_var[idx].is_lexical)
- get_op = OP_get_var_ref_check;
- else
- get_op = OP_get_var_ref;
- pos_next = optimize_scope_make_ref(ctx, s, bc, bc_buf, ls,
- pos_next,
- get_op, idx);
- } else {
- /* Create a dummy object with a named slot that is
- a reference to the closure variable */
- dbuf_putc(bc, OP_make_var_ref_ref);
- dbuf_put_u32(bc, JS_DupAtom(ctx, var_name));
- dbuf_put_u16(bc, idx);
- }
- break;
- case OP_scope_put_var:
- if (s->closure_var[idx].var_kind == JS_VAR_FUNCTION_NAME) {
- /* in non strict mode, modifying the function name is ignored */
- dbuf_putc(bc, OP_drop);
- goto done;
- }
- goto closure_scope_var;
- case OP_scope_get_ref:
- /* XXX: should create a dummy object with a named slot that is
- a reference to the closure variable */
- dbuf_putc(bc, OP_undefined);
- goto closure_scope_var;
- case OP_scope_get_var_undef:
- case OP_scope_get_var:
- case OP_scope_put_var_init:
- closure_scope_var:
- is_put = (op == OP_scope_put_var ||
- op == OP_scope_put_var_init);
- if (is_put) {
- if (s->closure_var[idx].is_lexical) {
- if (op == OP_scope_put_var_init) {
- /* 'this' can only be initialized once */
- if (var_name == JS_ATOM_this)
- dbuf_putc(bc, OP_put_var_ref_check_init);
- else
- dbuf_putc(bc, OP_put_var_ref);
- } else {
- dbuf_putc(bc, OP_put_var_ref_check);
- }
- } else {
- dbuf_putc(bc, OP_put_var_ref);
- }
- } else {
- if (s->closure_var[idx].is_lexical) {
- dbuf_putc(bc, OP_get_var_ref_check);
- } else {
- dbuf_putc(bc, OP_get_var_ref);
- }
- }
- dbuf_put_u16(bc, idx);
- break;
- case OP_scope_delete_var:
- dbuf_putc(bc, OP_push_false);
- break;
- }
- goto done;
- }
- }
- done:
- if (label_done >= 0) {
- dbuf_putc(bc, OP_label);
- dbuf_put_u32(bc, label_done);
- s->label_slots[label_done].pos2 = bc->size;
- }
- return pos_next;
- }
- /* search in all scopes */
- static int find_private_class_field_all(JSContext *ctx, JSFunctionDef *fd,
- JSAtom name, int scope_level)
- {
- int idx;
- idx = fd->scopes[scope_level].first;
- while (idx >= 0) {
- if (fd->vars[idx].var_name == name)
- return idx;
- idx = fd->vars[idx].scope_next;
- }
- return -1;
- }
- static void get_loc_or_ref(DynBuf *bc, BOOL is_ref, int idx)
- {
- /* if the field is not initialized, the error is catched when
- accessing it */
- if (is_ref)
- dbuf_putc(bc, OP_get_var_ref);
- else
- dbuf_putc(bc, OP_get_loc);
- dbuf_put_u16(bc, idx);
- }
- static int resolve_scope_private_field1(JSContext *ctx,
- BOOL *pis_ref, int *pvar_kind,
- JSFunctionDef *s,
- JSAtom var_name, int scope_level)
- {
- int idx, var_kind;
- JSFunctionDef *fd;
- BOOL is_ref;
- fd = s;
- is_ref = FALSE;
- for(;;) {
- idx = find_private_class_field_all(ctx, fd, var_name, scope_level);
- if (idx >= 0) {
- var_kind = fd->vars[idx].var_kind;
- if (is_ref) {
- capture_var(fd, &fd->vars[idx]);
- idx = get_closure_var(ctx, s, fd, JS_CLOSURE_LOCAL, idx, var_name,
- TRUE, TRUE, JS_VAR_NORMAL);
- if (idx < 0)
- return -1;
- }
- break;
- }
- scope_level = fd->parent_scope_level;
- if (!fd->parent) {
- if (fd->is_eval) {
- /* closure of the eval function (top level) */
- for (idx = 0; idx < fd->closure_var_count; idx++) {
- JSClosureVar *cv = &fd->closure_var[idx];
- if (cv->var_name == var_name) {
- var_kind = cv->var_kind;
- is_ref = TRUE;
- if (fd != s) {
- idx = get_closure_var(ctx, s, fd,
- JS_CLOSURE_REF,
- idx,
- cv->var_name, cv->is_const,
- cv->is_lexical,
- cv->var_kind);
- if (idx < 0)
- return -1;
- }
- goto done;
- }
- }
- }
- /* XXX: no line number info */
- JS_ThrowSyntaxErrorAtom(ctx, "undefined private field '%s'",
- var_name);
- return -1;
- } else {
- fd = fd->parent;
- }
- is_ref = TRUE;
- }
- done:
- *pis_ref = is_ref;
- *pvar_kind = var_kind;
- return idx;
- }
- /* return 0 if OK or -1 if the private field could not be resolved */
- static int resolve_scope_private_field(JSContext *ctx, JSFunctionDef *s,
- JSAtom var_name, int scope_level, int op,
- DynBuf *bc)
- {
- int idx, var_kind;
- BOOL is_ref;
- idx = resolve_scope_private_field1(ctx, &is_ref, &var_kind, s,
- var_name, scope_level);
- if (idx < 0)
- return -1;
- assert(var_kind != JS_VAR_NORMAL);
- switch (op) {
- case OP_scope_get_private_field:
- case OP_scope_get_private_field2:
- switch(var_kind) {
- case JS_VAR_PRIVATE_FIELD:
- if (op == OP_scope_get_private_field2)
- dbuf_putc(bc, OP_dup);
- get_loc_or_ref(bc, is_ref, idx);
- dbuf_putc(bc, OP_get_private_field);
- break;
- case JS_VAR_PRIVATE_METHOD:
- get_loc_or_ref(bc, is_ref, idx);
- dbuf_putc(bc, OP_check_brand);
- if (op != OP_scope_get_private_field2)
- dbuf_putc(bc, OP_nip);
- break;
- case JS_VAR_PRIVATE_GETTER:
- case JS_VAR_PRIVATE_GETTER_SETTER:
- if (op == OP_scope_get_private_field2)
- dbuf_putc(bc, OP_dup);
- get_loc_or_ref(bc, is_ref, idx);
- dbuf_putc(bc, OP_check_brand);
- dbuf_putc(bc, OP_call_method);
- dbuf_put_u16(bc, 0);
- break;
- case JS_VAR_PRIVATE_SETTER:
- /* XXX: add clearer error message */
- dbuf_putc(bc, OP_throw_error);
- dbuf_put_u32(bc, JS_DupAtom(ctx, var_name));
- dbuf_putc(bc, JS_THROW_VAR_RO);
- break;
- default:
- abort();
- }
- break;
- case OP_scope_put_private_field:
- switch(var_kind) {
- case JS_VAR_PRIVATE_FIELD:
- get_loc_or_ref(bc, is_ref, idx);
- dbuf_putc(bc, OP_put_private_field);
- break;
- case JS_VAR_PRIVATE_METHOD:
- case JS_VAR_PRIVATE_GETTER:
- /* XXX: add clearer error message */
- dbuf_putc(bc, OP_throw_error);
- dbuf_put_u32(bc, JS_DupAtom(ctx, var_name));
- dbuf_putc(bc, JS_THROW_VAR_RO);
- break;
- case JS_VAR_PRIVATE_SETTER:
- case JS_VAR_PRIVATE_GETTER_SETTER:
- {
- JSAtom setter_name = get_private_setter_name(ctx, var_name);
- if (setter_name == JS_ATOM_NULL)
- return -1;
- idx = resolve_scope_private_field1(ctx, &is_ref,
- &var_kind, s,
- setter_name, scope_level);
- JS_FreeAtom(ctx, setter_name);
- if (idx < 0)
- return -1;
- assert(var_kind == JS_VAR_PRIVATE_SETTER);
- get_loc_or_ref(bc, is_ref, idx);
- dbuf_putc(bc, OP_swap);
- /* obj func value */
- dbuf_putc(bc, OP_rot3r);
- /* value obj func */
- dbuf_putc(bc, OP_check_brand);
- dbuf_putc(bc, OP_rot3l);
- /* obj func value */
- dbuf_putc(bc, OP_call_method);
- dbuf_put_u16(bc, 1);
- dbuf_putc(bc, OP_drop);
- }
- break;
- default:
- abort();
- }
- break;
- case OP_scope_in_private_field:
- get_loc_or_ref(bc, is_ref, idx);
- dbuf_putc(bc, OP_private_in);
- break;
- default:
- abort();
- }
- return 0;
- }
- static void mark_eval_captured_variables(JSContext *ctx, JSFunctionDef *s,
- int scope_level)
- {
- int idx;
- JSVarDef *vd;
- for (idx = s->scopes[scope_level].first; idx >= 0;) {
- vd = &s->vars[idx];
- capture_var(s, vd);
- idx = vd->scope_next;
- }
- }
- /* XXX: should handle the argument scope generically */
- static BOOL is_var_in_arg_scope(JSAtom var_name, JSVarKindEnum var_kind)
- {
- return (var_name == JS_ATOM_home_object ||
- var_name == JS_ATOM_this_active_func ||
- var_name == JS_ATOM_new_target ||
- var_name == JS_ATOM_this ||
- var_name == JS_ATOM__arg_var_ ||
- var_kind == JS_VAR_FUNCTION_NAME);
- }
- static void add_eval_variables(JSContext *ctx, JSFunctionDef *s)
- {
- JSFunctionDef *fd;
- JSVarDef *vd;
- int i, scope_level, scope_idx;
- BOOL has_arguments_binding, has_this_binding, is_arg_scope;
- /* in non strict mode, variables are created in the caller's
- environment object */
- if (!s->is_eval && !(s->js_mode & JS_MODE_STRICT)) {
- s->var_object_idx = add_var(ctx, s, JS_ATOM__var_);
- if (s->has_parameter_expressions) {
- /* an additional variable object is needed for the
- argument scope */
- s->arg_var_object_idx = add_var(ctx, s, JS_ATOM__arg_var_);
- }
- }
- /* eval can potentially use 'arguments' so we must define it */
- has_this_binding = s->has_this_binding;
- if (has_this_binding) {
- if (s->this_var_idx < 0)
- s->this_var_idx = add_var_this(ctx, s);
- if (s->new_target_var_idx < 0)
- s->new_target_var_idx = add_var(ctx, s, JS_ATOM_new_target);
- if (s->is_derived_class_constructor && s->this_active_func_var_idx < 0)
- s->this_active_func_var_idx = add_var(ctx, s, JS_ATOM_this_active_func);
- if (s->has_home_object && s->home_object_var_idx < 0)
- s->home_object_var_idx = add_var(ctx, s, JS_ATOM_home_object);
- }
- has_arguments_binding = s->has_arguments_binding;
- if (has_arguments_binding) {
- add_arguments_var(ctx, s);
- /* also add an arguments binding in the argument scope to
- raise an error if a direct eval in the argument scope tries
- to redefine it */
- if (s->has_parameter_expressions && !(s->js_mode & JS_MODE_STRICT))
- add_arguments_arg(ctx, s);
- }
- if (s->is_func_expr && s->func_name != JS_ATOM_NULL)
- add_func_var(ctx, s, s->func_name);
- for(i = 0; i < s->arg_count; i++) {
- vd = &s->args[i];
- capture_var(s, vd);
- }
- for(i = 0; i < s->var_count; i++) {
- vd = &s->vars[i];
- /* do not close top level last result */
- if (vd->scope_level == 0 &&
- vd->var_name != JS_ATOM__ret_ &&
- vd->var_name != JS_ATOM_NULL) {
- capture_var(s, vd);
- }
- }
-
- /* eval can use all the variables of the enclosing functions, so
- they must be all put in the closure. The closure variables are
- ordered by scope. It works only because no closure are created
- before. */
- assert(s->is_eval || s->closure_var_count == 0);
- /* XXX: inefficient, but eval performance is less critical */
- fd = s;
- for(;;) {
- scope_level = fd->parent_scope_level;
- fd = fd->parent;
- if (!fd)
- break;
- /* add 'this' if it was not previously added */
- if (!has_this_binding && fd->has_this_binding) {
- if (fd->this_var_idx < 0)
- fd->this_var_idx = add_var_this(ctx, fd);
- if (fd->new_target_var_idx < 0)
- fd->new_target_var_idx = add_var(ctx, fd, JS_ATOM_new_target);
- if (fd->is_derived_class_constructor && fd->this_active_func_var_idx < 0)
- fd->this_active_func_var_idx = add_var(ctx, fd, JS_ATOM_this_active_func);
- if (fd->has_home_object && fd->home_object_var_idx < 0)
- fd->home_object_var_idx = add_var(ctx, fd, JS_ATOM_home_object);
- has_this_binding = TRUE;
- }
- /* add 'arguments' if it was not previously added */
- if (!has_arguments_binding && fd->has_arguments_binding) {
- add_arguments_var(ctx, fd);
- has_arguments_binding = TRUE;
- }
- /* add function name */
- if (fd->is_func_expr && fd->func_name != JS_ATOM_NULL)
- add_func_var(ctx, fd, fd->func_name);
- /* add lexical variables */
- scope_idx = fd->scopes[scope_level].first;
- while (scope_idx >= 0) {
- vd = &fd->vars[scope_idx];
- capture_var(fd, vd);
- get_closure_var(ctx, s, fd, JS_CLOSURE_LOCAL, scope_idx,
- vd->var_name, vd->is_const, vd->is_lexical, vd->var_kind);
- scope_idx = vd->scope_next;
- }
- is_arg_scope = (scope_idx == ARG_SCOPE_END);
- if (!is_arg_scope) {
- /* add unscoped variables */
- /* XXX: propagate is_const and var_kind too ? */
- for(i = 0; i < fd->arg_count; i++) {
- vd = &fd->args[i];
- if (vd->var_name != JS_ATOM_NULL) {
- capture_var(fd, vd);
- get_closure_var(ctx, s, fd,
- JS_CLOSURE_ARG, i, vd->var_name, FALSE,
- vd->is_lexical, JS_VAR_NORMAL);
- }
- }
- for(i = 0; i < fd->var_count; i++) {
- vd = &fd->vars[i];
- /* do not close top level last result */
- if (vd->scope_level == 0 &&
- vd->var_name != JS_ATOM__ret_ &&
- vd->var_name != JS_ATOM_NULL) {
- capture_var(fd, vd);
- get_closure_var(ctx, s, fd,
- JS_CLOSURE_LOCAL, i, vd->var_name, FALSE,
- vd->is_lexical, JS_VAR_NORMAL);
- }
- }
- } else {
- for(i = 0; i < fd->var_count; i++) {
- vd = &fd->vars[i];
- /* do not close top level last result */
- if (vd->scope_level == 0 && is_var_in_arg_scope(vd->var_name, vd->var_kind)) {
- capture_var(fd, vd);
- get_closure_var(ctx, s, fd,
- JS_CLOSURE_LOCAL, i, vd->var_name, FALSE,
- vd->is_lexical, JS_VAR_NORMAL);
- }
- }
- }
- if (fd->is_eval) {
- int idx;
- /* add direct eval variables (we are necessarily at the
- top level). */
- for (idx = 0; idx < fd->closure_var_count; idx++) {
- JSClosureVar *cv = &fd->closure_var[idx];
- /* Global variables are removed but module
- definitions are kept. */
- if (cv->closure_type != JS_CLOSURE_GLOBAL_REF &&
- cv->closure_type != JS_CLOSURE_GLOBAL_DECL &&
- cv->closure_type != JS_CLOSURE_GLOBAL) {
- get_closure_var(ctx, s, fd,
- JS_CLOSURE_REF,
- idx, cv->var_name, cv->is_const,
- cv->is_lexical, cv->var_kind);
- }
- }
- }
- }
- }
- static void set_closure_from_var(JSContext *ctx, JSClosureVar *cv,
- JSBytecodeVarDef *vd, int var_idx)
- {
- cv->closure_type = JS_CLOSURE_LOCAL;
- cv->is_const = vd->is_const;
- cv->is_lexical = vd->is_lexical;
- cv->var_kind = vd->var_kind;
- cv->var_idx = var_idx;
- cv->var_name = JS_DupAtom(ctx, vd->var_name);
- }
- /* for direct eval compilation: add references to the variables of the
- calling function */
- static __exception int add_closure_variables(JSContext *ctx, JSFunctionDef *s,
- JSFunctionBytecode *b, int scope_idx)
- {
- int i, count;
- JSBytecodeVarDef *vd;
- BOOL is_arg_scope;
- count = b->arg_count + b->var_count + b->closure_var_count;
- s->closure_var = NULL;
- s->closure_var_count = 0;
- s->closure_var_size = count;
- if (count == 0)
- return 0;
- s->closure_var = js_malloc(ctx, sizeof(s->closure_var[0]) * count);
- if (!s->closure_var)
- return -1;
- /* Add lexical variables in scope at the point of evaluation */
- for (i = scope_idx; i >= 0;) {
- vd = &b->vardefs[b->arg_count + i];
- if (vd->has_scope) {
- JSClosureVar *cv = &s->closure_var[s->closure_var_count++];
- set_closure_from_var(ctx, cv, vd, i);
- }
- i = vd->scope_next;
- }
- is_arg_scope = (i == ARG_SCOPE_END);
- if (!is_arg_scope) {
- /* Add argument variables */
- for(i = 0; i < b->arg_count; i++) {
- JSClosureVar *cv = &s->closure_var[s->closure_var_count++];
- vd = &b->vardefs[i];
- cv->closure_type = JS_CLOSURE_ARG;
- cv->is_const = FALSE;
- cv->is_lexical = FALSE;
- cv->var_kind = JS_VAR_NORMAL;
- cv->var_idx = i;
- cv->var_name = JS_DupAtom(ctx, vd->var_name);
- }
- /* Add local non lexical variables */
- for(i = 0; i < b->var_count; i++) {
- vd = &b->vardefs[b->arg_count + i];
- if (!vd->has_scope && vd->var_name != JS_ATOM__ret_) {
- JSClosureVar *cv = &s->closure_var[s->closure_var_count++];
- set_closure_from_var(ctx, cv, vd, i);
- }
- }
- } else {
- /* only add pseudo variables */
- for(i = 0; i < b->var_count; i++) {
- vd = &b->vardefs[b->arg_count + i];
- if (!vd->has_scope && is_var_in_arg_scope(vd->var_name, vd->var_kind)) {
- JSClosureVar *cv = &s->closure_var[s->closure_var_count++];
- set_closure_from_var(ctx, cv, vd, i);
- }
- }
- }
- for(i = 0; i < b->closure_var_count; i++) {
- JSClosureVar *cv0 = &b->closure_var[i];
- JSClosureVar *cv;
- switch(cv0->closure_type) {
- case JS_CLOSURE_LOCAL:
- case JS_CLOSURE_ARG:
- case JS_CLOSURE_REF:
- case JS_CLOSURE_MODULE_DECL:
- case JS_CLOSURE_MODULE_IMPORT:
- break;
- case JS_CLOSURE_GLOBAL_REF:
- case JS_CLOSURE_GLOBAL_DECL:
- case JS_CLOSURE_GLOBAL:
- continue; /* not necessary to add global variables */
- default:
- abort();
- }
- cv = &s->closure_var[s->closure_var_count++];
- cv->closure_type = JS_CLOSURE_REF;
- cv->is_const = cv0->is_const;
- cv->is_lexical = cv0->is_lexical;
- cv->var_kind = cv0->var_kind;
- cv->var_idx = i;
- cv->var_name = JS_DupAtom(ctx, cv0->var_name);
- }
- return 0;
- }
- typedef struct CodeContext {
- const uint8_t *bc_buf; /* code buffer */
- int bc_len; /* length of the code buffer */
- int pos; /* position past the matched code pattern */
- int line_num; /* last visited OP_line_num parameter or -1 */
- int op;
- int idx;
- int label;
- int val;
- JSAtom atom;
- } CodeContext;
- #define M2(op1, op2) ((op1) | ((op2) << 8))
- #define M3(op1, op2, op3) ((op1) | ((op2) << 8) | ((op3) << 16))
- #define M4(op1, op2, op3, op4) ((op1) | ((op2) << 8) | ((op3) << 16) | ((op4) << 24))
- static BOOL code_match(CodeContext *s, int pos, ...)
- {
- const uint8_t *tab = s->bc_buf;
- int op, len, op1, line_num, pos_next;
- va_list ap;
- BOOL ret = FALSE;
- line_num = -1;
- va_start(ap, pos);
- for(;;) {
- op1 = va_arg(ap, int);
- if (op1 == -1) {
- s->pos = pos;
- s->line_num = line_num;
- ret = TRUE;
- break;
- }
- for (;;) {
- if (pos >= s->bc_len)
- goto done;
- op = tab[pos];
- len = opcode_info[op].size;
- pos_next = pos + len;
- if (pos_next > s->bc_len)
- goto done;
- if (op == OP_line_num) {
- line_num = get_u32(tab + pos + 1);
- pos = pos_next;
- } else {
- break;
- }
- }
- if (op != op1) {
- if (op1 == (uint8_t)op1 || !op)
- break;
- if (op != (uint8_t)op1
- && op != (uint8_t)(op1 >> 8)
- && op != (uint8_t)(op1 >> 16)
- && op != (uint8_t)(op1 >> 24)) {
- break;
- }
- s->op = op;
- }
- pos++;
- switch(opcode_info[op].fmt) {
- case OP_FMT_loc8:
- case OP_FMT_u8:
- {
- int idx = tab[pos];
- int arg = va_arg(ap, int);
- if (arg == -1) {
- s->idx = idx;
- } else {
- if (arg != idx)
- goto done;
- }
- break;
- }
- case OP_FMT_u16:
- case OP_FMT_npop:
- case OP_FMT_loc:
- case OP_FMT_arg:
- case OP_FMT_var_ref:
- {
- int idx = get_u16(tab + pos);
- int arg = va_arg(ap, int);
- if (arg == -1) {
- s->idx = idx;
- } else {
- if (arg != idx)
- goto done;
- }
- break;
- }
- case OP_FMT_i32:
- case OP_FMT_u32:
- case OP_FMT_label:
- case OP_FMT_const:
- {
- s->label = get_u32(tab + pos);
- break;
- }
- case OP_FMT_label_u16:
- {
- s->label = get_u32(tab + pos);
- s->val = get_u16(tab + pos + 4);
- break;
- }
- case OP_FMT_atom:
- {
- s->atom = get_u32(tab + pos);
- break;
- }
- case OP_FMT_atom_u8:
- {
- s->atom = get_u32(tab + pos);
- s->val = get_u8(tab + pos + 4);
- break;
- }
- case OP_FMT_atom_u16:
- {
- s->atom = get_u32(tab + pos);
- s->val = get_u16(tab + pos + 4);
- break;
- }
- case OP_FMT_atom_label_u8:
- {
- s->atom = get_u32(tab + pos);
- s->label = get_u32(tab + pos + 4);
- s->val = get_u8(tab + pos + 8);
- break;
- }
- default:
- break;
- }
- pos = pos_next;
- }
- done:
- va_end(ap);
- return ret;
- }
- static void instantiate_hoisted_definitions(JSContext *ctx, JSFunctionDef *s, DynBuf *bc)
- {
- int i, idx, label_next = -1;
- /* add the hoisted functions in arguments and local variables */
- for(i = 0; i < s->arg_count; i++) {
- JSVarDef *vd = &s->args[i];
- if (vd->func_pool_idx >= 0) {
- dbuf_putc(bc, OP_fclosure);
- dbuf_put_u32(bc, vd->func_pool_idx);
- dbuf_putc(bc, OP_put_arg);
- dbuf_put_u16(bc, i);
- }
- }
- for(i = 0; i < s->var_count; i++) {
- JSVarDef *vd = &s->vars[i];
- if (vd->scope_level == 0 && vd->func_pool_idx >= 0) {
- dbuf_putc(bc, OP_fclosure);
- dbuf_put_u32(bc, vd->func_pool_idx);
- dbuf_putc(bc, OP_put_loc);
- dbuf_put_u16(bc, i);
- }
- }
- /* the module global variables must be initialized before
- evaluating the module so that the exported functions are
- visible if there are cyclic module references */
- if (s->module) {
- label_next = new_label_fd(s);
- if (label_next < 0) {
- dbuf_set_error(bc);
- return;
- }
- /* if 'this' is true, initialize the global variables and return */
- dbuf_putc(bc, OP_push_this);
- dbuf_putc(bc, OP_if_false);
- dbuf_put_u32(bc, label_next);
- update_label(s, label_next, 1);
- s->jump_size++;
- }
- /* add the global variables (only happens if s->is_global_var is
- true) */
- /* XXX: inefficient, add a closure index in JSGlobalVar */
- for(i = 0; i < s->global_var_count; i++) {
- JSGlobalVar *hf = &s->global_vars[i];
- BOOL has_var_obj = FALSE;
- BOOL force_init = hf->force_init;
- /* we are in an eval, so the closure contains all the
- enclosing variables */
- /* If the outer function has a variable environment,
- create a property for the variable there */
- for(idx = 0; idx < s->closure_var_count; idx++) {
- JSClosureVar *cv = &s->closure_var[idx];
- if (cv->var_name == hf->var_name) {
- force_init = FALSE;
- goto closure_found;
- }
- if (cv->var_name == JS_ATOM__var_ ||
- cv->var_name == JS_ATOM__arg_var_) {
- dbuf_putc(bc, OP_get_var_ref);
- dbuf_put_u16(bc, idx);
- has_var_obj = TRUE;
- force_init = TRUE;
- goto closure_found;
- }
- }
- abort();
- closure_found:
- if (hf->cpool_idx >= 0 || force_init) {
- if (hf->cpool_idx >= 0) {
- dbuf_putc(bc, OP_fclosure);
- dbuf_put_u32(bc, hf->cpool_idx);
- if (hf->var_name == JS_ATOM__default_) {
- /* set default export function name */
- dbuf_putc(bc, OP_set_name);
- dbuf_put_u32(bc, JS_DupAtom(ctx, JS_ATOM_default));
- }
- } else {
- dbuf_putc(bc, OP_undefined);
- }
- if (!has_var_obj) {
- dbuf_putc(bc, OP_put_var_ref);
- dbuf_put_u16(bc, idx);
- } else {
- dbuf_putc(bc, OP_define_field);
- dbuf_put_u32(bc, JS_DupAtom(ctx, hf->var_name));
- dbuf_putc(bc, OP_drop);
- }
- }
- JS_FreeAtom(ctx, hf->var_name);
- }
- if (s->module) {
- dbuf_putc(bc, OP_return_undef);
- dbuf_putc(bc, OP_label);
- dbuf_put_u32(bc, label_next);
- s->label_slots[label_next].pos2 = bc->size;
- }
- js_free(ctx, s->global_vars);
- s->global_vars = NULL;
- s->global_var_count = 0;
- s->global_var_size = 0;
- }
- static int skip_dead_code(JSFunctionDef *s, const uint8_t *bc_buf, int bc_len,
- int pos, int *linep)
- {
- int op, len, label;
- for (; pos < bc_len; pos += len) {
- op = bc_buf[pos];
- len = opcode_info[op].size;
- if (op == OP_line_num) {
- *linep = get_u32(bc_buf + pos + 1);
- } else
- if (op == OP_label) {
- label = get_u32(bc_buf + pos + 1);
- if (update_label(s, label, 0) > 0)
- break;
- #if 0
- if (s->label_slots[label].first_reloc) {
- printf("line %d: unreferenced label %d:%d has relocations\n",
- *linep, label, s->label_slots[label].pos2);
- }
- #endif
- assert(s->label_slots[label].first_reloc == NULL);
- } else {
- /* XXX: output a warning for unreachable code? */
- JSAtom atom;
- switch(opcode_info[op].fmt) {
- case OP_FMT_label:
- case OP_FMT_label_u16:
- label = get_u32(bc_buf + pos + 1);
- update_label(s, label, -1);
- break;
- case OP_FMT_atom_label_u8:
- case OP_FMT_atom_label_u16:
- label = get_u32(bc_buf + pos + 5);
- update_label(s, label, -1);
- /* fall thru */
- case OP_FMT_atom:
- case OP_FMT_atom_u8:
- case OP_FMT_atom_u16:
- atom = get_u32(bc_buf + pos + 1);
- JS_FreeAtom(s->ctx, atom);
- break;
- default:
- break;
- }
- }
- }
- return pos;
- }
- static int get_label_pos(JSFunctionDef *s, int label)
- {
- int i, pos;
- for (i = 0; i < 20; i++) {
- pos = s->label_slots[label].pos;
- for (;;) {
- switch (s->byte_code.buf[pos]) {
- case OP_line_num:
- case OP_label:
- pos += 5;
- continue;
- case OP_goto:
- label = get_u32(s->byte_code.buf + pos + 1);
- break;
- default:
- return pos;
- }
- break;
- }
- }
- return pos;
- }
- /* convert global variable accesses to local variables or closure
- variables when necessary */
- static __exception int resolve_variables(JSContext *ctx, JSFunctionDef *s)
- {
- int pos, pos_next, bc_len, op, len, line_num, i, idx;
- uint8_t *bc_buf;
- JSAtom var_name;
- DynBuf bc_out;
- CodeContext cc;
- int scope;
- cc.bc_buf = bc_buf = s->byte_code.buf;
- cc.bc_len = bc_len = s->byte_code.size;
- js_dbuf_bytecode_init(ctx, &bc_out);
- /* first pass for runtime checks (must be done before the
- variables are created) */
- /* XXX: inefficient */
- for(i = 0; i < s->global_var_count; i++) {
- JSGlobalVar *hf = &s->global_vars[i];
- /* check if global variable (XXX: simplify) */
- for(idx = 0; idx < s->closure_var_count; idx++) {
- JSClosureVar *cv = &s->closure_var[idx];
- if (cv->closure_type == JS_CLOSURE_GLOBAL_REF ||
- cv->closure_type == JS_CLOSURE_GLOBAL_DECL ||
- cv->closure_type == JS_CLOSURE_GLOBAL ||
- cv->closure_type == JS_CLOSURE_MODULE_DECL ||
- cv->closure_type == JS_CLOSURE_MODULE_IMPORT)
- goto next; /* don't look at global variables (they are at the end) */
- if (cv->var_name == hf->var_name) {
- if (s->eval_type == JS_EVAL_TYPE_DIRECT &&
- cv->is_lexical) {
- /* Check if a lexical variable is
- redefined as 'var'. XXX: Could abort
- compilation here, but for consistency
- with the other checks, we delay the
- error generation. */
- dbuf_putc(&bc_out, OP_throw_error);
- dbuf_put_u32(&bc_out, JS_DupAtom(ctx, hf->var_name));
- dbuf_putc(&bc_out, JS_THROW_VAR_REDECL);
- }
- goto next;
- }
- if (cv->var_name == JS_ATOM__var_ ||
- cv->var_name == JS_ATOM__arg_var_)
- goto next;
- }
- next: ;
- }
- line_num = 0; /* avoid warning */
- for (pos = 0; pos < bc_len; pos = pos_next) {
- op = bc_buf[pos];
- len = opcode_info[op].size;
- pos_next = pos + len;
- switch(op) {
- case OP_line_num:
- line_num = get_u32(bc_buf + pos + 1);
- s->line_number_size++;
- goto no_change;
- case OP_eval: /* convert scope index to adjusted variable index */
- {
- int call_argc = get_u16(bc_buf + pos + 1);
- scope = get_u16(bc_buf + pos + 1 + 2);
- mark_eval_captured_variables(ctx, s, scope);
- dbuf_putc(&bc_out, op);
- dbuf_put_u16(&bc_out, call_argc);
- dbuf_put_u16(&bc_out, s->scopes[scope].first - ARG_SCOPE_END);
- }
- break;
- case OP_apply_eval: /* convert scope index to adjusted variable index */
- scope = get_u16(bc_buf + pos + 1);
- mark_eval_captured_variables(ctx, s, scope);
- dbuf_putc(&bc_out, op);
- dbuf_put_u16(&bc_out, s->scopes[scope].first - ARG_SCOPE_END);
- break;
- case OP_scope_get_var_checkthis:
- case OP_scope_get_var_undef:
- case OP_scope_get_var:
- case OP_scope_put_var:
- case OP_scope_delete_var:
- case OP_scope_get_ref:
- case OP_scope_put_var_init:
- var_name = get_u32(bc_buf + pos + 1);
- scope = get_u16(bc_buf + pos + 5);
- pos_next = resolve_scope_var(ctx, s, var_name, scope, op, &bc_out,
- NULL, NULL, pos_next);
- JS_FreeAtom(ctx, var_name);
- break;
- case OP_scope_make_ref:
- {
- int label;
- LabelSlot *ls;
- var_name = get_u32(bc_buf + pos + 1);
- label = get_u32(bc_buf + pos + 5);
- scope = get_u16(bc_buf + pos + 9);
- ls = &s->label_slots[label];
- ls->ref_count--; /* always remove label reference */
- pos_next = resolve_scope_var(ctx, s, var_name, scope, op, &bc_out,
- bc_buf, ls, pos_next);
- JS_FreeAtom(ctx, var_name);
- }
- break;
- case OP_scope_get_private_field:
- case OP_scope_get_private_field2:
- case OP_scope_put_private_field:
- case OP_scope_in_private_field:
- {
- int ret;
- var_name = get_u32(bc_buf + pos + 1);
- scope = get_u16(bc_buf + pos + 5);
- ret = resolve_scope_private_field(ctx, s, var_name, scope, op, &bc_out);
- if (ret < 0)
- goto fail;
- JS_FreeAtom(ctx, var_name);
- }
- break;
- case OP_gosub:
- s->jump_size++;
- if (OPTIMIZE) {
- /* remove calls to empty finalizers */
- int label;
- LabelSlot *ls;
- label = get_u32(bc_buf + pos + 1);
- assert(label >= 0 && label < s->label_count);
- ls = &s->label_slots[label];
- if (code_match(&cc, ls->pos, OP_ret, -1)) {
- ls->ref_count--;
- break;
- }
- }
- goto no_change;
- case OP_drop:
- if (0) {
- /* remove drops before return_undef */
- /* do not perform this optimization in pass2 because
- it breaks patterns recognised in resolve_labels */
- int pos1 = pos_next;
- int line1 = line_num;
- while (code_match(&cc, pos1, OP_drop, -1)) {
- if (cc.line_num >= 0) line1 = cc.line_num;
- pos1 = cc.pos;
- }
- if (code_match(&cc, pos1, OP_return_undef, -1)) {
- pos_next = pos1;
- if (line1 != -1 && line1 != line_num) {
- line_num = line1;
- s->line_number_size++;
- dbuf_putc(&bc_out, OP_line_num);
- dbuf_put_u32(&bc_out, line_num);
- }
- break;
- }
- }
- goto no_change;
- case OP_insert3:
- if (OPTIMIZE) {
- /* Transformation: insert3 put_array_el|put_ref_value drop -> put_array_el|put_ref_value */
- if (code_match(&cc, pos_next, M2(OP_put_array_el, OP_put_ref_value), OP_drop, -1)) {
- dbuf_putc(&bc_out, cc.op);
- pos_next = cc.pos;
- if (cc.line_num != -1 && cc.line_num != line_num) {
- line_num = cc.line_num;
- s->line_number_size++;
- dbuf_putc(&bc_out, OP_line_num);
- dbuf_put_u32(&bc_out, line_num);
- }
- break;
- }
- }
- goto no_change;
- case OP_goto:
- s->jump_size++;
- /* fall thru */
- case OP_tail_call:
- case OP_tail_call_method:
- case OP_return:
- case OP_return_undef:
- case OP_throw:
- case OP_throw_error:
- case OP_ret:
- if (OPTIMIZE) {
- /* remove dead code */
- int line = -1;
- dbuf_put(&bc_out, bc_buf + pos, len);
- pos = skip_dead_code(s, bc_buf, bc_len, pos + len, &line);
- pos_next = pos;
- if (pos < bc_len && line >= 0 && line_num != line) {
- line_num = line;
- s->line_number_size++;
- dbuf_putc(&bc_out, OP_line_num);
- dbuf_put_u32(&bc_out, line_num);
- }
- break;
- }
- goto no_change;
- case OP_label:
- {
- int label;
- LabelSlot *ls;
- label = get_u32(bc_buf + pos + 1);
- assert(label >= 0 && label < s->label_count);
- ls = &s->label_slots[label];
- ls->pos2 = bc_out.size + opcode_info[op].size;
- }
- goto no_change;
- case OP_enter_scope:
- {
- int scope_idx, scope = get_u16(bc_buf + pos + 1);
- if (scope == s->body_scope) {
- instantiate_hoisted_definitions(ctx, s, &bc_out);
- }
- for(scope_idx = s->scopes[scope].first; scope_idx >= 0;) {
- JSVarDef *vd = &s->vars[scope_idx];
- if (vd->scope_level == scope) {
- if (scope_idx != s->arguments_arg_idx) {
- if (vd->var_kind == JS_VAR_FUNCTION_DECL ||
- vd->var_kind == JS_VAR_NEW_FUNCTION_DECL) {
- /* Initialize lexical variable upon entering scope */
- dbuf_putc(&bc_out, OP_fclosure);
- dbuf_put_u32(&bc_out, vd->func_pool_idx);
- dbuf_putc(&bc_out, OP_put_loc);
- dbuf_put_u16(&bc_out, scope_idx);
- } else {
- /* XXX: should check if variable can be used
- before initialization */
- dbuf_putc(&bc_out, OP_set_loc_uninitialized);
- dbuf_put_u16(&bc_out, scope_idx);
- }
- }
- scope_idx = vd->scope_next;
- } else {
- break;
- }
- }
- }
- break;
- case OP_leave_scope:
- {
- int scope_idx, scope = get_u16(bc_buf + pos + 1);
- for(scope_idx = s->scopes[scope].first; scope_idx >= 0;) {
- JSVarDef *vd = &s->vars[scope_idx];
- if (vd->scope_level == scope) {
- if (vd->is_captured) {
- dbuf_putc(&bc_out, OP_close_loc);
- dbuf_put_u16(&bc_out, scope_idx);
- }
- scope_idx = vd->scope_next;
- } else {
- break;
- }
- }
- }
- break;
- case OP_set_name:
- {
- /* remove dummy set_name opcodes */
- JSAtom name = get_u32(bc_buf + pos + 1);
- if (name == JS_ATOM_NULL)
- break;
- }
- goto no_change;
- case OP_if_false:
- case OP_if_true:
- case OP_catch:
- s->jump_size++;
- goto no_change;
- case OP_dup:
- if (OPTIMIZE) {
- /* Transformation: dup if_false(l1) drop, l1: if_false(l2) -> if_false(l2) */
- /* Transformation: dup if_true(l1) drop, l1: if_true(l2) -> if_true(l2) */
- if (code_match(&cc, pos_next, M2(OP_if_false, OP_if_true), OP_drop, -1)) {
- int lab0, lab1, op1, pos1, line1, pos2;
- lab0 = lab1 = cc.label;
- assert(lab1 >= 0 && lab1 < s->label_count);
- op1 = cc.op;
- pos1 = cc.pos;
- line1 = cc.line_num;
- while (code_match(&cc, (pos2 = get_label_pos(s, lab1)), OP_dup, op1, OP_drop, -1)) {
- lab1 = cc.label;
- }
- if (code_match(&cc, pos2, op1, -1)) {
- s->jump_size++;
- update_label(s, lab0, -1);
- update_label(s, cc.label, +1);
- dbuf_putc(&bc_out, op1);
- dbuf_put_u32(&bc_out, cc.label);
- pos_next = pos1;
- if (line1 != -1 && line1 != line_num) {
- line_num = line1;
- s->line_number_size++;
- dbuf_putc(&bc_out, OP_line_num);
- dbuf_put_u32(&bc_out, line_num);
- }
- break;
- }
- }
- }
- goto no_change;
- case OP_nop:
- /* remove erased code */
- break;
- case OP_set_class_name:
- /* only used during parsing */
- break;
- case OP_get_field_opt_chain: /* equivalent to OP_get_field */
- {
- JSAtom name = get_u32(bc_buf + pos + 1);
- dbuf_putc(&bc_out, OP_get_field);
- dbuf_put_u32(&bc_out, name);
- }
- break;
- case OP_get_array_el_opt_chain: /* equivalent to OP_get_array_el */
- dbuf_putc(&bc_out, OP_get_array_el);
- break;
- default:
- no_change:
- dbuf_put(&bc_out, bc_buf + pos, len);
- break;
- }
- }
- /* set the new byte code */
- dbuf_free(&s->byte_code);
- s->byte_code = bc_out;
- if (dbuf_error(&s->byte_code)) {
- JS_ThrowOutOfMemory(ctx);
- return -1;
- }
- return 0;
- fail:
- /* continue the copy to keep the atom refcounts consistent */
- /* XXX: find a better solution ? */
- for (; pos < bc_len; pos = pos_next) {
- op = bc_buf[pos];
- len = opcode_info[op].size;
- pos_next = pos + len;
- dbuf_put(&bc_out, bc_buf + pos, len);
- }
- dbuf_free(&s->byte_code);
- s->byte_code = bc_out;
- return -1;
- }
- /* the pc2line table gives a source position for each PC value */
- static void add_pc2line_info(JSFunctionDef *s, uint32_t pc, uint32_t source_pos)
- {
- if (s->line_number_slots != NULL
- && s->line_number_count < s->line_number_size
- && pc >= s->line_number_last_pc
- && source_pos != s->line_number_last) {
- s->line_number_slots[s->line_number_count].pc = pc;
- s->line_number_slots[s->line_number_count].source_pos = source_pos;
- s->line_number_count++;
- s->line_number_last_pc = pc;
- s->line_number_last = source_pos;
- }
- }
- /* XXX: could use a more compact storage */
- /* XXX: get_line_col_cached() is slow. For more predictable
- performance, line/cols could be stored every N source
- bytes. Alternatively, get_line_col_cached() could be issued in
- emit_source_pos() so that the deltas are more likely to be
- small. */
- static void compute_pc2line_info(JSFunctionDef *s)
- {
- if (!s->strip_debug) {
- int last_line_num, last_col_num;
- uint32_t last_pc = 0;
- int i, line_num, col_num;
- const uint8_t *buf_start = s->get_line_col_cache->buf_start;
- js_dbuf_init(s->ctx, &s->pc2line);
- last_line_num = get_line_col_cached(s->get_line_col_cache,
- &last_col_num,
- buf_start + s->source_pos);
- dbuf_put_leb128(&s->pc2line, last_line_num); /* line number minus 1 */
- dbuf_put_leb128(&s->pc2line, last_col_num); /* column number minus 1 */
- for (i = 0; i < s->line_number_count; i++) {
- uint32_t pc = s->line_number_slots[i].pc;
- uint32_t source_pos = s->line_number_slots[i].source_pos;
- int diff_pc, diff_line, diff_col;
- if (source_pos == -1)
- continue;
- diff_pc = pc - last_pc;
- if (diff_pc < 0)
- continue;
- line_num = get_line_col_cached(s->get_line_col_cache, &col_num,
- buf_start + source_pos);
- diff_line = line_num - last_line_num;
- diff_col = col_num - last_col_num;
- if (diff_line == 0 && diff_col == 0)
- continue;
- if (diff_line >= PC2LINE_BASE &&
- diff_line < PC2LINE_BASE + PC2LINE_RANGE &&
- diff_pc <= PC2LINE_DIFF_PC_MAX) {
- dbuf_putc(&s->pc2line, (diff_line - PC2LINE_BASE) +
- diff_pc * PC2LINE_RANGE + PC2LINE_OP_FIRST);
- } else {
- /* longer encoding */
- dbuf_putc(&s->pc2line, 0);
- dbuf_put_leb128(&s->pc2line, diff_pc);
- dbuf_put_sleb128(&s->pc2line, diff_line);
- }
- dbuf_put_sleb128(&s->pc2line, diff_col);
-
- last_pc = pc;
- last_line_num = line_num;
- last_col_num = col_num;
- }
- }
- }
- static RelocEntry *add_reloc(JSContext *ctx, LabelSlot *ls, uint32_t addr, int size)
- {
- RelocEntry *re;
- re = js_malloc(ctx, sizeof(*re));
- if (!re)
- return NULL;
- re->addr = addr;
- re->size = size;
- re->next = ls->first_reloc;
- ls->first_reloc = re;
- return re;
- }
- static BOOL code_has_label(CodeContext *s, int pos, int label)
- {
- while (pos < s->bc_len) {
- int op = s->bc_buf[pos];
- if (op == OP_line_num) {
- pos += 5;
- continue;
- }
- if (op == OP_label) {
- int lab = get_u32(s->bc_buf + pos + 1);
- if (lab == label)
- return TRUE;
- pos += 5;
- continue;
- }
- if (op == OP_goto) {
- int lab = get_u32(s->bc_buf + pos + 1);
- if (lab == label)
- return TRUE;
- }
- break;
- }
- return FALSE;
- }
- /* return the target label, following the OP_goto jumps
- the first opcode at destination is stored in *pop
- */
- static int find_jump_target(JSFunctionDef *s, int label0, int *pop, int *pline)
- {
- int i, pos, op, label;
- label = label0;
- update_label(s, label, -1);
- for (i = 0; i < 10; i++) {
- assert(label >= 0 && label < s->label_count);
- pos = s->label_slots[label].pos2;
- for (;;) {
- switch(op = s->byte_code.buf[pos]) {
- case OP_line_num:
- if (pline)
- *pline = get_u32(s->byte_code.buf + pos + 1);
- /* fall thru */
- case OP_label:
- pos += opcode_info[op].size;
- continue;
- case OP_goto:
- label = get_u32(s->byte_code.buf + pos + 1);
- break;
- case OP_drop:
- /* ignore drop opcodes if followed by OP_return_undef */
- while (s->byte_code.buf[++pos] == OP_drop)
- continue;
- if (s->byte_code.buf[pos] == OP_return_undef)
- op = OP_return_undef;
- /* fall thru */
- default:
- goto done;
- }
- break;
- }
- }
- /* cycle detected, could issue a warning */
- /* XXX: the combination of find_jump_target() and skip_dead_code()
- seems incorrect with cyclic labels. See for exemple:
- for (;;) {
- l:break l;
- l:break l;
- l:break l;
- l:break l;
- }
- Avoiding changing the target is just a workaround and might not
- suffice to completely fix the problem. */
- label = label0;
- done:
- *pop = op;
- update_label(s, label, +1);
- return label;
- }
- static void push_short_int(DynBuf *bc_out, int val)
- {
- #if SHORT_OPCODES
- if (val >= -1 && val <= 7) {
- dbuf_putc(bc_out, OP_push_0 + val);
- return;
- }
- if (val == (int8_t)val) {
- dbuf_putc(bc_out, OP_push_i8);
- dbuf_putc(bc_out, val);
- return;
- }
- if (val == (int16_t)val) {
- dbuf_putc(bc_out, OP_push_i16);
- dbuf_put_u16(bc_out, val);
- return;
- }
- #endif
- dbuf_putc(bc_out, OP_push_i32);
- dbuf_put_u32(bc_out, val);
- }
- static void put_short_code(DynBuf *bc_out, int op, int idx)
- {
- #if SHORT_OPCODES
- if (idx < 4) {
- switch (op) {
- case OP_get_loc:
- dbuf_putc(bc_out, OP_get_loc0 + idx);
- return;
- case OP_put_loc:
- dbuf_putc(bc_out, OP_put_loc0 + idx);
- return;
- case OP_set_loc:
- dbuf_putc(bc_out, OP_set_loc0 + idx);
- return;
- case OP_get_arg:
- dbuf_putc(bc_out, OP_get_arg0 + idx);
- return;
- case OP_put_arg:
- dbuf_putc(bc_out, OP_put_arg0 + idx);
- return;
- case OP_set_arg:
- dbuf_putc(bc_out, OP_set_arg0 + idx);
- return;
- case OP_get_var_ref:
- dbuf_putc(bc_out, OP_get_var_ref0 + idx);
- return;
- case OP_put_var_ref:
- dbuf_putc(bc_out, OP_put_var_ref0 + idx);
- return;
- case OP_set_var_ref:
- dbuf_putc(bc_out, OP_set_var_ref0 + idx);
- return;
- case OP_call:
- dbuf_putc(bc_out, OP_call0 + idx);
- return;
- }
- }
- if (idx < 256) {
- switch (op) {
- case OP_get_loc:
- dbuf_putc(bc_out, OP_get_loc8);
- dbuf_putc(bc_out, idx);
- return;
- case OP_put_loc:
- dbuf_putc(bc_out, OP_put_loc8);
- dbuf_putc(bc_out, idx);
- return;
- case OP_set_loc:
- dbuf_putc(bc_out, OP_set_loc8);
- dbuf_putc(bc_out, idx);
- return;
- }
- }
- #endif
- dbuf_putc(bc_out, op);
- dbuf_put_u16(bc_out, idx);
- }
- /* peephole optimizations and resolve goto/labels */
- static __exception int resolve_labels(JSContext *ctx, JSFunctionDef *s)
- {
- int pos, pos_next, bc_len, op, op1, len, i, line_num;
- const uint8_t *bc_buf;
- DynBuf bc_out;
- LabelSlot *label_slots, *ls;
- RelocEntry *re, *re_next;
- CodeContext cc;
- int label;
- #if SHORT_OPCODES
- JumpSlot *jp;
- #endif
- label_slots = s->label_slots;
- line_num = s->source_pos;
- cc.bc_buf = bc_buf = s->byte_code.buf;
- cc.bc_len = bc_len = s->byte_code.size;
- js_dbuf_bytecode_init(ctx, &bc_out);
- #if SHORT_OPCODES
- if (s->jump_size) {
- s->jump_slots = js_mallocz(s->ctx, sizeof(*s->jump_slots) * s->jump_size);
- if (s->jump_slots == NULL)
- return -1;
- }
- #endif
- /* XXX: Should skip this phase if not generating SHORT_OPCODES */
- if (s->line_number_size && !s->strip_debug) {
- s->line_number_slots = js_mallocz(s->ctx, sizeof(*s->line_number_slots) * s->line_number_size);
- if (s->line_number_slots == NULL)
- return -1;
- s->line_number_last = s->source_pos;
- s->line_number_last_pc = 0;
- }
- /* initialize the 'home_object' variable if needed */
- if (s->home_object_var_idx >= 0) {
- dbuf_putc(&bc_out, OP_special_object);
- dbuf_putc(&bc_out, OP_SPECIAL_OBJECT_HOME_OBJECT);
- put_short_code(&bc_out, OP_put_loc, s->home_object_var_idx);
- }
- /* initialize the 'this.active_func' variable if needed */
- if (s->this_active_func_var_idx >= 0) {
- dbuf_putc(&bc_out, OP_special_object);
- dbuf_putc(&bc_out, OP_SPECIAL_OBJECT_THIS_FUNC);
- put_short_code(&bc_out, OP_put_loc, s->this_active_func_var_idx);
- }
- /* initialize the 'new.target' variable if needed */
- if (s->new_target_var_idx >= 0) {
- dbuf_putc(&bc_out, OP_special_object);
- dbuf_putc(&bc_out, OP_SPECIAL_OBJECT_NEW_TARGET);
- put_short_code(&bc_out, OP_put_loc, s->new_target_var_idx);
- }
- /* initialize the 'this' variable if needed. In a derived class
- constructor, this is initially uninitialized. */
- if (s->this_var_idx >= 0) {
- if (s->is_derived_class_constructor) {
- dbuf_putc(&bc_out, OP_set_loc_uninitialized);
- dbuf_put_u16(&bc_out, s->this_var_idx);
- } else {
- dbuf_putc(&bc_out, OP_push_this);
- put_short_code(&bc_out, OP_put_loc, s->this_var_idx);
- }
- }
- /* initialize the 'arguments' variable if needed */
- if (s->arguments_var_idx >= 0) {
- if ((s->js_mode & JS_MODE_STRICT) || !s->has_simple_parameter_list) {
- dbuf_putc(&bc_out, OP_special_object);
- dbuf_putc(&bc_out, OP_SPECIAL_OBJECT_ARGUMENTS);
- } else {
- dbuf_putc(&bc_out, OP_special_object);
- dbuf_putc(&bc_out, OP_SPECIAL_OBJECT_MAPPED_ARGUMENTS);
- /* the arguments are implicitly captured because
- references to them are created with the 'argument'
- object */
- for(i = 0; i < s->arg_count; i++)
- capture_var(s, &s->args[i]);
- }
- if (s->arguments_arg_idx >= 0)
- put_short_code(&bc_out, OP_set_loc, s->arguments_arg_idx);
- put_short_code(&bc_out, OP_put_loc, s->arguments_var_idx);
- }
- /* initialize a reference to the current function if needed */
- if (s->func_var_idx >= 0) {
- dbuf_putc(&bc_out, OP_special_object);
- dbuf_putc(&bc_out, OP_SPECIAL_OBJECT_THIS_FUNC);
- put_short_code(&bc_out, OP_put_loc, s->func_var_idx);
- }
- /* initialize the variable environment object if needed */
- if (s->var_object_idx >= 0) {
- dbuf_putc(&bc_out, OP_special_object);
- dbuf_putc(&bc_out, OP_SPECIAL_OBJECT_VAR_OBJECT);
- put_short_code(&bc_out, OP_put_loc, s->var_object_idx);
- }
- if (s->arg_var_object_idx >= 0) {
- dbuf_putc(&bc_out, OP_special_object);
- dbuf_putc(&bc_out, OP_SPECIAL_OBJECT_VAR_OBJECT);
- put_short_code(&bc_out, OP_put_loc, s->arg_var_object_idx);
- }
- for (pos = 0; pos < bc_len; pos = pos_next) {
- int val;
- op = bc_buf[pos];
- len = opcode_info[op].size;
- pos_next = pos + len;
- switch(op) {
- case OP_line_num:
- /* line number info (for debug). We put it in a separate
- compressed table to reduce memory usage and get better
- performance */
- line_num = get_u32(bc_buf + pos + 1);
- break;
- case OP_label:
- {
- label = get_u32(bc_buf + pos + 1);
- assert(label >= 0 && label < s->label_count);
- ls = &label_slots[label];
- assert(ls->addr == -1);
- ls->addr = bc_out.size;
- /* resolve the relocation entries */
- for(re = ls->first_reloc; re != NULL; re = re_next) {
- int diff = ls->addr - re->addr;
- re_next = re->next;
- switch (re->size) {
- case 4:
- put_u32(bc_out.buf + re->addr, diff);
- break;
- case 2:
- assert(diff == (int16_t)diff);
- put_u16(bc_out.buf + re->addr, diff);
- break;
- case 1:
- assert(diff == (int8_t)diff);
- put_u8(bc_out.buf + re->addr, diff);
- break;
- }
- js_free(ctx, re);
- }
- ls->first_reloc = NULL;
- }
- break;
- case OP_call:
- case OP_call_method:
- {
- /* detect and transform tail calls */
- int argc;
- argc = get_u16(bc_buf + pos + 1);
- if (code_match(&cc, pos_next, OP_return, -1)) {
- if (cc.line_num >= 0) line_num = cc.line_num;
- add_pc2line_info(s, bc_out.size, line_num);
- put_short_code(&bc_out, op + 1, argc);
- pos_next = skip_dead_code(s, bc_buf, bc_len, cc.pos, &line_num);
- break;
- }
- add_pc2line_info(s, bc_out.size, line_num);
- put_short_code(&bc_out, op, argc);
- break;
- }
- goto no_change;
- case OP_return:
- case OP_return_undef:
- case OP_return_async:
- case OP_throw:
- case OP_throw_error:
- pos_next = skip_dead_code(s, bc_buf, bc_len, pos_next, &line_num);
- goto no_change;
- case OP_goto:
- label = get_u32(bc_buf + pos + 1);
- has_goto:
- if (OPTIMIZE) {
- int line1 = -1;
- /* Use custom matcher because multiple labels can follow */
- label = find_jump_target(s, label, &op1, &line1);
- if (code_has_label(&cc, pos_next, label)) {
- /* jump to next instruction: remove jump */
- update_label(s, label, -1);
- break;
- }
- if (op1 == OP_return || op1 == OP_return_undef || op1 == OP_throw) {
- /* jump to return/throw: remove jump, append return/throw */
- /* updating the line number obfuscates assembly listing */
- //if (line1 != -1) line_num = line1;
- update_label(s, label, -1);
- add_pc2line_info(s, bc_out.size, line_num);
- dbuf_putc(&bc_out, op1);
- pos_next = skip_dead_code(s, bc_buf, bc_len, pos_next, &line_num);
- break;
- }
- /* XXX: should duplicate single instructions followed by goto or return */
- /* For example, can match one of these followed by return:
- push_i32 / push_const / push_atom_value / get_var /
- undefined / null / push_false / push_true / get_ref_value /
- get_loc / get_arg / get_var_ref
- */
- }
- goto has_label;
- case OP_gosub:
- label = get_u32(bc_buf + pos + 1);
- if (0 && OPTIMIZE) {
- label = find_jump_target(s, label, &op1, NULL);
- if (op1 == OP_ret) {
- update_label(s, label, -1);
- /* empty finally clause: remove gosub */
- break;
- }
- }
- goto has_label;
- case OP_catch:
- label = get_u32(bc_buf + pos + 1);
- goto has_label;
- case OP_if_true:
- case OP_if_false:
- label = get_u32(bc_buf + pos + 1);
- if (OPTIMIZE) {
- label = find_jump_target(s, label, &op1, NULL);
- /* transform if_false/if_true(l1) label(l1) -> drop label(l1) */
- if (code_has_label(&cc, pos_next, label)) {
- update_label(s, label, -1);
- dbuf_putc(&bc_out, OP_drop);
- break;
- }
- /* transform if_false(l1) goto(l2) label(l1) -> if_false(l2) label(l1) */
- if (code_match(&cc, pos_next, OP_goto, -1)) {
- int pos1 = cc.pos;
- int line1 = cc.line_num;
- if (code_has_label(&cc, pos1, label)) {
- if (line1 != -1) line_num = line1;
- pos_next = pos1;
- update_label(s, label, -1);
- label = cc.label;
- op ^= OP_if_true ^ OP_if_false;
- }
- }
- }
- has_label:
- add_pc2line_info(s, bc_out.size, line_num);
- if (op == OP_goto) {
- pos_next = skip_dead_code(s, bc_buf, bc_len, pos_next, &line_num);
- }
- assert(label >= 0 && label < s->label_count);
- ls = &label_slots[label];
- #if SHORT_OPCODES
- jp = &s->jump_slots[s->jump_count++];
- jp->op = op;
- jp->size = 4;
- jp->pos = bc_out.size + 1;
- jp->label = label;
- if (ls->addr == -1) {
- int diff = ls->pos2 - pos - 1;
- if (diff < 128 && (op == OP_if_false || op == OP_if_true || op == OP_goto)) {
- jp->size = 1;
- jp->op = OP_if_false8 + (op - OP_if_false);
- dbuf_putc(&bc_out, OP_if_false8 + (op - OP_if_false));
- dbuf_putc(&bc_out, 0);
- if (!add_reloc(ctx, ls, bc_out.size - 1, 1))
- goto fail;
- break;
- }
- if (diff < 32768 && op == OP_goto) {
- jp->size = 2;
- jp->op = OP_goto16;
- dbuf_putc(&bc_out, OP_goto16);
- dbuf_put_u16(&bc_out, 0);
- if (!add_reloc(ctx, ls, bc_out.size - 2, 2))
- goto fail;
- break;
- }
- } else {
- int diff = ls->addr - bc_out.size - 1;
- if (diff == (int8_t)diff && (op == OP_if_false || op == OP_if_true || op == OP_goto)) {
- jp->size = 1;
- jp->op = OP_if_false8 + (op - OP_if_false);
- dbuf_putc(&bc_out, OP_if_false8 + (op - OP_if_false));
- dbuf_putc(&bc_out, diff);
- break;
- }
- if (diff == (int16_t)diff && op == OP_goto) {
- jp->size = 2;
- jp->op = OP_goto16;
- dbuf_putc(&bc_out, OP_goto16);
- dbuf_put_u16(&bc_out, diff);
- break;
- }
- }
- #endif
- dbuf_putc(&bc_out, op);
- dbuf_put_u32(&bc_out, ls->addr - bc_out.size);
- if (ls->addr == -1) {
- /* unresolved yet: create a new relocation entry */
- if (!add_reloc(ctx, ls, bc_out.size - 4, 4))
- goto fail;
- }
- break;
- case OP_with_get_var:
- case OP_with_put_var:
- case OP_with_delete_var:
- case OP_with_make_ref:
- case OP_with_get_ref:
- {
- JSAtom atom;
- int is_with;
- atom = get_u32(bc_buf + pos + 1);
- label = get_u32(bc_buf + pos + 5);
- is_with = bc_buf[pos + 9];
- if (OPTIMIZE) {
- label = find_jump_target(s, label, &op1, NULL);
- }
- assert(label >= 0 && label < s->label_count);
- ls = &label_slots[label];
- add_pc2line_info(s, bc_out.size, line_num);
- #if SHORT_OPCODES
- jp = &s->jump_slots[s->jump_count++];
- jp->op = op;
- jp->size = 4;
- jp->pos = bc_out.size + 5;
- jp->label = label;
- #endif
- dbuf_putc(&bc_out, op);
- dbuf_put_u32(&bc_out, atom);
- dbuf_put_u32(&bc_out, ls->addr - bc_out.size);
- if (ls->addr == -1) {
- /* unresolved yet: create a new relocation entry */
- if (!add_reloc(ctx, ls, bc_out.size - 4, 4))
- goto fail;
- }
- dbuf_putc(&bc_out, is_with);
- }
- break;
- case OP_drop:
- if (OPTIMIZE) {
- /* remove useless drops before return */
- if (code_match(&cc, pos_next, OP_return_undef, -1)) {
- if (cc.line_num >= 0) line_num = cc.line_num;
- break;
- }
- }
- goto no_change;
- case OP_null:
- #if SHORT_OPCODES
- if (OPTIMIZE) {
- /* transform null strict_eq into is_null */
- if (code_match(&cc, pos_next, OP_strict_eq, -1)) {
- if (cc.line_num >= 0) line_num = cc.line_num;
- add_pc2line_info(s, bc_out.size, line_num);
- dbuf_putc(&bc_out, OP_is_null);
- pos_next = cc.pos;
- break;
- }
- /* transform null strict_neq if_false/if_true -> is_null if_true/if_false */
- if (code_match(&cc, pos_next, OP_strict_neq, M2(OP_if_false, OP_if_true), -1)) {
- if (cc.line_num >= 0) line_num = cc.line_num;
- add_pc2line_info(s, bc_out.size, line_num);
- dbuf_putc(&bc_out, OP_is_null);
- pos_next = cc.pos;
- label = cc.label;
- op = cc.op ^ OP_if_false ^ OP_if_true;
- goto has_label;
- }
- }
- #endif
- /* fall thru */
- case OP_push_false:
- case OP_push_true:
- if (OPTIMIZE) {
- val = (op == OP_push_true);
- if (code_match(&cc, pos_next, M2(OP_if_false, OP_if_true), -1)) {
- has_constant_test:
- if (cc.line_num >= 0) line_num = cc.line_num;
- if (val == cc.op - OP_if_false) {
- /* transform null if_false(l1) -> goto l1 */
- /* transform false if_false(l1) -> goto l1 */
- /* transform true if_true(l1) -> goto l1 */
- pos_next = cc.pos;
- op = OP_goto;
- label = cc.label;
- goto has_goto;
- } else {
- /* transform null if_true(l1) -> nop */
- /* transform false if_true(l1) -> nop */
- /* transform true if_false(l1) -> nop */
- pos_next = cc.pos;
- update_label(s, cc.label, -1);
- break;
- }
- }
- }
- goto no_change;
- case OP_push_i32:
- if (OPTIMIZE) {
- /* transform i32(val) neg -> i32(-val) */
- val = get_i32(bc_buf + pos + 1);
- if ((val != INT32_MIN && val != 0)
- && code_match(&cc, pos_next, OP_neg, -1)) {
- if (cc.line_num >= 0) line_num = cc.line_num;
- if (code_match(&cc, cc.pos, OP_drop, -1)) {
- if (cc.line_num >= 0) line_num = cc.line_num;
- } else {
- add_pc2line_info(s, bc_out.size, line_num);
- push_short_int(&bc_out, -val);
- }
- pos_next = cc.pos;
- break;
- }
- /* remove push/drop pairs generated by the parser */
- if (code_match(&cc, pos_next, OP_drop, -1)) {
- if (cc.line_num >= 0) line_num = cc.line_num;
- pos_next = cc.pos;
- break;
- }
- /* Optimize constant tests: `if (0)`, `if (1)`, `if (!0)`... */
- if (code_match(&cc, pos_next, M2(OP_if_false, OP_if_true), -1)) {
- val = (val != 0);
- goto has_constant_test;
- }
- add_pc2line_info(s, bc_out.size, line_num);
- push_short_int(&bc_out, val);
- break;
- }
- goto no_change;
- case OP_push_bigint_i32:
- if (OPTIMIZE) {
- /* transform i32(val) neg -> i32(-val) */
- val = get_i32(bc_buf + pos + 1);
- if (val != INT32_MIN
- && code_match(&cc, pos_next, OP_neg, -1)) {
- if (cc.line_num >= 0) line_num = cc.line_num;
- if (code_match(&cc, cc.pos, OP_drop, -1)) {
- if (cc.line_num >= 0) line_num = cc.line_num;
- } else {
- add_pc2line_info(s, bc_out.size, line_num);
- dbuf_putc(&bc_out, OP_push_bigint_i32);
- dbuf_put_u32(&bc_out, -val);
- }
- pos_next = cc.pos;
- break;
- }
- }
- goto no_change;
- #if SHORT_OPCODES
- case OP_push_const:
- case OP_fclosure:
- if (OPTIMIZE) {
- int idx = get_u32(bc_buf + pos + 1);
- if (idx < 256) {
- add_pc2line_info(s, bc_out.size, line_num);
- dbuf_putc(&bc_out, OP_push_const8 + op - OP_push_const);
- dbuf_putc(&bc_out, idx);
- break;
- }
- }
- goto no_change;
- case OP_get_field:
- if (OPTIMIZE) {
- JSAtom atom = get_u32(bc_buf + pos + 1);
- if (atom == JS_ATOM_length) {
- JS_FreeAtom(ctx, atom);
- add_pc2line_info(s, bc_out.size, line_num);
- dbuf_putc(&bc_out, OP_get_length);
- break;
- }
- }
- goto no_change;
- #endif
- case OP_push_atom_value:
- if (OPTIMIZE) {
- JSAtom atom = get_u32(bc_buf + pos + 1);
- /* remove push/drop pairs generated by the parser */
- if (code_match(&cc, pos_next, OP_drop, -1)) {
- JS_FreeAtom(ctx, atom);
- if (cc.line_num >= 0) line_num = cc.line_num;
- pos_next = cc.pos;
- break;
- }
- #if SHORT_OPCODES
- if (atom == JS_ATOM_empty_string) {
- JS_FreeAtom(ctx, atom);
- add_pc2line_info(s, bc_out.size, line_num);
- dbuf_putc(&bc_out, OP_push_empty_string);
- break;
- }
- #endif
- }
- goto no_change;
- case OP_to_propkey:
- if (OPTIMIZE) {
- /* remove redundant to_propkey opcodes when storing simple data */
- if (code_match(&cc, pos_next, M3(OP_get_loc, OP_get_arg, OP_get_var_ref), -1, OP_put_array_el, -1)
- || code_match(&cc, pos_next, M3(OP_push_i32, OP_push_const, OP_push_atom_value), OP_put_array_el, -1)
- || code_match(&cc, pos_next, M4(OP_undefined, OP_null, OP_push_true, OP_push_false), OP_put_array_el, -1)) {
- break;
- }
- }
- goto no_change;
- case OP_undefined:
- if (OPTIMIZE) {
- /* remove push/drop pairs generated by the parser */
- if (code_match(&cc, pos_next, OP_drop, -1)) {
- if (cc.line_num >= 0) line_num = cc.line_num;
- pos_next = cc.pos;
- break;
- }
- /* transform undefined return -> return_undefined */
- if (code_match(&cc, pos_next, OP_return, -1)) {
- if (cc.line_num >= 0) line_num = cc.line_num;
- add_pc2line_info(s, bc_out.size, line_num);
- dbuf_putc(&bc_out, OP_return_undef);
- pos_next = cc.pos;
- break;
- }
- /* transform undefined if_true(l1)/if_false(l1) -> nop/goto(l1) */
- if (code_match(&cc, pos_next, M2(OP_if_false, OP_if_true), -1)) {
- val = 0;
- goto has_constant_test;
- }
- #if SHORT_OPCODES
- /* transform undefined strict_eq -> is_undefined */
- if (code_match(&cc, pos_next, OP_strict_eq, -1)) {
- if (cc.line_num >= 0) line_num = cc.line_num;
- add_pc2line_info(s, bc_out.size, line_num);
- dbuf_putc(&bc_out, OP_is_undefined);
- pos_next = cc.pos;
- break;
- }
- /* transform undefined strict_neq if_false/if_true -> is_undefined if_true/if_false */
- if (code_match(&cc, pos_next, OP_strict_neq, M2(OP_if_false, OP_if_true), -1)) {
- if (cc.line_num >= 0) line_num = cc.line_num;
- add_pc2line_info(s, bc_out.size, line_num);
- dbuf_putc(&bc_out, OP_is_undefined);
- pos_next = cc.pos;
- label = cc.label;
- op = cc.op ^ OP_if_false ^ OP_if_true;
- goto has_label;
- }
- #endif
- }
- goto no_change;
- case OP_insert2:
- if (OPTIMIZE) {
- /* Transformation:
- insert2 put_field(a) drop -> put_field(a)
- */
- if (code_match(&cc, pos_next, OP_put_field, OP_drop, -1)) {
- if (cc.line_num >= 0) line_num = cc.line_num;
- add_pc2line_info(s, bc_out.size, line_num);
- dbuf_putc(&bc_out, OP_put_field);
- dbuf_put_u32(&bc_out, cc.atom);
- pos_next = cc.pos;
- break;
- }
- }
- goto no_change;
- case OP_dup:
- if (OPTIMIZE) {
- /* Transformation: dup put_x(n) drop -> put_x(n) */
- int op1, line2 = -1;
- /* Transformation: dup put_x(n) -> set_x(n) */
- if (code_match(&cc, pos_next, M4(OP_put_loc, OP_put_loc_check, OP_put_arg, OP_put_var_ref), -1, -1)) {
- if (cc.line_num >= 0) line_num = cc.line_num;
- op1 = cc.op + 1; /* put_x -> set_x */
- pos_next = cc.pos;
- if (code_match(&cc, cc.pos, OP_drop, -1)) {
- if (cc.line_num >= 0) line_num = cc.line_num;
- op1 -= 1; /* set_x drop -> put_x */
- pos_next = cc.pos;
- if (code_match(&cc, cc.pos, op1 - 1, cc.idx, -1)) {
- line2 = cc.line_num; /* delay line number update */
- op1 += 1; /* put_x(n) get_x(n) -> set_x(n) */
- pos_next = cc.pos;
- }
- }
- add_pc2line_info(s, bc_out.size, line_num);
- put_short_code(&bc_out, op1, cc.idx);
- if (line2 >= 0) line_num = line2;
- break;
- }
- }
- goto no_change;
- case OP_get_loc:
- if (OPTIMIZE) {
- /* transformation:
- get_loc(n) post_dec put_loc(n) drop -> dec_loc(n)
- get_loc(n) post_inc put_loc(n) drop -> inc_loc(n)
- get_loc(n) dec dup put_loc(n) drop -> dec_loc(n)
- get_loc(n) inc dup put_loc(n) drop -> inc_loc(n)
- */
- int idx;
- idx = get_u16(bc_buf + pos + 1);
- if (idx >= 256)
- goto no_change;
- if (code_match(&cc, pos_next, M2(OP_post_dec, OP_post_inc), OP_put_loc, idx, OP_drop, -1) ||
- code_match(&cc, pos_next, M2(OP_dec, OP_inc), OP_dup, OP_put_loc, idx, OP_drop, -1)) {
- if (cc.line_num >= 0) line_num = cc.line_num;
- add_pc2line_info(s, bc_out.size, line_num);
- dbuf_putc(&bc_out, (cc.op == OP_inc || cc.op == OP_post_inc) ? OP_inc_loc : OP_dec_loc);
- dbuf_putc(&bc_out, idx);
- pos_next = cc.pos;
- break;
- }
- /* transformation:
- get_loc(n) push_atom_value(x) add dup put_loc(n) drop -> push_atom_value(x) add_loc(n)
- */
- if (code_match(&cc, pos_next, OP_push_atom_value, OP_add, OP_dup, OP_put_loc, idx, OP_drop, -1)) {
- if (cc.line_num >= 0) line_num = cc.line_num;
- add_pc2line_info(s, bc_out.size, line_num);
- #if SHORT_OPCODES
- if (cc.atom == JS_ATOM_empty_string) {
- JS_FreeAtom(ctx, cc.atom);
- dbuf_putc(&bc_out, OP_push_empty_string);
- } else
- #endif
- {
- dbuf_putc(&bc_out, OP_push_atom_value);
- dbuf_put_u32(&bc_out, cc.atom);
- }
- dbuf_putc(&bc_out, OP_add_loc);
- dbuf_putc(&bc_out, idx);
- pos_next = cc.pos;
- break;
- }
- /* transformation:
- get_loc(n) push_i32(x) add dup put_loc(n) drop -> push_i32(x) add_loc(n)
- */
- if (code_match(&cc, pos_next, OP_push_i32, OP_add, OP_dup, OP_put_loc, idx, OP_drop, -1)) {
- if (cc.line_num >= 0) line_num = cc.line_num;
- add_pc2line_info(s, bc_out.size, line_num);
- push_short_int(&bc_out, cc.label);
- dbuf_putc(&bc_out, OP_add_loc);
- dbuf_putc(&bc_out, idx);
- pos_next = cc.pos;
- break;
- }
- /* transformation: XXX: also do these:
- get_loc(n) get_loc(x) add dup put_loc(n) drop -> get_loc(x) add_loc(n)
- get_loc(n) get_arg(x) add dup put_loc(n) drop -> get_arg(x) add_loc(n)
- get_loc(n) get_var_ref(x) add dup put_loc(n) drop -> get_var_ref(x) add_loc(n)
- */
- if (code_match(&cc, pos_next, M3(OP_get_loc, OP_get_arg, OP_get_var_ref), -1, OP_add, OP_dup, OP_put_loc, idx, OP_drop, -1)) {
- if (cc.line_num >= 0) line_num = cc.line_num;
- add_pc2line_info(s, bc_out.size, line_num);
- put_short_code(&bc_out, cc.op, cc.idx);
- dbuf_putc(&bc_out, OP_add_loc);
- dbuf_putc(&bc_out, idx);
- pos_next = cc.pos;
- break;
- }
- add_pc2line_info(s, bc_out.size, line_num);
- put_short_code(&bc_out, op, idx);
- break;
- }
- goto no_change;
- #if SHORT_OPCODES
- case OP_get_arg:
- case OP_get_var_ref:
- if (OPTIMIZE) {
- int idx;
- idx = get_u16(bc_buf + pos + 1);
- add_pc2line_info(s, bc_out.size, line_num);
- put_short_code(&bc_out, op, idx);
- break;
- }
- goto no_change;
- #endif
- case OP_put_loc:
- case OP_put_loc_check:
- case OP_put_arg:
- case OP_put_var_ref:
- if (OPTIMIZE) {
- /* transformation: put_x(n) get_x(n) -> set_x(n) */
- int idx;
- idx = get_u16(bc_buf + pos + 1);
- if (code_match(&cc, pos_next, op - 1, idx, -1)) {
- if (cc.line_num >= 0) line_num = cc.line_num;
- add_pc2line_info(s, bc_out.size, line_num);
- put_short_code(&bc_out, op + 1, idx);
- pos_next = cc.pos;
- break;
- }
- add_pc2line_info(s, bc_out.size, line_num);
- put_short_code(&bc_out, op, idx);
- break;
- }
- goto no_change;
- case OP_post_inc:
- case OP_post_dec:
- if (OPTIMIZE) {
- /* transformation:
- post_inc put_x drop -> inc put_x
- post_inc perm3 put_field drop -> inc put_field
- post_inc perm4 put_array_el drop -> inc put_array_el
- */
- int op1, idx;
- if (code_match(&cc, pos_next, M3(OP_put_loc, OP_put_arg, OP_put_var_ref), -1, OP_drop, -1)) {
- if (cc.line_num >= 0) line_num = cc.line_num;
- op1 = cc.op;
- idx = cc.idx;
- pos_next = cc.pos;
- if (code_match(&cc, cc.pos, op1 - 1, idx, -1)) {
- if (cc.line_num >= 0) line_num = cc.line_num;
- op1 += 1; /* put_x(n) get_x(n) -> set_x(n) */
- pos_next = cc.pos;
- }
- add_pc2line_info(s, bc_out.size, line_num);
- dbuf_putc(&bc_out, OP_dec + (op - OP_post_dec));
- put_short_code(&bc_out, op1, idx);
- break;
- }
- if (code_match(&cc, pos_next, OP_perm3, OP_put_field, OP_drop, -1)) {
- if (cc.line_num >= 0) line_num = cc.line_num;
- add_pc2line_info(s, bc_out.size, line_num);
- dbuf_putc(&bc_out, OP_dec + (op - OP_post_dec));
- dbuf_putc(&bc_out, OP_put_field);
- dbuf_put_u32(&bc_out, cc.atom);
- pos_next = cc.pos;
- break;
- }
- if (code_match(&cc, pos_next, OP_perm4, OP_put_array_el, OP_drop, -1)) {
- if (cc.line_num >= 0) line_num = cc.line_num;
- add_pc2line_info(s, bc_out.size, line_num);
- dbuf_putc(&bc_out, OP_dec + (op - OP_post_dec));
- dbuf_putc(&bc_out, OP_put_array_el);
- pos_next = cc.pos;
- break;
- }
- }
- goto no_change;
- #if SHORT_OPCODES
- case OP_typeof:
- if (OPTIMIZE) {
- /* simplify typeof tests */
- if (code_match(&cc, pos_next, OP_push_atom_value, M4(OP_strict_eq, OP_strict_neq, OP_eq, OP_neq), -1)) {
- if (cc.line_num >= 0) line_num = cc.line_num;
- int op1 = (cc.op == OP_strict_eq || cc.op == OP_eq) ? OP_strict_eq : OP_strict_neq;
- int op2 = -1;
- switch (cc.atom) {
- case JS_ATOM_undefined:
- op2 = OP_typeof_is_undefined;
- break;
- case JS_ATOM_function:
- op2 = OP_typeof_is_function;
- break;
- }
- if (op2 >= 0) {
- /* transform typeof(s) == "<type>" into is_<type> */
- if (op1 == OP_strict_eq) {
- add_pc2line_info(s, bc_out.size, line_num);
- dbuf_putc(&bc_out, op2);
- JS_FreeAtom(ctx, cc.atom);
- pos_next = cc.pos;
- break;
- }
- if (op1 == OP_strict_neq && code_match(&cc, cc.pos, OP_if_false, -1)) {
- /* transform typeof(s) != "<type>" if_false into is_<type> if_true */
- if (cc.line_num >= 0) line_num = cc.line_num;
- add_pc2line_info(s, bc_out.size, line_num);
- dbuf_putc(&bc_out, op2);
- JS_FreeAtom(ctx, cc.atom);
- pos_next = cc.pos;
- label = cc.label;
- op = OP_if_true;
- goto has_label;
- }
- }
- }
- }
- goto no_change;
- #endif
- default:
- no_change:
- add_pc2line_info(s, bc_out.size, line_num);
- dbuf_put(&bc_out, bc_buf + pos, len);
- break;
- }
- }
- /* check that there were no missing labels */
- for(i = 0; i < s->label_count; i++) {
- assert(label_slots[i].first_reloc == NULL);
- }
- #if SHORT_OPCODES
- if (OPTIMIZE) {
- /* more jump optimizations */
- int patch_offsets = 0;
- for (i = 0, jp = s->jump_slots; i < s->jump_count; i++, jp++) {
- LabelSlot *ls;
- JumpSlot *jp1;
- int j, pos, diff, delta;
- delta = 3;
- switch (op = jp->op) {
- case OP_goto16:
- delta = 1;
- /* fall thru */
- case OP_if_false:
- case OP_if_true:
- case OP_goto:
- pos = jp->pos;
- diff = s->label_slots[jp->label].addr - pos;
- if (diff >= -128 && diff <= 127 + delta) {
- //put_u8(bc_out.buf + pos, diff);
- jp->size = 1;
- if (op == OP_goto16) {
- bc_out.buf[pos - 1] = jp->op = OP_goto8;
- } else {
- bc_out.buf[pos - 1] = jp->op = OP_if_false8 + (op - OP_if_false);
- }
- goto shrink;
- } else
- if (diff == (int16_t)diff && op == OP_goto) {
- //put_u16(bc_out.buf + pos, diff);
- jp->size = 2;
- delta = 2;
- bc_out.buf[pos - 1] = jp->op = OP_goto16;
- shrink:
- /* XXX: should reduce complexity, using 2 finger copy scheme */
- memmove(bc_out.buf + pos + jp->size, bc_out.buf + pos + jp->size + delta,
- bc_out.size - pos - jp->size - delta);
- bc_out.size -= delta;
- patch_offsets++;
- for (j = 0, ls = s->label_slots; j < s->label_count; j++, ls++) {
- if (ls->addr > pos)
- ls->addr -= delta;
- }
- for (j = i + 1, jp1 = jp + 1; j < s->jump_count; j++, jp1++) {
- if (jp1->pos > pos)
- jp1->pos -= delta;
- }
- for (j = 0; j < s->line_number_count; j++) {
- if (s->line_number_slots[j].pc > pos)
- s->line_number_slots[j].pc -= delta;
- }
- continue;
- }
- break;
- }
- }
- if (patch_offsets) {
- JumpSlot *jp1;
- int j;
- for (j = 0, jp1 = s->jump_slots; j < s->jump_count; j++, jp1++) {
- int diff1 = s->label_slots[jp1->label].addr - jp1->pos;
- switch (jp1->size) {
- case 1:
- put_u8(bc_out.buf + jp1->pos, diff1);
- break;
- case 2:
- put_u16(bc_out.buf + jp1->pos, diff1);
- break;
- case 4:
- put_u32(bc_out.buf + jp1->pos, diff1);
- break;
- }
- }
- }
- }
- js_free(ctx, s->jump_slots);
- s->jump_slots = NULL;
- #endif
- js_free(ctx, s->label_slots);
- s->label_slots = NULL;
- /* XXX: should delay until copying to runtime bytecode function */
- compute_pc2line_info(s);
- js_free(ctx, s->line_number_slots);
- s->line_number_slots = NULL;
- /* set the new byte code */
- dbuf_free(&s->byte_code);
- s->byte_code = bc_out;
- s->use_short_opcodes = TRUE;
- if (dbuf_error(&s->byte_code)) {
- JS_ThrowOutOfMemory(ctx);
- return -1;
- }
- return 0;
- fail:
- /* XXX: not safe */
- dbuf_free(&bc_out);
- return -1;
- }
- /* compute the maximum stack size needed by the function */
- typedef struct StackSizeState {
- int bc_len;
- int stack_len_max;
- uint16_t *stack_level_tab;
- int32_t *catch_pos_tab;
- int *pc_stack;
- int pc_stack_len;
- int pc_stack_size;
- } StackSizeState;
- /* 'op' is only used for error indication */
- static __exception int ss_check(JSContext *ctx, StackSizeState *s,
- int pos, int op, int stack_len, int catch_pos)
- {
- if ((unsigned)pos >= s->bc_len) {
- JS_ThrowInternalError(ctx, "bytecode buffer overflow (op=%d, pc=%d)", op, pos);
- return -1;
- }
- if (stack_len > s->stack_len_max) {
- s->stack_len_max = stack_len;
- if (s->stack_len_max > JS_STACK_SIZE_MAX) {
- JS_ThrowInternalError(ctx, "stack overflow (op=%d, pc=%d)", op, pos);
- return -1;
- }
- }
- if (s->stack_level_tab[pos] != 0xffff) {
- /* already explored: check that the stack size is consistent */
- if (s->stack_level_tab[pos] != stack_len) {
- JS_ThrowInternalError(ctx, "inconsistent stack size: %d %d (pc=%d)",
- s->stack_level_tab[pos], stack_len, pos);
- return -1;
- } else if (s->catch_pos_tab[pos] != catch_pos) {
- JS_ThrowInternalError(ctx, "inconsistent catch position: %d %d (pc=%d)",
- s->catch_pos_tab[pos], catch_pos, pos);
- return -1;
- } else {
- return 0;
- }
- }
- /* mark as explored and store the stack size */
- s->stack_level_tab[pos] = stack_len;
- s->catch_pos_tab[pos] = catch_pos;
- /* queue the new PC to explore */
- if (js_resize_array(ctx, (void **)&s->pc_stack, sizeof(s->pc_stack[0]),
- &s->pc_stack_size, s->pc_stack_len + 1))
- return -1;
- s->pc_stack[s->pc_stack_len++] = pos;
- return 0;
- }
- static __exception int compute_stack_size(JSContext *ctx,
- JSFunctionDef *fd,
- int *pstack_size)
- {
- StackSizeState s_s, *s = &s_s;
- int i, diff, n_pop, pos_next, stack_len, pos, op, catch_pos, catch_level;
- const JSOpCode *oi;
- const uint8_t *bc_buf;
- bc_buf = fd->byte_code.buf;
- s->bc_len = fd->byte_code.size;
- /* bc_len > 0 */
- s->stack_level_tab = js_malloc(ctx, sizeof(s->stack_level_tab[0]) *
- s->bc_len);
- if (!s->stack_level_tab)
- return -1;
- for(i = 0; i < s->bc_len; i++)
- s->stack_level_tab[i] = 0xffff;
- s->pc_stack = NULL;
- s->catch_pos_tab = js_malloc(ctx, sizeof(s->catch_pos_tab[0]) *
- s->bc_len);
- if (!s->catch_pos_tab)
- goto fail;
- s->stack_len_max = 0;
- s->pc_stack_len = 0;
- s->pc_stack_size = 0;
- /* breadth-first graph exploration */
- if (ss_check(ctx, s, 0, OP_invalid, 0, -1))
- goto fail;
- while (s->pc_stack_len > 0) {
- pos = s->pc_stack[--s->pc_stack_len];
- stack_len = s->stack_level_tab[pos];
- catch_pos = s->catch_pos_tab[pos];
- op = bc_buf[pos];
- if (op == 0 || op >= OP_COUNT) {
- JS_ThrowInternalError(ctx, "invalid opcode (op=%d, pc=%d)", op, pos);
- goto fail;
- }
- oi = &short_opcode_info(op);
- #if defined(DUMP_BYTECODE) && (DUMP_BYTECODE & 64)
- printf("%5d: %10s %5d %5d\n", pos, oi->name, stack_len, catch_pos);
- #endif
- pos_next = pos + oi->size;
- if (pos_next > s->bc_len) {
- JS_ThrowInternalError(ctx, "bytecode buffer overflow (op=%d, pc=%d)", op, pos);
- goto fail;
- }
- n_pop = oi->n_pop;
- /* call pops a variable number of arguments */
- if (oi->fmt == OP_FMT_npop || oi->fmt == OP_FMT_npop_u16) {
- n_pop += get_u16(bc_buf + pos + 1);
- } else {
- #if SHORT_OPCODES
- if (oi->fmt == OP_FMT_npopx) {
- n_pop += op - OP_call0;
- }
- #endif
- }
- if (stack_len < n_pop) {
- JS_ThrowInternalError(ctx, "stack underflow (op=%d, pc=%d)", op, pos);
- goto fail;
- }
- stack_len += oi->n_push - n_pop;
- if (stack_len > s->stack_len_max) {
- s->stack_len_max = stack_len;
- if (s->stack_len_max > JS_STACK_SIZE_MAX) {
- JS_ThrowInternalError(ctx, "stack overflow (op=%d, pc=%d)", op, pos);
- goto fail;
- }
- }
- switch(op) {
- case OP_tail_call:
- case OP_tail_call_method:
- case OP_return:
- case OP_return_undef:
- case OP_return_async:
- case OP_throw:
- case OP_throw_error:
- case OP_ret:
- goto done_insn;
- case OP_goto:
- diff = get_u32(bc_buf + pos + 1);
- pos_next = pos + 1 + diff;
- break;
- #if SHORT_OPCODES
- case OP_goto16:
- diff = (int16_t)get_u16(bc_buf + pos + 1);
- pos_next = pos + 1 + diff;
- break;
- case OP_goto8:
- diff = (int8_t)bc_buf[pos + 1];
- pos_next = pos + 1 + diff;
- break;
- case OP_if_true8:
- case OP_if_false8:
- diff = (int8_t)bc_buf[pos + 1];
- if (ss_check(ctx, s, pos + 1 + diff, op, stack_len, catch_pos))
- goto fail;
- break;
- #endif
- case OP_if_true:
- case OP_if_false:
- diff = get_u32(bc_buf + pos + 1);
- if (ss_check(ctx, s, pos + 1 + diff, op, stack_len, catch_pos))
- goto fail;
- break;
- case OP_gosub:
- diff = get_u32(bc_buf + pos + 1);
- if (ss_check(ctx, s, pos + 1 + diff, op, stack_len + 1, catch_pos))
- goto fail;
- break;
- case OP_with_get_var:
- case OP_with_delete_var:
- diff = get_u32(bc_buf + pos + 5);
- if (ss_check(ctx, s, pos + 5 + diff, op, stack_len + 1, catch_pos))
- goto fail;
- break;
- case OP_with_make_ref:
- case OP_with_get_ref:
- diff = get_u32(bc_buf + pos + 5);
- if (ss_check(ctx, s, pos + 5 + diff, op, stack_len + 2, catch_pos))
- goto fail;
- break;
- case OP_with_put_var:
- diff = get_u32(bc_buf + pos + 5);
- if (ss_check(ctx, s, pos + 5 + diff, op, stack_len - 1, catch_pos))
- goto fail;
- break;
- case OP_catch:
- diff = get_u32(bc_buf + pos + 1);
- if (ss_check(ctx, s, pos + 1 + diff, op, stack_len, catch_pos))
- goto fail;
- catch_pos = pos;
- break;
- case OP_for_of_start:
- case OP_for_await_of_start:
- catch_pos = pos;
- break;
- /* we assume the catch offset entry is only removed with
- some op codes */
- case OP_drop:
- catch_level = stack_len;
- goto check_catch;
- case OP_nip:
- catch_level = stack_len - 1;
- goto check_catch;
- case OP_nip1:
- catch_level = stack_len - 1;
- goto check_catch;
- case OP_iterator_close:
- catch_level = stack_len + 2;
- check_catch:
- /* Note: for for_of_start/for_await_of_start we consider
- the catch offset is on the first stack entry instead of
- the thirst */
- if (catch_pos >= 0) {
- int level;
- level = s->stack_level_tab[catch_pos];
- if (bc_buf[catch_pos] != OP_catch)
- level++; /* for_of_start, for_wait_of_start */
- /* catch_level = stack_level before op_catch is executed ? */
- if (catch_level == level) {
- catch_pos = s->catch_pos_tab[catch_pos];
- }
- }
- break;
- case OP_nip_catch:
- if (catch_pos < 0) {
- JS_ThrowInternalError(ctx, "nip_catch: no catch op (pc=%d)", pos);
- goto fail;
- }
- stack_len = s->stack_level_tab[catch_pos];
- if (bc_buf[catch_pos] != OP_catch)
- stack_len++; /* for_of_start, for_wait_of_start */
- stack_len++; /* no stack overflow is possible by construction */
- catch_pos = s->catch_pos_tab[catch_pos];
- break;
- default:
- break;
- }
- if (ss_check(ctx, s, pos_next, op, stack_len, catch_pos))
- goto fail;
- done_insn: ;
- }
- js_free(ctx, s->pc_stack);
- js_free(ctx, s->catch_pos_tab);
- js_free(ctx, s->stack_level_tab);
- *pstack_size = s->stack_len_max;
- return 0;
- fail:
- js_free(ctx, s->pc_stack);
- js_free(ctx, s->catch_pos_tab);
- js_free(ctx, s->stack_level_tab);
- *pstack_size = 0;
- return -1;
- }
- static int add_global_variables(JSContext *ctx, JSFunctionDef *fd)
- {
- int i, idx;
- JSModuleDef *m = fd->module;
- JSExportEntry *me;
- JSGlobalVar *hf;
- BOOL need_global_closures;
-
- /* Script: add the defined global variables. In the non strict
- direct eval not in global scope, the global variables are
- created in the enclosing scope so they are not created as
- variable references.
- In modules, the imported global variables were added as closure
- global variables in js_parse_import().
- */
- need_global_closures = TRUE;
- if (fd->eval_type == JS_EVAL_TYPE_DIRECT && !(fd->js_mode & JS_MODE_STRICT)) {
- /* XXX: add a flag ? */
- for(idx = 0; idx < fd->closure_var_count; idx++) {
- JSClosureVar *cv = &fd->closure_var[idx];
- if (cv->var_name == JS_ATOM__var_ ||
- cv->var_name == JS_ATOM__arg_var_) {
- need_global_closures = FALSE;
- break;
- }
- }
- }
- if (need_global_closures) {
- JSClosureTypeEnum closure_type;
- if (fd->module)
- closure_type = JS_CLOSURE_MODULE_DECL;
- else
- closure_type = JS_CLOSURE_GLOBAL_DECL;
- for(i = 0; i < fd->global_var_count; i++) {
- JSVarKindEnum var_kind;
- hf = &fd->global_vars[i];
- if (hf->cpool_idx >= 0 && !hf->is_lexical) {
- var_kind = JS_VAR_GLOBAL_FUNCTION_DECL;
- } else {
- var_kind = JS_VAR_NORMAL;
- }
- if (add_closure_var(ctx, fd, closure_type, i, hf->var_name, hf->is_const,
- hf->is_lexical, var_kind) < 0)
- return -1;
- }
- }
- if (fd->module) {
- /* resolve the variable names of the local exports */
- for(i = 0; i < m->export_entries_count; i++) {
- me = &m->export_entries[i];
- if (me->export_type == JS_EXPORT_TYPE_LOCAL) {
- idx = find_closure_var(ctx, fd, me->local_name);
- if (idx < 0) {
- JS_ThrowSyntaxErrorAtom(ctx, "exported variable '%s' does not exist",
- me->local_name);
- return -1;
- }
- me->u.local.var_idx = idx;
- }
- }
- }
- return 0;
- }
- /* create a function object from a function definition. The function
- definition is freed. All the child functions are also created. It
- must be done this way to resolve all the variables. */
- static JSValue js_create_function(JSContext *ctx, JSFunctionDef *fd)
- {
- JSValue func_obj;
- JSFunctionBytecode *b;
- struct list_head *el, *el1;
- int stack_size, scope, idx;
- int function_size, byte_code_offset, cpool_offset;
- int closure_var_offset, vardefs_offset;
- BOOL strip_var_debug;
-
- /* recompute scope linkage */
- for (scope = 0; scope < fd->scope_count; scope++) {
- fd->scopes[scope].first = -1;
- }
- if (fd->has_parameter_expressions) {
- /* special end of variable list marker for the argument scope */
- fd->scopes[ARG_SCOPE_INDEX].first = ARG_SCOPE_END;
- }
- for (idx = 0; idx < fd->var_count; idx++) {
- JSVarDef *vd = &fd->vars[idx];
- vd->scope_next = fd->scopes[vd->scope_level].first;
- fd->scopes[vd->scope_level].first = idx;
- }
- for (scope = 2; scope < fd->scope_count; scope++) {
- JSVarScope *sd = &fd->scopes[scope];
- if (sd->first < 0)
- sd->first = fd->scopes[sd->parent].first;
- }
- for (idx = 0; idx < fd->var_count; idx++) {
- JSVarDef *vd = &fd->vars[idx];
- if (vd->scope_next < 0 && vd->scope_level > 1) {
- scope = fd->scopes[vd->scope_level].parent;
- vd->scope_next = fd->scopes[scope].first;
- }
- }
- /* if the function contains an eval call, the closure variables
- are used to compile the eval and they must be ordered by scope,
- so it is necessary to create the closure variables before any
- other variable lookup is done. */
- if (fd->has_eval_call)
- add_eval_variables(ctx, fd);
- /* add the module global variables in the closure */
- if (fd->is_eval) {
- if (add_global_variables(ctx, fd))
- goto fail;
- }
- /* first create all the child functions */
- list_for_each_safe(el, el1, &fd->child_list) {
- JSFunctionDef *fd1;
- int cpool_idx;
- fd1 = list_entry(el, JSFunctionDef, link);
- cpool_idx = fd1->parent_cpool_idx;
- func_obj = js_create_function(ctx, fd1);
- if (JS_IsException(func_obj))
- goto fail;
- /* save it in the constant pool */
- assert(cpool_idx >= 0);
- fd->cpool[cpool_idx] = func_obj;
- }
- #if defined(DUMP_BYTECODE) && (DUMP_BYTECODE & 4)
- if (!fd->strip_debug) {
- printf("pass 1\n");
- dump_byte_code(ctx, 1, fd->byte_code.buf, fd->byte_code.size,
- NULL, fd->args, fd->arg_count, fd->vars, fd->var_count,
- fd->closure_var, fd->closure_var_count,
- fd->cpool, fd->cpool_count, fd->source,
- fd->label_slots, NULL);
- printf("\n");
- }
- #endif
- if (resolve_variables(ctx, fd))
- goto fail;
- #if defined(DUMP_BYTECODE) && (DUMP_BYTECODE & 2)
- if (!fd->strip_debug) {
- printf("pass 2\n");
- dump_byte_code(ctx, 2, fd->byte_code.buf, fd->byte_code.size,
- NULL, fd->args, fd->arg_count, fd->vars, fd->var_count,
- fd->closure_var, fd->closure_var_count,
- fd->cpool, fd->cpool_count, fd->source,
- fd->label_slots, NULL);
- printf("\n");
- }
- #endif
- if (resolve_labels(ctx, fd))
- goto fail;
- if (compute_stack_size(ctx, fd, &stack_size) < 0)
- goto fail;
- if (fd->strip_debug) {
- function_size = offsetof(JSFunctionBytecode, debug);
- } else {
- function_size = sizeof(*b);
- }
- cpool_offset = function_size;
- function_size += fd->cpool_count * sizeof(*fd->cpool);
- vardefs_offset = function_size;
- function_size += (fd->arg_count + fd->var_count) * sizeof(*b->vardefs);
- closure_var_offset = function_size;
- function_size += fd->closure_var_count * sizeof(*fd->closure_var);
- byte_code_offset = function_size;
- function_size += fd->byte_code.size;
- b = js_mallocz(ctx, function_size);
- if (!b)
- goto fail;
- js_rc(b)->ref_count = 1;
- b->byte_code_buf = (void *)((uint8_t*)b + byte_code_offset);
- b->byte_code_len = fd->byte_code.size;
- memcpy(b->byte_code_buf, fd->byte_code.buf, fd->byte_code.size);
- js_free(ctx, fd->byte_code.buf);
- fd->byte_code.buf = NULL;
- strip_var_debug = fd->strip_debug && !fd->has_eval_call; /* XXX: check */
- b->func_name = fd->func_name;
- if (fd->arg_count + fd->var_count > 0) {
- int i;
- b->vardefs = (void *)((uint8_t*)b + vardefs_offset);
- for(i = 0; i < fd->arg_count; i++) {
- JSVarDef *vd = &fd->args[i];
- JSBytecodeVarDef *vd1 = &b->vardefs[i];
- if (strip_var_debug) {
- JS_FreeAtom(ctx, vd->var_name);
- vd1->var_name = JS_ATOM_NULL;
- } else {
- vd1->var_name = vd->var_name;
- }
- vd1->has_scope = (vd->scope_level != 0);
- vd1->scope_next = vd->scope_next;
- vd1->is_const = vd->is_const;
- vd1->is_lexical = vd->is_lexical;
- vd1->is_captured = vd->is_captured;
- vd1->var_kind = vd->var_kind;
- vd1->var_ref_idx = vd->var_ref_idx;
- }
-
- for(i = 0; i < fd->var_count; i++) {
- JSVarDef *vd = &fd->vars[i];
- JSBytecodeVarDef *vd1 = &b->vardefs[i + fd->arg_count];
- if (strip_var_debug) {
- JS_FreeAtom(ctx, vd->var_name);
- vd1->var_name = JS_ATOM_NULL;
- } else {
- vd1->var_name = vd->var_name;
- }
- vd1->has_scope = (vd->scope_level != 0);
- vd1->scope_next = vd->scope_next;
- vd1->is_const = vd->is_const;
- vd1->is_lexical = vd->is_lexical;
- vd1->is_captured = vd->is_captured;
- vd1->var_kind = vd->var_kind;
- vd1->var_ref_idx = vd->var_ref_idx;
- }
- b->var_count = fd->var_count;
- b->arg_count = fd->arg_count;
- b->defined_arg_count = fd->defined_arg_count;
- b->var_ref_count = fd->var_ref_count;
- js_free(ctx, fd->args);
- js_free(ctx, fd->vars);
- }
- b->cpool_count = fd->cpool_count;
- if (b->cpool_count) {
- b->cpool = (void *)((uint8_t*)b + cpool_offset);
- memcpy(b->cpool, fd->cpool, b->cpool_count * sizeof(*b->cpool));
- }
- js_free(ctx, fd->cpool);
- fd->cpool = NULL;
- b->stack_size = stack_size;
- if (fd->strip_debug) {
- JS_FreeAtom(ctx, fd->filename);
- dbuf_free(&fd->pc2line); // probably useless
- } else {
- /* XXX: source and pc2line info should be packed at the end of the
- JSFunctionBytecode structure, avoiding allocation overhead
- */
- b->has_debug = 1;
- b->debug.filename = fd->filename;
- //DynBuf pc2line;
- //compute_pc2line_info(fd, &pc2line);
- //js_free(ctx, fd->line_number_slots)
- b->debug.pc2line_buf = js_realloc(ctx, fd->pc2line.buf, fd->pc2line.size);
- if (!b->debug.pc2line_buf)
- b->debug.pc2line_buf = fd->pc2line.buf;
- b->debug.pc2line_len = fd->pc2line.size;
- b->debug.source = fd->source;
- b->debug.source_len = fd->source_len;
- }
- if (fd->scopes != fd->def_scope_array)
- js_free(ctx, fd->scopes);
- b->closure_var_count = fd->closure_var_count;
- if (b->closure_var_count) {
- if (strip_var_debug) {
- int i;
- for(i = 0; i < fd->closure_var_count; i++) {
- JSClosureVar *cv = &fd->closure_var[i];
- if (cv->closure_type != JS_CLOSURE_GLOBAL_REF &&
- cv->closure_type != JS_CLOSURE_GLOBAL_DECL &&
- cv->closure_type != JS_CLOSURE_GLOBAL &&
- cv->closure_type != JS_CLOSURE_MODULE_DECL &&
- cv->closure_type != JS_CLOSURE_MODULE_IMPORT) {
- JS_FreeAtom(ctx, cv->var_name);
- cv->var_name = JS_ATOM_NULL;
- }
- }
- }
- b->closure_var = (void *)((uint8_t*)b + closure_var_offset);
- memcpy(b->closure_var, fd->closure_var, b->closure_var_count * sizeof(*b->closure_var));
- }
- js_free(ctx, fd->closure_var);
- fd->closure_var = NULL;
- b->has_prototype = fd->has_prototype;
- b->has_simple_parameter_list = fd->has_simple_parameter_list;
- b->js_mode = fd->js_mode;
- b->is_derived_class_constructor = fd->is_derived_class_constructor;
- b->func_kind = fd->func_kind;
- b->need_home_object = (fd->home_object_var_idx >= 0 ||
- fd->need_home_object);
- b->new_target_allowed = fd->new_target_allowed;
- b->super_call_allowed = fd->super_call_allowed;
- b->super_allowed = fd->super_allowed;
- b->arguments_allowed = fd->arguments_allowed;
- b->is_direct_or_indirect_eval = (fd->eval_type == JS_EVAL_TYPE_DIRECT ||
- fd->eval_type == JS_EVAL_TYPE_INDIRECT);
- b->realm = JS_DupContext(ctx);
- add_gc_object(ctx->rt, &b->header, JS_GC_OBJ_TYPE_FUNCTION_BYTECODE);
- #if defined(DUMP_BYTECODE) && (DUMP_BYTECODE & 1)
- if (!fd->strip_debug) {
- js_dump_function_bytecode(ctx, b);
- }
- #endif
- if (fd->parent) {
- /* remove from parent list */
- list_del(&fd->link);
- }
- js_free(ctx, fd);
- return JS_MKPTR(JS_TAG_FUNCTION_BYTECODE, b);
- fail:
- js_free_function_def(ctx, fd);
- return JS_EXCEPTION;
- }
- static void free_function_bytecode(JSRuntime *rt, JSFunctionBytecode *b)
- {
- int i;
- #if 0
- {
- char buf[ATOM_GET_STR_BUF_SIZE];
- printf("freeing %s\n",
- JS_AtomGetStrRT(rt, buf, sizeof(buf), b->func_name));
- }
- #endif
- if (b->byte_code_buf)
- free_bytecode_atoms(rt, b->byte_code_buf, b->byte_code_len, TRUE);
- if (b->vardefs) {
- for(i = 0; i < b->arg_count + b->var_count; i++) {
- JS_FreeAtomRT(rt, b->vardefs[i].var_name);
- }
- }
- for(i = 0; i < b->cpool_count; i++)
- JS_FreeValueRT(rt, b->cpool[i]);
- for(i = 0; i < b->closure_var_count; i++) {
- JSClosureVar *cv = &b->closure_var[i];
- JS_FreeAtomRT(rt, cv->var_name);
- }
- if (b->realm)
- JS_FreeContext(b->realm);
- JS_FreeAtomRT(rt, b->func_name);
- if (b->has_debug) {
- JS_FreeAtomRT(rt, b->debug.filename);
- js_free_rt(rt, b->debug.pc2line_buf);
- js_free_rt(rt, b->debug.source);
- }
- remove_gc_object(&b->header);
- if (rt->gc_phase == JS_GC_PHASE_REMOVE_CYCLES && js_rc(b)->ref_count != 0) {
- list_add_tail(&b->header.link, &rt->gc_zero_ref_count_list);
- } else {
- js_free_rt(rt, b);
- }
- }
- static __exception int js_parse_directives(JSParseState *s)
- {
- char str[20];
- JSParsePos pos;
- BOOL has_semi;
- if (s->token.val != TOK_STRING)
- return 0;
- js_parse_get_pos(s, &pos);
- while(s->token.val == TOK_STRING) {
- /* Copy actual source string representation */
- snprintf(str, sizeof str, "%.*s",
- (int)(s->buf_ptr - s->token.ptr - 2), s->token.ptr + 1);
- if (next_token(s))
- return -1;
- has_semi = FALSE;
- switch (s->token.val) {
- case ';':
- if (next_token(s))
- return -1;
- has_semi = TRUE;
- break;
- case '}':
- case TOK_EOF:
- has_semi = TRUE;
- break;
- case TOK_NUMBER:
- case TOK_STRING:
- case TOK_TEMPLATE:
- case TOK_IDENT:
- case TOK_REGEXP:
- case TOK_DEC:
- case TOK_INC:
- case TOK_NULL:
- case TOK_FALSE:
- case TOK_TRUE:
- case TOK_IF:
- case TOK_RETURN:
- case TOK_VAR:
- case TOK_THIS:
- case TOK_DELETE:
- case TOK_TYPEOF:
- case TOK_NEW:
- case TOK_DO:
- case TOK_WHILE:
- case TOK_FOR:
- case TOK_SWITCH:
- case TOK_THROW:
- case TOK_TRY:
- case TOK_FUNCTION:
- case TOK_DEBUGGER:
- case TOK_WITH:
- case TOK_CLASS:
- case TOK_CONST:
- case TOK_ENUM:
- case TOK_EXPORT:
- case TOK_IMPORT:
- case TOK_SUPER:
- case TOK_INTERFACE:
- case TOK_LET:
- case TOK_PACKAGE:
- case TOK_PRIVATE:
- case TOK_PROTECTED:
- case TOK_PUBLIC:
- case TOK_STATIC:
- /* automatic insertion of ';' */
- if (s->got_lf)
- has_semi = TRUE;
- break;
- default:
- break;
- }
- if (!has_semi)
- break;
- if (!strcmp(str, "use strict")) {
- s->cur_func->has_use_strict = TRUE;
- s->cur_func->js_mode |= JS_MODE_STRICT;
- }
- }
- return js_parse_seek_token(s, &pos);
- }
- /* return TRUE if the keyword is forbidden only in strict mode */
- static BOOL is_strict_future_keyword(JSAtom atom)
- {
- return (atom >= JS_ATOM_LAST_KEYWORD + 1 && atom <= JS_ATOM_LAST_STRICT_KEYWORD);
- }
- static int js_parse_function_check_names(JSParseState *s, JSFunctionDef *fd,
- JSAtom func_name)
- {
- JSAtom name;
- int i, idx;
- if (fd->js_mode & JS_MODE_STRICT) {
- if (!fd->has_simple_parameter_list && fd->has_use_strict) {
- return js_parse_error(s, "\"use strict\" not allowed in function with default or destructuring parameter");
- }
- if (func_name == JS_ATOM_eval || func_name == JS_ATOM_arguments ||
- is_strict_future_keyword(func_name)) {
- return js_parse_error(s, "invalid function name in strict code");
- }
- for (idx = 0; idx < fd->arg_count; idx++) {
- name = fd->args[idx].var_name;
- if (name == JS_ATOM_eval || name == JS_ATOM_arguments ||
- is_strict_future_keyword(name)) {
- return js_parse_error(s, "invalid argument name in strict code");
- }
- }
- }
- /* check async_generator case */
- if ((fd->js_mode & JS_MODE_STRICT)
- || !fd->has_simple_parameter_list
- || (fd->func_type == JS_PARSE_FUNC_METHOD && fd->func_kind == JS_FUNC_ASYNC)
- || fd->func_type == JS_PARSE_FUNC_ARROW
- || fd->func_type == JS_PARSE_FUNC_METHOD) {
- for (idx = 0; idx < fd->arg_count; idx++) {
- name = fd->args[idx].var_name;
- if (name != JS_ATOM_NULL) {
- for (i = 0; i < idx; i++) {
- if (fd->args[i].var_name == name)
- goto duplicate;
- }
- /* Check if argument name duplicates a destructuring parameter */
- /* XXX: should have a flag for such variables */
- for (i = 0; i < fd->var_count; i++) {
- if (fd->vars[i].var_name == name &&
- fd->vars[i].scope_level == 0)
- goto duplicate;
- }
- }
- }
- }
- return 0;
- duplicate:
- return js_parse_error(s, "duplicate argument names not allowed in this context");
- }
- /* create a function to initialize class fields */
- static JSFunctionDef *js_parse_function_class_fields_init(JSParseState *s)
- {
- JSFunctionDef *fd;
- fd = js_new_function_def(s->ctx, s->cur_func, FALSE, FALSE,
- s->filename, s->buf_start,
- &s->get_line_col_cache);
- if (!fd)
- return NULL;
- fd->func_name = JS_ATOM_NULL;
- fd->has_prototype = FALSE;
- fd->has_home_object = TRUE;
- fd->has_arguments_binding = FALSE;
- fd->has_this_binding = TRUE;
- fd->is_derived_class_constructor = FALSE;
- fd->new_target_allowed = TRUE;
- fd->super_call_allowed = FALSE;
- fd->super_allowed = fd->has_home_object;
- fd->arguments_allowed = FALSE;
- fd->func_kind = JS_FUNC_NORMAL;
- fd->func_type = JS_PARSE_FUNC_METHOD;
- return fd;
- }
- /* func_name must be JS_ATOM_NULL for JS_PARSE_FUNC_STATEMENT and
- JS_PARSE_FUNC_EXPR, JS_PARSE_FUNC_ARROW and JS_PARSE_FUNC_VAR */
- static __exception int js_parse_function_decl2(JSParseState *s,
- JSParseFunctionEnum func_type,
- JSFunctionKindEnum func_kind,
- JSAtom func_name,
- const uint8_t *ptr,
- JSParseExportEnum export_flag,
- JSFunctionDef **pfd)
- {
- JSContext *ctx = s->ctx;
- JSFunctionDef *fd = s->cur_func;
- BOOL is_expr;
- int func_idx, lexical_func_idx = -1;
- BOOL has_opt_arg;
- BOOL create_func_var = FALSE;
- is_expr = (func_type != JS_PARSE_FUNC_STATEMENT &&
- func_type != JS_PARSE_FUNC_VAR);
- if (func_type == JS_PARSE_FUNC_STATEMENT ||
- func_type == JS_PARSE_FUNC_VAR ||
- func_type == JS_PARSE_FUNC_EXPR) {
- if (func_kind == JS_FUNC_NORMAL &&
- token_is_pseudo_keyword(s, JS_ATOM_async) &&
- peek_token(s, TRUE) != '\n') {
- if (next_token(s))
- return -1;
- func_kind = JS_FUNC_ASYNC;
- }
- if (next_token(s))
- return -1;
- if (s->token.val == '*') {
- if (next_token(s))
- return -1;
- func_kind |= JS_FUNC_GENERATOR;
- }
- if (s->token.val == TOK_IDENT) {
- if (s->token.u.ident.is_reserved ||
- (s->token.u.ident.atom == JS_ATOM_yield &&
- func_type == JS_PARSE_FUNC_EXPR &&
- (func_kind & JS_FUNC_GENERATOR)) ||
- (s->token.u.ident.atom == JS_ATOM_await &&
- ((func_type == JS_PARSE_FUNC_EXPR &&
- (func_kind & JS_FUNC_ASYNC)) ||
- func_type == JS_PARSE_FUNC_CLASS_STATIC_INIT))) {
- return js_parse_error_reserved_identifier(s);
- }
- }
- if (s->token.val == TOK_IDENT ||
- (((s->token.val == TOK_YIELD && !(fd->js_mode & JS_MODE_STRICT)) ||
- (s->token.val == TOK_AWAIT && !s->is_module)) &&
- func_type == JS_PARSE_FUNC_EXPR)) {
- func_name = JS_DupAtom(ctx, s->token.u.ident.atom);
- if (next_token(s)) {
- JS_FreeAtom(ctx, func_name);
- return -1;
- }
- } else {
- if (func_type != JS_PARSE_FUNC_EXPR &&
- export_flag != JS_PARSE_EXPORT_DEFAULT) {
- return js_parse_error(s, "function name expected");
- }
- }
- } else if (func_type != JS_PARSE_FUNC_ARROW) {
- func_name = JS_DupAtom(ctx, func_name);
- }
- if (fd->is_eval && fd->eval_type == JS_EVAL_TYPE_MODULE &&
- (func_type == JS_PARSE_FUNC_STATEMENT || func_type == JS_PARSE_FUNC_VAR)) {
- JSGlobalVar *hf;
- hf = find_global_var(fd, func_name);
- /* XXX: should check scope chain */
- if (hf && hf->scope_level == fd->scope_level) {
- js_parse_error(s, "invalid redefinition of global identifier in module code");
- JS_FreeAtom(ctx, func_name);
- return -1;
- }
- }
- if (func_type == JS_PARSE_FUNC_VAR) {
- if (!(fd->js_mode & JS_MODE_STRICT)
- && func_kind == JS_FUNC_NORMAL
- && find_lexical_decl(ctx, fd, func_name, fd->scope_first, FALSE) < 0
- && !((func_idx = find_var(ctx, fd, func_name)) >= 0 && (func_idx & ARGUMENT_VAR_OFFSET))
- && !(func_name == JS_ATOM_arguments && fd->has_arguments_binding)) {
- create_func_var = TRUE;
- }
- /* Create the lexical name here so that the function closure
- contains it */
- if (fd->is_eval &&
- (fd->eval_type == JS_EVAL_TYPE_GLOBAL ||
- fd->eval_type == JS_EVAL_TYPE_MODULE) &&
- fd->scope_level == fd->body_scope) {
- /* avoid creating a lexical variable in the global
- scope. XXX: check annex B */
- JSGlobalVar *hf;
- hf = find_global_var(fd, func_name);
- /* XXX: should check scope chain */
- if (hf && hf->scope_level == fd->scope_level) {
- js_parse_error(s, "invalid redefinition of global identifier");
- JS_FreeAtom(ctx, func_name);
- return -1;
- }
- } else {
- /* Always create a lexical name, fail if at the same scope as
- existing name */
- /* Lexical variable will be initialized upon entering scope */
- lexical_func_idx = define_var(s, fd, func_name,
- func_kind != JS_FUNC_NORMAL ?
- JS_VAR_DEF_NEW_FUNCTION_DECL :
- JS_VAR_DEF_FUNCTION_DECL);
- if (lexical_func_idx < 0) {
- JS_FreeAtom(ctx, func_name);
- return -1;
- }
- }
- }
- fd = js_new_function_def(ctx, fd, FALSE, is_expr,
- s->filename, ptr,
- &s->get_line_col_cache);
- if (!fd) {
- JS_FreeAtom(ctx, func_name);
- return -1;
- }
- if (pfd)
- *pfd = fd;
- s->cur_func = fd;
- fd->func_name = func_name;
- /* XXX: test !fd->is_generator is always false */
- fd->has_prototype = (func_type == JS_PARSE_FUNC_STATEMENT ||
- func_type == JS_PARSE_FUNC_VAR ||
- func_type == JS_PARSE_FUNC_EXPR) &&
- func_kind == JS_FUNC_NORMAL;
- fd->has_home_object = (func_type == JS_PARSE_FUNC_METHOD ||
- func_type == JS_PARSE_FUNC_GETTER ||
- func_type == JS_PARSE_FUNC_SETTER ||
- func_type == JS_PARSE_FUNC_CLASS_CONSTRUCTOR ||
- func_type == JS_PARSE_FUNC_DERIVED_CLASS_CONSTRUCTOR);
- fd->has_arguments_binding = (func_type != JS_PARSE_FUNC_ARROW &&
- func_type != JS_PARSE_FUNC_CLASS_STATIC_INIT);
- fd->has_this_binding = fd->has_arguments_binding;
- fd->is_derived_class_constructor = (func_type == JS_PARSE_FUNC_DERIVED_CLASS_CONSTRUCTOR);
- if (func_type == JS_PARSE_FUNC_ARROW) {
- fd->new_target_allowed = fd->parent->new_target_allowed;
- fd->super_call_allowed = fd->parent->super_call_allowed;
- fd->super_allowed = fd->parent->super_allowed;
- fd->arguments_allowed = fd->parent->arguments_allowed;
- } else if (func_type == JS_PARSE_FUNC_CLASS_STATIC_INIT) {
- fd->new_target_allowed = TRUE; // although new.target === undefined
- fd->super_call_allowed = FALSE;
- fd->super_allowed = TRUE;
- fd->arguments_allowed = FALSE;
- } else {
- fd->new_target_allowed = TRUE;
- fd->super_call_allowed = fd->is_derived_class_constructor;
- fd->super_allowed = fd->has_home_object;
- fd->arguments_allowed = TRUE;
- }
- /* fd->in_function_body == FALSE prevents yield/await during the parsing
- of the arguments in generator/async functions. They are parsed as
- regular identifiers for other function kinds. */
- fd->func_kind = func_kind;
- fd->func_type = func_type;
- if (func_type == JS_PARSE_FUNC_CLASS_CONSTRUCTOR ||
- func_type == JS_PARSE_FUNC_DERIVED_CLASS_CONSTRUCTOR) {
- /* error if not invoked as a constructor */
- emit_op(s, OP_check_ctor);
- }
- if (func_type == JS_PARSE_FUNC_CLASS_CONSTRUCTOR) {
- emit_class_field_init(s);
- }
- /* parse arguments */
- fd->has_simple_parameter_list = TRUE;
- fd->has_parameter_expressions = FALSE;
- has_opt_arg = FALSE;
- if (func_type == JS_PARSE_FUNC_ARROW && s->token.val == TOK_IDENT) {
- JSAtom name;
- if (s->token.u.ident.is_reserved) {
- js_parse_error_reserved_identifier(s);
- goto fail;
- }
- name = s->token.u.ident.atom;
- if (add_arg(ctx, fd, name) < 0)
- goto fail;
- fd->defined_arg_count = 1;
- } else if (func_type != JS_PARSE_FUNC_CLASS_STATIC_INIT) {
- if (s->token.val == '(') {
- int skip_bits;
- /* if there is an '=' inside the parameter list, we
- consider there is a parameter expression inside */
- js_parse_skip_parens_token(s, &skip_bits, FALSE);
- if (skip_bits & SKIP_HAS_ASSIGNMENT)
- fd->has_parameter_expressions = TRUE;
- if (next_token(s))
- goto fail;
- } else {
- if (js_parse_expect(s, '('))
- goto fail;
- }
- if (fd->has_parameter_expressions) {
- fd->scope_level = -1; /* force no parent scope */
- if (push_scope(s) < 0)
- return -1;
- }
- while (s->token.val != ')') {
- JSAtom name;
- BOOL rest = FALSE;
- int idx, has_initializer;
- if (s->token.val == TOK_ELLIPSIS) {
- if (func_type == JS_PARSE_FUNC_SETTER)
- goto fail_accessor;
- fd->has_simple_parameter_list = FALSE;
- rest = TRUE;
- if (next_token(s))
- goto fail;
- }
- if (s->token.val == '[' || s->token.val == '{') {
- fd->has_simple_parameter_list = FALSE;
- if (rest) {
- emit_op(s, OP_rest);
- emit_u16(s, fd->arg_count);
- } else {
- /* unnamed arg for destructuring */
- idx = add_arg(ctx, fd, JS_ATOM_NULL);
- emit_op(s, OP_get_arg);
- emit_u16(s, idx);
- }
- has_initializer = js_parse_destructuring_element(s, fd->has_parameter_expressions ? TOK_LET : TOK_VAR, 1, TRUE, -1, TRUE, FALSE);
- if (has_initializer < 0)
- goto fail;
- if (has_initializer)
- has_opt_arg = TRUE;
- if (!has_opt_arg)
- fd->defined_arg_count++;
- } else if (s->token.val == TOK_IDENT) {
- if (s->token.u.ident.is_reserved) {
- js_parse_error_reserved_identifier(s);
- goto fail;
- }
- name = s->token.u.ident.atom;
- if (name == JS_ATOM_yield && fd->func_kind == JS_FUNC_GENERATOR) {
- js_parse_error_reserved_identifier(s);
- goto fail;
- }
- if (fd->has_parameter_expressions) {
- if (js_parse_check_duplicate_parameter(s, name))
- goto fail;
- if (define_var(s, fd, name, JS_VAR_DEF_LET) < 0)
- goto fail;
- }
- /* XXX: could avoid allocating an argument if rest is true */
- idx = add_arg(ctx, fd, name);
- if (idx < 0)
- goto fail;
- if (next_token(s))
- goto fail;
- if (rest) {
- emit_op(s, OP_rest);
- emit_u16(s, idx);
- if (fd->has_parameter_expressions) {
- emit_op(s, OP_dup);
- emit_op(s, OP_scope_put_var_init);
- emit_atom(s, name);
- emit_u16(s, fd->scope_level);
- }
- emit_op(s, OP_put_arg);
- emit_u16(s, idx);
- fd->has_simple_parameter_list = FALSE;
- has_opt_arg = TRUE;
- } else if (s->token.val == '=') {
- int label;
- fd->has_simple_parameter_list = FALSE;
- has_opt_arg = TRUE;
- if (next_token(s))
- goto fail;
- label = new_label(s);
- emit_op(s, OP_get_arg);
- emit_u16(s, idx);
- emit_op(s, OP_dup);
- emit_op(s, OP_undefined);
- emit_op(s, OP_strict_eq);
- emit_goto(s, OP_if_false, label);
- emit_op(s, OP_drop);
- if (js_parse_assign_expr(s))
- goto fail;
- set_object_name(s, name);
- emit_op(s, OP_dup);
- emit_op(s, OP_put_arg);
- emit_u16(s, idx);
- emit_label(s, label);
- emit_op(s, OP_scope_put_var_init);
- emit_atom(s, name);
- emit_u16(s, fd->scope_level);
- } else {
- if (!has_opt_arg) {
- fd->defined_arg_count++;
- }
- if (fd->has_parameter_expressions) {
- /* copy the argument to the argument scope */
- emit_op(s, OP_get_arg);
- emit_u16(s, idx);
- emit_op(s, OP_scope_put_var_init);
- emit_atom(s, name);
- emit_u16(s, fd->scope_level);
- }
- }
- } else {
- js_parse_error(s, "missing formal parameter");
- goto fail;
- }
- if (rest && s->token.val != ')') {
- js_parse_expect(s, ')');
- goto fail;
- }
- if (s->token.val == ')')
- break;
- if (js_parse_expect(s, ','))
- goto fail;
- }
- if ((func_type == JS_PARSE_FUNC_GETTER && fd->arg_count != 0) ||
- (func_type == JS_PARSE_FUNC_SETTER && fd->arg_count != 1)) {
- fail_accessor:
- js_parse_error(s, "invalid number of arguments for getter or setter");
- goto fail;
- }
- }
- if (fd->has_parameter_expressions) {
- int idx;
- /* Copy the variables in the argument scope to the variable
- scope (see FunctionDeclarationInstantiation() in spec). The
- normal arguments are already present, so no need to copy
- them. */
- idx = fd->scopes[fd->scope_level].first;
- while (idx >= 0) {
- JSVarDef *vd = &fd->vars[idx];
- if (vd->scope_level != fd->scope_level)
- break;
- if (find_var(ctx, fd, vd->var_name) < 0) {
- if (add_var(ctx, fd, vd->var_name) < 0)
- goto fail;
- vd = &fd->vars[idx]; /* fd->vars may have been reallocated */
- emit_op(s, OP_scope_get_var);
- emit_atom(s, vd->var_name);
- emit_u16(s, fd->scope_level);
- emit_op(s, OP_scope_put_var);
- emit_atom(s, vd->var_name);
- emit_u16(s, 0);
- }
- idx = vd->scope_next;
- }
- /* the argument scope has no parent, hence we don't use pop_scope(s) */
- emit_op(s, OP_leave_scope);
- emit_u16(s, fd->scope_level);
- /* set the variable scope as the current scope */
- fd->scope_level = 0;
- fd->scope_first = fd->scopes[fd->scope_level].first;
- }
- if (next_token(s))
- goto fail;
- /* generator function: yield after the parameters are evaluated */
- if (func_kind == JS_FUNC_GENERATOR ||
- func_kind == JS_FUNC_ASYNC_GENERATOR)
- emit_op(s, OP_initial_yield);
- /* in generators, yield expression is forbidden during the parsing
- of the arguments */
- fd->in_function_body = TRUE;
- push_scope(s); /* enter body scope */
- fd->body_scope = fd->scope_level;
- if (s->token.val == TOK_ARROW && func_type == JS_PARSE_FUNC_ARROW) {
- if (next_token(s))
- goto fail;
- if (s->token.val != '{') {
- if (js_parse_function_check_names(s, fd, func_name))
- goto fail;
- if (js_parse_assign_expr(s))
- goto fail;
- if (func_kind != JS_FUNC_NORMAL)
- emit_op(s, OP_return_async);
- else
- emit_op(s, OP_return);
- if (!fd->strip_source) {
- /* save the function source code */
- /* the end of the function source code is after the last
- token of the function source stored into s->last_ptr */
- fd->source_len = s->last_ptr - ptr;
- fd->source = js_strndup(ctx, (const char *)ptr, fd->source_len);
- if (!fd->source)
- goto fail;
- }
- goto done;
- }
- }
- if (func_type != JS_PARSE_FUNC_CLASS_STATIC_INIT) {
- if (js_parse_expect(s, '{'))
- goto fail;
- }
- if (js_parse_directives(s))
- goto fail;
- /* in strict_mode, check function and argument names */
- if (js_parse_function_check_names(s, fd, func_name))
- goto fail;
- while (s->token.val != '}') {
- if (js_parse_source_element(s))
- goto fail;
- }
- if (!fd->strip_source) {
- /* save the function source code */
- fd->source_len = s->buf_ptr - ptr;
- fd->source = js_strndup(ctx, (const char *)ptr, fd->source_len);
- if (!fd->source)
- goto fail;
- }
- if (next_token(s)) {
- /* consume the '}' */
- goto fail;
- }
- /* in case there is no return, add one */
- if (js_is_live_code(s)) {
- emit_return(s, FALSE);
- }
- done:
- s->cur_func = fd->parent;
- /* Reparse identifiers after the function is terminated so that
- the token is parsed in the englobing function. It could be done
- by just using next_token() here for normal functions, but it is
- necessary for arrow functions with an expression body. */
- reparse_ident_token(s);
- /* create the function object */
- {
- int idx;
- JSAtom func_name = fd->func_name;
- /* the real object will be set at the end of the compilation */
- idx = cpool_add(s, JS_NULL);
- fd->parent_cpool_idx = idx;
- if (is_expr) {
- /* for constructors, no code needs to be generated here */
- if (func_type != JS_PARSE_FUNC_CLASS_CONSTRUCTOR &&
- func_type != JS_PARSE_FUNC_DERIVED_CLASS_CONSTRUCTOR) {
- /* OP_fclosure creates the function object from the bytecode
- and adds the scope information */
- emit_op(s, OP_fclosure);
- emit_u32(s, idx);
- if (func_name == JS_ATOM_NULL) {
- emit_op(s, OP_set_name);
- emit_u32(s, JS_ATOM_NULL);
- }
- }
- } else if (func_type == JS_PARSE_FUNC_VAR) {
- emit_op(s, OP_fclosure);
- emit_u32(s, idx);
- if (create_func_var) {
- if (s->cur_func->is_global_var) {
- JSGlobalVar *hf;
- /* the global variable must be defined at the start of the
- function */
- hf = add_global_var(ctx, s->cur_func, func_name);
- if (!hf)
- goto fail;
- /* it is considered as defined at the top level
- (needed for annex B.3.3.4 and B.3.3.5
- checks) */
- hf->scope_level = 0;
- hf->force_init = ((s->cur_func->js_mode & JS_MODE_STRICT) != 0);
- /* store directly into global var, bypass lexical scope */
- emit_op(s, OP_dup);
- emit_op(s, OP_scope_put_var);
- emit_atom(s, func_name);
- emit_u16(s, 0);
- } else {
- /* do not call define_var to bypass lexical scope check */
- func_idx = find_var(ctx, s->cur_func, func_name);
- if (func_idx < 0) {
- func_idx = add_var(ctx, s->cur_func, func_name);
- if (func_idx < 0)
- goto fail;
- }
- /* store directly into local var, bypass lexical catch scope */
- emit_op(s, OP_dup);
- emit_op(s, OP_scope_put_var);
- emit_atom(s, func_name);
- emit_u16(s, 0);
- }
- }
- if (lexical_func_idx >= 0) {
- /* lexical variable will be initialized upon entering scope */
- s->cur_func->vars[lexical_func_idx].func_pool_idx = idx;
- emit_op(s, OP_drop);
- } else {
- /* store function object into its lexical name */
- /* XXX: could use OP_put_loc directly */
- emit_op(s, OP_scope_put_var_init);
- emit_atom(s, func_name);
- emit_u16(s, s->cur_func->scope_level);
- }
- } else {
- if (!s->cur_func->is_global_var) {
- int var_idx = define_var(s, s->cur_func, func_name, JS_VAR_DEF_VAR);
- if (var_idx < 0)
- goto fail;
- /* the variable will be assigned at the top of the function */
- if (var_idx & ARGUMENT_VAR_OFFSET) {
- s->cur_func->args[var_idx - ARGUMENT_VAR_OFFSET].func_pool_idx = idx;
- } else {
- s->cur_func->vars[var_idx].func_pool_idx = idx;
- }
- } else {
- JSAtom func_var_name;
- JSGlobalVar *hf;
- if (func_name == JS_ATOM_NULL)
- func_var_name = JS_ATOM__default_; /* export default */
- else
- func_var_name = func_name;
- /* the variable will be assigned at the top of the function */
- hf = add_global_var(ctx, s->cur_func, func_var_name);
- if (!hf)
- goto fail;
- hf->cpool_idx = idx;
- if (export_flag != JS_PARSE_EXPORT_NONE) {
- if (!add_export_entry(s, s->cur_func->module, func_var_name,
- export_flag == JS_PARSE_EXPORT_NAMED ? func_var_name : JS_ATOM_default, JS_EXPORT_TYPE_LOCAL))
- goto fail;
- }
- }
- }
- }
- return 0;
- fail:
- s->cur_func = fd->parent;
- js_free_function_def(ctx, fd);
- if (pfd)
- *pfd = NULL;
- return -1;
- }
- static __exception int js_parse_function_decl(JSParseState *s,
- JSParseFunctionEnum func_type,
- JSFunctionKindEnum func_kind,
- JSAtom func_name,
- const uint8_t *ptr)
- {
- return js_parse_function_decl2(s, func_type, func_kind, func_name, ptr,
- JS_PARSE_EXPORT_NONE, NULL);
- }
- static __exception int js_parse_program(JSParseState *s)
- {
- JSFunctionDef *fd = s->cur_func;
- int idx;
- if (next_token(s))
- return -1;
- if (js_parse_directives(s))
- return -1;
- fd->is_global_var = (fd->eval_type == JS_EVAL_TYPE_GLOBAL) ||
- (fd->eval_type == JS_EVAL_TYPE_MODULE) ||
- !(fd->js_mode & JS_MODE_STRICT);
- if (!s->is_module) {
- /* hidden variable for the return value */
- fd->eval_ret_idx = idx = add_var(s->ctx, fd, JS_ATOM__ret_);
- if (idx < 0)
- return -1;
- }
- while (s->token.val != TOK_EOF) {
- if (js_parse_source_element(s))
- return -1;
- }
- if (!s->is_module) {
- /* return the value of the hidden variable eval_ret_idx */
- if (fd->func_kind == JS_FUNC_ASYNC) {
- /* wrap the return value in an object so that promises can
- be safely returned */
- emit_op(s, OP_object);
- emit_op(s, OP_dup);
- emit_op(s, OP_get_loc);
- emit_u16(s, fd->eval_ret_idx);
- emit_op(s, OP_put_field);
- emit_atom(s, JS_ATOM_value);
- } else {
- emit_op(s, OP_get_loc);
- emit_u16(s, fd->eval_ret_idx);
- }
- emit_return(s, TRUE);
- } else {
- emit_return(s, FALSE);
- }
- return 0;
- }
- static void js_parse_init(JSContext *ctx, JSParseState *s,
- const char *input, size_t input_len,
- const char *filename)
- {
- memset(s, 0, sizeof(*s));
- s->ctx = ctx;
- s->filename = filename;
- s->buf_start = s->buf_ptr = (const uint8_t *)input;
- s->buf_end = s->buf_ptr + input_len;
- s->token.val = ' ';
- s->token.ptr = s->buf_ptr;
- s->get_line_col_cache.ptr = s->buf_start;
- s->get_line_col_cache.buf_start = s->buf_start;
- s->get_line_col_cache.line_num = 0;
- s->get_line_col_cache.col_num = 0;
- }
- static JSValue JS_EvalFunctionInternal(JSContext *ctx, JSValue fun_obj,
- JSValueConst this_obj,
- JSVarRef **var_refs, JSStackFrame *sf)
- {
- JSValue ret_val;
- uint32_t tag;
- tag = JS_VALUE_GET_TAG(fun_obj);
- if (tag == JS_TAG_FUNCTION_BYTECODE) {
- fun_obj = js_closure(ctx, fun_obj, var_refs, sf, TRUE);
- if (JS_IsException(fun_obj))
- return JS_EXCEPTION;
- ret_val = JS_CallFree(ctx, fun_obj, this_obj, 0, NULL);
- } else if (tag == JS_TAG_MODULE) {
- JSModuleDef *m;
- m = JS_VALUE_GET_PTR(fun_obj);
- /* the module refcount should be >= 2 */
- JS_FreeValue(ctx, fun_obj);
- if (js_create_module_function(ctx, m) < 0)
- goto fail;
- if (js_link_module(ctx, m) < 0)
- goto fail;
- ret_val = js_evaluate_module(ctx, m);
- if (JS_IsException(ret_val)) {
- fail:
- return JS_EXCEPTION;
- }
- } else {
- JS_FreeValue(ctx, fun_obj);
- ret_val = JS_ThrowTypeError(ctx, "bytecode function expected");
- }
- return ret_val;
- }
- JSValue JS_EvalFunction(JSContext *ctx, JSValue fun_obj)
- {
- return JS_EvalFunctionInternal(ctx, fun_obj, ctx->global_obj, NULL, NULL);
- }
- /* 'input' must be zero terminated i.e. input[input_len] = '\0'. */
- static JSValue __JS_EvalInternal(JSContext *ctx, JSValueConst this_obj,
- const char *input, size_t input_len,
- const char *filename, int flags, int scope_idx)
- {
- JSParseState s1, *s = &s1;
- int err, js_mode, eval_type;
- JSValue fun_obj, ret_val;
- JSStackFrame *sf;
- JSVarRef **var_refs;
- JSFunctionBytecode *b;
- JSFunctionDef *fd;
- JSModuleDef *m;
- js_parse_init(ctx, s, input, input_len, filename);
- skip_shebang(&s->buf_ptr, s->buf_end);
- eval_type = flags & JS_EVAL_TYPE_MASK;
- m = NULL;
- if (eval_type == JS_EVAL_TYPE_DIRECT) {
- JSObject *p;
- sf = ctx->rt->current_stack_frame;
- assert(sf != NULL);
- assert(JS_VALUE_GET_TAG(sf->cur_func) == JS_TAG_OBJECT);
- p = JS_VALUE_GET_OBJ(sf->cur_func);
- assert(js_class_has_bytecode(p->class_id));
- b = p->u.func.function_bytecode;
- var_refs = p->u.func.var_refs;
- js_mode = b->js_mode;
- } else {
- sf = NULL;
- b = NULL;
- var_refs = NULL;
- js_mode = 0;
- if (flags & JS_EVAL_FLAG_STRICT)
- js_mode |= JS_MODE_STRICT;
- if (eval_type == JS_EVAL_TYPE_MODULE) {
- JSAtom module_name = JS_NewAtom(ctx, filename);
- if (module_name == JS_ATOM_NULL)
- return JS_EXCEPTION;
- m = js_new_module_def(ctx, module_name);
- if (!m)
- return JS_EXCEPTION;
- js_mode |= JS_MODE_STRICT;
- }
- }
- fd = js_new_function_def(ctx, NULL, TRUE, FALSE, filename,
- s->buf_start, &s->get_line_col_cache);
- if (!fd)
- goto fail1;
- s->cur_func = fd;
- fd->eval_type = eval_type;
- fd->has_this_binding = (eval_type != JS_EVAL_TYPE_DIRECT);
- if (eval_type == JS_EVAL_TYPE_DIRECT) {
- fd->new_target_allowed = b->new_target_allowed;
- fd->super_call_allowed = b->super_call_allowed;
- fd->super_allowed = b->super_allowed;
- fd->arguments_allowed = b->arguments_allowed;
- } else {
- fd->new_target_allowed = FALSE;
- fd->super_call_allowed = FALSE;
- fd->super_allowed = FALSE;
- fd->arguments_allowed = TRUE;
- }
- fd->js_mode = js_mode;
- fd->func_name = JS_DupAtom(ctx, JS_ATOM__eval_);
- if (b) {
- if (add_closure_variables(ctx, fd, b, scope_idx))
- goto fail;
- }
- fd->module = m;
- if (m != NULL || (flags & JS_EVAL_FLAG_ASYNC)) {
- fd->in_function_body = TRUE;
- fd->func_kind = JS_FUNC_ASYNC;
- }
- s->is_module = (m != NULL);
- s->allow_html_comments = !s->is_module;
- push_scope(s); /* body scope */
- fd->body_scope = fd->scope_level;
- err = js_parse_program(s);
- if (err) {
- fail:
- free_token(s, &s->token);
- js_free_function_def(ctx, fd);
- goto fail1;
- }
- if (m != NULL)
- m->has_tla = fd->has_await;
- /* create the function object and all the enclosed functions */
- fun_obj = js_create_function(ctx, fd);
- if (JS_IsException(fun_obj))
- goto fail1;
- /* Could add a flag to avoid resolution if necessary */
- if (m) {
- m->func_obj = fun_obj;
- if (js_resolve_module(ctx, m) < 0)
- goto fail1;
- fun_obj = JS_NewModuleValue(ctx, m);
- }
- if (flags & JS_EVAL_FLAG_COMPILE_ONLY) {
- ret_val = fun_obj;
- } else {
- ret_val = JS_EvalFunctionInternal(ctx, fun_obj, this_obj, var_refs, sf);
- }
- return ret_val;
- fail1:
- /* XXX: should free all the unresolved dependencies */
- if (m)
- JS_FreeValue(ctx, JS_MKPTR(JS_TAG_MODULE, m));
- return JS_EXCEPTION;
- }
- /* the indirection is needed to make 'eval' optional */
- static JSValue JS_EvalInternal(JSContext *ctx, JSValueConst this_obj,
- const char *input, size_t input_len,
- const char *filename, int flags, int scope_idx)
- {
- BOOL backtrace_barrier = ((flags & JS_EVAL_FLAG_BACKTRACE_BARRIER) != 0);
- int saved_js_mode = 0;
- JSValue ret;
-
- if (unlikely(!ctx->eval_internal)) {
- return JS_ThrowTypeError(ctx, "eval is not supported");
- }
- if (backtrace_barrier && ctx->rt->current_stack_frame) {
- saved_js_mode = ctx->rt->current_stack_frame->js_mode;
- ctx->rt->current_stack_frame->js_mode |= JS_MODE_BACKTRACE_BARRIER;
- }
- ret = ctx->eval_internal(ctx, this_obj, input, input_len, filename,
- flags, scope_idx);
- if (backtrace_barrier && ctx->rt->current_stack_frame)
- ctx->rt->current_stack_frame->js_mode = saved_js_mode;
- return ret;
- }
- static JSValue JS_EvalObject(JSContext *ctx, JSValueConst this_obj,
- JSValueConst val, int flags, int scope_idx)
- {
- JSValue ret;
- const char *str;
- size_t len;
- if (!JS_IsString(val))
- return JS_DupValue(ctx, val);
- str = JS_ToCStringLen(ctx, &len, val);
- if (!str)
- return JS_EXCEPTION;
- ret = JS_EvalInternal(ctx, this_obj, str, len, "<input>", flags, scope_idx);
- JS_FreeCString(ctx, str);
- return ret;
- }
- JSValue JS_EvalThis(JSContext *ctx, JSValueConst this_obj,
- const char *input, size_t input_len,
- const char *filename, int eval_flags)
- {
- int eval_type = eval_flags & JS_EVAL_TYPE_MASK;
- JSValue ret;
- assert(eval_type == JS_EVAL_TYPE_GLOBAL ||
- eval_type == JS_EVAL_TYPE_MODULE);
- ret = JS_EvalInternal(ctx, this_obj, input, input_len, filename,
- eval_flags, -1);
- return ret;
- }
- JSValue JS_Eval(JSContext *ctx, const char *input, size_t input_len,
- const char *filename, int eval_flags)
- {
- return JS_EvalThis(ctx, ctx->global_obj, input, input_len, filename,
- eval_flags);
- }
- int JS_ResolveModule(JSContext *ctx, JSValueConst obj)
- {
- if (JS_VALUE_GET_TAG(obj) == JS_TAG_MODULE) {
- JSModuleDef *m = JS_VALUE_GET_PTR(obj);
- if (js_resolve_module(ctx, m) < 0) {
- js_free_modules(ctx, JS_FREE_MODULE_NOT_RESOLVED);
- return -1;
- }
- }
- return 0;
- }
- /*******************************************************************/
- /* object list */
- typedef struct {
- JSObject *obj;
- uint32_t hash_next; /* -1 if no next entry */
- } JSObjectListEntry;
- /* XXX: reuse it to optimize weak references */
- typedef struct {
- JSObjectListEntry *object_tab;
- int object_count;
- int object_size;
- uint32_t *hash_table;
- uint32_t hash_size;
- } JSObjectList;
- static void js_object_list_init(JSObjectList *s)
- {
- memset(s, 0, sizeof(*s));
- }
- static uint32_t js_object_list_get_hash(JSObject *p, uint32_t hash_size)
- {
- return ((uintptr_t)p * 3163) & (hash_size - 1);
- }
- static int js_object_list_resize_hash(JSContext *ctx, JSObjectList *s,
- uint32_t new_hash_size)
- {
- JSObjectListEntry *e;
- uint32_t i, h, *new_hash_table;
- new_hash_table = js_malloc(ctx, sizeof(new_hash_table[0]) * new_hash_size);
- if (!new_hash_table)
- return -1;
- js_free(ctx, s->hash_table);
- s->hash_table = new_hash_table;
- s->hash_size = new_hash_size;
- for(i = 0; i < s->hash_size; i++) {
- s->hash_table[i] = -1;
- }
- for(i = 0; i < s->object_count; i++) {
- e = &s->object_tab[i];
- h = js_object_list_get_hash(e->obj, s->hash_size);
- e->hash_next = s->hash_table[h];
- s->hash_table[h] = i;
- }
- return 0;
- }
- /* the reference count of 'obj' is not modified. Return 0 if OK, -1 if
- memory error */
- static int js_object_list_add(JSContext *ctx, JSObjectList *s, JSObject *obj)
- {
- JSObjectListEntry *e;
- uint32_t h, new_hash_size;
- if (js_resize_array(ctx, (void *)&s->object_tab,
- sizeof(s->object_tab[0]),
- &s->object_size, s->object_count + 1))
- return -1;
- if (unlikely((s->object_count + 1) >= s->hash_size)) {
- new_hash_size = max_uint32(s->hash_size, 4);
- while (new_hash_size <= s->object_count)
- new_hash_size *= 2;
- if (js_object_list_resize_hash(ctx, s, new_hash_size))
- return -1;
- }
- e = &s->object_tab[s->object_count++];
- h = js_object_list_get_hash(obj, s->hash_size);
- e->obj = obj;
- e->hash_next = s->hash_table[h];
- s->hash_table[h] = s->object_count - 1;
- return 0;
- }
- /* return -1 if not present or the object index */
- static int js_object_list_find(JSContext *ctx, JSObjectList *s, JSObject *obj)
- {
- JSObjectListEntry *e;
- uint32_t h, p;
- /* must test empty size because there is no hash table */
- if (s->object_count == 0)
- return -1;
- h = js_object_list_get_hash(obj, s->hash_size);
- p = s->hash_table[h];
- while (p != -1) {
- e = &s->object_tab[p];
- if (e->obj == obj)
- return p;
- p = e->hash_next;
- }
- return -1;
- }
- static void js_object_list_end(JSContext *ctx, JSObjectList *s)
- {
- js_free(ctx, s->object_tab);
- js_free(ctx, s->hash_table);
- }
- /*******************************************************************/
- /* binary object writer & reader */
- typedef enum BCTagEnum {
- BC_TAG_NULL = 1,
- BC_TAG_UNDEFINED,
- BC_TAG_BOOL_FALSE,
- BC_TAG_BOOL_TRUE,
- BC_TAG_INT32,
- BC_TAG_FLOAT64,
- BC_TAG_STRING,
- BC_TAG_OBJECT,
- BC_TAG_ARRAY,
- BC_TAG_BIG_INT,
- BC_TAG_TEMPLATE_OBJECT,
- BC_TAG_FUNCTION_BYTECODE,
- BC_TAG_MODULE,
- BC_TAG_TYPED_ARRAY,
- BC_TAG_ARRAY_BUFFER,
- BC_TAG_SHARED_ARRAY_BUFFER,
- BC_TAG_DATE,
- BC_TAG_OBJECT_VALUE,
- BC_TAG_OBJECT_REFERENCE,
- } BCTagEnum;
- #define BC_VERSION 5
- typedef struct BCWriterState {
- JSContext *ctx;
- DynBuf dbuf;
- BOOL allow_bytecode : 8;
- BOOL allow_sab : 8;
- BOOL allow_reference : 8;
- uint32_t first_atom;
- uint32_t *atom_to_idx;
- int atom_to_idx_size;
- JSAtom *idx_to_atom;
- int idx_to_atom_count;
- int idx_to_atom_size;
- uint8_t **sab_tab;
- int sab_tab_len;
- int sab_tab_size;
- /* list of referenced objects (used if allow_reference = TRUE) */
- JSObjectList object_list;
- } BCWriterState;
- #ifdef DUMP_READ_OBJECT
- static const char * const bc_tag_str[] = {
- "invalid",
- "null",
- "undefined",
- "false",
- "true",
- "int32",
- "float64",
- "string",
- "object",
- "array",
- "bigint",
- "template",
- "function",
- "module",
- "TypedArray",
- "ArrayBuffer",
- "SharedArrayBuffer",
- "Date",
- "ObjectValue",
- "ObjectReference",
- };
- #endif
- static inline BOOL is_be(void)
- {
- union {
- uint16_t a;
- uint8_t b;
- } u = {0x100};
- return u.b;
- }
- static void bc_put_u8(BCWriterState *s, uint8_t v)
- {
- dbuf_putc(&s->dbuf, v);
- }
- static void bc_put_u16(BCWriterState *s, uint16_t v)
- {
- if (is_be())
- v = bswap16(v);
- dbuf_put_u16(&s->dbuf, v);
- }
- static __maybe_unused void bc_put_u32(BCWriterState *s, uint32_t v)
- {
- if (is_be())
- v = bswap32(v);
- dbuf_put_u32(&s->dbuf, v);
- }
- static void bc_put_u64(BCWriterState *s, uint64_t v)
- {
- if (is_be())
- v = bswap64(v);
- dbuf_put(&s->dbuf, (uint8_t *)&v, sizeof(v));
- }
- static void bc_put_leb128(BCWriterState *s, uint32_t v)
- {
- dbuf_put_leb128(&s->dbuf, v);
- }
- static void bc_put_sleb128(BCWriterState *s, int32_t v)
- {
- dbuf_put_sleb128(&s->dbuf, v);
- }
- static void bc_set_flags(uint32_t *pflags, int *pidx, uint32_t val, int n)
- {
- *pflags = *pflags | (val << *pidx);
- *pidx += n;
- }
- static int bc_atom_to_idx(BCWriterState *s, uint32_t *pres, JSAtom atom)
- {
- uint32_t v;
- if (atom < s->first_atom || __JS_AtomIsTaggedInt(atom)) {
- *pres = atom;
- return 0;
- }
- atom -= s->first_atom;
- if (atom < s->atom_to_idx_size && s->atom_to_idx[atom] != 0) {
- *pres = s->atom_to_idx[atom];
- return 0;
- }
- if (atom >= s->atom_to_idx_size) {
- int old_size, i;
- old_size = s->atom_to_idx_size;
- if (js_resize_array(s->ctx, (void **)&s->atom_to_idx,
- sizeof(s->atom_to_idx[0]), &s->atom_to_idx_size,
- atom + 1))
- return -1;
- /* XXX: could add a specific js_resize_array() function to do it */
- for(i = old_size; i < s->atom_to_idx_size; i++)
- s->atom_to_idx[i] = 0;
- }
- if (js_resize_array(s->ctx, (void **)&s->idx_to_atom,
- sizeof(s->idx_to_atom[0]),
- &s->idx_to_atom_size, s->idx_to_atom_count + 1))
- goto fail;
- v = s->idx_to_atom_count++;
- s->idx_to_atom[v] = atom + s->first_atom;
- v += s->first_atom;
- s->atom_to_idx[atom] = v;
- *pres = v;
- return 0;
- fail:
- *pres = 0;
- return -1;
- }
- static int bc_put_atom(BCWriterState *s, JSAtom atom)
- {
- uint32_t v;
- if (__JS_AtomIsTaggedInt(atom)) {
- v = (__JS_AtomToUInt32(atom) << 1) | 1;
- } else {
- if (bc_atom_to_idx(s, &v, atom))
- return -1;
- v <<= 1;
- }
- bc_put_leb128(s, v);
- return 0;
- }
- static void bc_byte_swap(uint8_t *bc_buf, int bc_len)
- {
- int pos, len, op, fmt;
- pos = 0;
- while (pos < bc_len) {
- op = bc_buf[pos];
- len = short_opcode_info(op).size;
- fmt = short_opcode_info(op).fmt;
- switch(fmt) {
- case OP_FMT_u16:
- case OP_FMT_i16:
- case OP_FMT_label16:
- case OP_FMT_npop:
- case OP_FMT_loc:
- case OP_FMT_arg:
- case OP_FMT_var_ref:
- put_u16(bc_buf + pos + 1,
- bswap16(get_u16(bc_buf + pos + 1)));
- break;
- case OP_FMT_i32:
- case OP_FMT_u32:
- case OP_FMT_const:
- case OP_FMT_label:
- case OP_FMT_atom:
- case OP_FMT_atom_u8:
- put_u32(bc_buf + pos + 1,
- bswap32(get_u32(bc_buf + pos + 1)));
- break;
- case OP_FMT_atom_u16:
- case OP_FMT_label_u16:
- put_u32(bc_buf + pos + 1,
- bswap32(get_u32(bc_buf + pos + 1)));
- put_u16(bc_buf + pos + 1 + 4,
- bswap16(get_u16(bc_buf + pos + 1 + 4)));
- break;
- case OP_FMT_atom_label_u8:
- case OP_FMT_atom_label_u16:
- put_u32(bc_buf + pos + 1,
- bswap32(get_u32(bc_buf + pos + 1)));
- put_u32(bc_buf + pos + 1 + 4,
- bswap32(get_u32(bc_buf + pos + 1 + 4)));
- if (fmt == OP_FMT_atom_label_u16) {
- put_u16(bc_buf + pos + 1 + 4 + 4,
- bswap16(get_u16(bc_buf + pos + 1 + 4 + 4)));
- }
- break;
- case OP_FMT_npop_u16:
- put_u16(bc_buf + pos + 1,
- bswap16(get_u16(bc_buf + pos + 1)));
- put_u16(bc_buf + pos + 1 + 2,
- bswap16(get_u16(bc_buf + pos + 1 + 2)));
- break;
- default:
- break;
- }
- pos += len;
- }
- }
- static int JS_WriteFunctionBytecode(BCWriterState *s,
- const uint8_t *bc_buf1, int bc_len)
- {
- int pos, len, op;
- JSAtom atom;
- uint8_t *bc_buf;
- uint32_t val;
- bc_buf = js_malloc(s->ctx, bc_len);
- if (!bc_buf)
- return -1;
- memcpy(bc_buf, bc_buf1, bc_len);
- pos = 0;
- while (pos < bc_len) {
- op = bc_buf[pos];
- len = short_opcode_info(op).size;
- switch(short_opcode_info(op).fmt) {
- case OP_FMT_atom:
- case OP_FMT_atom_u8:
- case OP_FMT_atom_u16:
- case OP_FMT_atom_label_u8:
- case OP_FMT_atom_label_u16:
- atom = get_u32(bc_buf + pos + 1);
- if (bc_atom_to_idx(s, &val, atom))
- goto fail;
- put_u32(bc_buf + pos + 1, val);
- break;
- default:
- break;
- }
- pos += len;
- }
- if (is_be())
- bc_byte_swap(bc_buf, bc_len);
- dbuf_put(&s->dbuf, bc_buf, bc_len);
- js_free(s->ctx, bc_buf);
- return 0;
- fail:
- js_free(s->ctx, bc_buf);
- return -1;
- }
- static void JS_WriteString(BCWriterState *s, JSString *p)
- {
- int i;
- bc_put_leb128(s, ((uint32_t)p->len << 1) | p->is_wide_char);
- if (p->is_wide_char) {
- for(i = 0; i < p->len; i++)
- bc_put_u16(s, p->u.str16[i]);
- } else {
- dbuf_put(&s->dbuf, p->u.str8, p->len);
- }
- }
- static int JS_WriteBigInt(BCWriterState *s, JSValueConst obj)
- {
- JSBigIntBuf buf;
- JSBigInt *p;
- uint32_t len, i;
- js_limb_t v, b;
- int shift;
-
- bc_put_u8(s, BC_TAG_BIG_INT);
- if (JS_VALUE_GET_TAG(obj) == JS_TAG_SHORT_BIG_INT)
- p = js_bigint_set_short(&buf, obj);
- else
- p = JS_VALUE_GET_PTR(obj);
- if (p->len == 1 && p->tab[0] == 0) {
- /* zero case */
- len = 0;
- } else {
- /* compute the length of the two's complement representation
- in bytes */
- len = p->len * (JS_LIMB_BITS / 8);
- v = p->tab[p->len - 1];
- shift = JS_LIMB_BITS - 8;
- while (shift > 0) {
- b = (v >> shift) & 0xff;
- if (b != 0x00 && b != 0xff)
- break;
- if ((b & 1) != ((v >> (shift - 1)) & 1))
- break;
- shift -= 8;
- len--;
- }
- }
- bc_put_leb128(s, len);
- if (len > 0) {
- for(i = 0; i < (len / (JS_LIMB_BITS / 8)); i++) {
- #if JS_LIMB_BITS == 32
- bc_put_u32(s, p->tab[i]);
- #else
- bc_put_u64(s, p->tab[i]);
- #endif
- }
- for(i = 0; i < len % (JS_LIMB_BITS / 8); i++) {
- bc_put_u8(s, (p->tab[p->len - 1] >> (i * 8)) & 0xff);
- }
- }
- return 0;
- }
- static int JS_WriteObjectRec(BCWriterState *s, JSValueConst obj);
- static int JS_WriteFunctionTag(BCWriterState *s, JSValueConst obj)
- {
- JSFunctionBytecode *b = JS_VALUE_GET_PTR(obj);
- uint32_t flags;
- int idx, i;
- bc_put_u8(s, BC_TAG_FUNCTION_BYTECODE);
- flags = idx = 0;
- bc_set_flags(&flags, &idx, b->has_prototype, 1);
- bc_set_flags(&flags, &idx, b->has_simple_parameter_list, 1);
- bc_set_flags(&flags, &idx, b->is_derived_class_constructor, 1);
- bc_set_flags(&flags, &idx, b->need_home_object, 1);
- bc_set_flags(&flags, &idx, b->func_kind, 2);
- bc_set_flags(&flags, &idx, b->new_target_allowed, 1);
- bc_set_flags(&flags, &idx, b->super_call_allowed, 1);
- bc_set_flags(&flags, &idx, b->super_allowed, 1);
- bc_set_flags(&flags, &idx, b->arguments_allowed, 1);
- bc_set_flags(&flags, &idx, b->has_debug, 1);
- bc_set_flags(&flags, &idx, b->is_direct_or_indirect_eval, 1);
- assert(idx <= 16);
- bc_put_u16(s, flags);
- bc_put_u8(s, b->js_mode);
- bc_put_atom(s, b->func_name);
- bc_put_leb128(s, b->arg_count);
- bc_put_leb128(s, b->var_count);
- bc_put_leb128(s, b->defined_arg_count);
- bc_put_leb128(s, b->stack_size);
- bc_put_leb128(s, b->var_ref_count);
- bc_put_leb128(s, b->closure_var_count);
- bc_put_leb128(s, b->cpool_count);
- bc_put_leb128(s, b->byte_code_len);
- if (b->vardefs) {
- bc_put_leb128(s, b->arg_count + b->var_count);
- for(i = 0; i < b->arg_count + b->var_count; i++) {
- JSBytecodeVarDef *vd = &b->vardefs[i];
- bc_put_atom(s, vd->var_name);
- bc_put_leb128(s, vd->scope_next + 1);
- bc_put_leb128(s, vd->var_ref_idx);
- flags = idx = 0;
- bc_set_flags(&flags, &idx, vd->var_kind, 4);
- bc_set_flags(&flags, &idx, vd->is_const, 1);
- bc_set_flags(&flags, &idx, vd->is_lexical, 1);
- bc_set_flags(&flags, &idx, vd->is_captured, 1);
- bc_set_flags(&flags, &idx, vd->has_scope, 1);
- assert(idx <= 8);
- bc_put_u8(s, flags);
- }
- } else {
- bc_put_leb128(s, 0);
- }
- for(i = 0; i < b->closure_var_count; i++) {
- JSClosureVar *cv = &b->closure_var[i];
- bc_put_atom(s, cv->var_name);
- bc_put_leb128(s, cv->var_idx);
- flags = idx = 0;
- bc_set_flags(&flags, &idx, cv->closure_type, 3);
- bc_set_flags(&flags, &idx, cv->is_const, 1);
- bc_set_flags(&flags, &idx, cv->is_lexical, 1);
- bc_set_flags(&flags, &idx, cv->var_kind, 4);
- assert(idx <= 16);
- bc_put_u16(s, flags);
- }
- if (JS_WriteFunctionBytecode(s, b->byte_code_buf, b->byte_code_len))
- goto fail;
- if (b->has_debug) {
- bc_put_atom(s, b->debug.filename);
- bc_put_leb128(s, b->debug.pc2line_len);
- dbuf_put(&s->dbuf, b->debug.pc2line_buf, b->debug.pc2line_len);
- if (b->debug.source) {
- bc_put_leb128(s, b->debug.source_len);
- dbuf_put(&s->dbuf, (uint8_t *)b->debug.source, b->debug.source_len);
- } else {
- bc_put_leb128(s, 0);
- }
- }
- for(i = 0; i < b->cpool_count; i++) {
- if (JS_WriteObjectRec(s, b->cpool[i]))
- goto fail;
- }
- return 0;
- fail:
- return -1;
- }
- static int JS_WriteModule(BCWriterState *s, JSValueConst obj)
- {
- JSModuleDef *m = JS_VALUE_GET_PTR(obj);
- int i;
- bc_put_u8(s, BC_TAG_MODULE);
- bc_put_atom(s, m->module_name);
- bc_put_leb128(s, m->req_module_entries_count);
- for(i = 0; i < m->req_module_entries_count; i++) {
- JSReqModuleEntry *rme = &m->req_module_entries[i];
- bc_put_atom(s, rme->module_name);
- if (JS_WriteObjectRec(s, rme->attributes))
- goto fail;
- }
- bc_put_leb128(s, m->export_entries_count);
- for(i = 0; i < m->export_entries_count; i++) {
- JSExportEntry *me = &m->export_entries[i];
- bc_put_u8(s, me->export_type);
- if (me->export_type == JS_EXPORT_TYPE_LOCAL) {
- bc_put_leb128(s, me->u.local.var_idx);
- } else {
- bc_put_leb128(s, me->u.req_module_idx);
- bc_put_atom(s, me->local_name);
- }
- bc_put_atom(s, me->export_name);
- }
- bc_put_leb128(s, m->star_export_entries_count);
- for(i = 0; i < m->star_export_entries_count; i++) {
- JSStarExportEntry *se = &m->star_export_entries[i];
- bc_put_leb128(s, se->req_module_idx);
- }
- bc_put_leb128(s, m->import_entries_count);
- for(i = 0; i < m->import_entries_count; i++) {
- JSImportEntry *mi = &m->import_entries[i];
- bc_put_leb128(s, mi->var_idx);
- bc_put_u8(s, mi->is_star);
- bc_put_atom(s, mi->import_name);
- bc_put_leb128(s, mi->req_module_idx);
- }
- bc_put_u8(s, m->has_tla);
- if (JS_WriteObjectRec(s, m->func_obj))
- goto fail;
- return 0;
- fail:
- return -1;
- }
- /* XXX: be compatible with the structured clone algorithm */
- static int JS_WriteArray(BCWriterState *s, JSValueConst obj)
- {
- JSContext *ctx = s->ctx;
- JSObject *p = JS_VALUE_GET_OBJ(obj);
- uint32_t i, len;
- int ret;
- BOOL is_template;
- JSShapeProperty *prs;
- JSProperty *pr;
-
- if (s->allow_bytecode && !p->extensible) {
- /* not extensible array: we consider it is a
- template when we are saving bytecode */
- bc_put_u8(s, BC_TAG_TEMPLATE_OBJECT);
- is_template = TRUE;
- } else {
- bc_put_u8(s, BC_TAG_ARRAY);
- is_template = FALSE;
- }
- if (js_get_length32(ctx, &len, obj)) /* no side effect */
- goto fail;
- bc_put_leb128(s, len);
- if (p->fast_array) {
- for(i = 0; i < p->u.array.count; i++) {
- ret = JS_WriteObjectRec(s, p->u.array.u.values[i]);
- if (ret)
- goto fail;
- }
- for(i = p->u.array.count; i < len; i++) {
- ret = JS_WriteObjectRec(s, JS_UNDEFINED);
- if (ret)
- goto fail;
- }
- } else {
- for(i = 0; i < len; i++) {
- JSAtom atom;
- atom = JS_NewAtomUInt32(ctx, i);
- if (atom == JS_ATOM_NULL)
- goto fail;
- prs = find_own_property(&pr, p, atom);
- JS_FreeAtom(ctx, atom);
- if (prs && (prs->flags & JS_PROP_ENUMERABLE)) {
- if (prs->flags & JS_PROP_TMASK) {
- JS_ThrowTypeError(ctx, "only value properties are supported");
- goto fail;
- }
- ret = JS_WriteObjectRec(s, pr->u.value);
- if (ret)
- goto fail;
- } else {
- ret = JS_WriteObjectRec(s, JS_UNDEFINED);
- if (ret)
- goto fail;
- }
- }
- }
- if (is_template) {
- /* the 'raw' property is not enumerable */
- prs = find_own_property(&pr, p, JS_ATOM_raw);
- if (prs) {
- if (prs->flags & JS_PROP_TMASK) {
- JS_ThrowTypeError(ctx, "only value properties are supported");
- goto fail;
- }
- ret = JS_WriteObjectRec(s, pr->u.value);
- if (ret)
- goto fail;
- } else {
- ret = JS_WriteObjectRec(s, JS_UNDEFINED);
- if (ret)
- goto fail;
- }
- }
- return 0;
- fail:
- return -1;
- }
- static int JS_WriteObjectTag(BCWriterState *s, JSValueConst obj)
- {
- JSObject *p = JS_VALUE_GET_OBJ(obj);
- uint32_t i, prop_count;
- JSShape *sh;
- JSShapeProperty *pr;
- int pass;
- JSAtom atom;
- bc_put_u8(s, BC_TAG_OBJECT);
- prop_count = 0;
- sh = p->shape;
- for(pass = 0; pass < 2; pass++) {
- if (pass == 1)
- bc_put_leb128(s, prop_count);
- for(i = 0, pr = get_shape_prop(sh); i < sh->prop_count; i++, pr++) {
- atom = pr->atom;
- if (atom != JS_ATOM_NULL &&
- JS_AtomIsString(s->ctx, atom) &&
- (pr->flags & JS_PROP_ENUMERABLE)) {
- if (pr->flags & JS_PROP_TMASK) {
- JS_ThrowTypeError(s->ctx, "only value properties are supported");
- goto fail;
- }
- if (pass == 0) {
- prop_count++;
- } else {
- bc_put_atom(s, atom);
- if (JS_WriteObjectRec(s, p->prop[i].u.value))
- goto fail;
- }
- }
- }
- }
- return 0;
- fail:
- return -1;
- }
- static int JS_WriteTypedArray(BCWriterState *s, JSValueConst obj)
- {
- JSObject *p = JS_VALUE_GET_OBJ(obj);
- JSTypedArray *ta = p->u.typed_array;
- bc_put_u8(s, BC_TAG_TYPED_ARRAY);
- bc_put_u8(s, p->class_id - JS_CLASS_UINT8C_ARRAY);
- bc_put_leb128(s, p->u.array.count);
- bc_put_leb128(s, ta->offset);
- if (JS_WriteObjectRec(s, JS_MKPTR(JS_TAG_OBJECT, ta->buffer)))
- return -1;
- return 0;
- }
- static int JS_WriteArrayBuffer(BCWriterState *s, JSValueConst obj)
- {
- JSObject *p = JS_VALUE_GET_OBJ(obj);
- JSArrayBuffer *abuf = p->u.array_buffer;
- if (abuf->detached) {
- JS_ThrowTypeErrorDetachedArrayBuffer(s->ctx);
- return -1;
- }
- bc_put_u8(s, BC_TAG_ARRAY_BUFFER);
- bc_put_leb128(s, abuf->byte_length);
- bc_put_leb128(s, abuf->max_byte_length);
- dbuf_put(&s->dbuf, abuf->data, abuf->byte_length);
- return 0;
- }
- static int JS_WriteSharedArrayBuffer(BCWriterState *s, JSValueConst obj)
- {
- JSObject *p = JS_VALUE_GET_OBJ(obj);
- JSArrayBuffer *abuf = p->u.array_buffer;
- assert(!abuf->detached); /* SharedArrayBuffer are never detached */
- bc_put_u8(s, BC_TAG_SHARED_ARRAY_BUFFER);
- bc_put_leb128(s, abuf->byte_length);
- bc_put_leb128(s, abuf->max_byte_length);
- bc_put_u64(s, (uintptr_t)abuf->data);
- if (js_resize_array(s->ctx, (void **)&s->sab_tab, sizeof(s->sab_tab[0]),
- &s->sab_tab_size, s->sab_tab_len + 1))
- return -1;
- /* keep the SAB pointer so that the user can clone it or free it */
- s->sab_tab[s->sab_tab_len++] = abuf->data;
- return 0;
- }
- static int JS_WriteObjectRec(BCWriterState *s, JSValueConst obj)
- {
- uint32_t tag;
- if (js_check_stack_overflow(s->ctx->rt, 0)) {
- JS_ThrowStackOverflow(s->ctx);
- return -1;
- }
- tag = JS_VALUE_GET_NORM_TAG(obj);
- switch(tag) {
- case JS_TAG_NULL:
- bc_put_u8(s, BC_TAG_NULL);
- break;
- case JS_TAG_UNDEFINED:
- bc_put_u8(s, BC_TAG_UNDEFINED);
- break;
- case JS_TAG_BOOL:
- bc_put_u8(s, BC_TAG_BOOL_FALSE + JS_VALUE_GET_INT(obj));
- break;
- case JS_TAG_INT:
- bc_put_u8(s, BC_TAG_INT32);
- bc_put_sleb128(s, JS_VALUE_GET_INT(obj));
- break;
- case JS_TAG_FLOAT64:
- {
- JSFloat64Union u;
- bc_put_u8(s, BC_TAG_FLOAT64);
- u.d = JS_VALUE_GET_FLOAT64(obj);
- bc_put_u64(s, u.u64);
- }
- break;
- case JS_TAG_STRING:
- {
- JSString *p = JS_VALUE_GET_STRING(obj);
- bc_put_u8(s, BC_TAG_STRING);
- JS_WriteString(s, p);
- }
- break;
- case JS_TAG_STRING_ROPE:
- {
- JSValue str;
- int ret;
- str = JS_ToString(s->ctx, obj);
- if (JS_IsException(str))
- goto fail;
- ret = JS_WriteObjectRec(s, str);
- JS_FreeValue(s->ctx, str);
- if (ret)
- goto fail;
- }
- break;
- case JS_TAG_FUNCTION_BYTECODE:
- if (!s->allow_bytecode)
- goto invalid_tag;
- if (JS_WriteFunctionTag(s, obj))
- goto fail;
- break;
- case JS_TAG_MODULE:
- if (!s->allow_bytecode)
- goto invalid_tag;
- if (JS_WriteModule(s, obj))
- goto fail;
- break;
- case JS_TAG_OBJECT:
- {
- JSObject *p = JS_VALUE_GET_OBJ(obj);
- int ret, idx;
- if (s->allow_reference) {
- idx = js_object_list_find(s->ctx, &s->object_list, p);
- if (idx >= 0) {
- bc_put_u8(s, BC_TAG_OBJECT_REFERENCE);
- bc_put_leb128(s, idx);
- break;
- } else {
- if (js_object_list_add(s->ctx, &s->object_list, p))
- goto fail;
- }
- } else {
- if (p->tmp_mark) {
- JS_ThrowTypeError(s->ctx, "circular reference");
- goto fail;
- }
- p->tmp_mark = 1;
- }
- switch(p->class_id) {
- case JS_CLASS_ARRAY:
- ret = JS_WriteArray(s, obj);
- break;
- case JS_CLASS_OBJECT:
- ret = JS_WriteObjectTag(s, obj);
- break;
- case JS_CLASS_ARRAY_BUFFER:
- ret = JS_WriteArrayBuffer(s, obj);
- break;
- case JS_CLASS_SHARED_ARRAY_BUFFER:
- if (!s->allow_sab)
- goto invalid_tag;
- ret = JS_WriteSharedArrayBuffer(s, obj);
- break;
- case JS_CLASS_DATE:
- bc_put_u8(s, BC_TAG_DATE);
- ret = JS_WriteObjectRec(s, p->u.object_data);
- break;
- case JS_CLASS_NUMBER:
- case JS_CLASS_STRING:
- case JS_CLASS_BOOLEAN:
- case JS_CLASS_BIG_INT:
- bc_put_u8(s, BC_TAG_OBJECT_VALUE);
- ret = JS_WriteObjectRec(s, p->u.object_data);
- break;
- default:
- if (p->class_id >= JS_CLASS_UINT8C_ARRAY &&
- p->class_id <= JS_CLASS_FLOAT64_ARRAY) {
- ret = JS_WriteTypedArray(s, obj);
- } else {
- JS_ThrowTypeError(s->ctx, "unsupported object class");
- ret = -1;
- }
- break;
- }
- p->tmp_mark = 0;
- if (ret)
- goto fail;
- }
- break;
- case JS_TAG_SHORT_BIG_INT:
- case JS_TAG_BIG_INT:
- if (JS_WriteBigInt(s, obj))
- goto fail;
- break;
- default:
- invalid_tag:
- JS_ThrowInternalError(s->ctx, "unsupported tag (%d)", tag);
- goto fail;
- }
- return 0;
- fail:
- return -1;
- }
- /* create the atom table */
- static int JS_WriteObjectAtoms(BCWriterState *s)
- {
- JSRuntime *rt = s->ctx->rt;
- DynBuf dbuf1;
- int i, atoms_size;
- dbuf1 = s->dbuf;
- js_dbuf_init(s->ctx, &s->dbuf);
- bc_put_u8(s, BC_VERSION);
- bc_put_leb128(s, s->idx_to_atom_count);
- for(i = 0; i < s->idx_to_atom_count; i++) {
- JSAtomStruct *p = rt->atom_array[s->idx_to_atom[i]];
- JS_WriteString(s, p);
- }
- /* XXX: should check for OOM in above phase */
- /* move the atoms at the start */
- /* XXX: could just append dbuf1 data, but it uses more memory if
- dbuf1 is larger than dbuf */
- atoms_size = s->dbuf.size;
- if (dbuf_claim(&dbuf1, atoms_size))
- goto fail;
- memmove(dbuf1.buf + atoms_size, dbuf1.buf, dbuf1.size);
- memcpy(dbuf1.buf, s->dbuf.buf, atoms_size);
- dbuf1.size += atoms_size;
- dbuf_free(&s->dbuf);
- s->dbuf = dbuf1;
- return 0;
- fail:
- dbuf_free(&dbuf1);
- return -1;
- }
- uint8_t *JS_WriteObject2(JSContext *ctx, size_t *psize, JSValueConst obj,
- int flags, uint8_t ***psab_tab, size_t *psab_tab_len)
- {
- BCWriterState ss, *s = &ss;
- memset(s, 0, sizeof(*s));
- s->ctx = ctx;
- s->allow_bytecode = ((flags & JS_WRITE_OBJ_BYTECODE) != 0);
- s->allow_sab = ((flags & JS_WRITE_OBJ_SAB) != 0);
- s->allow_reference = ((flags & JS_WRITE_OBJ_REFERENCE) != 0);
- /* XXX: could use a different version when bytecode is included */
- if (s->allow_bytecode)
- s->first_atom = JS_ATOM_END;
- else
- s->first_atom = 1;
- js_dbuf_init(ctx, &s->dbuf);
- js_object_list_init(&s->object_list);
- if (JS_WriteObjectRec(s, obj))
- goto fail;
- if (JS_WriteObjectAtoms(s))
- goto fail;
- js_object_list_end(ctx, &s->object_list);
- js_free(ctx, s->atom_to_idx);
- js_free(ctx, s->idx_to_atom);
- *psize = s->dbuf.size;
- if (psab_tab)
- *psab_tab = s->sab_tab;
- if (psab_tab_len)
- *psab_tab_len = s->sab_tab_len;
- return s->dbuf.buf;
- fail:
- js_object_list_end(ctx, &s->object_list);
- js_free(ctx, s->atom_to_idx);
- js_free(ctx, s->idx_to_atom);
- dbuf_free(&s->dbuf);
- *psize = 0;
- if (psab_tab)
- *psab_tab = NULL;
- if (psab_tab_len)
- *psab_tab_len = 0;
- return NULL;
- }
- uint8_t *JS_WriteObject(JSContext *ctx, size_t *psize, JSValueConst obj,
- int flags)
- {
- return JS_WriteObject2(ctx, psize, obj, flags, NULL, NULL);
- }
- typedef struct BCReaderState {
- JSContext *ctx;
- const uint8_t *buf_start, *ptr, *buf_end;
- uint32_t first_atom;
- uint32_t idx_to_atom_count;
- JSAtom *idx_to_atom;
- int error_state;
- BOOL allow_sab : 8;
- BOOL allow_bytecode : 8;
- BOOL is_rom_data : 8;
- BOOL allow_reference : 8;
- /* object references */
- JSObject **objects;
- int objects_count;
- int objects_size;
- #ifdef DUMP_READ_OBJECT
- const uint8_t *ptr_last;
- int level;
- #endif
- } BCReaderState;
- #ifdef DUMP_READ_OBJECT
- static void __attribute__((format(printf, 2, 3))) bc_read_trace(BCReaderState *s, const char *fmt, ...) {
- va_list ap;
- int i, n, n0;
- if (!s->ptr_last)
- s->ptr_last = s->buf_start;
- n = n0 = 0;
- if (s->ptr > s->ptr_last || s->ptr == s->buf_start) {
- n0 = printf("%04x: ", (int)(s->ptr_last - s->buf_start));
- n += n0;
- }
- for (i = 0; s->ptr_last < s->ptr; i++) {
- if ((i & 7) == 0 && i > 0) {
- printf("\n%*s", n0, "");
- n = n0;
- }
- n += printf(" %02x", *s->ptr_last++);
- }
- if (*fmt == '}')
- s->level--;
- if (n < 32 + s->level * 2) {
- printf("%*s", 32 + s->level * 2 - n, "");
- }
- va_start(ap, fmt);
- vfprintf(stdout, fmt, ap);
- va_end(ap);
- if (strchr(fmt, '{'))
- s->level++;
- }
- #else
- #define bc_read_trace(...)
- #endif
- static int bc_read_error_end(BCReaderState *s)
- {
- if (!s->error_state) {
- JS_ThrowSyntaxError(s->ctx, "read after the end of the buffer");
- }
- return s->error_state = -1;
- }
- static int bc_get_u8(BCReaderState *s, uint8_t *pval)
- {
- if (unlikely(s->buf_end - s->ptr < 1)) {
- *pval = 0; /* avoid warning */
- return bc_read_error_end(s);
- }
- *pval = *s->ptr++;
- return 0;
- }
- static int bc_get_u16(BCReaderState *s, uint16_t *pval)
- {
- uint16_t v;
- if (unlikely(s->buf_end - s->ptr < 2)) {
- *pval = 0; /* avoid warning */
- return bc_read_error_end(s);
- }
- v = get_u16(s->ptr);
- if (is_be())
- v = bswap16(v);
- *pval = v;
- s->ptr += 2;
- return 0;
- }
- static __maybe_unused int bc_get_u32(BCReaderState *s, uint32_t *pval)
- {
- uint32_t v;
- if (unlikely(s->buf_end - s->ptr < 4)) {
- *pval = 0; /* avoid warning */
- return bc_read_error_end(s);
- }
- v = get_u32(s->ptr);
- if (is_be())
- v = bswap32(v);
- *pval = v;
- s->ptr += 4;
- return 0;
- }
- static int bc_get_u64(BCReaderState *s, uint64_t *pval)
- {
- uint64_t v;
- if (unlikely(s->buf_end - s->ptr < 8)) {
- *pval = 0; /* avoid warning */
- return bc_read_error_end(s);
- }
- v = get_u64(s->ptr);
- if (is_be())
- v = bswap64(v);
- *pval = v;
- s->ptr += 8;
- return 0;
- }
- static int bc_get_leb128(BCReaderState *s, uint32_t *pval)
- {
- int ret;
- ret = get_leb128(pval, s->ptr, s->buf_end);
- if (unlikely(ret < 0))
- return bc_read_error_end(s);
- s->ptr += ret;
- return 0;
- }
- static int bc_get_sleb128(BCReaderState *s, int32_t *pval)
- {
- int ret;
- ret = get_sleb128(pval, s->ptr, s->buf_end);
- if (unlikely(ret < 0))
- return bc_read_error_end(s);
- s->ptr += ret;
- return 0;
- }
- /* XXX: used to read an `int` with a positive value */
- static int bc_get_leb128_int(BCReaderState *s, int *pval)
- {
- return bc_get_leb128(s, (uint32_t *)pval);
- }
- static int bc_get_leb128_u16(BCReaderState *s, uint16_t *pval)
- {
- uint32_t val;
- if (bc_get_leb128(s, &val)) {
- *pval = 0;
- return -1;
- }
- *pval = val;
- return 0;
- }
- static int bc_get_buf(BCReaderState *s, uint8_t *buf, uint32_t buf_len)
- {
- if (buf_len != 0) {
- if (unlikely(!buf || s->buf_end - s->ptr < buf_len))
- return bc_read_error_end(s);
- memcpy(buf, s->ptr, buf_len);
- s->ptr += buf_len;
- }
- return 0;
- }
- static int bc_idx_to_atom(BCReaderState *s, JSAtom *patom, uint32_t idx)
- {
- JSAtom atom;
- if (__JS_AtomIsTaggedInt(idx)) {
- atom = idx;
- } else if (idx < s->first_atom) {
- atom = JS_DupAtom(s->ctx, idx);
- } else {
- idx -= s->first_atom;
- if (idx >= s->idx_to_atom_count) {
- JS_ThrowSyntaxError(s->ctx, "invalid atom index (pos=%u)",
- (unsigned int)(s->ptr - s->buf_start));
- *patom = JS_ATOM_NULL;
- return s->error_state = -1;
- }
- atom = JS_DupAtom(s->ctx, s->idx_to_atom[idx]);
- }
- *patom = atom;
- return 0;
- }
- static int bc_get_atom(BCReaderState *s, JSAtom *patom)
- {
- uint32_t v;
- if (bc_get_leb128(s, &v))
- return -1;
- if (v & 1) {
- *patom = __JS_AtomFromUInt32(v >> 1);
- return 0;
- } else {
- return bc_idx_to_atom(s, patom, v >> 1);
- }
- }
- static JSString *JS_ReadString(BCReaderState *s)
- {
- uint32_t len;
- size_t size;
- BOOL is_wide_char;
- JSString *p;
- if (bc_get_leb128(s, &len))
- return NULL;
- is_wide_char = len & 1;
- len >>= 1;
- if (len > JS_STRING_LEN_MAX) {
- JS_ThrowInternalError(s->ctx, "string too long");
- return NULL;
- }
- p = js_alloc_string(s->ctx, len, is_wide_char);
- if (!p) {
- s->error_state = -1;
- return NULL;
- }
- size = (size_t)len << is_wide_char;
- if ((s->buf_end - s->ptr) < size) {
- bc_read_error_end(s);
- js_free_string(s->ctx->rt, p);
- return NULL;
- }
- memcpy(p->u.str8, s->ptr, size);
- s->ptr += size;
- if (is_wide_char) {
- if (is_be()) {
- uint32_t i;
- for (i = 0; i < len; i++)
- p->u.str16[i] = bswap16(p->u.str16[i]);
- }
- } else {
- p->u.str8[size] = '\0'; /* add the trailing zero for 8 bit strings */
- }
- #ifdef DUMP_READ_OBJECT
- JS_DumpString(s->ctx->rt, p); printf("\n");
- #endif
- return p;
- }
- static uint32_t bc_get_flags(uint32_t flags, int *pidx, int n)
- {
- uint32_t val;
- /* XXX: this does not work for n == 32 */
- val = (flags >> *pidx) & ((1U << n) - 1);
- *pidx += n;
- return val;
- }
- static int JS_ReadFunctionBytecode(BCReaderState *s, JSFunctionBytecode *b,
- int byte_code_offset, uint32_t bc_len)
- {
- uint8_t *bc_buf;
- int pos, len, op;
- JSAtom atom;
- uint32_t idx;
- if (s->is_rom_data) {
- /* directly use the input buffer */
- if (unlikely(s->buf_end - s->ptr < bc_len))
- return bc_read_error_end(s);
- bc_buf = (uint8_t *)s->ptr;
- s->ptr += bc_len;
- } else {
- bc_buf = (void *)((uint8_t*)b + byte_code_offset);
- if (bc_get_buf(s, bc_buf, bc_len))
- return -1;
- }
- b->byte_code_buf = bc_buf;
- if (is_be())
- bc_byte_swap(bc_buf, bc_len);
- pos = 0;
- while (pos < bc_len) {
- op = bc_buf[pos];
- len = short_opcode_info(op).size;
- switch(short_opcode_info(op).fmt) {
- case OP_FMT_atom:
- case OP_FMT_atom_u8:
- case OP_FMT_atom_u16:
- case OP_FMT_atom_label_u8:
- case OP_FMT_atom_label_u16:
- idx = get_u32(bc_buf + pos + 1);
- if (s->is_rom_data) {
- /* just increment the reference count of the atom */
- JS_DupAtom(s->ctx, (JSAtom)idx);
- } else {
- if (bc_idx_to_atom(s, &atom, idx)) {
- /* Note: the atoms will be freed up to this position */
- b->byte_code_len = pos;
- return -1;
- }
- put_u32(bc_buf + pos + 1, atom);
- #ifdef DUMP_READ_OBJECT
- bc_read_trace(s, "at %d, fixup atom: ", pos + 1); print_atom(s->ctx, atom); printf("\n");
- #endif
- }
- break;
- default:
- break;
- }
- pos += len;
- }
- return 0;
- }
- static JSValue JS_ReadBigInt(BCReaderState *s)
- {
- JSValue obj = JS_UNDEFINED;
- uint32_t len, i, n;
- JSBigInt *p;
- js_limb_t v;
- uint8_t v8;
-
- if (bc_get_leb128(s, &len))
- goto fail;
- bc_read_trace(s, "len=%" PRId64 "\n", (int64_t)len);
- if (len == 0) {
- /* zero case */
- bc_read_trace(s, "}\n");
- return __JS_NewShortBigInt(s->ctx, 0);
- }
- p = js_bigint_new(s->ctx, (len - 1) / (JS_LIMB_BITS / 8) + 1);
- if (!p)
- goto fail;
- for(i = 0; i < len / (JS_LIMB_BITS / 8); i++) {
- #if JS_LIMB_BITS == 32
- if (bc_get_u32(s, &v))
- goto fail;
- #else
- if (bc_get_u64(s, &v))
- goto fail;
- #endif
- p->tab[i] = v;
- }
- n = len % (JS_LIMB_BITS / 8);
- if (n != 0) {
- int shift;
- v = 0;
- for(i = 0; i < n; i++) {
- if (bc_get_u8(s, &v8))
- goto fail;
- v |= (js_limb_t)v8 << (i * 8);
- }
- shift = JS_LIMB_BITS - n * 8;
- /* extend the sign */
- if (shift != 0) {
- v = (js_slimb_t)(v << shift) >> shift;
- }
- p->tab[p->len - 1] = v;
- }
- bc_read_trace(s, "}\n");
- return JS_CompactBigInt(s->ctx, p);
- fail:
- JS_FreeValue(s->ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue JS_ReadObjectRec(BCReaderState *s);
- static int BC_add_object_ref1(BCReaderState *s, JSObject *p)
- {
- if (s->allow_reference) {
- if (js_resize_array(s->ctx, (void *)&s->objects,
- sizeof(s->objects[0]),
- &s->objects_size, s->objects_count + 1))
- return -1;
- s->objects[s->objects_count++] = p;
- }
- return 0;
- }
- static int BC_add_object_ref(BCReaderState *s, JSValueConst obj)
- {
- return BC_add_object_ref1(s, JS_VALUE_GET_OBJ(obj));
- }
- static JSValue JS_ReadFunctionTag(BCReaderState *s)
- {
- JSContext *ctx = s->ctx;
- JSFunctionBytecode bc, *b;
- JSValue obj = JS_UNDEFINED;
- uint16_t v16;
- uint8_t v8;
- int idx, i, local_count;
- int cpool_offset, byte_code_offset;
- int closure_var_offset, vardefs_offset;
- uint64_t function_size;
-
- memset(&bc, 0, sizeof(bc));
- if (bc_get_u16(s, &v16))
- goto fail;
- idx = 0;
- bc.has_prototype = bc_get_flags(v16, &idx, 1);
- bc.has_simple_parameter_list = bc_get_flags(v16, &idx, 1);
- bc.is_derived_class_constructor = bc_get_flags(v16, &idx, 1);
- bc.need_home_object = bc_get_flags(v16, &idx, 1);
- bc.func_kind = bc_get_flags(v16, &idx, 2);
- bc.new_target_allowed = bc_get_flags(v16, &idx, 1);
- bc.super_call_allowed = bc_get_flags(v16, &idx, 1);
- bc.super_allowed = bc_get_flags(v16, &idx, 1);
- bc.arguments_allowed = bc_get_flags(v16, &idx, 1);
- bc.has_debug = bc_get_flags(v16, &idx, 1);
- bc.is_direct_or_indirect_eval = bc_get_flags(v16, &idx, 1);
- bc.read_only_bytecode = s->is_rom_data;
- if (bc_get_u8(s, &v8))
- goto fail;
- bc.js_mode = v8;
- if (bc_get_atom(s, &bc.func_name)) //@ atom leak if failure
- goto fail;
- if (bc_get_leb128_u16(s, &bc.arg_count))
- goto fail;
- if (bc_get_leb128_u16(s, &bc.var_count))
- goto fail;
- if (bc_get_leb128_u16(s, &bc.defined_arg_count))
- goto fail;
- if (bc_get_leb128_u16(s, &bc.stack_size))
- goto fail;
- if (bc_get_leb128_u16(s, &bc.var_ref_count))
- goto fail;
- if (bc_get_leb128_int(s, &bc.closure_var_count))
- goto fail;
- if (bc_get_leb128_int(s, &bc.cpool_count))
- goto fail;
- if (bc_get_leb128_int(s, &bc.byte_code_len))
- goto fail;
- if (bc_get_leb128_int(s, &local_count))
- goto fail;
- if (bc.has_debug) {
- function_size = sizeof(*b);
- } else {
- function_size = offsetof(JSFunctionBytecode, debug);
- }
- cpool_offset = function_size;
- function_size += (uint64_t)bc.cpool_count * sizeof(*bc.cpool);
- vardefs_offset = function_size;
- function_size += (uint64_t)local_count * sizeof(*bc.vardefs);
- closure_var_offset = function_size;
- function_size += (uint64_t)bc.closure_var_count * sizeof(*bc.closure_var);
- byte_code_offset = function_size;
- if (!bc.read_only_bytecode) {
- function_size += bc.byte_code_len;
- }
- if (function_size > INT32_MAX)
- return JS_ThrowOutOfMemory(ctx);
- b = js_mallocz(ctx, function_size);
- if (!b)
- return JS_EXCEPTION;
- memcpy(b, &bc, offsetof(JSFunctionBytecode, debug));
- if (local_count != 0) {
- b->vardefs = (void *)((uint8_t*)b + vardefs_offset);
- }
- if (b->closure_var_count != 0) {
- b->closure_var = (void *)((uint8_t*)b + closure_var_offset);
- }
- if (b->cpool_count != 0) {
- b->cpool = (void *)((uint8_t*)b + cpool_offset);
- }
- js_rc(b)->ref_count = 1;
- add_gc_object(ctx->rt, &b->header, JS_GC_OBJ_TYPE_FUNCTION_BYTECODE);
- obj = JS_MKPTR(JS_TAG_FUNCTION_BYTECODE, b);
- #ifdef DUMP_READ_OBJECT
- bc_read_trace(s, "name: "); print_atom(s->ctx, b->func_name); printf("\n");
- #endif
- bc_read_trace(s, "args=%d vars=%d defargs=%d closures=%d cpool=%d\n",
- b->arg_count, b->var_count, b->defined_arg_count,
- b->closure_var_count, b->cpool_count);
- bc_read_trace(s, "stack=%d bclen=%d locals=%d\n",
- b->stack_size, b->byte_code_len, local_count);
- if (local_count != 0) {
- bc_read_trace(s, "vars {\n");
- for(i = 0; i < local_count; i++) {
- JSBytecodeVarDef *vd = &b->vardefs[i];
- if (bc_get_atom(s, &vd->var_name))
- goto fail;
- if (bc_get_leb128_int(s, &vd->scope_next))
- goto fail;
- vd->scope_next--;
- if (bc_get_leb128_u16(s, &vd->var_ref_idx))
- goto fail;
- if (bc_get_u8(s, &v8))
- goto fail;
- idx = 0;
- vd->var_kind = bc_get_flags(v8, &idx, 4);
- vd->is_const = bc_get_flags(v8, &idx, 1);
- vd->is_lexical = bc_get_flags(v8, &idx, 1);
- vd->is_captured = bc_get_flags(v8, &idx, 1);
- vd->has_scope = bc_get_flags(v8, &idx, 1);
- #ifdef DUMP_READ_OBJECT
- bc_read_trace(s, "name: "); print_atom(s->ctx, vd->var_name); printf("\n");
- #endif
- }
- bc_read_trace(s, "}\n");
- }
- if (b->closure_var_count != 0) {
- bc_read_trace(s, "closure vars {\n");
- for(i = 0; i < b->closure_var_count; i++) {
- JSClosureVar *cv = &b->closure_var[i];
- int var_idx;
- if (bc_get_atom(s, &cv->var_name))
- goto fail;
- if (bc_get_leb128_int(s, &var_idx))
- goto fail;
- cv->var_idx = var_idx;
- if (bc_get_u16(s, &v16))
- goto fail;
- idx = 0;
- cv->closure_type = bc_get_flags(v16, &idx, 3);
- cv->is_const = bc_get_flags(v16, &idx, 1);
- cv->is_lexical = bc_get_flags(v16, &idx, 1);
- cv->var_kind = bc_get_flags(v16, &idx, 4);
- #ifdef DUMP_READ_OBJECT
- bc_read_trace(s, "name: "); print_atom(s->ctx, cv->var_name); printf("\n");
- #endif
- }
- bc_read_trace(s, "}\n");
- }
- {
- bc_read_trace(s, "bytecode {\n");
- if (JS_ReadFunctionBytecode(s, b, byte_code_offset, b->byte_code_len))
- goto fail;
- bc_read_trace(s, "}\n");
- }
- if (b->has_debug) {
- /* read optional debug information */
- bc_read_trace(s, "debug {\n");
- if (bc_get_atom(s, &b->debug.filename))
- goto fail;
- #ifdef DUMP_READ_OBJECT
- bc_read_trace(s, "filename: "); print_atom(s->ctx, b->debug.filename); printf("\n");
- #endif
- if (bc_get_leb128_int(s, &b->debug.pc2line_len))
- goto fail;
- if (b->debug.pc2line_len) {
- b->debug.pc2line_buf = js_mallocz(ctx, b->debug.pc2line_len);
- if (!b->debug.pc2line_buf)
- goto fail;
- if (bc_get_buf(s, b->debug.pc2line_buf, b->debug.pc2line_len))
- goto fail;
- }
- if (bc_get_leb128_int(s, &b->debug.source_len))
- goto fail;
- if (b->debug.source_len) {
- bc_read_trace(s, "source: %d bytes\n", b->source_len);
- b->debug.source = js_mallocz(ctx, b->debug.source_len);
- if (!b->debug.source)
- goto fail;
- if (bc_get_buf(s, (uint8_t *)b->debug.source, b->debug.source_len))
- goto fail;
- }
- bc_read_trace(s, "}\n");
- }
- if (b->cpool_count != 0) {
- bc_read_trace(s, "cpool {\n");
- for(i = 0; i < b->cpool_count; i++) {
- JSValue val;
- val = JS_ReadObjectRec(s);
- if (JS_IsException(val))
- goto fail;
- b->cpool[i] = val;
- }
- bc_read_trace(s, "}\n");
- }
- b->realm = JS_DupContext(ctx);
- return obj;
- fail:
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue JS_ReadModule(BCReaderState *s)
- {
- JSContext *ctx = s->ctx;
- JSValue obj;
- JSModuleDef *m = NULL;
- JSAtom module_name;
- int i;
- uint8_t v8;
- if (bc_get_atom(s, &module_name))
- goto fail;
- #ifdef DUMP_READ_OBJECT
- bc_read_trace(s, "name: "); print_atom(s->ctx, module_name); printf("\n");
- #endif
- m = js_new_module_def(ctx, module_name);
- if (!m)
- goto fail;
- obj = JS_NewModuleValue(ctx, m);
- if (bc_get_leb128_int(s, &m->req_module_entries_count))
- goto fail;
- if (m->req_module_entries_count != 0) {
- m->req_module_entries_size = m->req_module_entries_count;
- m->req_module_entries = js_mallocz(ctx, sizeof(m->req_module_entries[0]) * m->req_module_entries_size);
- if (!m->req_module_entries)
- goto fail;
- for(i = 0; i < m->req_module_entries_count; i++) {
- JSReqModuleEntry *rme = &m->req_module_entries[i];
- JSValue val;
- if (bc_get_atom(s, &rme->module_name))
- goto fail;
- val = JS_ReadObjectRec(s);
- if (JS_IsException(val))
- goto fail;
- rme->attributes = val;
- }
- }
- if (bc_get_leb128_int(s, &m->export_entries_count))
- goto fail;
- if (m->export_entries_count != 0) {
- m->export_entries_size = m->export_entries_count;
- m->export_entries = js_mallocz(ctx, sizeof(m->export_entries[0]) * m->export_entries_size);
- if (!m->export_entries)
- goto fail;
- for(i = 0; i < m->export_entries_count; i++) {
- JSExportEntry *me = &m->export_entries[i];
- if (bc_get_u8(s, &v8))
- goto fail;
- me->export_type = v8;
- if (me->export_type == JS_EXPORT_TYPE_LOCAL) {
- if (bc_get_leb128_int(s, &me->u.local.var_idx))
- goto fail;
- } else {
- if (bc_get_leb128_int(s, &me->u.req_module_idx))
- goto fail;
- if (bc_get_atom(s, &me->local_name))
- goto fail;
- }
- if (bc_get_atom(s, &me->export_name))
- goto fail;
- }
- }
- if (bc_get_leb128_int(s, &m->star_export_entries_count))
- goto fail;
- if (m->star_export_entries_count != 0) {
- m->star_export_entries_size = m->star_export_entries_count;
- m->star_export_entries = js_mallocz(ctx, sizeof(m->star_export_entries[0]) * m->star_export_entries_size);
- if (!m->star_export_entries)
- goto fail;
- for(i = 0; i < m->star_export_entries_count; i++) {
- JSStarExportEntry *se = &m->star_export_entries[i];
- if (bc_get_leb128_int(s, &se->req_module_idx))
- goto fail;
- }
- }
- if (bc_get_leb128_int(s, &m->import_entries_count))
- goto fail;
- if (m->import_entries_count != 0) {
- m->import_entries_size = m->import_entries_count;
- m->import_entries = js_mallocz(ctx, sizeof(m->import_entries[0]) * m->import_entries_size);
- if (!m->import_entries)
- goto fail;
- for(i = 0; i < m->import_entries_count; i++) {
- JSImportEntry *mi = &m->import_entries[i];
- uint8_t v8;
- if (bc_get_leb128_int(s, &mi->var_idx))
- goto fail;
- if (bc_get_u8(s, &v8))
- goto fail;
- mi->is_star = (v8 != 0);
- if (bc_get_atom(s, &mi->import_name))
- goto fail;
- if (bc_get_leb128_int(s, &mi->req_module_idx))
- goto fail;
- }
- }
- if (bc_get_u8(s, &v8))
- goto fail;
- m->has_tla = (v8 != 0);
- m->func_obj = JS_ReadObjectRec(s);
- if (JS_IsException(m->func_obj))
- goto fail;
- return obj;
- fail:
- if (m) {
- JS_FreeValue(ctx, JS_MKPTR(JS_TAG_MODULE, m));
- }
- return JS_EXCEPTION;
- }
- static JSValue JS_ReadObjectTag(BCReaderState *s)
- {
- JSContext *ctx = s->ctx;
- JSValue obj;
- uint32_t prop_count, i;
- JSAtom atom;
- JSValue val;
- int ret;
- obj = JS_NewObject(ctx);
- if (BC_add_object_ref(s, obj))
- goto fail;
- if (bc_get_leb128(s, &prop_count))
- goto fail;
- for(i = 0; i < prop_count; i++) {
- if (bc_get_atom(s, &atom))
- goto fail;
- #ifdef DUMP_READ_OBJECT
- bc_read_trace(s, "propname: "); print_atom(s->ctx, atom); printf("\n");
- #endif
- val = JS_ReadObjectRec(s);
- if (JS_IsException(val)) {
- JS_FreeAtom(ctx, atom);
- goto fail;
- }
- ret = JS_DefinePropertyValue(ctx, obj, atom, val, JS_PROP_C_W_E);
- JS_FreeAtom(ctx, atom);
- if (ret < 0)
- goto fail;
- }
- return obj;
- fail:
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue JS_ReadArray(BCReaderState *s, int tag)
- {
- JSContext *ctx = s->ctx;
- JSValue obj;
- uint32_t len, i;
- JSValue val;
- int ret, prop_flags;
- BOOL is_template;
- obj = JS_NewArray(ctx);
- if (BC_add_object_ref(s, obj))
- goto fail;
- is_template = (tag == BC_TAG_TEMPLATE_OBJECT);
- if (bc_get_leb128(s, &len))
- goto fail;
- for(i = 0; i < len; i++) {
- val = JS_ReadObjectRec(s);
- if (JS_IsException(val))
- goto fail;
- if (is_template)
- prop_flags = JS_PROP_ENUMERABLE;
- else
- prop_flags = JS_PROP_C_W_E;
- ret = JS_DefinePropertyValueUint32(ctx, obj, i, val,
- prop_flags);
- if (ret < 0)
- goto fail;
- }
- if (is_template) {
- val = JS_ReadObjectRec(s);
- if (JS_IsException(val))
- goto fail;
- if (!JS_IsUndefined(val)) {
- ret = JS_DefinePropertyValue(ctx, obj, JS_ATOM_raw, val, 0);
- if (ret < 0)
- goto fail;
- }
- JS_PreventExtensions(ctx, obj);
- }
- return obj;
- fail:
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue JS_ReadTypedArray(BCReaderState *s)
- {
- JSContext *ctx = s->ctx;
- JSValue obj = JS_UNDEFINED, array_buffer = JS_UNDEFINED;
- uint8_t array_tag;
- JSValueConst args[3];
- uint32_t offset, len, idx;
- if (bc_get_u8(s, &array_tag))
- return JS_EXCEPTION;
- if (array_tag >= JS_TYPED_ARRAY_COUNT)
- return JS_ThrowTypeError(ctx, "invalid typed array");
- if (bc_get_leb128(s, &len))
- return JS_EXCEPTION;
- if (bc_get_leb128(s, &offset))
- return JS_EXCEPTION;
- /* XXX: this hack could be avoided if the typed array could be
- created before the array buffer */
- idx = s->objects_count;
- if (BC_add_object_ref1(s, NULL))
- goto fail;
- array_buffer = JS_ReadObjectRec(s);
- if (JS_IsException(array_buffer))
- return JS_EXCEPTION;
- if (!js_get_array_buffer(ctx, array_buffer)) {
- JS_FreeValue(ctx, array_buffer);
- return JS_EXCEPTION;
- }
- args[0] = array_buffer;
- args[1] = JS_NewInt64(ctx, offset);
- args[2] = JS_NewInt64(ctx, len);
- obj = js_typed_array_constructor(ctx, JS_UNDEFINED,
- 3, args,
- JS_CLASS_UINT8C_ARRAY + array_tag);
- if (JS_IsException(obj))
- goto fail;
- if (s->allow_reference) {
- s->objects[idx] = JS_VALUE_GET_OBJ(obj);
- }
- JS_FreeValue(ctx, array_buffer);
- return obj;
- fail:
- JS_FreeValue(ctx, array_buffer);
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue JS_ReadArrayBuffer(BCReaderState *s)
- {
- JSContext *ctx = s->ctx;
- uint32_t byte_length, max_byte_length;
- uint64_t max_byte_length_u64, *pmax_byte_length = NULL;
- JSValue obj;
- if (bc_get_leb128(s, &byte_length))
- return JS_EXCEPTION;
- if (bc_get_leb128(s, &max_byte_length))
- return JS_EXCEPTION;
- if (max_byte_length < byte_length)
- return JS_ThrowTypeError(ctx, "invalid array buffer");
- if (max_byte_length != UINT32_MAX) {
- max_byte_length_u64 = max_byte_length;
- pmax_byte_length = &max_byte_length_u64;
- }
- if (unlikely(s->buf_end - s->ptr < byte_length)) {
- bc_read_error_end(s);
- return JS_EXCEPTION;
- }
- // makes a copy of the input
- obj = js_array_buffer_constructor3(ctx, JS_UNDEFINED,
- byte_length, pmax_byte_length,
- JS_CLASS_ARRAY_BUFFER,
- (uint8_t*)s->ptr,
- js_array_buffer_free, NULL,
- /*alloc_flag*/TRUE);
- if (JS_IsException(obj))
- goto fail;
- if (BC_add_object_ref(s, obj))
- goto fail;
- s->ptr += byte_length;
- return obj;
- fail:
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue JS_ReadSharedArrayBuffer(BCReaderState *s)
- {
- JSContext *ctx = s->ctx;
- uint32_t byte_length, max_byte_length;
- uint64_t max_byte_length_u64, *pmax_byte_length = NULL;
- uint8_t *data_ptr;
- JSValue obj;
- uint64_t u64;
- if (bc_get_leb128(s, &byte_length))
- return JS_EXCEPTION;
- if (bc_get_leb128(s, &max_byte_length))
- return JS_EXCEPTION;
- if (max_byte_length < byte_length)
- return JS_ThrowTypeError(ctx, "invalid array buffer");
- if (max_byte_length != UINT32_MAX) {
- max_byte_length_u64 = max_byte_length;
- pmax_byte_length = &max_byte_length_u64;
- }
- if (bc_get_u64(s, &u64))
- return JS_EXCEPTION;
- data_ptr = (uint8_t *)(uintptr_t)u64;
- /* the SharedArrayBuffer is cloned */
- obj = js_array_buffer_constructor3(ctx, JS_UNDEFINED,
- byte_length, pmax_byte_length,
- JS_CLASS_SHARED_ARRAY_BUFFER,
- data_ptr,
- NULL, NULL, FALSE);
- if (JS_IsException(obj))
- goto fail;
- if (BC_add_object_ref(s, obj))
- goto fail;
- return obj;
- fail:
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue JS_ReadDate(BCReaderState *s)
- {
- JSContext *ctx = s->ctx;
- JSValue val, obj = JS_UNDEFINED;
- val = JS_ReadObjectRec(s);
- if (JS_IsException(val))
- goto fail;
- if (!JS_IsNumber(val)) {
- JS_ThrowTypeError(ctx, "Number tag expected for date");
- goto fail;
- }
- obj = JS_NewObjectProtoClass(ctx, ctx->class_proto[JS_CLASS_DATE],
- JS_CLASS_DATE);
- if (JS_IsException(obj))
- goto fail;
- if (BC_add_object_ref(s, obj))
- goto fail;
- JS_SetObjectData(ctx, obj, val);
- return obj;
- fail:
- JS_FreeValue(ctx, val);
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue JS_ReadObjectValue(BCReaderState *s)
- {
- JSContext *ctx = s->ctx;
- JSValue val, obj = JS_UNDEFINED;
- val = JS_ReadObjectRec(s);
- if (JS_IsException(val))
- goto fail;
- obj = JS_ToObject(ctx, val);
- if (JS_IsException(obj))
- goto fail;
- if (BC_add_object_ref(s, obj))
- goto fail;
- JS_FreeValue(ctx, val);
- return obj;
- fail:
- JS_FreeValue(ctx, val);
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue JS_ReadObjectRec(BCReaderState *s)
- {
- JSContext *ctx = s->ctx;
- uint8_t tag;
- JSValue obj = JS_UNDEFINED;
- if (js_check_stack_overflow(ctx->rt, 0))
- return JS_ThrowStackOverflow(ctx);
- if (bc_get_u8(s, &tag))
- return JS_EXCEPTION;
- bc_read_trace(s, "%s {\n", bc_tag_str[tag]);
- switch(tag) {
- case BC_TAG_NULL:
- obj = JS_NULL;
- break;
- case BC_TAG_UNDEFINED:
- obj = JS_UNDEFINED;
- break;
- case BC_TAG_BOOL_FALSE:
- case BC_TAG_BOOL_TRUE:
- obj = JS_NewBool(ctx, tag - BC_TAG_BOOL_FALSE);
- break;
- case BC_TAG_INT32:
- {
- int32_t val;
- if (bc_get_sleb128(s, &val))
- return JS_EXCEPTION;
- bc_read_trace(s, "%d\n", val);
- obj = JS_NewInt32(ctx, val);
- }
- break;
- case BC_TAG_FLOAT64:
- {
- JSFloat64Union u;
- if (bc_get_u64(s, &u.u64))
- return JS_EXCEPTION;
- bc_read_trace(s, "%g\n", u.d);
- obj = __JS_NewFloat64(ctx, u.d);
- }
- break;
- case BC_TAG_STRING:
- {
- JSString *p;
- p = JS_ReadString(s);
- if (!p)
- return JS_EXCEPTION;
- obj = JS_MKPTR(JS_TAG_STRING, p);
- }
- break;
- case BC_TAG_FUNCTION_BYTECODE:
- if (!s->allow_bytecode)
- goto invalid_tag;
- obj = JS_ReadFunctionTag(s);
- break;
- case BC_TAG_MODULE:
- if (!s->allow_bytecode)
- goto invalid_tag;
- obj = JS_ReadModule(s);
- break;
- case BC_TAG_OBJECT:
- obj = JS_ReadObjectTag(s);
- break;
- case BC_TAG_ARRAY:
- case BC_TAG_TEMPLATE_OBJECT:
- obj = JS_ReadArray(s, tag);
- break;
- case BC_TAG_TYPED_ARRAY:
- obj = JS_ReadTypedArray(s);
- break;
- case BC_TAG_ARRAY_BUFFER:
- obj = JS_ReadArrayBuffer(s);
- break;
- case BC_TAG_SHARED_ARRAY_BUFFER:
- if (!s->allow_sab || !ctx->rt->sab_funcs.sab_dup)
- goto invalid_tag;
- obj = JS_ReadSharedArrayBuffer(s);
- break;
- case BC_TAG_DATE:
- obj = JS_ReadDate(s);
- break;
- case BC_TAG_OBJECT_VALUE:
- obj = JS_ReadObjectValue(s);
- break;
- case BC_TAG_BIG_INT:
- obj = JS_ReadBigInt(s);
- break;
- case BC_TAG_OBJECT_REFERENCE:
- {
- uint32_t val;
- if (!s->allow_reference)
- return JS_ThrowSyntaxError(ctx, "object references are not allowed");
- if (bc_get_leb128(s, &val))
- return JS_EXCEPTION;
- bc_read_trace(s, "%u\n", val);
- if (val >= s->objects_count) {
- return JS_ThrowSyntaxError(ctx, "invalid object reference (%u >= %u)",
- val, s->objects_count);
- }
- obj = JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, s->objects[val]));
- }
- break;
- default:
- invalid_tag:
- return JS_ThrowSyntaxError(ctx, "invalid tag (tag=%d pos=%u)",
- tag, (unsigned int)(s->ptr - s->buf_start));
- }
- bc_read_trace(s, "}\n");
- return obj;
- }
- static int JS_ReadObjectAtoms(BCReaderState *s)
- {
- uint8_t v8;
- JSString *p;
- int i;
- JSAtom atom;
- if (bc_get_u8(s, &v8))
- return -1;
- if (v8 != BC_VERSION) {
- JS_ThrowSyntaxError(s->ctx, "invalid version (%d expected=%d)",
- v8, BC_VERSION);
- return -1;
- }
- if (bc_get_leb128(s, &s->idx_to_atom_count))
- return -1;
- bc_read_trace(s, "%d atom indexes {\n", s->idx_to_atom_count);
- if (s->idx_to_atom_count != 0) {
- s->idx_to_atom = js_mallocz(s->ctx, s->idx_to_atom_count *
- sizeof(s->idx_to_atom[0]));
- if (!s->idx_to_atom)
- return s->error_state = -1;
- }
- for(i = 0; i < s->idx_to_atom_count; i++) {
- p = JS_ReadString(s);
- if (!p)
- return -1;
- atom = JS_NewAtomStr(s->ctx, p);
- if (atom == JS_ATOM_NULL)
- return s->error_state = -1;
- s->idx_to_atom[i] = atom;
- if (s->is_rom_data && (atom != (i + s->first_atom)))
- s->is_rom_data = FALSE; /* atoms must be relocated */
- }
- bc_read_trace(s, "}\n");
- return 0;
- }
- static void bc_reader_free(BCReaderState *s)
- {
- int i;
- if (s->idx_to_atom) {
- for(i = 0; i < s->idx_to_atom_count; i++) {
- JS_FreeAtom(s->ctx, s->idx_to_atom[i]);
- }
- js_free(s->ctx, s->idx_to_atom);
- }
- js_free(s->ctx, s->objects);
- }
- JSValue JS_ReadObject(JSContext *ctx, const uint8_t *buf, size_t buf_len,
- int flags)
- {
- BCReaderState ss, *s = &ss;
- JSValue obj;
- ctx->binary_object_count += 1;
- ctx->binary_object_size += buf_len;
- memset(s, 0, sizeof(*s));
- s->ctx = ctx;
- s->buf_start = buf;
- s->buf_end = buf + buf_len;
- s->ptr = buf;
- s->allow_bytecode = ((flags & JS_READ_OBJ_BYTECODE) != 0);
- s->is_rom_data = ((flags & JS_READ_OBJ_ROM_DATA) != 0);
- s->allow_sab = ((flags & JS_READ_OBJ_SAB) != 0);
- s->allow_reference = ((flags & JS_READ_OBJ_REFERENCE) != 0);
- if (s->allow_bytecode)
- s->first_atom = JS_ATOM_END;
- else
- s->first_atom = 1;
- if (JS_ReadObjectAtoms(s)) {
- obj = JS_EXCEPTION;
- } else {
- obj = JS_ReadObjectRec(s);
- }
- bc_reader_free(s);
- return obj;
- }
- /*******************************************************************/
- /* runtime functions & objects */
- static JSValue js_string_constructor(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv);
- static JSValue js_boolean_constructor(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv);
- static JSValue js_number_constructor(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv);
- static int check_function(JSContext *ctx, JSValueConst obj)
- {
- if (likely(JS_IsFunction(ctx, obj)))
- return 0;
- JS_ThrowTypeError(ctx, "not a function");
- return -1;
- }
- static int check_exception_free(JSContext *ctx, JSValue obj)
- {
- JS_FreeValue(ctx, obj);
- return JS_IsException(obj);
- }
- static JSAtom find_atom(JSContext *ctx, const char *name)
- {
- JSAtom atom;
- int len;
- if (*name == '[') {
- name++;
- len = strlen(name) - 1;
- /* We assume 8 bit non null strings, which is the case for these
- symbols */
- for(atom = JS_ATOM_Symbol_toPrimitive; atom < JS_ATOM_END; atom++) {
- JSAtomStruct *p = ctx->rt->atom_array[atom];
- JSString *str = p;
- if (str->len == len && !memcmp(str->u.str8, name, len))
- return JS_DupAtom(ctx, atom);
- }
- abort();
- } else {
- atom = JS_NewAtom(ctx, name);
- }
- return atom;
- }
- static JSValue JS_NewObjectProtoList(JSContext *ctx, JSValueConst proto,
- const JSCFunctionListEntry *fields, int n_fields)
- {
- JSValue obj;
- obj = JS_NewObjectProtoClassAlloc(ctx, proto, JS_CLASS_OBJECT, n_fields);
- if (JS_IsException(obj))
- return obj;
- if (JS_SetPropertyFunctionList(ctx, obj, fields, n_fields)) {
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- return obj;
- }
- static JSValue JS_InstantiateFunctionListItem2(JSContext *ctx, JSObject *p,
- JSAtom atom, void *opaque)
- {
- const JSCFunctionListEntry *e = opaque;
- JSValue val, proto;
- switch(e->def_type) {
- case JS_DEF_CFUNC:
- val = JS_NewCFunction2(ctx, e->u.func.cfunc.generic,
- e->name, e->u.func.length, e->u.func.cproto, e->magic);
- break;
- case JS_DEF_PROP_STRING:
- val = JS_NewAtomString(ctx, e->u.str);
- break;
- case JS_DEF_OBJECT:
- /* XXX: could add a flag */
- if (atom == JS_ATOM_Symbol_unscopables)
- proto = JS_NULL;
- else
- proto = ctx->class_proto[JS_CLASS_OBJECT];
- val = JS_NewObjectProtoList(ctx, proto,
- e->u.prop_list.tab, e->u.prop_list.len);
- break;
- default:
- abort();
- }
- return val;
- }
- static int JS_InstantiateFunctionListItem(JSContext *ctx, JSValueConst obj,
- JSAtom atom,
- const JSCFunctionListEntry *e)
- {
- JSValue val;
- int prop_flags = e->prop_flags;
- switch(e->def_type) {
- case JS_DEF_ALIAS: /* using autoinit for aliases is not safe */
- {
- JSAtom atom1 = find_atom(ctx, e->u.alias.name);
- switch (e->u.alias.base) {
- case -1:
- val = JS_GetProperty(ctx, obj, atom1);
- break;
- case 0:
- val = JS_GetProperty(ctx, ctx->global_obj, atom1);
- break;
- case 1:
- val = JS_GetProperty(ctx, ctx->class_proto[JS_CLASS_ARRAY], atom1);
- break;
- default:
- abort();
- }
- JS_FreeAtom(ctx, atom1);
- if (JS_IsException(val))
- return -1;
- if (atom == JS_ATOM_Symbol_toPrimitive) {
- /* Symbol.toPrimitive functions are not writable */
- prop_flags = JS_PROP_CONFIGURABLE;
- } else if (atom == JS_ATOM_Symbol_hasInstance) {
- /* Function.prototype[Symbol.hasInstance] is not writable nor configurable */
- prop_flags = 0;
- }
- }
- break;
- case JS_DEF_CFUNC:
- if (atom == JS_ATOM_Symbol_toPrimitive) {
- /* Symbol.toPrimitive functions are not writable */
- prop_flags = JS_PROP_CONFIGURABLE;
- } else if (atom == JS_ATOM_Symbol_hasInstance) {
- /* Function.prototype[Symbol.hasInstance] is not writable nor configurable */
- prop_flags = 0;
- }
- if (JS_DefineAutoInitProperty(ctx, obj, atom, JS_AUTOINIT_ID_PROP,
- (void *)e, prop_flags) < 0)
- return -1;
- return 0;
- case JS_DEF_CGETSET: /* XXX: use autoinit again ? */
- case JS_DEF_CGETSET_MAGIC:
- {
- JSValue getter, setter;
- char buf[64];
- getter = JS_UNDEFINED;
- if (e->u.getset.get.generic) {
- snprintf(buf, sizeof(buf), "get %s", e->name);
- getter = JS_NewCFunction2(ctx, e->u.getset.get.generic,
- buf, 0, e->def_type == JS_DEF_CGETSET_MAGIC ? JS_CFUNC_getter_magic : JS_CFUNC_getter,
- e->magic);
- if (JS_IsException(getter))
- return -1;
- }
- setter = JS_UNDEFINED;
- if (e->u.getset.set.generic) {
- snprintf(buf, sizeof(buf), "set %s", e->name);
- setter = JS_NewCFunction2(ctx, e->u.getset.set.generic,
- buf, 1, e->def_type == JS_DEF_CGETSET_MAGIC ? JS_CFUNC_setter_magic : JS_CFUNC_setter,
- e->magic);
- if (JS_IsException(setter)) {
- JS_FreeValue(ctx, getter);
- return -1;
- }
- }
- if (JS_DefinePropertyGetSet(ctx, obj, atom, getter, setter, prop_flags) < 0)
- return -1;
- return 0;
- }
- break;
- case JS_DEF_PROP_INT32:
- val = JS_NewInt32(ctx, e->u.i32);
- break;
- case JS_DEF_PROP_INT64:
- val = JS_NewInt64(ctx, e->u.i64);
- break;
- case JS_DEF_PROP_DOUBLE:
- val = __JS_NewFloat64(ctx, e->u.f64);
- break;
- case JS_DEF_PROP_UNDEFINED:
- val = JS_UNDEFINED;
- break;
- case JS_DEF_PROP_ATOM:
- val = JS_AtomToValue(ctx, e->u.i32);
- break;
- case JS_DEF_PROP_BOOL:
- val = JS_NewBool(ctx, e->u.i32);
- break;
- case JS_DEF_PROP_STRING:
- case JS_DEF_OBJECT:
- if (JS_DefineAutoInitProperty(ctx, obj, atom, JS_AUTOINIT_ID_PROP,
- (void *)e, prop_flags) < 0)
- return -1;
- return 0;
- default:
- abort();
- }
- if (JS_DefinePropertyValue(ctx, obj, atom, val, prop_flags) < 0)
- return -1;
- return 0;
- }
- int JS_SetPropertyFunctionList(JSContext *ctx, JSValueConst obj,
- const JSCFunctionListEntry *tab, int len)
- {
- int i, ret;
- for (i = 0; i < len; i++) {
- const JSCFunctionListEntry *e = &tab[i];
- JSAtom atom = find_atom(ctx, e->name);
- if (atom == JS_ATOM_NULL)
- return -1;
- ret = JS_InstantiateFunctionListItem(ctx, obj, atom, e);
- JS_FreeAtom(ctx, atom);
- if (ret)
- return -1;
- }
- return 0;
- }
- int JS_AddModuleExportList(JSContext *ctx, JSModuleDef *m,
- const JSCFunctionListEntry *tab, int len)
- {
- int i;
- for(i = 0; i < len; i++) {
- if (JS_AddModuleExport(ctx, m, tab[i].name))
- return -1;
- }
- return 0;
- }
- int JS_SetModuleExportList(JSContext *ctx, JSModuleDef *m,
- const JSCFunctionListEntry *tab, int len)
- {
- int i;
- JSValue val;
- for(i = 0; i < len; i++) {
- const JSCFunctionListEntry *e = &tab[i];
- switch(e->def_type) {
- case JS_DEF_CFUNC:
- val = JS_NewCFunction2(ctx, e->u.func.cfunc.generic,
- e->name, e->u.func.length, e->u.func.cproto, e->magic);
- break;
- case JS_DEF_PROP_STRING:
- val = JS_NewString(ctx, e->u.str);
- break;
- case JS_DEF_PROP_INT32:
- val = JS_NewInt32(ctx, e->u.i32);
- break;
- case JS_DEF_PROP_INT64:
- val = JS_NewInt64(ctx, e->u.i64);
- break;
- case JS_DEF_PROP_DOUBLE:
- val = __JS_NewFloat64(ctx, e->u.f64);
- break;
- case JS_DEF_OBJECT:
- val = JS_NewObjectProtoList(ctx, ctx->class_proto[JS_CLASS_OBJECT],
- e->u.prop_list.tab, e->u.prop_list.len);
- break;
- default:
- abort();
- }
- if (JS_SetModuleExport(ctx, m, e->name, val))
- return -1;
- }
- return 0;
- }
- /* Note: 'func_obj' is not necessarily a constructor */
- static int JS_SetConstructor2(JSContext *ctx,
- JSValueConst func_obj,
- JSValueConst proto,
- int proto_flags, int ctor_flags)
- {
- if (JS_DefinePropertyValue(ctx, func_obj, JS_ATOM_prototype,
- JS_DupValue(ctx, proto), proto_flags) < 0)
- return -1;
- if (JS_DefinePropertyValue(ctx, proto, JS_ATOM_constructor,
- JS_DupValue(ctx, func_obj),
- ctor_flags) < 0)
- return -1;
- set_cycle_flag(ctx, func_obj);
- set_cycle_flag(ctx, proto);
- return 0;
- }
- /* return 0 if OK, -1 if exception */
- int JS_SetConstructor(JSContext *ctx, JSValueConst func_obj,
- JSValueConst proto)
- {
- return JS_SetConstructor2(ctx, func_obj, proto,
- 0, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
- }
- #define JS_NEW_CTOR_NO_GLOBAL (1 << 0) /* don't create a global binding */
- #define JS_NEW_CTOR_PROTO_CLASS (1 << 1) /* the prototype class is 'class_id' instead of JS_CLASS_OBJECT */
- #define JS_NEW_CTOR_PROTO_EXIST (1 << 2) /* the prototype is already defined */
- #define JS_NEW_CTOR_READONLY (1 << 3) /* read-only constructor field */
- /* Return the constructor and. Define it as a global variable unless
- JS_NEW_CTOR_NO_GLOBAL is set. The new class inherit from
- parent_ctor if it is not JS_UNDEFINED. if class_id is != -1,
- class_proto[class_id] is set. */
- static JSValue JS_NewCConstructor(JSContext *ctx, int class_id, const char *name,
- JSCFunction *func, int length, JSCFunctionEnum cproto, int magic,
- JSValueConst parent_ctor,
- const JSCFunctionListEntry *ctor_fields, int n_ctor_fields,
- const JSCFunctionListEntry *proto_fields, int n_proto_fields,
- int flags)
- {
- JSValue ctor = JS_UNDEFINED, proto, parent_proto;
- int proto_class_id, proto_flags, ctor_flags;
- proto_flags = 0;
- if (flags & JS_NEW_CTOR_READONLY) {
- ctor_flags = JS_PROP_CONFIGURABLE;
- } else {
- ctor_flags = JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE;
- }
-
- if (JS_IsUndefined(parent_ctor)) {
- parent_proto = JS_DupValue(ctx, ctx->class_proto[JS_CLASS_OBJECT]);
- parent_ctor = ctx->function_proto;
- } else {
- parent_proto = JS_GetProperty(ctx, parent_ctor, JS_ATOM_prototype);
- if (JS_IsException(parent_proto))
- return JS_EXCEPTION;
- }
-
- if (flags & JS_NEW_CTOR_PROTO_EXIST) {
- proto = JS_DupValue(ctx, ctx->class_proto[class_id]);
- } else {
- if (flags & JS_NEW_CTOR_PROTO_CLASS)
- proto_class_id = class_id;
- else
- proto_class_id = JS_CLASS_OBJECT;
- /* one additional field: constructor */
- proto = JS_NewObjectProtoClassAlloc(ctx, parent_proto, proto_class_id,
- n_proto_fields + 1);
- if (JS_IsException(proto))
- goto fail;
- if (class_id >= 0)
- ctx->class_proto[class_id] = JS_DupValue(ctx, proto);
- }
- if (JS_SetPropertyFunctionList(ctx, proto, proto_fields, n_proto_fields))
- goto fail;
- /* additional fields: name, length, prototype */
- ctor = JS_NewCFunction3(ctx, func, name, length, cproto, magic, parent_ctor,
- n_ctor_fields + 3);
- if (JS_IsException(ctor))
- goto fail;
- if (JS_SetPropertyFunctionList(ctx, ctor, ctor_fields, n_ctor_fields))
- goto fail;
- if (!(flags & JS_NEW_CTOR_NO_GLOBAL)) {
- if (JS_DefinePropertyValueStr(ctx, ctx->global_obj, name,
- JS_DupValue(ctx, ctor),
- JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE) < 0)
- goto fail;
- }
- JS_SetConstructor2(ctx, ctor, proto, proto_flags, ctor_flags);
- JS_FreeValue(ctx, proto);
- JS_FreeValue(ctx, parent_proto);
- return ctor;
- fail:
- JS_FreeValue(ctx, proto);
- JS_FreeValue(ctx, parent_proto);
- JS_FreeValue(ctx, ctor);
- return JS_EXCEPTION;
- }
- static JSValue js_global_eval(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- return JS_EvalObject(ctx, ctx->global_obj, argv[0], JS_EVAL_TYPE_INDIRECT, -1);
- }
- static JSValue js_global_isNaN(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- double d;
- if (unlikely(JS_ToFloat64(ctx, &d, argv[0])))
- return JS_EXCEPTION;
- return JS_NewBool(ctx, isnan(d));
- }
- static JSValue js_global_isFinite(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- double d;
- if (unlikely(JS_ToFloat64(ctx, &d, argv[0])))
- return JS_EXCEPTION;
- return JS_NewBool(ctx, isfinite(d));
- }
- /* Object class */
- static JSValue JS_ToObject(JSContext *ctx, JSValueConst val)
- {
- int tag = JS_VALUE_GET_NORM_TAG(val);
- JSValue obj;
- switch(tag) {
- default:
- case JS_TAG_NULL:
- case JS_TAG_UNDEFINED:
- return JS_ThrowTypeError(ctx, "cannot convert to object");
- case JS_TAG_OBJECT:
- case JS_TAG_EXCEPTION:
- return JS_DupValue(ctx, val);
- case JS_TAG_SHORT_BIG_INT:
- case JS_TAG_BIG_INT:
- obj = JS_NewObjectClass(ctx, JS_CLASS_BIG_INT);
- goto set_value;
- case JS_TAG_INT:
- case JS_TAG_FLOAT64:
- obj = JS_NewObjectClass(ctx, JS_CLASS_NUMBER);
- goto set_value;
- case JS_TAG_STRING:
- case JS_TAG_STRING_ROPE:
- /* XXX: should call the string constructor */
- {
- JSValue str;
- str = JS_ToString(ctx, val); /* ensure that we never store a rope */
- if (JS_IsException(str))
- return JS_EXCEPTION;
- obj = JS_NewObjectClass(ctx, JS_CLASS_STRING);
- if (!JS_IsException(obj)) {
- JS_DefinePropertyValue(ctx, obj, JS_ATOM_length,
- JS_NewInt32(ctx, JS_VALUE_GET_STRING(str)->len), 0);
- JS_SetObjectData(ctx, obj, JS_DupValue(ctx, str));
- }
- JS_FreeValue(ctx, str);
- return obj;
- }
- case JS_TAG_BOOL:
- obj = JS_NewObjectClass(ctx, JS_CLASS_BOOLEAN);
- goto set_value;
- case JS_TAG_SYMBOL:
- obj = JS_NewObjectClass(ctx, JS_CLASS_SYMBOL);
- set_value:
- if (!JS_IsException(obj))
- JS_SetObjectData(ctx, obj, JS_DupValue(ctx, val));
- return obj;
- }
- }
- static JSValue JS_ToObjectFree(JSContext *ctx, JSValue val)
- {
- JSValue obj = JS_ToObject(ctx, val);
- JS_FreeValue(ctx, val);
- return obj;
- }
- static int js_obj_to_desc(JSContext *ctx, JSPropertyDescriptor *d,
- JSValueConst desc)
- {
- JSValue val, getter, setter;
- int flags;
- if (!JS_IsObject(desc)) {
- JS_ThrowTypeErrorNotAnObject(ctx);
- return -1;
- }
- flags = 0;
- val = JS_UNDEFINED;
- getter = JS_UNDEFINED;
- setter = JS_UNDEFINED;
- if (JS_HasProperty(ctx, desc, JS_ATOM_enumerable)) {
- JSValue prop = JS_GetProperty(ctx, desc, JS_ATOM_enumerable);
- if (JS_IsException(prop))
- goto fail;
- flags |= JS_PROP_HAS_ENUMERABLE;
- if (JS_ToBoolFree(ctx, prop))
- flags |= JS_PROP_ENUMERABLE;
- }
- if (JS_HasProperty(ctx, desc, JS_ATOM_configurable)) {
- JSValue prop = JS_GetProperty(ctx, desc, JS_ATOM_configurable);
- if (JS_IsException(prop))
- goto fail;
- flags |= JS_PROP_HAS_CONFIGURABLE;
- if (JS_ToBoolFree(ctx, prop))
- flags |= JS_PROP_CONFIGURABLE;
- }
- if (JS_HasProperty(ctx, desc, JS_ATOM_value)) {
- flags |= JS_PROP_HAS_VALUE;
- val = JS_GetProperty(ctx, desc, JS_ATOM_value);
- if (JS_IsException(val))
- goto fail;
- }
- if (JS_HasProperty(ctx, desc, JS_ATOM_writable)) {
- JSValue prop = JS_GetProperty(ctx, desc, JS_ATOM_writable);
- if (JS_IsException(prop))
- goto fail;
- flags |= JS_PROP_HAS_WRITABLE;
- if (JS_ToBoolFree(ctx, prop))
- flags |= JS_PROP_WRITABLE;
- }
- if (JS_HasProperty(ctx, desc, JS_ATOM_get)) {
- flags |= JS_PROP_HAS_GET;
- getter = JS_GetProperty(ctx, desc, JS_ATOM_get);
- if (JS_IsException(getter) ||
- !(JS_IsUndefined(getter) || JS_IsFunction(ctx, getter))) {
- JS_ThrowTypeError(ctx, "invalid getter");
- goto fail;
- }
- }
- if (JS_HasProperty(ctx, desc, JS_ATOM_set)) {
- flags |= JS_PROP_HAS_SET;
- setter = JS_GetProperty(ctx, desc, JS_ATOM_set);
- if (JS_IsException(setter) ||
- !(JS_IsUndefined(setter) || JS_IsFunction(ctx, setter))) {
- JS_ThrowTypeError(ctx, "invalid setter");
- goto fail;
- }
- }
- if ((flags & (JS_PROP_HAS_SET | JS_PROP_HAS_GET)) &&
- (flags & (JS_PROP_HAS_VALUE | JS_PROP_HAS_WRITABLE))) {
- JS_ThrowTypeError(ctx, "cannot have setter/getter and value or writable");
- goto fail;
- }
- d->flags = flags;
- d->value = val;
- d->getter = getter;
- d->setter = setter;
- return 0;
- fail:
- JS_FreeValue(ctx, val);
- JS_FreeValue(ctx, getter);
- JS_FreeValue(ctx, setter);
- return -1;
- }
- static __exception int JS_DefinePropertyDesc(JSContext *ctx, JSValueConst obj,
- JSAtom prop, JSValueConst desc,
- int flags)
- {
- JSPropertyDescriptor d;
- int ret;
- if (js_obj_to_desc(ctx, &d, desc) < 0)
- return -1;
- ret = JS_DefineProperty(ctx, obj, prop,
- d.value, d.getter, d.setter, d.flags | flags);
- js_free_desc(ctx, &d);
- return ret;
- }
- static __exception int JS_ObjectDefineProperties(JSContext *ctx,
- JSValueConst obj,
- JSValueConst properties)
- {
- JSValue props, desc;
- JSObject *p;
- JSPropertyEnum *atoms;
- uint32_t len, i;
- int ret = -1;
- if (!JS_IsObject(obj)) {
- JS_ThrowTypeErrorNotAnObject(ctx);
- return -1;
- }
- desc = JS_UNDEFINED;
- props = JS_ToObject(ctx, properties);
- if (JS_IsException(props))
- return -1;
- p = JS_VALUE_GET_OBJ(props);
- /* XXX: not done in the same order as the spec */
- if (JS_GetOwnPropertyNamesInternal(ctx, &atoms, &len, p, JS_GPN_ENUM_ONLY | JS_GPN_STRING_MASK | JS_GPN_SYMBOL_MASK) < 0)
- goto exception;
- for(i = 0; i < len; i++) {
- JS_FreeValue(ctx, desc);
- desc = JS_GetProperty(ctx, props, atoms[i].atom);
- if (JS_IsException(desc))
- goto exception;
- if (JS_DefinePropertyDesc(ctx, obj, atoms[i].atom, desc, JS_PROP_THROW) < 0)
- goto exception;
- }
- ret = 0;
- exception:
- JS_FreePropertyEnum(ctx, atoms, len);
- JS_FreeValue(ctx, props);
- JS_FreeValue(ctx, desc);
- return ret;
- }
- static JSValue js_object_constructor(JSContext *ctx, JSValueConst new_target,
- int argc, JSValueConst *argv)
- {
- JSValue ret;
- if (!JS_IsUndefined(new_target) &&
- JS_VALUE_GET_OBJ(new_target) !=
- JS_VALUE_GET_OBJ(JS_GetActiveFunction(ctx))) {
- ret = js_create_from_ctor(ctx, new_target, JS_CLASS_OBJECT);
- } else {
- int tag = JS_VALUE_GET_NORM_TAG(argv[0]);
- switch(tag) {
- case JS_TAG_NULL:
- case JS_TAG_UNDEFINED:
- ret = JS_NewObject(ctx);
- break;
- default:
- ret = JS_ToObject(ctx, argv[0]);
- break;
- }
- }
- return ret;
- }
- static JSValue js_object_create(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValueConst proto, props;
- JSValue obj;
- proto = argv[0];
- if (!JS_IsObject(proto) && !JS_IsNull(proto))
- return JS_ThrowTypeError(ctx, "not a prototype");
- obj = JS_NewObjectProto(ctx, proto);
- if (JS_IsException(obj))
- return JS_EXCEPTION;
- props = argv[1];
- if (!JS_IsUndefined(props)) {
- if (JS_ObjectDefineProperties(ctx, obj, props)) {
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- }
- return obj;
- }
- static JSValue js_object_getPrototypeOf(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- JSValueConst val;
- val = argv[0];
- if (JS_VALUE_GET_TAG(val) != JS_TAG_OBJECT) {
- /* ES6 feature non compatible with ES5.1: primitive types are
- accepted */
- if (magic || JS_VALUE_GET_TAG(val) == JS_TAG_NULL ||
- JS_VALUE_GET_TAG(val) == JS_TAG_UNDEFINED)
- return JS_ThrowTypeErrorNotAnObject(ctx);
- }
- return JS_GetPrototype(ctx, val);
- }
- static JSValue js_object_setPrototypeOf(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValueConst obj;
- obj = argv[0];
- if (JS_SetPrototypeInternal(ctx, obj, argv[1], TRUE) < 0)
- return JS_EXCEPTION;
- return JS_DupValue(ctx, obj);
- }
- /* magic = 1 if called as Reflect.defineProperty */
- static JSValue js_object_defineProperty(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- JSValueConst obj, prop, desc;
- int ret, flags;
- JSAtom atom;
- obj = argv[0];
- prop = argv[1];
- desc = argv[2];
- if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)
- return JS_ThrowTypeErrorNotAnObject(ctx);
- atom = JS_ValueToAtom(ctx, prop);
- if (unlikely(atom == JS_ATOM_NULL))
- return JS_EXCEPTION;
- flags = 0;
- if (!magic)
- flags |= JS_PROP_THROW;
- ret = JS_DefinePropertyDesc(ctx, obj, atom, desc, flags);
- JS_FreeAtom(ctx, atom);
- if (ret < 0) {
- return JS_EXCEPTION;
- } else if (magic) {
- return JS_NewBool(ctx, ret);
- } else {
- return JS_DupValue(ctx, obj);
- }
- }
- static JSValue js_object_defineProperties(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- // defineProperties(obj, properties)
- JSValueConst obj = argv[0];
- if (JS_ObjectDefineProperties(ctx, obj, argv[1]))
- return JS_EXCEPTION;
- else
- return JS_DupValue(ctx, obj);
- }
- /* magic = 1 if called as __defineSetter__ */
- static JSValue js_object___defineGetter__(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- JSValue obj;
- JSValueConst prop, value, get, set;
- int ret, flags;
- JSAtom atom;
- prop = argv[0];
- value = argv[1];
- obj = JS_ToObject(ctx, this_val);
- if (JS_IsException(obj))
- return JS_EXCEPTION;
- if (check_function(ctx, value)) {
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- atom = JS_ValueToAtom(ctx, prop);
- if (unlikely(atom == JS_ATOM_NULL)) {
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- flags = JS_PROP_THROW |
- JS_PROP_HAS_ENUMERABLE | JS_PROP_ENUMERABLE |
- JS_PROP_HAS_CONFIGURABLE | JS_PROP_CONFIGURABLE;
- if (magic) {
- get = JS_UNDEFINED;
- set = value;
- flags |= JS_PROP_HAS_SET;
- } else {
- get = value;
- set = JS_UNDEFINED;
- flags |= JS_PROP_HAS_GET;
- }
- ret = JS_DefineProperty(ctx, obj, atom, JS_UNDEFINED, get, set, flags);
- JS_FreeValue(ctx, obj);
- JS_FreeAtom(ctx, atom);
- if (ret < 0) {
- return JS_EXCEPTION;
- } else {
- return JS_UNDEFINED;
- }
- }
- static JSValue js_object_getOwnPropertyDescriptor(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- JSValueConst prop;
- JSAtom atom;
- JSValue ret, obj;
- JSPropertyDescriptor desc;
- int res, flags;
- if (magic) {
- /* Reflect.getOwnPropertyDescriptor case */
- if (JS_VALUE_GET_TAG(argv[0]) != JS_TAG_OBJECT)
- return JS_ThrowTypeErrorNotAnObject(ctx);
- obj = JS_DupValue(ctx, argv[0]);
- } else {
- obj = JS_ToObject(ctx, argv[0]);
- if (JS_IsException(obj))
- return obj;
- }
- prop = argv[1];
- atom = JS_ValueToAtom(ctx, prop);
- if (unlikely(atom == JS_ATOM_NULL))
- goto exception;
- ret = JS_UNDEFINED;
- if (JS_VALUE_GET_TAG(obj) == JS_TAG_OBJECT) {
- res = JS_GetOwnPropertyInternal(ctx, &desc, JS_VALUE_GET_OBJ(obj), atom);
- if (res < 0)
- goto exception;
- if (res) {
- ret = JS_NewObject(ctx);
- if (JS_IsException(ret))
- goto exception1;
- flags = JS_PROP_C_W_E | JS_PROP_THROW;
- if (desc.flags & JS_PROP_GETSET) {
- if (JS_DefinePropertyValue(ctx, ret, JS_ATOM_get, JS_DupValue(ctx, desc.getter), flags) < 0
- || JS_DefinePropertyValue(ctx, ret, JS_ATOM_set, JS_DupValue(ctx, desc.setter), flags) < 0)
- goto exception1;
- } else {
- if (JS_DefinePropertyValue(ctx, ret, JS_ATOM_value, JS_DupValue(ctx, desc.value), flags) < 0
- || JS_DefinePropertyValue(ctx, ret, JS_ATOM_writable,
- JS_NewBool(ctx, desc.flags & JS_PROP_WRITABLE), flags) < 0)
- goto exception1;
- }
- if (JS_DefinePropertyValue(ctx, ret, JS_ATOM_enumerable,
- JS_NewBool(ctx, desc.flags & JS_PROP_ENUMERABLE), flags) < 0
- || JS_DefinePropertyValue(ctx, ret, JS_ATOM_configurable,
- JS_NewBool(ctx, desc.flags & JS_PROP_CONFIGURABLE), flags) < 0)
- goto exception1;
- js_free_desc(ctx, &desc);
- }
- }
- JS_FreeAtom(ctx, atom);
- JS_FreeValue(ctx, obj);
- return ret;
- exception1:
- js_free_desc(ctx, &desc);
- JS_FreeValue(ctx, ret);
- exception:
- JS_FreeAtom(ctx, atom);
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue js_object_getOwnPropertyDescriptors(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- //getOwnPropertyDescriptors(obj)
- JSValue obj, r;
- JSObject *p;
- JSPropertyEnum *props;
- uint32_t len, i;
- r = JS_UNDEFINED;
- obj = JS_ToObject(ctx, argv[0]);
- if (JS_IsException(obj))
- return JS_EXCEPTION;
- p = JS_VALUE_GET_OBJ(obj);
- if (JS_GetOwnPropertyNamesInternal(ctx, &props, &len, p,
- JS_GPN_STRING_MASK | JS_GPN_SYMBOL_MASK))
- goto exception;
- r = JS_NewObject(ctx);
- if (JS_IsException(r))
- goto exception;
- for(i = 0; i < len; i++) {
- JSValue atomValue, desc;
- JSValueConst args[2];
- atomValue = JS_AtomToValue(ctx, props[i].atom);
- if (JS_IsException(atomValue))
- goto exception;
- args[0] = obj;
- args[1] = atomValue;
- desc = js_object_getOwnPropertyDescriptor(ctx, JS_UNDEFINED, 2, args, 0);
- JS_FreeValue(ctx, atomValue);
- if (JS_IsException(desc))
- goto exception;
- if (!JS_IsUndefined(desc)) {
- if (JS_DefinePropertyValue(ctx, r, props[i].atom, desc,
- JS_PROP_C_W_E | JS_PROP_THROW) < 0)
- goto exception;
- }
- }
- JS_FreePropertyEnum(ctx, props, len);
- JS_FreeValue(ctx, obj);
- return r;
- exception:
- JS_FreePropertyEnum(ctx, props, len);
- JS_FreeValue(ctx, obj);
- JS_FreeValue(ctx, r);
- return JS_EXCEPTION;
- }
- static JSValue JS_GetOwnPropertyNames2(JSContext *ctx, JSValueConst obj1,
- int flags, int kind)
- {
- JSValue obj, r, val, key, value;
- JSObject *p;
- JSPropertyEnum *atoms;
- uint32_t len, i, j;
- r = JS_UNDEFINED;
- val = JS_UNDEFINED;
- obj = JS_ToObject(ctx, obj1);
- if (JS_IsException(obj))
- return JS_EXCEPTION;
- p = JS_VALUE_GET_OBJ(obj);
- if (JS_GetOwnPropertyNamesInternal(ctx, &atoms, &len, p, flags & ~JS_GPN_ENUM_ONLY))
- goto exception;
- r = JS_NewArray(ctx);
- if (JS_IsException(r))
- goto exception;
- for(j = i = 0; i < len; i++) {
- JSAtom atom = atoms[i].atom;
- if (flags & JS_GPN_ENUM_ONLY) {
- JSPropertyDescriptor desc;
- int res;
- /* Check if property is still enumerable */
- res = JS_GetOwnPropertyInternal(ctx, &desc, p, atom);
- if (res < 0)
- goto exception;
- if (!res)
- continue;
- js_free_desc(ctx, &desc);
- if (!(desc.flags & JS_PROP_ENUMERABLE))
- continue;
- }
- switch(kind) {
- default:
- case JS_ITERATOR_KIND_KEY:
- val = JS_AtomToValue(ctx, atom);
- if (JS_IsException(val))
- goto exception;
- break;
- case JS_ITERATOR_KIND_VALUE:
- val = JS_GetProperty(ctx, obj, atom);
- if (JS_IsException(val))
- goto exception;
- break;
- case JS_ITERATOR_KIND_KEY_AND_VALUE:
- val = JS_NewArray(ctx);
- if (JS_IsException(val))
- goto exception;
- key = JS_AtomToValue(ctx, atom);
- if (JS_IsException(key))
- goto exception1;
- if (JS_CreateDataPropertyUint32(ctx, val, 0, key, JS_PROP_THROW) < 0)
- goto exception1;
- value = JS_GetProperty(ctx, obj, atom);
- if (JS_IsException(value))
- goto exception1;
- if (JS_CreateDataPropertyUint32(ctx, val, 1, value, JS_PROP_THROW) < 0)
- goto exception1;
- break;
- }
- if (JS_CreateDataPropertyUint32(ctx, r, j++, val, 0) < 0)
- goto exception;
- }
- goto done;
- exception1:
- JS_FreeValue(ctx, val);
- exception:
- JS_FreeValue(ctx, r);
- r = JS_EXCEPTION;
- done:
- JS_FreePropertyEnum(ctx, atoms, len);
- JS_FreeValue(ctx, obj);
- return r;
- }
- static JSValue js_object_getOwnPropertyNames(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- return JS_GetOwnPropertyNames2(ctx, argv[0],
- JS_GPN_STRING_MASK, JS_ITERATOR_KIND_KEY);
- }
- static JSValue js_object_getOwnPropertySymbols(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- return JS_GetOwnPropertyNames2(ctx, argv[0],
- JS_GPN_SYMBOL_MASK, JS_ITERATOR_KIND_KEY);
- }
- static JSValue js_object_keys(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int kind)
- {
- return JS_GetOwnPropertyNames2(ctx, argv[0],
- JS_GPN_ENUM_ONLY | JS_GPN_STRING_MASK, kind);
- }
- static JSValue js_object_isExtensible(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int reflect)
- {
- JSValueConst obj;
- int ret;
- obj = argv[0];
- if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT) {
- if (reflect)
- return JS_ThrowTypeErrorNotAnObject(ctx);
- else
- return JS_FALSE;
- }
- ret = JS_IsExtensible(ctx, obj);
- if (ret < 0)
- return JS_EXCEPTION;
- else
- return JS_NewBool(ctx, ret);
- }
- static JSValue js_object_preventExtensions(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int reflect)
- {
- JSValueConst obj;
- int ret;
- obj = argv[0];
- if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT) {
- if (reflect)
- return JS_ThrowTypeErrorNotAnObject(ctx);
- else
- return JS_DupValue(ctx, obj);
- }
- ret = JS_PreventExtensions(ctx, obj);
- if (ret < 0)
- return JS_EXCEPTION;
- if (reflect) {
- return JS_NewBool(ctx, ret);
- } else {
- if (!ret)
- return JS_ThrowTypeError(ctx, "proxy preventExtensions handler returned false");
- return JS_DupValue(ctx, obj);
- }
- }
- static JSValue js_object_hasOwnProperty(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue obj;
- JSAtom atom;
- JSObject *p;
- BOOL ret;
- atom = JS_ValueToAtom(ctx, argv[0]); /* must be done first */
- if (unlikely(atom == JS_ATOM_NULL))
- return JS_EXCEPTION;
- obj = JS_ToObject(ctx, this_val);
- if (JS_IsException(obj)) {
- JS_FreeAtom(ctx, atom);
- return obj;
- }
- p = JS_VALUE_GET_OBJ(obj);
- ret = JS_GetOwnPropertyInternal(ctx, NULL, p, atom);
- JS_FreeAtom(ctx, atom);
- JS_FreeValue(ctx, obj);
- if (ret < 0)
- return JS_EXCEPTION;
- else
- return JS_NewBool(ctx, ret);
- }
- static JSValue js_object_hasOwn(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue obj;
- JSAtom atom;
- JSObject *p;
- BOOL ret;
- obj = JS_ToObject(ctx, argv[0]);
- if (JS_IsException(obj))
- return obj;
- atom = JS_ValueToAtom(ctx, argv[1]);
- if (unlikely(atom == JS_ATOM_NULL)) {
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- p = JS_VALUE_GET_OBJ(obj);
- ret = JS_GetOwnPropertyInternal(ctx, NULL, p, atom);
- JS_FreeAtom(ctx, atom);
- JS_FreeValue(ctx, obj);
- if (ret < 0)
- return JS_EXCEPTION;
- else
- return JS_NewBool(ctx, ret);
- }
- static JSValue js_object_valueOf(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- return JS_ToObject(ctx, this_val);
- }
- static JSValue js_object_toString(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue obj, tag;
- int is_array;
- JSAtom atom;
- JSObject *p;
- if (JS_IsNull(this_val)) {
- tag = js_new_string8(ctx, "Null");
- } else if (JS_IsUndefined(this_val)) {
- tag = js_new_string8(ctx, "Undefined");
- } else {
- obj = JS_ToObject(ctx, this_val);
- if (JS_IsException(obj))
- return obj;
- is_array = JS_IsArray(ctx, obj);
- if (is_array < 0) {
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- if (is_array) {
- atom = JS_ATOM_Array;
- } else if (JS_IsFunction(ctx, obj)) {
- atom = JS_ATOM_Function;
- } else {
- p = JS_VALUE_GET_OBJ(obj);
- switch(p->class_id) {
- case JS_CLASS_STRING:
- case JS_CLASS_ARGUMENTS:
- case JS_CLASS_MAPPED_ARGUMENTS:
- case JS_CLASS_ERROR:
- case JS_CLASS_BOOLEAN:
- case JS_CLASS_NUMBER:
- case JS_CLASS_DATE:
- case JS_CLASS_REGEXP:
- atom = ctx->rt->class_array[p->class_id].class_name;
- break;
- default:
- atom = JS_ATOM_Object;
- break;
- }
- }
- tag = JS_GetProperty(ctx, obj, JS_ATOM_Symbol_toStringTag);
- JS_FreeValue(ctx, obj);
- if (JS_IsException(tag))
- return JS_EXCEPTION;
- if (!JS_IsString(tag)) {
- JS_FreeValue(ctx, tag);
- tag = JS_AtomToString(ctx, atom);
- }
- }
- return JS_ConcatString3(ctx, "[object ", tag, "]");
- }
- static JSValue js_object_toLocaleString(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- return JS_Invoke(ctx, this_val, JS_ATOM_toString, 0, NULL);
- }
- static JSValue js_object_assign(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- // Object.assign(obj, source1)
- JSValue obj, s;
- int i;
- s = JS_UNDEFINED;
- obj = JS_ToObject(ctx, argv[0]);
- if (JS_IsException(obj))
- goto exception;
- for (i = 1; i < argc; i++) {
- if (!JS_IsNull(argv[i]) && !JS_IsUndefined(argv[i])) {
- s = JS_ToObject(ctx, argv[i]);
- if (JS_IsException(s))
- goto exception;
- if (JS_CopyDataProperties(ctx, obj, s, JS_UNDEFINED, TRUE))
- goto exception;
- JS_FreeValue(ctx, s);
- }
- }
- return obj;
- exception:
- JS_FreeValue(ctx, obj);
- JS_FreeValue(ctx, s);
- return JS_EXCEPTION;
- }
- static JSValue js_object_seal(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int freeze_flag)
- {
- JSValueConst obj = argv[0];
- JSObject *p;
- JSPropertyEnum *props;
- uint32_t len, i;
- int flags, desc_flags, res;
- if (!JS_IsObject(obj))
- return JS_DupValue(ctx, obj);
- res = JS_PreventExtensions(ctx, obj);
- if (res < 0)
- return JS_EXCEPTION;
- if (!res) {
- return JS_ThrowTypeError(ctx, "proxy preventExtensions handler returned false");
- }
- p = JS_VALUE_GET_OBJ(obj);
- flags = JS_GPN_STRING_MASK | JS_GPN_SYMBOL_MASK;
- if (JS_GetOwnPropertyNamesInternal(ctx, &props, &len, p, flags))
- return JS_EXCEPTION;
- for(i = 0; i < len; i++) {
- JSPropertyDescriptor desc;
- JSAtom prop = props[i].atom;
- desc_flags = JS_PROP_THROW | JS_PROP_HAS_CONFIGURABLE;
- if (freeze_flag) {
- res = JS_GetOwnPropertyInternal(ctx, &desc, p, prop);
- if (res < 0)
- goto exception;
- if (res) {
- if (desc.flags & JS_PROP_WRITABLE)
- desc_flags |= JS_PROP_HAS_WRITABLE;
- js_free_desc(ctx, &desc);
- }
- }
- if (JS_DefineProperty(ctx, obj, prop, JS_UNDEFINED,
- JS_UNDEFINED, JS_UNDEFINED, desc_flags) < 0)
- goto exception;
- }
- JS_FreePropertyEnum(ctx, props, len);
- return JS_DupValue(ctx, obj);
- exception:
- JS_FreePropertyEnum(ctx, props, len);
- return JS_EXCEPTION;
- }
- static JSValue js_object_isSealed(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int is_frozen)
- {
- JSValueConst obj = argv[0];
- JSObject *p;
- JSPropertyEnum *props;
- uint32_t len, i;
- int flags, res;
- if (!JS_IsObject(obj))
- return JS_TRUE;
- p = JS_VALUE_GET_OBJ(obj);
- flags = JS_GPN_STRING_MASK | JS_GPN_SYMBOL_MASK;
- if (JS_GetOwnPropertyNamesInternal(ctx, &props, &len, p, flags))
- return JS_EXCEPTION;
- for(i = 0; i < len; i++) {
- JSPropertyDescriptor desc;
- JSAtom prop = props[i].atom;
- res = JS_GetOwnPropertyInternal(ctx, &desc, p, prop);
- if (res < 0)
- goto exception;
- if (res) {
- js_free_desc(ctx, &desc);
- if ((desc.flags & JS_PROP_CONFIGURABLE)
- || (is_frozen && (desc.flags & JS_PROP_WRITABLE))) {
- res = FALSE;
- goto done;
- }
- }
- }
- res = JS_IsExtensible(ctx, obj);
- if (res < 0)
- return JS_EXCEPTION;
- res ^= 1;
- done:
- JS_FreePropertyEnum(ctx, props, len);
- return JS_NewBool(ctx, res);
- exception:
- JS_FreePropertyEnum(ctx, props, len);
- return JS_EXCEPTION;
- }
- static JSValue js_object_fromEntries(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue obj, iter, next_method = JS_UNDEFINED;
- JSValueConst iterable;
- BOOL done;
- /* RequireObjectCoercible() not necessary because it is tested in
- JS_GetIterator() by JS_GetProperty() */
- iterable = argv[0];
- obj = JS_NewObject(ctx);
- if (JS_IsException(obj))
- return obj;
- iter = JS_GetIterator(ctx, iterable, FALSE);
- if (JS_IsException(iter))
- goto fail;
- next_method = JS_GetProperty(ctx, iter, JS_ATOM_next);
- if (JS_IsException(next_method))
- goto fail;
- for(;;) {
- JSValue key, value, item;
- item = JS_IteratorNext(ctx, iter, next_method, 0, NULL, &done);
- if (JS_IsException(item))
- goto fail;
- if (done)
- break;
- key = JS_UNDEFINED;
- value = JS_UNDEFINED;
- if (!JS_IsObject(item)) {
- JS_ThrowTypeErrorNotAnObject(ctx);
- goto fail1;
- }
- key = JS_GetPropertyUint32(ctx, item, 0);
- if (JS_IsException(key))
- goto fail1;
- value = JS_GetPropertyUint32(ctx, item, 1);
- if (JS_IsException(value)) {
- JS_FreeValue(ctx, key);
- goto fail1;
- }
- if (JS_DefinePropertyValueValue(ctx, obj, key, value,
- JS_PROP_C_W_E | JS_PROP_THROW) < 0) {
- fail1:
- JS_FreeValue(ctx, item);
- goto fail;
- }
- JS_FreeValue(ctx, item);
- }
- JS_FreeValue(ctx, next_method);
- JS_FreeValue(ctx, iter);
- return obj;
- fail:
- if (JS_IsObject(iter)) {
- /* close the iterator object, preserving pending exception */
- JS_IteratorClose(ctx, iter, TRUE);
- }
- JS_FreeValue(ctx, next_method);
- JS_FreeValue(ctx, iter);
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue js_object_is(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- return JS_NewBool(ctx, js_same_value(ctx, argv[0], argv[1]));
- }
- static JSValue JS_SpeciesConstructor(JSContext *ctx, JSValueConst obj,
- JSValueConst defaultConstructor)
- {
- JSValue ctor, species;
- if (!JS_IsObject(obj))
- return JS_ThrowTypeErrorNotAnObject(ctx);
- ctor = JS_GetProperty(ctx, obj, JS_ATOM_constructor);
- if (JS_IsException(ctor))
- return ctor;
- if (JS_IsUndefined(ctor))
- return JS_DupValue(ctx, defaultConstructor);
- if (!JS_IsObject(ctor)) {
- JS_FreeValue(ctx, ctor);
- return JS_ThrowTypeErrorNotAnObject(ctx);
- }
- species = JS_GetProperty(ctx, ctor, JS_ATOM_Symbol_species);
- JS_FreeValue(ctx, ctor);
- if (JS_IsException(species))
- return species;
- if (JS_IsUndefined(species) || JS_IsNull(species))
- return JS_DupValue(ctx, defaultConstructor);
- if (!JS_IsConstructor(ctx, species)) {
- JS_ThrowTypeErrorNotAConstructor(ctx, species);
- JS_FreeValue(ctx, species);
- return JS_EXCEPTION;
- }
- return species;
- }
- static JSValue js_object_get___proto__(JSContext *ctx, JSValueConst this_val)
- {
- JSValue val, ret;
- val = JS_ToObject(ctx, this_val);
- if (JS_IsException(val))
- return val;
- ret = JS_GetPrototype(ctx, val);
- JS_FreeValue(ctx, val);
- return ret;
- }
- static JSValue js_object_set___proto__(JSContext *ctx, JSValueConst this_val,
- JSValueConst proto)
- {
- if (JS_IsUndefined(this_val) || JS_IsNull(this_val))
- return JS_ThrowTypeErrorNotAnObject(ctx);
- if (!JS_IsObject(proto) && !JS_IsNull(proto))
- return JS_UNDEFINED;
- if (JS_SetPrototypeInternal(ctx, this_val, proto, TRUE) < 0)
- return JS_EXCEPTION;
- else
- return JS_UNDEFINED;
- }
- static JSValue js_object_isPrototypeOf(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue obj, v1;
- JSValueConst v;
- int res;
- v = argv[0];
- if (!JS_IsObject(v))
- return JS_FALSE;
- obj = JS_ToObject(ctx, this_val);
- if (JS_IsException(obj))
- return JS_EXCEPTION;
- v1 = JS_DupValue(ctx, v);
- for(;;) {
- v1 = JS_GetPrototypeFree(ctx, v1);
- if (JS_IsException(v1))
- goto exception;
- if (JS_IsNull(v1)) {
- res = FALSE;
- break;
- }
- if (JS_VALUE_GET_OBJ(obj) == JS_VALUE_GET_OBJ(v1)) {
- res = TRUE;
- break;
- }
- /* avoid infinite loop (possible with proxies) */
- if (js_poll_interrupts(ctx))
- goto exception;
- }
- JS_FreeValue(ctx, v1);
- JS_FreeValue(ctx, obj);
- return JS_NewBool(ctx, res);
- exception:
- JS_FreeValue(ctx, v1);
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue js_object_propertyIsEnumerable(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue obj = JS_UNDEFINED, res = JS_EXCEPTION;
- JSAtom prop;
- JSPropertyDescriptor desc;
- int has_prop;
- prop = JS_ValueToAtom(ctx, argv[0]);
- if (unlikely(prop == JS_ATOM_NULL))
- goto exception;
- obj = JS_ToObject(ctx, this_val);
- if (JS_IsException(obj))
- goto exception;
- has_prop = JS_GetOwnPropertyInternal(ctx, &desc, JS_VALUE_GET_OBJ(obj), prop);
- if (has_prop < 0)
- goto exception;
- if (has_prop) {
- res = JS_NewBool(ctx, desc.flags & JS_PROP_ENUMERABLE);
- js_free_desc(ctx, &desc);
- } else {
- res = JS_FALSE;
- }
- exception:
- JS_FreeAtom(ctx, prop);
- JS_FreeValue(ctx, obj);
- return res;
- }
- static JSValue js_object___lookupGetter__(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int setter)
- {
- JSValue obj, res = JS_EXCEPTION;
- JSAtom prop = JS_ATOM_NULL;
- JSPropertyDescriptor desc;
- int has_prop;
- obj = JS_ToObject(ctx, this_val);
- if (JS_IsException(obj))
- goto exception;
- prop = JS_ValueToAtom(ctx, argv[0]);
- if (unlikely(prop == JS_ATOM_NULL))
- goto exception;
- for (;;) {
- has_prop = JS_GetOwnPropertyInternal(ctx, &desc, JS_VALUE_GET_OBJ(obj), prop);
- if (has_prop < 0)
- goto exception;
- if (has_prop) {
- if (desc.flags & JS_PROP_GETSET)
- res = JS_DupValue(ctx, setter ? desc.setter : desc.getter);
- else
- res = JS_UNDEFINED;
- js_free_desc(ctx, &desc);
- break;
- }
- obj = JS_GetPrototypeFree(ctx, obj);
- if (JS_IsException(obj))
- goto exception;
- if (JS_IsNull(obj)) {
- res = JS_UNDEFINED;
- break;
- }
- /* avoid infinite loop (possible with proxies) */
- if (js_poll_interrupts(ctx))
- goto exception;
- }
- exception:
- JS_FreeAtom(ctx, prop);
- JS_FreeValue(ctx, obj);
- return res;
- }
- static const JSCFunctionListEntry js_object_funcs[] = {
- JS_CFUNC_DEF("create", 2, js_object_create ),
- JS_CFUNC_MAGIC_DEF("getPrototypeOf", 1, js_object_getPrototypeOf, 0 ),
- JS_CFUNC_DEF("setPrototypeOf", 2, js_object_setPrototypeOf ),
- JS_CFUNC_MAGIC_DEF("defineProperty", 3, js_object_defineProperty, 0 ),
- JS_CFUNC_DEF("defineProperties", 2, js_object_defineProperties ),
- JS_CFUNC_DEF("getOwnPropertyNames", 1, js_object_getOwnPropertyNames ),
- JS_CFUNC_DEF("getOwnPropertySymbols", 1, js_object_getOwnPropertySymbols ),
- JS_CFUNC_MAGIC_DEF("groupBy", 2, js_object_groupBy, 0 ),
- JS_CFUNC_MAGIC_DEF("keys", 1, js_object_keys, JS_ITERATOR_KIND_KEY ),
- JS_CFUNC_MAGIC_DEF("values", 1, js_object_keys, JS_ITERATOR_KIND_VALUE ),
- JS_CFUNC_MAGIC_DEF("entries", 1, js_object_keys, JS_ITERATOR_KIND_KEY_AND_VALUE ),
- JS_CFUNC_MAGIC_DEF("isExtensible", 1, js_object_isExtensible, 0 ),
- JS_CFUNC_MAGIC_DEF("preventExtensions", 1, js_object_preventExtensions, 0 ),
- JS_CFUNC_MAGIC_DEF("getOwnPropertyDescriptor", 2, js_object_getOwnPropertyDescriptor, 0 ),
- JS_CFUNC_DEF("getOwnPropertyDescriptors", 1, js_object_getOwnPropertyDescriptors ),
- JS_CFUNC_DEF("is", 2, js_object_is ),
- JS_CFUNC_DEF("assign", 2, js_object_assign ),
- JS_CFUNC_MAGIC_DEF("seal", 1, js_object_seal, 0 ),
- JS_CFUNC_MAGIC_DEF("freeze", 1, js_object_seal, 1 ),
- JS_CFUNC_MAGIC_DEF("isSealed", 1, js_object_isSealed, 0 ),
- JS_CFUNC_MAGIC_DEF("isFrozen", 1, js_object_isSealed, 1 ),
- JS_CFUNC_DEF("fromEntries", 1, js_object_fromEntries ),
- JS_CFUNC_DEF("hasOwn", 2, js_object_hasOwn ),
- };
- static const JSCFunctionListEntry js_object_proto_funcs[] = {
- JS_CFUNC_DEF("toString", 0, js_object_toString ),
- JS_CFUNC_DEF("toLocaleString", 0, js_object_toLocaleString ),
- JS_CFUNC_DEF("valueOf", 0, js_object_valueOf ),
- JS_CFUNC_DEF("hasOwnProperty", 1, js_object_hasOwnProperty ),
- JS_CFUNC_DEF("isPrototypeOf", 1, js_object_isPrototypeOf ),
- JS_CFUNC_DEF("propertyIsEnumerable", 1, js_object_propertyIsEnumerable ),
- JS_CGETSET_DEF("__proto__", js_object_get___proto__, js_object_set___proto__ ),
- JS_CFUNC_MAGIC_DEF("__defineGetter__", 2, js_object___defineGetter__, 0 ),
- JS_CFUNC_MAGIC_DEF("__defineSetter__", 2, js_object___defineGetter__, 1 ),
- JS_CFUNC_MAGIC_DEF("__lookupGetter__", 1, js_object___lookupGetter__, 0 ),
- JS_CFUNC_MAGIC_DEF("__lookupSetter__", 1, js_object___lookupGetter__, 1 ),
- };
- /* Function class */
- static JSValue js_function_proto(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- return JS_UNDEFINED;
- }
- /* XXX: add a specific eval mode so that Function("}), ({") is rejected */
- static JSValue js_function_constructor(JSContext *ctx, JSValueConst new_target,
- int argc, JSValueConst *argv, int magic)
- {
- JSFunctionKindEnum func_kind = magic;
- int i, n, ret;
- JSValue s, proto, obj = JS_UNDEFINED;
- StringBuffer b_s, *b = &b_s;
- string_buffer_init(ctx, b, 0);
- string_buffer_putc8(b, '(');
- if (func_kind == JS_FUNC_ASYNC || func_kind == JS_FUNC_ASYNC_GENERATOR) {
- string_buffer_puts8(b, "async ");
- }
- string_buffer_puts8(b, "function");
- if (func_kind == JS_FUNC_GENERATOR || func_kind == JS_FUNC_ASYNC_GENERATOR) {
- string_buffer_putc8(b, '*');
- }
- string_buffer_puts8(b, " anonymous(");
- n = argc - 1;
- for(i = 0; i < n; i++) {
- if (i != 0) {
- string_buffer_putc8(b, ',');
- }
- if (string_buffer_concat_value(b, argv[i]))
- goto fail;
- }
- string_buffer_puts8(b, "\n) {\n");
- if (n >= 0) {
- if (string_buffer_concat_value(b, argv[n]))
- goto fail;
- }
- string_buffer_puts8(b, "\n})");
- s = string_buffer_end(b);
- if (JS_IsException(s))
- goto fail1;
- obj = JS_EvalObject(ctx, ctx->global_obj, s, JS_EVAL_TYPE_INDIRECT, -1);
- JS_FreeValue(ctx, s);
- if (JS_IsException(obj))
- goto fail1;
- if (!JS_IsUndefined(new_target)) {
- /* set the prototype */
- proto = JS_GetProperty(ctx, new_target, JS_ATOM_prototype);
- if (JS_IsException(proto))
- goto fail1;
- if (!JS_IsObject(proto)) {
- JSContext *realm;
- JS_FreeValue(ctx, proto);
- realm = JS_GetFunctionRealm(ctx, new_target);
- if (!realm)
- goto fail1;
- proto = JS_DupValue(ctx, realm->class_proto[func_kind_to_class_id[func_kind]]);
- }
- ret = JS_SetPrototypeInternal(ctx, obj, proto, TRUE);
- JS_FreeValue(ctx, proto);
- if (ret < 0)
- goto fail1;
- }
- return obj;
- fail:
- string_buffer_free(b);
- fail1:
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static __exception int js_get_length32(JSContext *ctx, uint32_t *pres,
- JSValueConst obj)
- {
- JSValue len_val;
- len_val = JS_GetProperty(ctx, obj, JS_ATOM_length);
- if (JS_IsException(len_val)) {
- *pres = 0;
- return -1;
- }
- return JS_ToUint32Free(ctx, pres, len_val);
- }
- static __exception int js_get_length64(JSContext *ctx, int64_t *pres,
- JSValueConst obj)
- {
- JSValue len_val;
- len_val = JS_GetProperty(ctx, obj, JS_ATOM_length);
- if (JS_IsException(len_val)) {
- *pres = 0;
- return -1;
- }
- return JS_ToLengthFree(ctx, pres, len_val);
- }
- static void free_arg_list(JSContext *ctx, JSValue *tab, uint32_t len)
- {
- uint32_t i;
- for(i = 0; i < len; i++) {
- JS_FreeValue(ctx, tab[i]);
- }
- js_free(ctx, tab);
- }
- /* XXX: should use ValueArray */
- static JSValue *build_arg_list(JSContext *ctx, uint32_t *plen,
- JSValueConst array_arg)
- {
- uint32_t len, i;
- int64_t len64;
- JSValue *tab, ret;
- JSObject *p;
- if (JS_VALUE_GET_TAG(array_arg) != JS_TAG_OBJECT) {
- JS_ThrowTypeError(ctx, "not a object");
- return NULL;
- }
- if (js_get_length64(ctx, &len64, array_arg))
- return NULL;
- if (len64 > JS_MAX_LOCAL_VARS) {
- // XXX: check for stack overflow?
- JS_ThrowRangeError(ctx, "too many arguments in function call (only %d allowed)",
- JS_MAX_LOCAL_VARS);
- return NULL;
- }
- len = len64;
- /* avoid allocating 0 bytes */
- tab = js_mallocz(ctx, sizeof(tab[0]) * max_uint32(1, len));
- if (!tab)
- return NULL;
- p = JS_VALUE_GET_OBJ(array_arg);
- if ((p->class_id == JS_CLASS_ARRAY || p->class_id == JS_CLASS_ARGUMENTS || p->class_id == JS_CLASS_MAPPED_ARGUMENTS) &&
- p->fast_array &&
- len == p->u.array.count) {
- if (p->class_id == JS_CLASS_MAPPED_ARGUMENTS) {
- for(i = 0; i < len; i++) {
- tab[i] = JS_DupValue(ctx, *p->u.array.u.var_refs[i]->pvalue);
- }
- } else {
- for(i = 0; i < len; i++) {
- tab[i] = JS_DupValue(ctx, p->u.array.u.values[i]);
- }
- }
- } else {
- for(i = 0; i < len; i++) {
- ret = JS_GetPropertyUint32(ctx, array_arg, i);
- if (JS_IsException(ret)) {
- free_arg_list(ctx, tab, i);
- return NULL;
- }
- tab[i] = ret;
- }
- }
- *plen = len;
- return tab;
- }
- /* magic value: 0 = normal apply, 1 = apply for constructor, 2 =
- Reflect.apply */
- static JSValue js_function_apply(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- JSValueConst this_arg, array_arg;
- uint32_t len;
- JSValue *tab, ret;
- if (check_function(ctx, this_val))
- return JS_EXCEPTION;
- this_arg = argv[0];
- array_arg = argv[1];
- if ((JS_VALUE_GET_TAG(array_arg) == JS_TAG_UNDEFINED ||
- JS_VALUE_GET_TAG(array_arg) == JS_TAG_NULL) && magic != 2) {
- return JS_Call(ctx, this_val, this_arg, 0, NULL);
- }
- tab = build_arg_list(ctx, &len, array_arg);
- if (!tab)
- return JS_EXCEPTION;
- if (magic & 1) {
- ret = JS_CallConstructor2(ctx, this_val, this_arg, len, (JSValueConst *)tab);
- } else {
- ret = JS_Call(ctx, this_val, this_arg, len, (JSValueConst *)tab);
- }
- free_arg_list(ctx, tab, len);
- return ret;
- }
- static JSValue js_function_call(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- if (argc <= 0) {
- return JS_Call(ctx, this_val, JS_UNDEFINED, 0, NULL);
- } else {
- return JS_Call(ctx, this_val, argv[0], argc - 1, argv + 1);
- }
- }
- static JSValue js_function_bind(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSBoundFunction *bf;
- JSValue func_obj, name1, len_val;
- JSObject *p;
- int arg_count, i, ret;
- if (check_function(ctx, this_val))
- return JS_EXCEPTION;
- func_obj = JS_NewObjectProtoClass(ctx, ctx->function_proto,
- JS_CLASS_BOUND_FUNCTION);
- if (JS_IsException(func_obj))
- return JS_EXCEPTION;
- p = JS_VALUE_GET_OBJ(func_obj);
- p->is_constructor = JS_IsConstructor(ctx, this_val);
- arg_count = max_int(0, argc - 1);
- bf = js_malloc(ctx, sizeof(*bf) + arg_count * sizeof(JSValue));
- if (!bf)
- goto exception;
- bf->func_obj = JS_DupValue(ctx, this_val);
- bf->this_val = JS_DupValue(ctx, argv[0]);
- bf->argc = arg_count;
- for(i = 0; i < arg_count; i++) {
- bf->argv[i] = JS_DupValue(ctx, argv[i + 1]);
- }
- p->u.bound_function = bf;
- /* XXX: the spec could be simpler by only using GetOwnProperty */
- ret = JS_GetOwnProperty(ctx, NULL, this_val, JS_ATOM_length);
- if (ret < 0)
- goto exception;
- if (!ret) {
- len_val = JS_NewInt32(ctx, 0);
- } else {
- len_val = JS_GetProperty(ctx, this_val, JS_ATOM_length);
- if (JS_IsException(len_val))
- goto exception;
- if (JS_VALUE_GET_TAG(len_val) == JS_TAG_INT) {
- /* most common case */
- int len1 = JS_VALUE_GET_INT(len_val);
- if (len1 <= arg_count)
- len1 = 0;
- else
- len1 -= arg_count;
- len_val = JS_NewInt32(ctx, len1);
- } else if (JS_VALUE_GET_NORM_TAG(len_val) == JS_TAG_FLOAT64) {
- double d = JS_VALUE_GET_FLOAT64(len_val);
- if (isnan(d)) {
- d = 0.0;
- } else {
- d = trunc(d);
- if (d <= (double)arg_count)
- d = 0.0;
- else
- d -= (double)arg_count; /* also converts -0 to +0 */
- }
- len_val = JS_NewFloat64(ctx, d);
- } else {
- JS_FreeValue(ctx, len_val);
- len_val = JS_NewInt32(ctx, 0);
- }
- }
- JS_DefinePropertyValue(ctx, func_obj, JS_ATOM_length,
- len_val, JS_PROP_CONFIGURABLE);
- name1 = JS_GetProperty(ctx, this_val, JS_ATOM_name);
- if (JS_IsException(name1))
- goto exception;
- if (!JS_IsString(name1)) {
- JS_FreeValue(ctx, name1);
- name1 = JS_AtomToString(ctx, JS_ATOM_empty_string);
- }
- name1 = JS_ConcatString3(ctx, "bound ", name1, "");
- if (JS_IsException(name1))
- goto exception;
- JS_DefinePropertyValue(ctx, func_obj, JS_ATOM_name, name1,
- JS_PROP_CONFIGURABLE);
- return func_obj;
- exception:
- JS_FreeValue(ctx, func_obj);
- return JS_EXCEPTION;
- }
- static JSValue js_function_toString(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSObject *p;
- JSFunctionKindEnum func_kind = JS_FUNC_NORMAL;
- if (check_function(ctx, this_val))
- return JS_EXCEPTION;
- p = JS_VALUE_GET_OBJ(this_val);
- if (js_class_has_bytecode(p->class_id)) {
- JSFunctionBytecode *b = p->u.func.function_bytecode;
- if (b->has_debug && b->debug.source) {
- return JS_NewStringLen(ctx, b->debug.source, b->debug.source_len);
- }
- func_kind = b->func_kind;
- }
- {
- JSValue name;
- const char *pref, *suff;
- switch(func_kind) {
- default:
- case JS_FUNC_NORMAL:
- pref = "function ";
- break;
- case JS_FUNC_GENERATOR:
- pref = "function *";
- break;
- case JS_FUNC_ASYNC:
- pref = "async function ";
- break;
- case JS_FUNC_ASYNC_GENERATOR:
- pref = "async function *";
- break;
- }
- suff = "() {\n [native code]\n}";
- name = JS_GetProperty(ctx, this_val, JS_ATOM_name);
- if (JS_IsUndefined(name))
- name = JS_AtomToString(ctx, JS_ATOM_empty_string);
- return JS_ConcatString3(ctx, pref, name, suff);
- }
- }
- static JSValue js_function_hasInstance(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- int ret;
- ret = JS_OrdinaryIsInstanceOf(ctx, argv[0], this_val);
- if (ret < 0)
- return JS_EXCEPTION;
- else
- return JS_NewBool(ctx, ret);
- }
- static const JSCFunctionListEntry js_function_proto_funcs[] = {
- JS_CFUNC_DEF("call", 1, js_function_call ),
- JS_CFUNC_MAGIC_DEF("apply", 2, js_function_apply, 0 ),
- JS_CFUNC_DEF("bind", 1, js_function_bind ),
- JS_CFUNC_DEF("toString", 0, js_function_toString ),
- JS_CFUNC_DEF("[Symbol.hasInstance]", 1, js_function_hasInstance ),
- JS_CGETSET_DEF("fileName", js_function_proto_fileName, NULL ),
- JS_CGETSET_MAGIC_DEF("lineNumber", js_function_proto_lineNumber, NULL, 0 ),
- JS_CGETSET_MAGIC_DEF("columnNumber", js_function_proto_lineNumber, NULL, 1 ),
- };
- /* Error class */
- static JSValue iterator_to_array(JSContext *ctx, JSValueConst items)
- {
- JSValue iter, next_method = JS_UNDEFINED;
- JSValue v, r = JS_UNDEFINED;
- int64_t k;
- BOOL done;
- iter = JS_GetIterator(ctx, items, FALSE);
- if (JS_IsException(iter))
- goto exception;
- next_method = JS_GetProperty(ctx, iter, JS_ATOM_next);
- if (JS_IsException(next_method))
- goto exception;
- r = JS_NewArray(ctx);
- if (JS_IsException(r))
- goto exception;
- for (k = 0;; k++) {
- v = JS_IteratorNext(ctx, iter, next_method, 0, NULL, &done);
- if (JS_IsException(v))
- goto exception_close;
- if (done)
- break;
- if (JS_DefinePropertyValueInt64(ctx, r, k, v,
- JS_PROP_C_W_E | JS_PROP_THROW) < 0)
- goto exception_close;
- }
- done:
- JS_FreeValue(ctx, next_method);
- JS_FreeValue(ctx, iter);
- return r;
- exception_close:
- JS_IteratorClose(ctx, iter, TRUE);
- exception:
- JS_FreeValue(ctx, r);
- r = JS_EXCEPTION;
- goto done;
- }
- static JSValue js_error_constructor(JSContext *ctx, JSValueConst new_target,
- int argc, JSValueConst *argv, int magic)
- {
- JSValue obj, msg, proto;
- JSValueConst message, options;
- int arg_index;
- if (JS_IsUndefined(new_target))
- new_target = JS_GetActiveFunction(ctx);
- proto = JS_GetProperty(ctx, new_target, JS_ATOM_prototype);
- if (JS_IsException(proto))
- return proto;
- if (!JS_IsObject(proto)) {
- JSContext *realm;
- JSValueConst proto1;
- JS_FreeValue(ctx, proto);
- realm = JS_GetFunctionRealm(ctx, new_target);
- if (!realm)
- return JS_EXCEPTION;
- if (magic < 0) {
- proto1 = realm->class_proto[JS_CLASS_ERROR];
- } else {
- proto1 = realm->native_error_proto[magic];
- }
- proto = JS_DupValue(ctx, proto1);
- }
- obj = JS_NewObjectProtoClass(ctx, proto, JS_CLASS_ERROR);
- JS_FreeValue(ctx, proto);
- if (JS_IsException(obj))
- return obj;
- arg_index = (magic == JS_AGGREGATE_ERROR);
- message = argv[arg_index++];
- if (!JS_IsUndefined(message)) {
- msg = JS_ToString(ctx, message);
- if (unlikely(JS_IsException(msg)))
- goto exception;
- JS_DefinePropertyValue(ctx, obj, JS_ATOM_message, msg,
- JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
- }
- if (arg_index < argc) {
- options = argv[arg_index];
- if (JS_IsObject(options)) {
- int present = JS_HasProperty(ctx, options, JS_ATOM_cause);
- if (present < 0)
- goto exception;
- if (present) {
- JSValue cause = JS_GetProperty(ctx, options, JS_ATOM_cause);
- if (JS_IsException(cause))
- goto exception;
- JS_DefinePropertyValue(ctx, obj, JS_ATOM_cause, cause,
- JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
- }
- }
- }
- if (magic == JS_AGGREGATE_ERROR) {
- JSValue error_list = iterator_to_array(ctx, argv[0]);
- if (JS_IsException(error_list))
- goto exception;
- JS_DefinePropertyValue(ctx, obj, JS_ATOM_errors, error_list,
- JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
- }
- /* skip the Error() function in the backtrace */
- build_backtrace(ctx, obj, NULL, 0, 0, JS_BACKTRACE_FLAG_SKIP_FIRST_LEVEL);
- return obj;
- exception:
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue js_error_toString(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue name, msg;
- if (!JS_IsObject(this_val))
- return JS_ThrowTypeErrorNotAnObject(ctx);
- name = JS_GetProperty(ctx, this_val, JS_ATOM_name);
- if (JS_IsUndefined(name))
- name = JS_AtomToString(ctx, JS_ATOM_Error);
- else
- name = JS_ToStringFree(ctx, name);
- if (JS_IsException(name))
- return JS_EXCEPTION;
- msg = JS_GetProperty(ctx, this_val, JS_ATOM_message);
- if (JS_IsUndefined(msg))
- msg = JS_AtomToString(ctx, JS_ATOM_empty_string);
- else
- msg = JS_ToStringFree(ctx, msg);
- if (JS_IsException(msg)) {
- JS_FreeValue(ctx, name);
- return JS_EXCEPTION;
- }
- if (!JS_IsEmptyString(name) && !JS_IsEmptyString(msg))
- name = JS_ConcatString3(ctx, "", name, ": ");
- return JS_ConcatString(ctx, name, msg);
- }
- static const JSCFunctionListEntry js_error_proto_funcs[] = {
- JS_CFUNC_DEF("toString", 0, js_error_toString ),
- JS_PROP_STRING_DEF("name", "Error", JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE ),
- JS_PROP_STRING_DEF("message", "", JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE ),
- };
- /* 2 entries for each native error class */
- /* Note: we use an atom to avoid the autoinit definition which does
- not work in get_prop_string() */
- static const JSCFunctionListEntry js_native_error_proto_funcs[] = {
- #define DEF(name) \
- JS_PROP_ATOM_DEF("name", name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE ),\
- JS_PROP_STRING_DEF("message", "", JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE ),
-
- DEF(JS_ATOM_EvalError)
- DEF(JS_ATOM_RangeError)
- DEF(JS_ATOM_ReferenceError)
- DEF(JS_ATOM_SyntaxError)
- DEF(JS_ATOM_TypeError)
- DEF(JS_ATOM_URIError)
- DEF(JS_ATOM_InternalError)
- DEF(JS_ATOM_AggregateError)
- #undef DEF
- };
- static JSValue js_error_isError(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- return JS_NewBool(ctx, JS_IsError(ctx, argv[0]));
- }
- static const JSCFunctionListEntry js_error_funcs[] = {
- JS_CFUNC_DEF("isError", 1, js_error_isError),
- };
- /* AggregateError */
- /* used by C code. */
- static JSValue js_aggregate_error_constructor(JSContext *ctx,
- JSValueConst errors)
- {
- JSValue obj;
- obj = JS_NewObjectProtoClass(ctx,
- ctx->native_error_proto[JS_AGGREGATE_ERROR],
- JS_CLASS_ERROR);
- if (JS_IsException(obj))
- return obj;
- JS_DefinePropertyValue(ctx, obj, JS_ATOM_errors, JS_DupValue(ctx, errors),
- JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
- return obj;
- }
- /* Array */
- static int JS_CopySubArray(JSContext *ctx,
- JSValueConst obj, int64_t to_pos,
- int64_t from_pos, int64_t count, int dir)
- {
- JSObject *p;
- int64_t i, from, to, len;
- JSValue val;
- int fromPresent;
- p = NULL;
- if (JS_VALUE_GET_TAG(obj) == JS_TAG_OBJECT) {
- p = JS_VALUE_GET_OBJ(obj);
- if (p->class_id != JS_CLASS_ARRAY || !p->fast_array) {
- p = NULL;
- }
- }
- for (i = 0; i < count; ) {
- if (dir < 0) {
- from = from_pos + count - i - 1;
- to = to_pos + count - i - 1;
- } else {
- from = from_pos + i;
- to = to_pos + i;
- }
- if (p && p->fast_array &&
- from >= 0 && from < (len = p->u.array.count) &&
- to >= 0 && to < len) {
- int64_t l, j;
- /* Fast path for fast arrays. Since we don't look at the
- prototype chain, we can optimize only the cases where
- all the elements are present in the array. */
- l = count - i;
- if (dir < 0) {
- l = min_int64(l, from + 1);
- l = min_int64(l, to + 1);
- for(j = 0; j < l; j++) {
- set_value(ctx, &p->u.array.u.values[to - j],
- JS_DupValue(ctx, p->u.array.u.values[from - j]));
- }
- } else {
- l = min_int64(l, len - from);
- l = min_int64(l, len - to);
- for(j = 0; j < l; j++) {
- set_value(ctx, &p->u.array.u.values[to + j],
- JS_DupValue(ctx, p->u.array.u.values[from + j]));
- }
- }
- i += l;
- } else {
- fromPresent = JS_TryGetPropertyInt64(ctx, obj, from, &val);
- if (fromPresent < 0)
- goto exception;
- if (fromPresent) {
- if (JS_SetPropertyInt64(ctx, obj, to, val) < 0)
- goto exception;
- } else {
- if (JS_DeletePropertyInt64(ctx, obj, to, JS_PROP_THROW) < 0)
- goto exception;
- }
- i++;
- }
- }
- return 0;
- exception:
- return -1;
- }
- static JSValue js_array_constructor(JSContext *ctx, JSValueConst new_target,
- int argc, JSValueConst *argv)
- {
- JSValue obj;
- int i;
- obj = js_create_from_ctor(ctx, new_target, JS_CLASS_ARRAY);
- if (JS_IsException(obj))
- return obj;
- if (argc == 1 && JS_IsNumber(argv[0])) {
- uint32_t len;
- if (JS_ToArrayLengthFree(ctx, &len, JS_DupValue(ctx, argv[0]), TRUE))
- goto fail;
- if (JS_SetProperty(ctx, obj, JS_ATOM_length, JS_NewUint32(ctx, len)) < 0)
- goto fail;
- } else {
- for(i = 0; i < argc; i++) {
- if (JS_SetPropertyUint32(ctx, obj, i, JS_DupValue(ctx, argv[i])) < 0)
- goto fail;
- }
- }
- return obj;
- fail:
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue js_array_from(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- // from(items, mapfn = void 0, this_arg = void 0)
- JSValueConst items = argv[0], mapfn, this_arg;
- JSValueConst args[2];
- JSValue iter, r, v, v2, arrayLike, next_method, enum_obj;
- int64_t k, len;
- int done, mapping;
- mapping = FALSE;
- mapfn = JS_UNDEFINED;
- this_arg = JS_UNDEFINED;
- r = JS_UNDEFINED;
- arrayLike = JS_UNDEFINED;
- iter = JS_UNDEFINED;
- enum_obj = JS_UNDEFINED;
- next_method = JS_UNDEFINED;
- if (argc > 1) {
- mapfn = argv[1];
- if (!JS_IsUndefined(mapfn)) {
- if (check_function(ctx, mapfn))
- goto exception;
- mapping = 1;
- if (argc > 2)
- this_arg = argv[2];
- }
- }
- iter = JS_GetProperty(ctx, items, JS_ATOM_Symbol_iterator);
- if (JS_IsException(iter))
- goto exception;
- if (!JS_IsUndefined(iter) && !JS_IsNull(iter)) {
- if (!JS_IsFunction(ctx, iter)) {
- JS_ThrowTypeError(ctx, "value is not iterable");
- goto exception;
- }
- if (JS_IsConstructor(ctx, this_val))
- r = JS_CallConstructor(ctx, this_val, 0, NULL);
- else
- r = JS_NewArray(ctx);
- if (JS_IsException(r))
- goto exception;
- enum_obj = JS_GetIterator2(ctx, items, iter);
- if (JS_IsException(enum_obj))
- goto exception;
- next_method = JS_GetProperty(ctx, enum_obj, JS_ATOM_next);
- if (JS_IsException(next_method))
- goto exception;
- for (k = 0;; k++) {
- v = JS_IteratorNext(ctx, enum_obj, next_method, 0, NULL, &done);
- if (JS_IsException(v))
- goto exception;
- if (done)
- break;
- if (mapping) {
- args[0] = v;
- args[1] = JS_NewInt32(ctx, k);
- v2 = JS_Call(ctx, mapfn, this_arg, 2, args);
- JS_FreeValue(ctx, v);
- v = v2;
- if (JS_IsException(v))
- goto exception_close;
- }
- if (JS_DefinePropertyValueInt64(ctx, r, k, v,
- JS_PROP_C_W_E | JS_PROP_THROW) < 0)
- goto exception_close;
- }
- } else {
- arrayLike = JS_ToObject(ctx, items);
- if (JS_IsException(arrayLike))
- goto exception;
- if (js_get_length64(ctx, &len, arrayLike) < 0)
- goto exception;
- v = JS_NewInt64(ctx, len);
- args[0] = v;
- if (JS_IsConstructor(ctx, this_val)) {
- r = JS_CallConstructor(ctx, this_val, 1, args);
- } else {
- r = js_array_constructor(ctx, JS_UNDEFINED, 1, args);
- }
- JS_FreeValue(ctx, v);
- if (JS_IsException(r))
- goto exception;
- for(k = 0; k < len; k++) {
- v = JS_GetPropertyInt64(ctx, arrayLike, k);
- if (JS_IsException(v))
- goto exception;
- if (mapping) {
- args[0] = v;
- args[1] = JS_NewInt32(ctx, k);
- v2 = JS_Call(ctx, mapfn, this_arg, 2, args);
- JS_FreeValue(ctx, v);
- v = v2;
- if (JS_IsException(v))
- goto exception;
- }
- if (JS_DefinePropertyValueInt64(ctx, r, k, v,
- JS_PROP_C_W_E | JS_PROP_THROW) < 0)
- goto exception;
- }
- }
- if (JS_SetProperty(ctx, r, JS_ATOM_length, JS_NewUint32(ctx, k)) < 0)
- goto exception;
- goto done;
- exception_close:
- JS_IteratorClose(ctx, enum_obj, TRUE);
- exception:
- JS_FreeValue(ctx, r);
- r = JS_EXCEPTION;
- done:
- JS_FreeValue(ctx, arrayLike);
- JS_FreeValue(ctx, iter);
- JS_FreeValue(ctx, enum_obj);
- JS_FreeValue(ctx, next_method);
- return r;
- }
- static JSValue js_array_of(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue obj, args[1];
- int i;
- if (JS_IsConstructor(ctx, this_val)) {
- args[0] = JS_NewInt32(ctx, argc);
- obj = JS_CallConstructor(ctx, this_val, 1, (JSValueConst *)args);
- } else {
- obj = JS_NewArray(ctx);
- }
- if (JS_IsException(obj))
- return JS_EXCEPTION;
- for(i = 0; i < argc; i++) {
- if (JS_CreateDataPropertyUint32(ctx, obj, i, JS_DupValue(ctx, argv[i]),
- JS_PROP_THROW) < 0) {
- goto fail;
- }
- }
- if (JS_SetProperty(ctx, obj, JS_ATOM_length, JS_NewUint32(ctx, argc)) < 0) {
- fail:
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- return obj;
- }
- static JSValue js_array_isArray(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- int ret;
- ret = JS_IsArray(ctx, argv[0]);
- if (ret < 0)
- return JS_EXCEPTION;
- else
- return JS_NewBool(ctx, ret);
- }
- static JSValue js_get_this(JSContext *ctx,
- JSValueConst this_val)
- {
- return JS_DupValue(ctx, this_val);
- }
- static JSValue JS_ArraySpeciesCreate(JSContext *ctx, JSValueConst obj,
- JSValueConst len_val)
- {
- JSValue ctor, ret, species;
- int res;
- JSContext *realm;
- res = JS_IsArray(ctx, obj);
- if (res < 0)
- return JS_EXCEPTION;
- if (!res)
- return js_array_constructor(ctx, JS_UNDEFINED, 1, &len_val);
- ctor = JS_GetProperty(ctx, obj, JS_ATOM_constructor);
- if (JS_IsException(ctor))
- return ctor;
- if (JS_IsConstructor(ctx, ctor)) {
- /* legacy web compatibility */
- realm = JS_GetFunctionRealm(ctx, ctor);
- if (!realm) {
- JS_FreeValue(ctx, ctor);
- return JS_EXCEPTION;
- }
- if (realm != ctx &&
- js_same_value(ctx, ctor, realm->array_ctor)) {
- JS_FreeValue(ctx, ctor);
- ctor = JS_UNDEFINED;
- }
- }
- if (JS_IsObject(ctor)) {
- species = JS_GetProperty(ctx, ctor, JS_ATOM_Symbol_species);
- JS_FreeValue(ctx, ctor);
- if (JS_IsException(species))
- return species;
- ctor = species;
- if (JS_IsNull(ctor))
- ctor = JS_UNDEFINED;
- }
- if (JS_IsUndefined(ctor)) {
- return js_array_constructor(ctx, JS_UNDEFINED, 1, &len_val);
- } else {
- ret = JS_CallConstructor(ctx, ctor, 1, &len_val);
- JS_FreeValue(ctx, ctor);
- return ret;
- }
- }
- static const JSCFunctionListEntry js_array_funcs[] = {
- JS_CFUNC_DEF("isArray", 1, js_array_isArray ),
- JS_CFUNC_DEF("from", 1, js_array_from ),
- JS_CFUNC_DEF("of", 0, js_array_of ),
- JS_CGETSET_DEF("[Symbol.species]", js_get_this, NULL ),
- };
- static int JS_isConcatSpreadable(JSContext *ctx, JSValueConst obj)
- {
- JSValue val;
- if (!JS_IsObject(obj))
- return FALSE;
- val = JS_GetProperty(ctx, obj, JS_ATOM_Symbol_isConcatSpreadable);
- if (JS_IsException(val))
- return -1;
- if (!JS_IsUndefined(val))
- return JS_ToBoolFree(ctx, val);
- return JS_IsArray(ctx, obj);
- }
- static JSValue js_array_at(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue obj, ret;
- int64_t len, idx;
- JSValue *arrp;
- uint32_t count;
- obj = JS_ToObject(ctx, this_val);
- if (js_get_length64(ctx, &len, obj))
- goto exception;
- if (JS_ToInt64Sat(ctx, &idx, argv[0]))
- goto exception;
- if (idx < 0)
- idx = len + idx;
- if (idx < 0 || idx >= len) {
- ret = JS_UNDEFINED;
- } else if (js_get_fast_array(ctx, obj, &arrp, &count) && idx < count) {
- ret = JS_DupValue(ctx, arrp[idx]);
- } else {
- int present = JS_TryGetPropertyInt64(ctx, obj, idx, &ret);
- if (present < 0)
- goto exception;
- if (!present)
- ret = JS_UNDEFINED;
- }
- JS_FreeValue(ctx, obj);
- return ret;
- exception:
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue js_array_with(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue arr, obj, ret, *arrp, *pval;
- JSObject *p;
- int64_t i, len, idx;
- uint32_t count32;
- ret = JS_EXCEPTION;
- arr = JS_UNDEFINED;
- obj = JS_ToObject(ctx, this_val);
- if (js_get_length64(ctx, &len, obj))
- goto exception;
- if (JS_ToInt64Sat(ctx, &idx, argv[0]))
- goto exception;
- if (idx < 0)
- idx = len + idx;
- if (idx < 0 || idx >= len) {
- JS_ThrowRangeError(ctx, "invalid array index: %" PRId64, idx);
- goto exception;
- }
- arr = js_allocate_fast_array(ctx, len);
- if (JS_IsException(arr))
- goto exception;
- p = JS_VALUE_GET_OBJ(arr);
- i = 0;
- pval = p->u.array.u.values;
- if (js_get_fast_array(ctx, obj, &arrp, &count32) && count32 == len) {
- for (; i < idx; i++, pval++)
- *pval = JS_DupValue(ctx, arrp[i]);
- *pval = JS_DupValue(ctx, argv[1]);
- for (i++, pval++; i < len; i++, pval++)
- *pval = JS_DupValue(ctx, arrp[i]);
- } else {
- for (; i < idx; i++, pval++)
- if (-1 == JS_TryGetPropertyInt64(ctx, obj, i, pval))
- goto exception;
- *pval = JS_DupValue(ctx, argv[1]);
- for (i++, pval++; i < len; i++, pval++) {
- if (-1 == JS_TryGetPropertyInt64(ctx, obj, i, pval))
- goto exception;
- }
- }
- ret = arr;
- arr = JS_UNDEFINED;
- exception:
- JS_FreeValue(ctx, arr);
- JS_FreeValue(ctx, obj);
- return ret;
- }
- static JSValue js_array_concat(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue obj, arr, val;
- JSValueConst e;
- int64_t len, k, n;
- int i, res;
- arr = JS_UNDEFINED;
- obj = JS_ToObject(ctx, this_val);
- if (JS_IsException(obj))
- goto exception;
- arr = JS_ArraySpeciesCreate(ctx, obj, JS_NewInt32(ctx, 0));
- if (JS_IsException(arr))
- goto exception;
- n = 0;
- for (i = -1; i < argc; i++) {
- if (i < 0)
- e = obj;
- else
- e = argv[i];
- res = JS_isConcatSpreadable(ctx, e);
- if (res < 0)
- goto exception;
- if (res) {
- if (js_get_length64(ctx, &len, e))
- goto exception;
- if (n + len > MAX_SAFE_INTEGER) {
- JS_ThrowTypeError(ctx, "Array loo long");
- goto exception;
- }
- for (k = 0; k < len; k++, n++) {
- res = JS_TryGetPropertyInt64(ctx, e, k, &val);
- if (res < 0)
- goto exception;
- if (res) {
- if (JS_DefinePropertyValueInt64(ctx, arr, n, val,
- JS_PROP_C_W_E | JS_PROP_THROW) < 0)
- goto exception;
- }
- }
- } else {
- if (n >= MAX_SAFE_INTEGER) {
- JS_ThrowTypeError(ctx, "Array loo long");
- goto exception;
- }
- if (JS_DefinePropertyValueInt64(ctx, arr, n, JS_DupValue(ctx, e),
- JS_PROP_C_W_E | JS_PROP_THROW) < 0)
- goto exception;
- n++;
- }
- }
- if (JS_SetProperty(ctx, arr, JS_ATOM_length, JS_NewInt64(ctx, n)) < 0)
- goto exception;
- JS_FreeValue(ctx, obj);
- return arr;
- exception:
- JS_FreeValue(ctx, arr);
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- #define special_every 0
- #define special_some 1
- #define special_forEach 2
- #define special_map 3
- #define special_filter 4
- #define special_TA 8
- static JSValue js_typed_array___speciesCreate(JSContext *ctx,
- JSValueConst this_val,
- int argc, JSValueConst *argv);
- static JSValue js_array_every(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int special)
- {
- JSValue obj, val, index_val, res, ret;
- JSValueConst args[3];
- JSValueConst func, this_arg;
- int64_t len, k, n;
- int present;
- ret = JS_UNDEFINED;
- val = JS_UNDEFINED;
- if (special & special_TA) {
- obj = JS_DupValue(ctx, this_val);
- len = js_typed_array_get_length_unsafe(ctx, obj);
- if (len < 0)
- goto exception;
- } else {
- obj = JS_ToObject(ctx, this_val);
- if (js_get_length64(ctx, &len, obj))
- goto exception;
- }
- func = argv[0];
- this_arg = JS_UNDEFINED;
- if (argc > 1)
- this_arg = argv[1];
- if (check_function(ctx, func))
- goto exception;
- switch (special) {
- case special_every:
- case special_every | special_TA:
- ret = JS_TRUE;
- break;
- case special_some:
- case special_some | special_TA:
- ret = JS_FALSE;
- break;
- case special_map:
- /* XXX: JS_ArraySpeciesCreate should take int64_t */
- ret = JS_ArraySpeciesCreate(ctx, obj, JS_NewInt64(ctx, len));
- if (JS_IsException(ret))
- goto exception;
- break;
- case special_filter:
- ret = JS_ArraySpeciesCreate(ctx, obj, JS_NewInt32(ctx, 0));
- if (JS_IsException(ret))
- goto exception;
- break;
- case special_map | special_TA:
- args[0] = obj;
- args[1] = JS_NewInt32(ctx, len);
- ret = js_typed_array___speciesCreate(ctx, JS_UNDEFINED, 2, args);
- if (JS_IsException(ret))
- goto exception;
- break;
- case special_filter | special_TA:
- ret = JS_NewArray(ctx);
- if (JS_IsException(ret))
- goto exception;
- break;
- }
- n = 0;
- for(k = 0; k < len; k++) {
- if (special & special_TA) {
- val = JS_GetPropertyInt64(ctx, obj, k);
- if (JS_IsException(val))
- goto exception;
- present = TRUE;
- } else {
- present = JS_TryGetPropertyInt64(ctx, obj, k, &val);
- if (present < 0)
- goto exception;
- }
- if (present) {
- index_val = JS_NewInt64(ctx, k);
- if (JS_IsException(index_val))
- goto exception;
- args[0] = val;
- args[1] = index_val;
- args[2] = obj;
- res = JS_Call(ctx, func, this_arg, 3, args);
- JS_FreeValue(ctx, index_val);
- if (JS_IsException(res))
- goto exception;
- switch (special) {
- case special_every:
- case special_every | special_TA:
- if (!JS_ToBoolFree(ctx, res)) {
- ret = JS_FALSE;
- goto done;
- }
- break;
- case special_some:
- case special_some | special_TA:
- if (JS_ToBoolFree(ctx, res)) {
- ret = JS_TRUE;
- goto done;
- }
- break;
- case special_map:
- if (JS_DefinePropertyValueInt64(ctx, ret, k, res,
- JS_PROP_C_W_E | JS_PROP_THROW) < 0)
- goto exception;
- break;
- case special_map | special_TA:
- if (JS_SetPropertyValue(ctx, ret, JS_NewInt32(ctx, k), res, JS_PROP_THROW) < 0)
- goto exception;
- break;
- case special_filter:
- case special_filter | special_TA:
- if (JS_ToBoolFree(ctx, res)) {
- if (JS_DefinePropertyValueInt64(ctx, ret, n++, JS_DupValue(ctx, val),
- JS_PROP_C_W_E | JS_PROP_THROW) < 0)
- goto exception;
- }
- break;
- default:
- JS_FreeValue(ctx, res);
- break;
- }
- JS_FreeValue(ctx, val);
- val = JS_UNDEFINED;
- }
- }
- done:
- if (special == (special_filter | special_TA)) {
- JSValue arr;
- args[0] = obj;
- args[1] = JS_NewInt32(ctx, n);
- arr = js_typed_array___speciesCreate(ctx, JS_UNDEFINED, 2, args);
- if (JS_IsException(arr))
- goto exception;
- args[0] = ret;
- res = JS_Invoke(ctx, arr, JS_ATOM_set, 1, args);
- if (check_exception_free(ctx, res)) {
- JS_FreeValue(ctx, arr);
- goto exception;
- }
- JS_FreeValue(ctx, ret);
- ret = arr;
- }
- JS_FreeValue(ctx, val);
- JS_FreeValue(ctx, obj);
- return ret;
- exception:
- JS_FreeValue(ctx, ret);
- JS_FreeValue(ctx, val);
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- #define special_reduce 0
- #define special_reduceRight 1
- static JSValue js_array_reduce(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int special)
- {
- JSValue obj, val, index_val, acc, acc1;
- JSValueConst args[4];
- JSValueConst func;
- int64_t len, k, k1;
- int present;
- acc = JS_UNDEFINED;
- val = JS_UNDEFINED;
- if (special & special_TA) {
- obj = JS_DupValue(ctx, this_val);
- len = js_typed_array_get_length_unsafe(ctx, obj);
- if (len < 0)
- goto exception;
- } else {
- obj = JS_ToObject(ctx, this_val);
- if (js_get_length64(ctx, &len, obj))
- goto exception;
- }
- func = argv[0];
- if (check_function(ctx, func))
- goto exception;
- k = 0;
- if (argc > 1) {
- acc = JS_DupValue(ctx, argv[1]);
- } else {
- for(;;) {
- if (k >= len) {
- JS_ThrowTypeError(ctx, "empty array");
- goto exception;
- }
- k1 = (special & special_reduceRight) ? len - k - 1 : k;
- k++;
- if (special & special_TA) {
- acc = JS_GetPropertyInt64(ctx, obj, k1);
- if (JS_IsException(acc))
- goto exception;
- break;
- } else {
- present = JS_TryGetPropertyInt64(ctx, obj, k1, &acc);
- if (present < 0)
- goto exception;
- if (present)
- break;
- }
- }
- }
- for (; k < len; k++) {
- k1 = (special & special_reduceRight) ? len - k - 1 : k;
- if (special & special_TA) {
- val = JS_GetPropertyInt64(ctx, obj, k1);
- if (JS_IsException(val))
- goto exception;
- present = TRUE;
- } else {
- present = JS_TryGetPropertyInt64(ctx, obj, k1, &val);
- if (present < 0)
- goto exception;
- }
- if (present) {
- index_val = JS_NewInt64(ctx, k1);
- if (JS_IsException(index_val))
- goto exception;
- args[0] = acc;
- args[1] = val;
- args[2] = index_val;
- args[3] = obj;
- acc1 = JS_Call(ctx, func, JS_UNDEFINED, 4, args);
- JS_FreeValue(ctx, index_val);
- JS_FreeValue(ctx, val);
- val = JS_UNDEFINED;
- if (JS_IsException(acc1))
- goto exception;
- JS_FreeValue(ctx, acc);
- acc = acc1;
- }
- }
- JS_FreeValue(ctx, obj);
- return acc;
- exception:
- JS_FreeValue(ctx, acc);
- JS_FreeValue(ctx, val);
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue js_array_fill(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue obj;
- int64_t len, start, end;
- obj = JS_ToObject(ctx, this_val);
- if (js_get_length64(ctx, &len, obj))
- goto exception;
- start = 0;
- if (argc > 1 && !JS_IsUndefined(argv[1])) {
- if (JS_ToInt64Clamp(ctx, &start, argv[1], 0, len, len))
- goto exception;
- }
- end = len;
- if (argc > 2 && !JS_IsUndefined(argv[2])) {
- if (JS_ToInt64Clamp(ctx, &end, argv[2], 0, len, len))
- goto exception;
- }
- /* XXX: should special case fast arrays */
- while (start < end) {
- if (JS_SetPropertyInt64(ctx, obj, start,
- JS_DupValue(ctx, argv[0])) < 0)
- goto exception;
- start++;
- }
- return obj;
- exception:
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue js_array_includes(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue obj, val;
- int64_t len, n;
- JSValue *arrp;
- uint32_t count;
- int res;
- obj = JS_ToObject(ctx, this_val);
- if (js_get_length64(ctx, &len, obj))
- goto exception;
- res = FALSE;
- if (len > 0) {
- n = 0;
- if (argc > 1) {
- if (JS_ToInt64Clamp(ctx, &n, argv[1], 0, len, len))
- goto exception;
- }
- if (js_get_fast_array(ctx, obj, &arrp, &count)) {
- for (; n < count; n++) {
- if (js_strict_eq2(ctx, JS_DupValue(ctx, argv[0]),
- JS_DupValue(ctx, arrp[n]),
- JS_EQ_SAME_VALUE_ZERO)) {
- res = TRUE;
- goto done;
- }
- }
- }
- for (; n < len; n++) {
- val = JS_GetPropertyInt64(ctx, obj, n);
- if (JS_IsException(val))
- goto exception;
- if (js_strict_eq2(ctx, JS_DupValue(ctx, argv[0]), val,
- JS_EQ_SAME_VALUE_ZERO)) {
- res = TRUE;
- break;
- }
- }
- }
- done:
- JS_FreeValue(ctx, obj);
- return JS_NewBool(ctx, res);
- exception:
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue js_array_indexOf(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue obj, val;
- int64_t len, n, res;
- JSValue *arrp;
- uint32_t count;
- obj = JS_ToObject(ctx, this_val);
- if (js_get_length64(ctx, &len, obj))
- goto exception;
- res = -1;
- if (len > 0) {
- n = 0;
- if (argc > 1) {
- if (JS_ToInt64Clamp(ctx, &n, argv[1], 0, len, len))
- goto exception;
- }
- if (js_get_fast_array(ctx, obj, &arrp, &count)) {
- for (; n < count; n++) {
- if (js_strict_eq2(ctx, JS_DupValue(ctx, argv[0]),
- JS_DupValue(ctx, arrp[n]), JS_EQ_STRICT)) {
- res = n;
- goto done;
- }
- }
- }
- for (; n < len; n++) {
- int present = JS_TryGetPropertyInt64(ctx, obj, n, &val);
- if (present < 0)
- goto exception;
- if (present) {
- if (js_strict_eq2(ctx, JS_DupValue(ctx, argv[0]), val, JS_EQ_STRICT)) {
- res = n;
- break;
- }
- }
- }
- }
- done:
- JS_FreeValue(ctx, obj);
- return JS_NewInt64(ctx, res);
- exception:
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue js_array_lastIndexOf(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue obj, val;
- int64_t len, n, res;
- int present;
- obj = JS_ToObject(ctx, this_val);
- if (js_get_length64(ctx, &len, obj))
- goto exception;
- res = -1;
- if (len > 0) {
- n = len - 1;
- if (argc > 1) {
- if (JS_ToInt64Clamp(ctx, &n, argv[1], -1, len - 1, len))
- goto exception;
- }
- /* XXX: should special case fast arrays */
- for (; n >= 0; n--) {
- present = JS_TryGetPropertyInt64(ctx, obj, n, &val);
- if (present < 0)
- goto exception;
- if (present) {
- if (js_strict_eq2(ctx, JS_DupValue(ctx, argv[0]), val, JS_EQ_STRICT)) {
- res = n;
- break;
- }
- }
- }
- }
- JS_FreeValue(ctx, obj);
- return JS_NewInt64(ctx, res);
- exception:
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- enum {
- ArrayFind,
- ArrayFindIndex,
- ArrayFindLast,
- ArrayFindLastIndex,
- };
- static JSValue js_array_find(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int mode)
- {
- JSValueConst func, this_arg;
- JSValueConst args[3];
- JSValue obj, val, index_val, res;
- int64_t len, k, end;
- int dir;
- index_val = JS_UNDEFINED;
- val = JS_UNDEFINED;
- obj = JS_ToObject(ctx, this_val);
- if (js_get_length64(ctx, &len, obj))
- goto exception;
- func = argv[0];
- if (check_function(ctx, func))
- goto exception;
- this_arg = JS_UNDEFINED;
- if (argc > 1)
- this_arg = argv[1];
- k = 0;
- dir = 1;
- end = len;
- if (mode == ArrayFindLast || mode == ArrayFindLastIndex) {
- k = len - 1;
- dir = -1;
- end = -1;
- }
- // TODO(bnoordhuis) add fast path for fast arrays
- for(; k != end; k += dir) {
- index_val = JS_NewInt64(ctx, k);
- if (JS_IsException(index_val))
- goto exception;
- val = JS_GetPropertyValue(ctx, obj, index_val);
- if (JS_IsException(val))
- goto exception;
- args[0] = val;
- args[1] = index_val;
- args[2] = this_val;
- res = JS_Call(ctx, func, this_arg, 3, args);
- if (JS_IsException(res))
- goto exception;
- if (JS_ToBoolFree(ctx, res)) {
- if (mode == ArrayFindIndex || mode == ArrayFindLastIndex) {
- JS_FreeValue(ctx, val);
- JS_FreeValue(ctx, obj);
- return index_val;
- } else {
- JS_FreeValue(ctx, index_val);
- JS_FreeValue(ctx, obj);
- return val;
- }
- }
- JS_FreeValue(ctx, val);
- JS_FreeValue(ctx, index_val);
- }
- JS_FreeValue(ctx, obj);
- if (mode == ArrayFindIndex || mode == ArrayFindLastIndex)
- return JS_NewInt32(ctx, -1);
- else
- return JS_UNDEFINED;
- exception:
- JS_FreeValue(ctx, index_val);
- JS_FreeValue(ctx, val);
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue js_array_toString(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue obj, method, ret;
- obj = JS_ToObject(ctx, this_val);
- if (JS_IsException(obj))
- return JS_EXCEPTION;
- method = JS_GetProperty(ctx, obj, JS_ATOM_join);
- if (JS_IsException(method)) {
- ret = JS_EXCEPTION;
- } else
- if (!JS_IsFunction(ctx, method)) {
- /* Use intrinsic Object.prototype.toString */
- JS_FreeValue(ctx, method);
- ret = js_object_toString(ctx, obj, 0, NULL);
- } else {
- ret = JS_CallFree(ctx, method, obj, 0, NULL);
- }
- JS_FreeValue(ctx, obj);
- return ret;
- }
- static JSValue js_array_join(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int toLocaleString)
- {
- JSValue obj, sep = JS_UNDEFINED, el;
- StringBuffer b_s, *b = &b_s;
- JSString *p = NULL;
- int64_t i, n;
- int c;
- obj = JS_ToObject(ctx, this_val);
- if (js_get_length64(ctx, &n, obj))
- goto exception;
- c = ','; /* default separator */
- if (!toLocaleString && argc > 0 && !JS_IsUndefined(argv[0])) {
- sep = JS_ToString(ctx, argv[0]);
- if (JS_IsException(sep))
- goto exception;
- p = JS_VALUE_GET_STRING(sep);
- if (p->len == 1 && !p->is_wide_char)
- c = p->u.str8[0];
- else
- c = -1;
- }
- string_buffer_init(ctx, b, 0);
- for(i = 0; i < n; i++) {
- if (i > 0) {
- if (c >= 0) {
- string_buffer_putc8(b, c);
- } else {
- string_buffer_concat(b, p, 0, p->len);
- }
- }
- el = JS_GetPropertyUint32(ctx, obj, i);
- if (JS_IsException(el))
- goto fail;
- if (!JS_IsNull(el) && !JS_IsUndefined(el)) {
- if (toLocaleString) {
- el = JS_ToLocaleStringFree(ctx, el);
- }
- if (string_buffer_concat_value_free(b, el))
- goto fail;
- }
- }
- JS_FreeValue(ctx, sep);
- JS_FreeValue(ctx, obj);
- return string_buffer_end(b);
- fail:
- string_buffer_free(b);
- JS_FreeValue(ctx, sep);
- exception:
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue js_array_pop(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int shift)
- {
- JSValue obj, res = JS_UNDEFINED;
- int64_t len, newLen;
- JSValue *arrp;
- uint32_t count32;
- obj = JS_ToObject(ctx, this_val);
- if (js_get_length64(ctx, &len, obj))
- goto exception;
- newLen = 0;
- if (len > 0) {
- newLen = len - 1;
- /* Special case fast arrays */
- if (js_get_fast_array(ctx, obj, &arrp, &count32) && count32 == len) {
- JSObject *p = JS_VALUE_GET_OBJ(obj);
- if (shift) {
- res = arrp[0];
- memmove(arrp, arrp + 1, (count32 - 1) * sizeof(*arrp));
- p->u.array.count--;
- } else {
- res = arrp[count32 - 1];
- p->u.array.count--;
- }
- } else {
- if (shift) {
- res = JS_GetPropertyInt64(ctx, obj, 0);
- if (JS_IsException(res))
- goto exception;
- if (JS_CopySubArray(ctx, obj, 0, 1, len - 1, +1))
- goto exception;
- } else {
- res = JS_GetPropertyInt64(ctx, obj, newLen);
- if (JS_IsException(res))
- goto exception;
- }
- if (JS_DeletePropertyInt64(ctx, obj, newLen, JS_PROP_THROW) < 0)
- goto exception;
- }
- }
- if (JS_SetProperty(ctx, obj, JS_ATOM_length, JS_NewInt64(ctx, newLen)) < 0)
- goto exception;
- JS_FreeValue(ctx, obj);
- return res;
- exception:
- JS_FreeValue(ctx, res);
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue js_array_push(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int unshift)
- {
- JSValue obj;
- int i;
- int64_t len, from, newLen;
- if (likely(JS_VALUE_GET_TAG(this_val) == JS_TAG_OBJECT && !unshift)) {
- JSObject *p = JS_VALUE_GET_OBJ(this_val);
- if (likely(p->class_id == JS_CLASS_ARRAY && p->fast_array &&
- can_extend_fast_array(p) &&
- JS_VALUE_GET_TAG(p->prop[0].u.value) == JS_TAG_INT &&
- JS_VALUE_GET_INT(p->prop[0].u.value) == p->u.array.count &&
- (get_shape_prop(p->shape)->flags & JS_PROP_WRITABLE) != 0)) {
- /* fast case */
- uint32_t new_len;
- new_len = p->u.array.count + argc;
- if (likely(new_len <= INT32_MAX)) {
- if (unlikely(new_len > p->u.array.u1.size)) {
- if (expand_fast_array(ctx, p, new_len))
- return JS_EXCEPTION;
- }
- for(i = 0; i < argc; i++)
- p->u.array.u.values[p->u.array.count + i] = JS_DupValue(ctx, argv[i]);
- p->prop[0].u.value = JS_NewInt32(ctx, new_len);
- p->u.array.count = new_len;
- return JS_NewInt32(ctx, new_len);
- }
- }
- }
- obj = JS_ToObject(ctx, this_val);
- if (js_get_length64(ctx, &len, obj))
- goto exception;
- newLen = len + argc;
- if (newLen > MAX_SAFE_INTEGER) {
- JS_ThrowTypeError(ctx, "Array loo long");
- goto exception;
- }
- from = len;
- if (unshift && argc > 0) {
- if (JS_CopySubArray(ctx, obj, argc, 0, len, -1))
- goto exception;
- from = 0;
- }
- for(i = 0; i < argc; i++) {
- if (JS_SetPropertyInt64(ctx, obj, from + i,
- JS_DupValue(ctx, argv[i])) < 0)
- goto exception;
- }
- if (JS_SetProperty(ctx, obj, JS_ATOM_length, JS_NewInt64(ctx, newLen)) < 0)
- goto exception;
- JS_FreeValue(ctx, obj);
- return JS_NewInt64(ctx, newLen);
- exception:
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue js_array_reverse(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue obj, lval, hval;
- JSValue *arrp;
- int64_t len, l, h;
- int l_present, h_present;
- uint32_t count32;
- lval = JS_UNDEFINED;
- obj = JS_ToObject(ctx, this_val);
- if (js_get_length64(ctx, &len, obj))
- goto exception;
- /* Special case fast arrays */
- if (js_get_fast_array(ctx, obj, &arrp, &count32) && count32 == len) {
- uint32_t ll, hh;
- if (count32 > 1) {
- for (ll = 0, hh = count32 - 1; ll < hh; ll++, hh--) {
- lval = arrp[ll];
- arrp[ll] = arrp[hh];
- arrp[hh] = lval;
- }
- }
- return obj;
- }
- for (l = 0, h = len - 1; l < h; l++, h--) {
- l_present = JS_TryGetPropertyInt64(ctx, obj, l, &lval);
- if (l_present < 0)
- goto exception;
- h_present = JS_TryGetPropertyInt64(ctx, obj, h, &hval);
- if (h_present < 0)
- goto exception;
- if (h_present) {
- if (JS_SetPropertyInt64(ctx, obj, l, hval) < 0)
- goto exception;
- if (l_present) {
- if (JS_SetPropertyInt64(ctx, obj, h, lval) < 0) {
- lval = JS_UNDEFINED;
- goto exception;
- }
- lval = JS_UNDEFINED;
- } else {
- if (JS_DeletePropertyInt64(ctx, obj, h, JS_PROP_THROW) < 0)
- goto exception;
- }
- } else {
- if (l_present) {
- if (JS_DeletePropertyInt64(ctx, obj, l, JS_PROP_THROW) < 0)
- goto exception;
- if (JS_SetPropertyInt64(ctx, obj, h, lval) < 0) {
- lval = JS_UNDEFINED;
- goto exception;
- }
- lval = JS_UNDEFINED;
- }
- }
- }
- return obj;
- exception:
- JS_FreeValue(ctx, lval);
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- // Note: a.toReversed() is a.slice().reverse() with the twist that a.slice()
- // leaves holes in sparse arrays intact whereas a.toReversed() replaces them
- // with undefined, thus in effect creating a dense array.
- // Does not use Array[@@species], always returns a base Array.
- static JSValue js_array_toReversed(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue arr, obj, ret, *arrp, *pval;
- JSObject *p;
- int64_t i, len;
- uint32_t count32;
- ret = JS_EXCEPTION;
- arr = JS_UNDEFINED;
- obj = JS_ToObject(ctx, this_val);
- if (js_get_length64(ctx, &len, obj))
- goto exception;
- arr = js_allocate_fast_array(ctx, len);
- if (JS_IsException(arr))
- goto exception;
- if (len > 0) {
- p = JS_VALUE_GET_OBJ(arr);
- i = len - 1;
- pval = p->u.array.u.values;
- if (js_get_fast_array(ctx, obj, &arrp, &count32) && count32 == len) {
- for (; i >= 0; i--, pval++)
- *pval = JS_DupValue(ctx, arrp[i]);
- } else {
- // Query order is observable; test262 expects descending order.
- for (; i >= 0; i--, pval++) {
- if (-1 == JS_TryGetPropertyInt64(ctx, obj, i, pval))
- goto exception;
- }
- }
- }
- ret = arr;
- arr = JS_UNDEFINED;
- exception:
- JS_FreeValue(ctx, arr);
- JS_FreeValue(ctx, obj);
- return ret;
- }
- static JSValue js_array_slice(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int splice)
- {
- JSValue obj, arr, val, len_val;
- int64_t len, start, k, final, n, count, del_count, new_len;
- int kPresent;
- JSValue *arrp;
- uint32_t count32, i, item_count;
- arr = JS_UNDEFINED;
- obj = JS_ToObject(ctx, this_val);
- if (js_get_length64(ctx, &len, obj))
- goto exception;
- if (JS_ToInt64Clamp(ctx, &start, argv[0], 0, len, len))
- goto exception;
- if (splice) {
- if (argc == 0) {
- item_count = 0;
- del_count = 0;
- } else
- if (argc == 1) {
- item_count = 0;
- del_count = len - start;
- } else {
- item_count = argc - 2;
- if (JS_ToInt64Clamp(ctx, &del_count, argv[1], 0, len - start, 0))
- goto exception;
- }
- if (len + item_count - del_count > MAX_SAFE_INTEGER) {
- JS_ThrowTypeError(ctx, "Array loo long");
- goto exception;
- }
- count = del_count;
- } else {
- item_count = 0; /* avoid warning */
- final = len;
- if (!JS_IsUndefined(argv[1])) {
- if (JS_ToInt64Clamp(ctx, &final, argv[1], 0, len, len))
- goto exception;
- }
- count = max_int64(final - start, 0);
- }
- len_val = JS_NewInt64(ctx, count);
- arr = JS_ArraySpeciesCreate(ctx, obj, len_val);
- JS_FreeValue(ctx, len_val);
- if (JS_IsException(arr))
- goto exception;
- k = start;
- final = start + count;
- n = 0;
- /* The fast array test on arr ensures that
- JS_CreateDataPropertyUint32() won't modify obj in case arr is
- an exotic object */
- /* Special case fast arrays */
- if (js_get_fast_array(ctx, obj, &arrp, &count32) &&
- js_is_fast_array(ctx, arr)) {
- /* XXX: should share code with fast array constructor */
- for (; k < final && k < count32; k++, n++) {
- if (JS_CreateDataPropertyUint32(ctx, arr, n, JS_DupValue(ctx, arrp[k]), JS_PROP_THROW) < 0)
- goto exception;
- }
- }
- /* Copy the remaining elements if any (handle case of inherited properties) */
- for (; k < final; k++, n++) {
- kPresent = JS_TryGetPropertyInt64(ctx, obj, k, &val);
- if (kPresent < 0)
- goto exception;
- if (kPresent) {
- if (JS_CreateDataPropertyUint32(ctx, arr, n, val, JS_PROP_THROW) < 0)
- goto exception;
- }
- }
- if (JS_SetProperty(ctx, arr, JS_ATOM_length, JS_NewInt64(ctx, n)) < 0)
- goto exception;
- if (splice) {
- new_len = len + item_count - del_count;
- if (item_count != del_count) {
- if (JS_CopySubArray(ctx, obj, start + item_count,
- start + del_count, len - (start + del_count),
- item_count <= del_count ? +1 : -1) < 0)
- goto exception;
- for (k = len; k-- > new_len; ) {
- if (JS_DeletePropertyInt64(ctx, obj, k, JS_PROP_THROW) < 0)
- goto exception;
- }
- }
- for (i = 0; i < item_count; i++) {
- if (JS_SetPropertyInt64(ctx, obj, start + i, JS_DupValue(ctx, argv[i + 2])) < 0)
- goto exception;
- }
- if (JS_SetProperty(ctx, obj, JS_ATOM_length, JS_NewInt64(ctx, new_len)) < 0)
- goto exception;
- }
- JS_FreeValue(ctx, obj);
- return arr;
- exception:
- JS_FreeValue(ctx, obj);
- JS_FreeValue(ctx, arr);
- return JS_EXCEPTION;
- }
- static JSValue js_array_toSpliced(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue arr, obj, ret, *arrp, *pval, *last;
- JSObject *p;
- int64_t i, j, len, newlen, start, add, del;
- uint32_t count32;
- pval = NULL;
- last = NULL;
- ret = JS_EXCEPTION;
- arr = JS_UNDEFINED;
- obj = JS_ToObject(ctx, this_val);
- if (js_get_length64(ctx, &len, obj))
- goto exception;
- start = 0;
- if (argc > 0)
- if (JS_ToInt64Clamp(ctx, &start, argv[0], 0, len, len))
- goto exception;
- del = 0;
- if (argc > 0)
- del = len - start;
- if (argc > 1)
- if (JS_ToInt64Clamp(ctx, &del, argv[1], 0, del, 0))
- goto exception;
- add = 0;
- if (argc > 2)
- add = argc - 2;
- newlen = len + add - del;
- if (newlen > MAX_SAFE_INTEGER) {
- JS_ThrowTypeError(ctx, "invalid array length");
- goto exception;
- }
- arr = js_allocate_fast_array(ctx, newlen);
- if (JS_IsException(arr))
- goto exception;
- if (newlen <= 0)
- goto done;
- p = JS_VALUE_GET_OBJ(arr);
- pval = &p->u.array.u.values[0];
- last = &p->u.array.u.values[newlen];
- if (js_get_fast_array(ctx, obj, &arrp, &count32) && count32 == len) {
- for (i = 0; i < start; i++, pval++)
- *pval = JS_DupValue(ctx, arrp[i]);
- for (j = 0; j < add; j++, pval++)
- *pval = JS_DupValue(ctx, argv[2 + j]);
- for (i += del; i < len; i++, pval++)
- *pval = JS_DupValue(ctx, arrp[i]);
- } else {
- for (i = 0; i < start; i++, pval++)
- if (-1 == JS_TryGetPropertyInt64(ctx, obj, i, pval))
- goto exception;
- for (j = 0; j < add; j++, pval++)
- *pval = JS_DupValue(ctx, argv[2 + j]);
- for (i += del; i < len; i++, pval++)
- if (-1 == JS_TryGetPropertyInt64(ctx, obj, i, pval))
- goto exception;
- }
- assert(pval == last);
- done:
- ret = arr;
- arr = JS_UNDEFINED;
- exception:
- JS_FreeValue(ctx, arr);
- JS_FreeValue(ctx, obj);
- return ret;
- }
- static JSValue js_array_copyWithin(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue obj;
- int64_t len, from, to, final, count;
- obj = JS_ToObject(ctx, this_val);
- if (js_get_length64(ctx, &len, obj))
- goto exception;
- if (JS_ToInt64Clamp(ctx, &to, argv[0], 0, len, len))
- goto exception;
- if (JS_ToInt64Clamp(ctx, &from, argv[1], 0, len, len))
- goto exception;
- final = len;
- if (argc > 2 && !JS_IsUndefined(argv[2])) {
- if (JS_ToInt64Clamp(ctx, &final, argv[2], 0, len, len))
- goto exception;
- }
- count = min_int64(final - from, len - to);
- if (JS_CopySubArray(ctx, obj, to, from, count,
- (from < to && to < from + count) ? -1 : +1))
- goto exception;
- return obj;
- exception:
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static int64_t JS_FlattenIntoArray(JSContext *ctx, JSValueConst target,
- JSValueConst source, int64_t sourceLen,
- int64_t targetIndex, int depth,
- JSValueConst mapperFunction,
- JSValueConst thisArg)
- {
- JSValue element;
- int64_t sourceIndex, elementLen;
- int present, is_array;
- if (js_check_stack_overflow(ctx->rt, 0)) {
- JS_ThrowStackOverflow(ctx);
- return -1;
- }
- for (sourceIndex = 0; sourceIndex < sourceLen; sourceIndex++) {
- present = JS_TryGetPropertyInt64(ctx, source, sourceIndex, &element);
- if (present < 0)
- return -1;
- if (!present)
- continue;
- if (!JS_IsUndefined(mapperFunction)) {
- JSValueConst args[3] = { element, JS_NewInt64(ctx, sourceIndex), source };
- element = JS_Call(ctx, mapperFunction, thisArg, 3, args);
- JS_FreeValue(ctx, (JSValue)args[0]);
- JS_FreeValue(ctx, (JSValue)args[1]);
- if (JS_IsException(element))
- return -1;
- }
- if (depth > 0) {
- is_array = JS_IsArray(ctx, element);
- if (is_array < 0)
- goto fail;
- if (is_array) {
- if (js_get_length64(ctx, &elementLen, element) < 0)
- goto fail;
- targetIndex = JS_FlattenIntoArray(ctx, target, element,
- elementLen, targetIndex,
- depth - 1,
- JS_UNDEFINED, JS_UNDEFINED);
- if (targetIndex < 0)
- goto fail;
- JS_FreeValue(ctx, element);
- continue;
- }
- }
- if (targetIndex >= MAX_SAFE_INTEGER) {
- JS_ThrowTypeError(ctx, "Array too long");
- goto fail;
- }
- if (JS_DefinePropertyValueInt64(ctx, target, targetIndex, element,
- JS_PROP_C_W_E | JS_PROP_THROW) < 0)
- return -1;
- targetIndex++;
- }
- return targetIndex;
- fail:
- JS_FreeValue(ctx, element);
- return -1;
- }
- static JSValue js_array_flatten(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int map)
- {
- JSValue obj, arr;
- JSValueConst mapperFunction, thisArg;
- int64_t sourceLen;
- int depthNum;
- arr = JS_UNDEFINED;
- obj = JS_ToObject(ctx, this_val);
- if (js_get_length64(ctx, &sourceLen, obj))
- goto exception;
- depthNum = 1;
- mapperFunction = JS_UNDEFINED;
- thisArg = JS_UNDEFINED;
- if (map) {
- mapperFunction = argv[0];
- if (argc > 1) {
- thisArg = argv[1];
- }
- if (check_function(ctx, mapperFunction))
- goto exception;
- } else {
- if (argc > 0 && !JS_IsUndefined(argv[0])) {
- if (JS_ToInt32Sat(ctx, &depthNum, argv[0]) < 0)
- goto exception;
- }
- }
- arr = JS_ArraySpeciesCreate(ctx, obj, JS_NewInt32(ctx, 0));
- if (JS_IsException(arr))
- goto exception;
- if (JS_FlattenIntoArray(ctx, arr, obj, sourceLen, 0, depthNum,
- mapperFunction, thisArg) < 0)
- goto exception;
- JS_FreeValue(ctx, obj);
- return arr;
- exception:
- JS_FreeValue(ctx, obj);
- JS_FreeValue(ctx, arr);
- return JS_EXCEPTION;
- }
- /* Array sort */
- typedef struct ValueSlot {
- JSValue val;
- JSString *str;
- int64_t pos;
- } ValueSlot;
- struct array_sort_context {
- JSContext *ctx;
- int exception;
- int has_method;
- JSValueConst method;
- };
- static int js_array_cmp_generic(const void *a, const void *b, void *opaque) {
- struct array_sort_context *psc = opaque;
- JSContext *ctx = psc->ctx;
- JSValueConst argv[2];
- JSValue res;
- ValueSlot *ap = (ValueSlot *)(void *)a;
- ValueSlot *bp = (ValueSlot *)(void *)b;
- int cmp;
- if (psc->exception)
- return 0;
- if (psc->has_method) {
- /* custom sort function is specified as returning 0 for identical
- * objects: avoid method call overhead.
- */
- if (!memcmp(&ap->val, &bp->val, sizeof(ap->val)))
- goto cmp_same;
- argv[0] = ap->val;
- argv[1] = bp->val;
- res = JS_Call(ctx, psc->method, JS_UNDEFINED, 2, argv);
- if (JS_IsException(res))
- goto exception;
- if (JS_VALUE_GET_TAG(res) == JS_TAG_INT) {
- int val = JS_VALUE_GET_INT(res);
- cmp = (val > 0) - (val < 0);
- } else {
- double val;
- if (JS_ToFloat64Free(ctx, &val, res) < 0)
- goto exception;
- cmp = (val > 0) - (val < 0);
- }
- } else {
- /* Not supposed to bypass ToString even for identical objects as
- * tested in test262/test/built-ins/Array/prototype/sort/bug_596_1.js
- */
- if (!ap->str) {
- JSValue str = JS_ToString(ctx, ap->val);
- if (JS_IsException(str))
- goto exception;
- ap->str = JS_VALUE_GET_STRING(str);
- }
- if (!bp->str) {
- JSValue str = JS_ToString(ctx, bp->val);
- if (JS_IsException(str))
- goto exception;
- bp->str = JS_VALUE_GET_STRING(str);
- }
- cmp = js_string_compare(ctx, ap->str, bp->str);
- }
- if (cmp != 0)
- return cmp;
- cmp_same:
- /* make sort stable: compare array offsets */
- return (ap->pos > bp->pos) - (ap->pos < bp->pos);
- exception:
- psc->exception = 1;
- return 0;
- }
- static JSValue js_array_sort(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- struct array_sort_context asc = { ctx, 0, 0, argv[0] };
- JSValue obj = JS_UNDEFINED;
- ValueSlot *array = NULL;
- size_t array_size = 0, pos = 0, n = 0;
- int64_t i, len, undefined_count = 0;
- int present;
- if (!JS_IsUndefined(asc.method)) {
- if (check_function(ctx, asc.method))
- goto exception;
- asc.has_method = 1;
- }
- obj = JS_ToObject(ctx, this_val);
- if (js_get_length64(ctx, &len, obj))
- goto exception;
- /* XXX: should special case fast arrays */
- for (i = 0; i < len; i++) {
- if (pos >= array_size) {
- size_t new_size, slack;
- ValueSlot *new_array;
- new_size = (array_size + (array_size >> 1) + 31) & ~15;
- new_array = js_realloc2(ctx, array, new_size * sizeof(*array), &slack);
- if (new_array == NULL)
- goto exception;
- new_size += slack / sizeof(*new_array);
- array = new_array;
- array_size = new_size;
- }
- present = JS_TryGetPropertyInt64(ctx, obj, i, &array[pos].val);
- if (present < 0)
- goto exception;
- if (present == 0)
- continue;
- if (JS_IsUndefined(array[pos].val)) {
- undefined_count++;
- continue;
- }
- array[pos].str = NULL;
- array[pos].pos = i;
- pos++;
- }
- rqsort(array, pos, sizeof(*array), js_array_cmp_generic, &asc);
- if (asc.exception)
- goto exception;
- /* XXX: should special case fast arrays */
- while (n < pos) {
- if (array[n].str)
- JS_FreeValue(ctx, JS_MKPTR(JS_TAG_STRING, array[n].str));
- if (array[n].pos == n) {
- JS_FreeValue(ctx, array[n].val);
- } else {
- if (JS_SetPropertyInt64(ctx, obj, n, array[n].val) < 0) {
- n++;
- goto exception;
- }
- }
- n++;
- }
- js_free(ctx, array);
- for (i = n; undefined_count-- > 0; i++) {
- if (JS_SetPropertyInt64(ctx, obj, i, JS_UNDEFINED) < 0)
- goto fail;
- }
- for (; i < len; i++) {
- if (JS_DeletePropertyInt64(ctx, obj, i, JS_PROP_THROW) < 0)
- goto fail;
- }
- return obj;
- exception:
- for (; n < pos; n++) {
- JS_FreeValue(ctx, array[n].val);
- if (array[n].str)
- JS_FreeValue(ctx, JS_MKPTR(JS_TAG_STRING, array[n].str));
- }
- js_free(ctx, array);
- fail:
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- // Note: a.toSorted() is a.slice().sort() with the twist that a.slice()
- // leaves holes in sparse arrays intact whereas a.toSorted() replaces them
- // with undefined, thus in effect creating a dense array.
- // Does not use Array[@@species], always returns a base Array.
- static JSValue js_array_toSorted(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue arr, obj, ret, *arrp, *pval;
- JSObject *p;
- int64_t i, len;
- uint32_t count32;
- int ok;
- ok = JS_IsUndefined(argv[0]) || JS_IsFunction(ctx, argv[0]);
- if (!ok)
- return JS_ThrowTypeError(ctx, "not a function");
- ret = JS_EXCEPTION;
- arr = JS_UNDEFINED;
- obj = JS_ToObject(ctx, this_val);
- if (js_get_length64(ctx, &len, obj))
- goto exception;
- arr = js_allocate_fast_array(ctx, len);
- if (JS_IsException(arr))
- goto exception;
- if (len > 0) {
- p = JS_VALUE_GET_OBJ(arr);
- i = 0;
- pval = p->u.array.u.values;
- if (js_get_fast_array(ctx, obj, &arrp, &count32) && count32 == len) {
- for (; i < len; i++, pval++)
- *pval = JS_DupValue(ctx, arrp[i]);
- } else {
- for (; i < len; i++, pval++) {
- if (-1 == JS_TryGetPropertyInt64(ctx, obj, i, pval))
- goto exception;
- }
- }
- }
- ret = js_array_sort(ctx, arr, argc, argv);
- if (JS_IsException(ret))
- goto exception;
- JS_FreeValue(ctx, ret);
- ret = arr;
- arr = JS_UNDEFINED;
- exception:
- JS_FreeValue(ctx, arr);
- JS_FreeValue(ctx, obj);
- return ret;
- }
- typedef struct JSArrayIteratorData {
- JSValue obj;
- JSIteratorKindEnum kind;
- uint32_t idx;
- } JSArrayIteratorData;
- static void js_array_iterator_finalizer(JSRuntime *rt, JSValue val)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JSArrayIteratorData *it = p->u.array_iterator_data;
- if (it) {
- JS_FreeValueRT(rt, it->obj);
- js_free_rt(rt, it);
- }
- }
- static void js_array_iterator_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JSArrayIteratorData *it = p->u.array_iterator_data;
- if (it) {
- JS_MarkValue(rt, it->obj, mark_func);
- }
- }
- static JSValue js_create_array_iterator(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- JSValue enum_obj, arr;
- JSArrayIteratorData *it;
- JSIteratorKindEnum kind;
- int class_id;
- kind = magic & 3;
- if (magic & 4) {
- /* string iterator case */
- arr = JS_ToStringCheckObject(ctx, this_val);
- class_id = JS_CLASS_STRING_ITERATOR;
- } else {
- arr = JS_ToObject(ctx, this_val);
- class_id = JS_CLASS_ARRAY_ITERATOR;
- }
- if (JS_IsException(arr))
- goto fail;
- enum_obj = JS_NewObjectClass(ctx, class_id);
- if (JS_IsException(enum_obj))
- goto fail;
- it = js_malloc(ctx, sizeof(*it));
- if (!it)
- goto fail1;
- it->obj = arr;
- it->kind = kind;
- it->idx = 0;
- JS_SetOpaque(enum_obj, it);
- return enum_obj;
- fail1:
- JS_FreeValue(ctx, enum_obj);
- fail:
- JS_FreeValue(ctx, arr);
- return JS_EXCEPTION;
- }
- static JSValue js_array_iterator_next(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv,
- BOOL *pdone, int magic)
- {
- JSArrayIteratorData *it;
- uint32_t len, idx;
- JSValue val, obj;
- JSObject *p;
- it = JS_GetOpaque2(ctx, this_val, JS_CLASS_ARRAY_ITERATOR);
- if (!it)
- goto fail1;
- if (JS_IsUndefined(it->obj))
- goto done;
- p = JS_VALUE_GET_OBJ(it->obj);
- if (p->class_id >= JS_CLASS_UINT8C_ARRAY &&
- p->class_id <= JS_CLASS_FLOAT64_ARRAY) {
- if (typed_array_is_oob(p)) {
- JS_ThrowTypeErrorArrayBufferOOB(ctx);
- goto fail1;
- }
- len = p->u.array.count;
- } else {
- if (js_get_length32(ctx, &len, it->obj)) {
- fail1:
- *pdone = FALSE;
- return JS_EXCEPTION;
- }
- }
- idx = it->idx;
- if (idx >= len) {
- JS_FreeValue(ctx, it->obj);
- it->obj = JS_UNDEFINED;
- done:
- *pdone = TRUE;
- return JS_UNDEFINED;
- }
- it->idx = idx + 1;
- *pdone = FALSE;
- if (it->kind == JS_ITERATOR_KIND_KEY) {
- return JS_NewUint32(ctx, idx);
- } else {
- val = JS_GetPropertyUint32(ctx, it->obj, idx);
- if (JS_IsException(val))
- return JS_EXCEPTION;
- if (it->kind == JS_ITERATOR_KIND_VALUE) {
- return val;
- } else {
- JSValueConst args[2];
- JSValue num;
- num = JS_NewUint32(ctx, idx);
- args[0] = num;
- args[1] = val;
- obj = js_create_array(ctx, 2, args);
- JS_FreeValue(ctx, val);
- JS_FreeValue(ctx, num);
- return obj;
- }
- }
- }
- /* Iterator Wrap */
- typedef struct JSIteratorWrapData {
- JSValue wrapped_iter;
- JSValue wrapped_next;
- } JSIteratorWrapData;
- static void js_iterator_wrap_finalizer(JSRuntime *rt, JSValue val)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JSIteratorWrapData *it = p->u.iterator_wrap_data;
- if (it) {
- JS_FreeValueRT(rt, it->wrapped_iter);
- JS_FreeValueRT(rt, it->wrapped_next);
- js_free_rt(rt, it);
- }
- }
- static void js_iterator_wrap_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JSIteratorWrapData *it = p->u.iterator_wrap_data;
- if (it) {
- JS_MarkValue(rt, it->wrapped_iter, mark_func);
- JS_MarkValue(rt, it->wrapped_next, mark_func);
- }
- }
- static JSValue js_iterator_wrap_next(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv,
- int *pdone, int magic)
- {
- JSIteratorWrapData *it;
- JSValue method, ret;
- it = JS_GetOpaque2(ctx, this_val, JS_CLASS_ITERATOR_WRAP);
- if (!it)
- return JS_EXCEPTION;
- if (magic == GEN_MAGIC_NEXT) {
- return JS_IteratorNext(ctx, it->wrapped_iter, it->wrapped_next, 0, NULL, pdone);
- } else {
- method = JS_GetProperty(ctx, it->wrapped_iter, JS_ATOM_return);
- if (JS_IsException(method))
- return JS_EXCEPTION;
- if (JS_IsNull(method) || JS_IsUndefined(method)) {
- *pdone = TRUE;
- return JS_UNDEFINED;
- }
- ret = JS_IteratorNext2(ctx, it->wrapped_iter, method, 0, NULL, pdone);
- JS_FreeValue(ctx, method);
- return ret;
- }
- }
- static const JSCFunctionListEntry js_iterator_wrap_proto_funcs[] = {
- JS_ITERATOR_NEXT_DEF("next", 0, js_iterator_wrap_next, GEN_MAGIC_NEXT ),
- JS_ITERATOR_NEXT_DEF("return", 0, js_iterator_wrap_next, GEN_MAGIC_RETURN ),
- };
- /* Iterator */
- static JSValue js_iterator_constructor_getset(JSContext *ctx,
- JSValueConst this_val,
- int argc, JSValueConst *argv,
- int magic,
- JSValue *func_data)
- {
- int ret;
- if (argc > 0) { // if setter
- if (!JS_IsObject(argv[0]))
- return JS_ThrowTypeErrorNotAnObject(ctx);
- ret = JS_DefinePropertyValue(ctx, this_val, JS_ATOM_constructor,
- JS_DupValue(ctx, argv[0]),
- JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
- if (ret < 0)
- return JS_EXCEPTION;
- return JS_UNDEFINED;
- } else {
- return JS_DupValue(ctx, func_data[0]);
- }
- }
- static JSValue js_iterator_constructor(JSContext *ctx, JSValueConst new_target,
- int argc, JSValueConst *argv)
- {
- JSObject *p;
- if (JS_TAG_OBJECT != JS_VALUE_GET_TAG(new_target))
- return JS_ThrowTypeError(ctx, "constructor requires 'new'");
- p = JS_VALUE_GET_OBJ(new_target);
- if (p->class_id == JS_CLASS_C_FUNCTION &&
- p->u.cfunc.c_function.generic == js_iterator_constructor) {
- return JS_ThrowTypeError(ctx, "abstract class not constructable");
- }
- return js_create_from_ctor(ctx, new_target, JS_CLASS_ITERATOR);
- }
- // note: deliberately doesn't use space-saving bit fields for
- // |index|, |count| and |running| because tcc miscompiles them
- typedef struct JSIteratorConcatData {
- int index, count; // elements (not pairs!) in values[] array
- BOOL running;
- JSValue iter, next, values[]; // array of (object, method) pairs
- } JSIteratorConcatData;
- static void js_iterator_concat_finalizer(JSRuntime *rt, JSValue val)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JSIteratorConcatData *it = p->u.iterator_concat_data;
- if (it) {
- JS_FreeValueRT(rt, it->iter);
- JS_FreeValueRT(rt, it->next);
- for (int i = it->index; i < it->count; i++)
- JS_FreeValueRT(rt, it->values[i]);
- js_free_rt(rt, it);
- }
- }
- static void js_iterator_concat_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JSIteratorConcatData *it = p->u.iterator_concat_data;
- if (it) {
- JS_MarkValue(rt, it->iter, mark_func);
- JS_MarkValue(rt, it->next, mark_func);
- for (int i = it->index; i < it->count; i++)
- JS_MarkValue(rt, it->values[i], mark_func);
- }
- }
- static JSValue js_iterator_concat_next(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv,
- int *pdone, int magic)
- {
- JSValue iter, item, next, val, *obj, *meth, ret;
- JSIteratorConcatData *it;
- int done;
- *pdone = FALSE;
- it = JS_GetOpaque2(ctx, this_val, JS_CLASS_ITERATOR_CONCAT);
- if (!it)
- return JS_EXCEPTION;
- if (it->running)
- return JS_ThrowTypeError(ctx, "already running");
- it->running = TRUE;
- for(;;) {
- if (it->index >= it->count) {
- *pdone = TRUE;
- ret = JS_UNDEFINED;
- break;
- }
- obj = &it->values[it->index + 0];
- meth = &it->values[it->index + 1];
- iter = it->iter;
- if (JS_IsUndefined(iter)) {
- iter = JS_GetIterator2(ctx, *obj, *meth);
- if (JS_IsException(iter))
- goto fail;
- it->iter = iter;
- }
- next = it->next;
- if (JS_IsUndefined(next)) {
- next = JS_GetProperty(ctx, iter, JS_ATOM_next);
- if (JS_IsException(next))
- goto fail;
- it->next = next;
- }
- item = JS_IteratorNext2(ctx, iter, next, 0, NULL, &done);
- if (JS_IsException(item))
- goto fail;
- if (done == 0) {
- ret = item;
- break;
- } else if (done == 2) {
- val = JS_GetProperty(ctx, item, JS_ATOM_done);
- if (JS_IsException(val)) {
- JS_FreeValue(ctx, item);
- fail:
- ret = JS_EXCEPTION;
- break;
- }
- done = JS_ToBoolFree(ctx, val);
- if (done)
- goto done_next;
- ret = JS_GetProperty(ctx, item, JS_ATOM_value);
- JS_FreeValue(ctx, item);
- break;
- } else {
- done_next:
- JS_FreeValue(ctx, item);
- JS_FreeValue(ctx, iter);
- JS_FreeValue(ctx, next);
- it->iter = JS_UNDEFINED;
- it->next = JS_UNDEFINED;
- JS_FreeValue(ctx, *meth);
- JS_FreeValue(ctx, *obj);
- it->index += 2;
- }
- }
- it->running = FALSE;
- return ret;
- }
- static JSValue js_iterator_concat_return(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSIteratorConcatData *it;
- JSValue ret;
- it = JS_GetOpaque2(ctx, this_val, JS_CLASS_ITERATOR_CONCAT);
- if (!it)
- return JS_EXCEPTION;
- if (it->running)
- return JS_ThrowTypeError(ctx, "already running");
- ret = JS_UNDEFINED;
- if (!JS_IsUndefined(it->iter)) {
- it->running = TRUE;
- ret = JS_GetProperty(ctx, it->iter, JS_ATOM_return);
- if (JS_IsException(ret)) {
- it->running = FALSE;
- return JS_EXCEPTION;
- }
- ret = JS_CallFree(ctx, ret, it->iter, 0, NULL);
- it->running = FALSE;
- }
- while (it->index < it->count)
- JS_FreeValue(ctx, it->values[it->index++]);
- JS_FreeValue(ctx, it->iter);
- JS_FreeValue(ctx, it->next);
- it->iter = JS_UNDEFINED;
- it->next = JS_UNDEFINED;
- return ret;
- }
- static const JSCFunctionListEntry js_iterator_concat_proto_funcs[] = {
- JS_ITERATOR_NEXT_DEF("next", 0, js_iterator_concat_next, 0 ),
- JS_CFUNC_DEF("return", 0, js_iterator_concat_return ),
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "Iterator Concat", JS_PROP_CONFIGURABLE ),
- };
- static JSValue js_iterator_concat(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSIteratorConcatData *it;
- JSValue obj, method;
- it = js_malloc(ctx, sizeof(*it) + 2*argc * sizeof(it->values[0]));
- if (!it)
- return JS_EXCEPTION;
- it->running = FALSE;
- it->index = 0;
- it->count = 0;
- it->iter = JS_UNDEFINED;
- it->next = JS_UNDEFINED;
- for (int i = 0; i < argc; i++) {
- JSValueConst obj = argv[i];
- if (!JS_IsObject(obj)) {
- JS_ThrowTypeErrorNotAnObject(ctx);
- goto fail;
- }
- method = JS_GetProperty(ctx, obj, JS_ATOM_Symbol_iterator);
- if (JS_IsException(method))
- goto fail;
- if (!JS_IsFunction(ctx, method)) {
- JS_ThrowTypeError(ctx, "not a function");
- JS_FreeValue(ctx, method);
- goto fail;
- }
- it->values[it->count++] = JS_DupValue(ctx, obj);
- it->values[it->count++] = method;
- }
- obj = JS_NewObjectClass(ctx, JS_CLASS_ITERATOR_CONCAT);
- if (JS_IsException(obj))
- goto fail;
- JS_SetOpaque(obj, it);
- return obj;
- fail:
- for (int i = 0; i < it->count; i++)
- JS_FreeValue(ctx, it->values[i]);
- js_free(ctx, it);
- return JS_EXCEPTION;
- }
- static JSValue js_iterator_from(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValueConst obj = argv[0];
- JSValue method, iter, wrapper;
- JSIteratorWrapData *it;
- int ret;
- if (!JS_IsObject(obj)) {
- if (!JS_IsString(obj))
- return JS_ThrowTypeError(ctx, "Iterator.from called on non-object");
- }
- method = JS_GetProperty(ctx, obj, JS_ATOM_Symbol_iterator);
- if (JS_IsException(method))
- return JS_EXCEPTION;
- if (JS_IsNull(method) || JS_IsUndefined(method)) {
- iter = JS_DupValue(ctx, obj);
- } else {
- iter = JS_GetIterator2(ctx, obj, method);
- JS_FreeValue(ctx, method);
- if (JS_IsException(iter))
- return JS_EXCEPTION;
- }
- wrapper = JS_UNDEFINED;
- method = JS_GetProperty(ctx, iter, JS_ATOM_next);
- if (JS_IsException(method))
- goto fail;
- ret = JS_OrdinaryIsInstanceOf(ctx, iter, ctx->iterator_ctor);
- if (ret < 0)
- goto fail;
- if (ret) {
- JS_FreeValue(ctx, method);
- return iter;
- }
-
- wrapper = JS_NewObjectClass(ctx, JS_CLASS_ITERATOR_WRAP);
- if (JS_IsException(wrapper))
- goto fail;
- it = js_malloc(ctx, sizeof(*it));
- if (!it)
- goto fail;
- it->wrapped_iter = iter;
- it->wrapped_next = method;
- JS_SetOpaque(wrapper, it);
- return wrapper;
- fail:
- JS_FreeValue(ctx, method);
- JS_FreeValue(ctx, iter);
- JS_FreeValue(ctx, wrapper);
- return JS_EXCEPTION;
- }
- typedef enum JSIteratorHelperKindEnum {
- JS_ITERATOR_HELPER_KIND_DROP,
- JS_ITERATOR_HELPER_KIND_EVERY,
- JS_ITERATOR_HELPER_KIND_FILTER,
- JS_ITERATOR_HELPER_KIND_FIND,
- JS_ITERATOR_HELPER_KIND_FLAT_MAP,
- JS_ITERATOR_HELPER_KIND_FOR_EACH,
- JS_ITERATOR_HELPER_KIND_MAP,
- JS_ITERATOR_HELPER_KIND_SOME,
- JS_ITERATOR_HELPER_KIND_TAKE,
- } JSIteratorHelperKindEnum;
- typedef struct JSIteratorHelperData {
- JSValue obj;
- JSValue next;
- JSValue func; // predicate (filter) or mapper (flatMap, map)
- JSValue inner; // innerValue (flatMap)
- int64_t count; // limit (drop, take) or counter (filter, map, flatMap)
- JSIteratorHelperKindEnum kind : 8;
- uint8_t executing : 1;
- uint8_t done : 1;
- } JSIteratorHelperData;
- static JSValue js_create_iterator_helper(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- JSValueConst func;
- JSValue obj, method;
- int64_t count;
- JSIteratorHelperData *it;
- if (!JS_IsObject(this_val))
- return JS_ThrowTypeErrorNotAnObject(ctx);
- func = JS_UNDEFINED;
- count = 0;
- switch(magic) {
- case JS_ITERATOR_HELPER_KIND_DROP:
- case JS_ITERATOR_HELPER_KIND_TAKE:
- {
- JSValue v;
- double dlimit;
- v = JS_ToNumber(ctx, argv[0]);
- if (JS_IsException(v))
- goto fail;
- // Check for Infinity.
- if (JS_ToFloat64(ctx, &dlimit, v)) {
- JS_FreeValue(ctx, v);
- goto fail;
- }
- if (isnan(dlimit)) {
- JS_FreeValue(ctx, v);
- goto range_error;
- }
- if (!isfinite(dlimit)) {
- JS_FreeValue(ctx, v);
- if (dlimit < 0)
- goto range_error;
- else
- count = MAX_SAFE_INTEGER;
- } else {
- v = JS_ToIntegerFree(ctx, v);
- if (JS_IsException(v))
- goto fail;
- if (JS_ToInt64Free(ctx, &count, v))
- goto fail;
- }
- if (count < 0)
- goto range_error;
- }
- break;
- case JS_ITERATOR_HELPER_KIND_FILTER:
- case JS_ITERATOR_HELPER_KIND_FLAT_MAP:
- case JS_ITERATOR_HELPER_KIND_MAP:
- {
- func = argv[0];
- if (check_function(ctx, func))
- goto fail;
- }
- break;
- default:
- abort();
- break;
- }
- method = JS_GetProperty(ctx, this_val, JS_ATOM_next);
- if (JS_IsException(method))
- goto fail;
- obj = JS_NewObjectClass(ctx, JS_CLASS_ITERATOR_HELPER);
- if (JS_IsException(obj)) {
- JS_FreeValue(ctx, method);
- goto fail;
- }
- it = js_malloc(ctx, sizeof(*it));
- if (!it) {
- JS_FreeValue(ctx, obj);
- JS_FreeValue(ctx, method);
- goto fail;
- }
- it->kind = magic;
- it->obj = JS_DupValue(ctx, this_val);
- it->func = JS_DupValue(ctx, func);
- it->next = method;
- it->inner = JS_UNDEFINED;
- it->count = count;
- it->executing = 0;
- it->done = 0;
- JS_SetOpaque(obj, it);
- return obj;
- range_error:
- JS_ThrowRangeError(ctx, "must be positive");
- fail:
- JS_IteratorClose(ctx, this_val, TRUE);
- return JS_EXCEPTION;
- }
- static JSValue js_iterator_proto_func(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- JSValue item, method, ret, func, index_val, r;
- JSValueConst args[2];
- int64_t idx;
- int done;
- if (!JS_IsObject(this_val))
- return JS_ThrowTypeErrorNotAnObject(ctx);
- func = JS_UNDEFINED;
- method = JS_UNDEFINED;
-
- if (check_function(ctx, argv[0]))
- goto fail;
- func = JS_DupValue(ctx, argv[0]);
- method = JS_GetProperty(ctx, this_val, JS_ATOM_next);
- if (JS_IsException(method))
- goto fail_no_close;
- r = JS_UNDEFINED;
- switch(magic) {
- case JS_ITERATOR_HELPER_KIND_EVERY:
- {
- r = JS_TRUE;
- for (idx = 0; /*empty*/; idx++) {
- item = JS_IteratorNext(ctx, this_val, method, 0, NULL, &done);
- if (JS_IsException(item))
- goto fail_no_close;
- if (done)
- break;
- index_val = JS_NewInt64(ctx, idx);
- args[0] = item;
- args[1] = index_val;
- ret = JS_Call(ctx, func, JS_UNDEFINED, countof(args), args);
- JS_FreeValue(ctx, item);
- JS_FreeValue(ctx, index_val);
- if (JS_IsException(ret))
- goto fail;
- if (!JS_ToBoolFree(ctx, ret)) {
- if (JS_IteratorClose(ctx, this_val, FALSE) < 0)
- r = JS_EXCEPTION;
- else
- r = JS_FALSE;
- break;
- }
- index_val = JS_UNDEFINED;
- ret = JS_UNDEFINED;
- item = JS_UNDEFINED;
- }
- }
- break;
- case JS_ITERATOR_HELPER_KIND_FIND:
- {
- for (idx = 0; /*empty*/; idx++) {
- item = JS_IteratorNext(ctx, this_val, method, 0, NULL, &done);
- if (JS_IsException(item))
- goto fail_no_close;
- if (done)
- break;
- index_val = JS_NewInt64(ctx, idx);
- args[0] = item;
- args[1] = index_val;
- ret = JS_Call(ctx, func, JS_UNDEFINED, countof(args), args);
- JS_FreeValue(ctx, index_val);
- if (JS_IsException(ret)) {
- JS_FreeValue(ctx, item);
- goto fail;
- }
- if (JS_ToBoolFree(ctx, ret)) {
- if (JS_IteratorClose(ctx, this_val, FALSE) < 0) {
- JS_FreeValue(ctx, item);
- r = JS_EXCEPTION;
- } else {
- r = item;
- }
- break;
- }
- JS_FreeValue(ctx, item);
- index_val = JS_UNDEFINED;
- ret = JS_UNDEFINED;
- item = JS_UNDEFINED;
- }
- }
- break;
- case JS_ITERATOR_HELPER_KIND_FOR_EACH:
- {
- for (idx = 0; /*empty*/; idx++) {
- item = JS_IteratorNext(ctx, this_val, method, 0, NULL, &done);
- if (JS_IsException(item))
- goto fail_no_close;
- if (done)
- break;
- index_val = JS_NewInt64(ctx, idx);
- args[0] = item;
- args[1] = index_val;
- ret = JS_Call(ctx, func, JS_UNDEFINED, countof(args), args);
- JS_FreeValue(ctx, item);
- JS_FreeValue(ctx, index_val);
- if (JS_IsException(ret))
- goto fail;
- JS_FreeValue(ctx, ret);
- index_val = JS_UNDEFINED;
- ret = JS_UNDEFINED;
- item = JS_UNDEFINED;
- }
- }
- break;
- case JS_ITERATOR_HELPER_KIND_SOME:
- {
- r = JS_FALSE;
- for (idx = 0; /*empty*/; idx++) {
- item = JS_IteratorNext(ctx, this_val, method, 0, NULL, &done);
- if (JS_IsException(item))
- goto fail_no_close;
- if (done)
- break;
- index_val = JS_NewInt64(ctx, idx);
- args[0] = item;
- args[1] = index_val;
- ret = JS_Call(ctx, func, JS_UNDEFINED, countof(args), args);
- JS_FreeValue(ctx, item);
- JS_FreeValue(ctx, index_val);
- if (JS_IsException(ret))
- goto fail;
- if (JS_ToBoolFree(ctx, ret)) {
- if (JS_IteratorClose(ctx, this_val, FALSE) < 0)
- r = JS_EXCEPTION;
- else
- r = JS_TRUE;
- break;
- }
- index_val = JS_UNDEFINED;
- ret = JS_UNDEFINED;
- item = JS_UNDEFINED;
- }
- }
- break;
- default:
- abort();
- break;
- }
- JS_FreeValue(ctx, func);
- JS_FreeValue(ctx, method);
- return r;
- fail:
- JS_IteratorClose(ctx, this_val, TRUE);
- fail_no_close:
- JS_FreeValue(ctx, func);
- JS_FreeValue(ctx, method);
- return JS_EXCEPTION;
- }
- static JSValue js_iterator_proto_reduce(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue item, method, ret, func, index_val, acc;
- JSValueConst args[3];
- int64_t idx;
- int done;
- if (!JS_IsObject(this_val))
- return JS_ThrowTypeErrorNotAnObject(ctx);
- acc = JS_UNDEFINED;
- func = JS_UNDEFINED;
- method = JS_UNDEFINED;
- if (check_function(ctx, argv[0]))
- goto exception;
- func = JS_DupValue(ctx, argv[0]);
- method = JS_GetProperty(ctx, this_val, JS_ATOM_next);
- if (JS_IsException(method))
- goto exception;
- if (argc > 1) {
- acc = JS_DupValue(ctx, argv[1]);
- idx = 0;
- } else {
- acc = JS_IteratorNext(ctx, this_val, method, 0, NULL, &done);
- if (JS_IsException(acc))
- goto exception_no_close;
- if (done) {
- JS_ThrowTypeError(ctx, "empty iterator");
- goto exception;
- }
- idx = 1;
- }
- for (/* empty */; /*empty*/; idx++) {
- item = JS_IteratorNext(ctx, this_val, method, 0, NULL, &done);
- if (JS_IsException(item))
- goto exception_no_close;
- if (done)
- break;
- index_val = JS_NewInt64(ctx, idx);
- args[0] = acc;
- args[1] = item;
- args[2] = index_val;
- ret = JS_Call(ctx, func, JS_UNDEFINED, countof(args), args);
- JS_FreeValue(ctx, item);
- JS_FreeValue(ctx, index_val);
- if (JS_IsException(ret))
- goto exception;
- JS_FreeValue(ctx, acc);
- acc = ret;
- index_val = JS_UNDEFINED;
- ret = JS_UNDEFINED;
- item = JS_UNDEFINED;
- }
- JS_FreeValue(ctx, func);
- JS_FreeValue(ctx, method);
- return acc;
- exception:
- JS_IteratorClose(ctx, this_val, TRUE);
- exception_no_close:
- JS_FreeValue(ctx, acc);
- JS_FreeValue(ctx, func);
- JS_FreeValue(ctx, method);
- return JS_EXCEPTION;
- }
- static JSValue js_iterator_proto_toArray(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue item, method, result;
- int64_t idx;
- int done;
- result = JS_UNDEFINED;
- if (!JS_IsObject(this_val))
- return JS_ThrowTypeErrorNotAnObject(ctx);
- method = JS_GetProperty(ctx, this_val, JS_ATOM_next);
- if (JS_IsException(method))
- return JS_EXCEPTION;
- result = JS_NewArray(ctx);
- if (JS_IsException(result))
- goto exception;
- for (idx = 0; /*empty*/; idx++) {
- item = JS_IteratorNext(ctx, this_val, method, 0, NULL, &done);
- if (JS_IsException(item))
- goto exception;
- if (done)
- break;
- if (JS_DefinePropertyValueInt64(ctx, result, idx, item,
- JS_PROP_C_W_E | JS_PROP_THROW) < 0)
- goto exception;
- }
- if (JS_SetProperty(ctx, result, JS_ATOM_length, JS_NewUint32(ctx, idx)) < 0)
- goto exception;
- JS_FreeValue(ctx, method);
- return result;
- exception:
- JS_FreeValue(ctx, result);
- JS_FreeValue(ctx, method);
- return JS_EXCEPTION;
- }
- static JSValue js_iterator_proto_iterator(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- return JS_DupValue(ctx, this_val);
- }
- static JSValue js_iterator_proto_get_toStringTag(JSContext *ctx, JSValueConst this_val)
- {
- return JS_AtomToString(ctx, JS_ATOM_Iterator);
- }
- static JSValue js_iterator_proto_set_toStringTag(JSContext *ctx, JSValueConst this_val, JSValueConst val)
- {
- int res;
- if (!JS_IsObject(this_val))
- return JS_ThrowTypeErrorNotAnObject(ctx);
- if (js_same_value(ctx, this_val, ctx->class_proto[JS_CLASS_ITERATOR]))
- return JS_ThrowTypeError(ctx, "Cannot assign to read only property");
- res = JS_GetOwnProperty(ctx, NULL, this_val, JS_ATOM_Symbol_toStringTag);
- if (res < 0)
- return JS_EXCEPTION;
- if (res) {
- if (JS_SetProperty(ctx, this_val, JS_ATOM_Symbol_toStringTag, JS_DupValue(ctx, val)) < 0)
- return JS_EXCEPTION;
- } else {
- if (JS_DefinePropertyValue(ctx, this_val, JS_ATOM_Symbol_toStringTag, JS_DupValue(ctx, val), JS_PROP_C_W_E) < 0)
- return JS_EXCEPTION;
- }
- return JS_UNDEFINED;
- }
- /* Iterator Helper */
- static void js_iterator_helper_finalizer(JSRuntime *rt, JSValue val)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JSIteratorHelperData *it = p->u.iterator_helper_data;
- if (it) {
- JS_FreeValueRT(rt, it->obj);
- JS_FreeValueRT(rt, it->func);
- JS_FreeValueRT(rt, it->next);
- JS_FreeValueRT(rt, it->inner);
- js_free_rt(rt, it);
- }
- }
- static void js_iterator_helper_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JSIteratorHelperData *it = p->u.iterator_helper_data;
- if (it) {
- JS_MarkValue(rt, it->obj, mark_func);
- JS_MarkValue(rt, it->func, mark_func);
- JS_MarkValue(rt, it->next, mark_func);
- JS_MarkValue(rt, it->inner, mark_func);
- }
- }
- static JSValue js_iterator_helper_next(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv,
- int *pdone, int magic)
- {
- JSIteratorHelperData *it;
- JSValue ret;
- *pdone = FALSE;
- it = JS_GetOpaque2(ctx, this_val, JS_CLASS_ITERATOR_HELPER);
- if (!it)
- return JS_EXCEPTION;
- if (it->executing)
- return JS_ThrowTypeError(ctx, "cannot invoke a running iterator");
- if (it->done) {
- *pdone = TRUE;
- return JS_UNDEFINED;
- }
- it->executing = 1;
- switch (it->kind) {
- case JS_ITERATOR_HELPER_KIND_DROP:
- {
- JSValue item, method;
- if (magic == GEN_MAGIC_NEXT) {
- method = JS_DupValue(ctx, it->next);
- } else {
- method = JS_GetProperty(ctx, it->obj, JS_ATOM_return);
- if (JS_IsException(method))
- goto fail;
- }
- while (it->count > 0) {
- it->count--;
- item = JS_IteratorNext(ctx, it->obj, method, 0, NULL, pdone);
- if (JS_IsException(item)) {
- JS_FreeValue(ctx, method);
- goto fail_no_close;
- }
- JS_FreeValue(ctx, item);
- if (magic == GEN_MAGIC_RETURN)
- *pdone = TRUE;
- if (*pdone) {
- JS_FreeValue(ctx, method);
- ret = JS_UNDEFINED;
- goto done;
- }
- }
- item = JS_IteratorNext(ctx, it->obj, method, 0, NULL, pdone);
- JS_FreeValue(ctx, method);
- if (JS_IsException(item))
- goto fail_no_close;
- ret = item;
- goto done;
- }
- break;
- case JS_ITERATOR_HELPER_KIND_FILTER:
- {
- JSValue item, method, selected, index_val;
- JSValueConst args[2];
- if (magic == GEN_MAGIC_NEXT) {
- method = JS_DupValue(ctx, it->next);
- } else {
- method = JS_GetProperty(ctx, it->obj, JS_ATOM_return);
- if (JS_IsException(method))
- goto fail;
- }
- filter_again:
- item = JS_IteratorNext(ctx, it->obj, method, 0, NULL, pdone);
- if (JS_IsException(item)) {
- JS_FreeValue(ctx, method);
- goto fail_no_close;
- }
- if (*pdone || magic == GEN_MAGIC_RETURN) {
- JS_FreeValue(ctx, method);
- ret = item;
- goto done;
- }
- index_val = JS_NewInt64(ctx, it->count++);
- args[0] = item;
- args[1] = index_val;
- selected = JS_Call(ctx, it->func, JS_UNDEFINED, countof(args), args);
- JS_FreeValue(ctx, index_val);
- if (JS_IsException(selected)) {
- JS_FreeValue(ctx, item);
- JS_FreeValue(ctx, method);
- goto fail;
- }
- if (JS_ToBoolFree(ctx, selected)) {
- JS_FreeValue(ctx, method);
- ret = item;
- goto done;
- }
- JS_FreeValue(ctx, item);
- goto filter_again;
- }
- break;
- case JS_ITERATOR_HELPER_KIND_FLAT_MAP:
- {
- JSValue item, method, index_val, iter;
- JSValueConst args[2];
- flat_map_again:
- if (JS_IsUndefined(it->inner)) {
- if (magic == GEN_MAGIC_NEXT) {
- method = JS_DupValue(ctx, it->next);
- } else {
- method = JS_GetProperty(ctx, it->obj, JS_ATOM_return);
- if (JS_IsException(method))
- goto fail;
- }
- item = JS_IteratorNext(ctx, it->obj, method, 0, NULL, pdone);
- JS_FreeValue(ctx, method);
- if (JS_IsException(item))
- goto fail_no_close;
- if (*pdone || magic == GEN_MAGIC_RETURN) {
- ret = item;
- goto done;
- }
- index_val = JS_NewInt64(ctx, it->count++);
- args[0] = item;
- args[1] = index_val;
- ret = JS_Call(ctx, it->func, JS_UNDEFINED, countof(args), args);
- JS_FreeValue(ctx, item);
- JS_FreeValue(ctx, index_val);
- if (JS_IsException(ret))
- goto fail;
- if (!JS_IsObject(ret)) {
- JS_FreeValue(ctx, ret);
- JS_ThrowTypeError(ctx, "not an object");
- goto fail;
- }
- method = JS_GetProperty(ctx, ret, JS_ATOM_Symbol_iterator);
- if (JS_IsException(method)) {
- JS_FreeValue(ctx, ret);
- goto fail;
- }
- if (JS_IsNull(method) || JS_IsUndefined(method)) {
- JS_FreeValue(ctx, method);
- iter = ret;
- } else {
- iter = JS_GetIterator2(ctx, ret, method);
- JS_FreeValue(ctx, method);
- JS_FreeValue(ctx, ret);
- if (JS_IsException(iter))
- goto fail;
- }
- it->inner = iter;
- }
- if (magic == GEN_MAGIC_NEXT)
- method = JS_GetProperty(ctx, it->inner, JS_ATOM_next);
- else
- method = JS_GetProperty(ctx, it->inner, JS_ATOM_return);
- if (JS_IsException(method)) {
- inner_fail:
- JS_IteratorClose(ctx, it->inner, FALSE);
- JS_FreeValue(ctx, it->inner);
- it->inner = JS_UNDEFINED;
- goto fail;
- }
- if (magic == GEN_MAGIC_RETURN && (JS_IsUndefined(method) || JS_IsNull(method))) {
- goto inner_end;
- } else {
- item = JS_IteratorNext(ctx, it->inner, method, 0, NULL, pdone);
- JS_FreeValue(ctx, method);
- if (JS_IsException(item))
- goto inner_fail;
- }
- if (*pdone) {
- inner_end:
- *pdone = FALSE; // The outer iterator must continue.
- JS_IteratorClose(ctx, it->inner, FALSE);
- JS_FreeValue(ctx, it->inner);
- it->inner = JS_UNDEFINED;
- goto flat_map_again;
- }
- ret = item;
- goto done;
- }
- break;
- case JS_ITERATOR_HELPER_KIND_MAP:
- {
- JSValue item, method, index_val;
- JSValueConst args[2];
- if (magic == GEN_MAGIC_NEXT) {
- method = JS_DupValue(ctx, it->next);
- } else {
- method = JS_GetProperty(ctx, it->obj, JS_ATOM_return);
- if (JS_IsException(method))
- goto fail;
- }
- item = JS_IteratorNext(ctx, it->obj, method, 0, NULL, pdone);
- JS_FreeValue(ctx, method);
- if (JS_IsException(item))
- goto fail_no_close;
- if (*pdone || magic == GEN_MAGIC_RETURN) {
- ret = item;
- goto done;
- }
- index_val = JS_NewInt64(ctx, it->count++);
- args[0] = item;
- args[1] = index_val;
- ret = JS_Call(ctx, it->func, JS_UNDEFINED, countof(args), args);
- JS_FreeValue(ctx, index_val);
- JS_FreeValue(ctx, item);
- if (JS_IsException(ret))
- goto fail;
- goto done;
- }
- break;
- case JS_ITERATOR_HELPER_KIND_TAKE:
- {
- JSValue item, method;
- if (it->count > 0) {
- if (magic == GEN_MAGIC_NEXT) {
- method = JS_DupValue(ctx, it->next);
- } else {
- method = JS_GetProperty(ctx, it->obj, JS_ATOM_return);
- if (JS_IsException(method))
- goto fail;
- }
- it->count--;
- item = JS_IteratorNext(ctx, it->obj, method, 0, NULL, pdone);
- JS_FreeValue(ctx, method);
- if (JS_IsException(item))
- goto fail_no_close;
- ret = item;
- goto done;
- }
- *pdone = TRUE;
- if (JS_IteratorClose(ctx, it->obj, FALSE))
- ret = JS_EXCEPTION;
- else
- ret = JS_UNDEFINED;
- goto done;
- }
- break;
- default:
- abort();
- }
- done:
- it->done = magic == GEN_MAGIC_NEXT ? *pdone : 1;
- it->executing = 0;
- return ret;
- fail:
- /* close the iterator object, preserving pending exception */
- JS_IteratorClose(ctx, it->obj, TRUE);
- fail_no_close:
- ret = JS_EXCEPTION;
- goto done;
- }
- static const JSCFunctionListEntry js_iterator_funcs[] = {
- JS_CFUNC_DEF("concat", 0, js_iterator_concat ),
- JS_CFUNC_DEF("from", 1, js_iterator_from ),
- };
- static const JSCFunctionListEntry js_iterator_proto_funcs[] = {
- JS_CFUNC_MAGIC_DEF("drop", 1, js_create_iterator_helper, JS_ITERATOR_HELPER_KIND_DROP ),
- JS_CFUNC_MAGIC_DEF("filter", 1, js_create_iterator_helper, JS_ITERATOR_HELPER_KIND_FILTER ),
- JS_CFUNC_MAGIC_DEF("flatMap", 1, js_create_iterator_helper, JS_ITERATOR_HELPER_KIND_FLAT_MAP ),
- JS_CFUNC_MAGIC_DEF("map", 1, js_create_iterator_helper, JS_ITERATOR_HELPER_KIND_MAP ),
- JS_CFUNC_MAGIC_DEF("take", 1, js_create_iterator_helper, JS_ITERATOR_HELPER_KIND_TAKE ),
- JS_CFUNC_MAGIC_DEF("every", 1, js_iterator_proto_func, JS_ITERATOR_HELPER_KIND_EVERY ),
- JS_CFUNC_MAGIC_DEF("find", 1, js_iterator_proto_func, JS_ITERATOR_HELPER_KIND_FIND),
- JS_CFUNC_MAGIC_DEF("forEach", 1, js_iterator_proto_func, JS_ITERATOR_HELPER_KIND_FOR_EACH ),
- JS_CFUNC_MAGIC_DEF("some", 1, js_iterator_proto_func, JS_ITERATOR_HELPER_KIND_SOME ),
- JS_CFUNC_DEF("reduce", 1, js_iterator_proto_reduce ),
- JS_CFUNC_DEF("toArray", 0, js_iterator_proto_toArray ),
- JS_CFUNC_DEF("[Symbol.iterator]", 0, js_iterator_proto_iterator ),
- JS_CGETSET_DEF("[Symbol.toStringTag]", js_iterator_proto_get_toStringTag, js_iterator_proto_set_toStringTag),
- };
- static const JSCFunctionListEntry js_iterator_helper_proto_funcs[] = {
- JS_ITERATOR_NEXT_DEF("next", 0, js_iterator_helper_next, GEN_MAGIC_NEXT ),
- JS_ITERATOR_NEXT_DEF("return", 0, js_iterator_helper_next, GEN_MAGIC_RETURN ),
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "Iterator Helper", JS_PROP_CONFIGURABLE ),
- };
- static const JSCFunctionListEntry js_array_unscopables_funcs[] = {
- JS_PROP_BOOL_DEF("at", TRUE, JS_PROP_C_W_E),
- JS_PROP_BOOL_DEF("copyWithin", TRUE, JS_PROP_C_W_E),
- JS_PROP_BOOL_DEF("entries", TRUE, JS_PROP_C_W_E),
- JS_PROP_BOOL_DEF("fill", TRUE, JS_PROP_C_W_E),
- JS_PROP_BOOL_DEF("find", TRUE, JS_PROP_C_W_E),
- JS_PROP_BOOL_DEF("findIndex", TRUE, JS_PROP_C_W_E),
- JS_PROP_BOOL_DEF("findLast", TRUE, JS_PROP_C_W_E),
- JS_PROP_BOOL_DEF("findLastIndex", TRUE, JS_PROP_C_W_E),
- JS_PROP_BOOL_DEF("flat", TRUE, JS_PROP_C_W_E),
- JS_PROP_BOOL_DEF("flatMap", TRUE, JS_PROP_C_W_E),
- JS_PROP_BOOL_DEF("includes", TRUE, JS_PROP_C_W_E),
- JS_PROP_BOOL_DEF("keys", TRUE, JS_PROP_C_W_E),
- JS_PROP_BOOL_DEF("toReversed", TRUE, JS_PROP_C_W_E),
- JS_PROP_BOOL_DEF("toSorted", TRUE, JS_PROP_C_W_E),
- JS_PROP_BOOL_DEF("toSpliced", TRUE, JS_PROP_C_W_E),
- JS_PROP_BOOL_DEF("values", TRUE, JS_PROP_C_W_E),
- };
- static const JSCFunctionListEntry js_array_proto_funcs[] = {
- JS_CFUNC_DEF("at", 1, js_array_at ),
- JS_CFUNC_DEF("with", 2, js_array_with ),
- JS_CFUNC_DEF("concat", 1, js_array_concat ),
- JS_CFUNC_MAGIC_DEF("every", 1, js_array_every, special_every ),
- JS_CFUNC_MAGIC_DEF("some", 1, js_array_every, special_some ),
- JS_CFUNC_MAGIC_DEF("forEach", 1, js_array_every, special_forEach ),
- JS_CFUNC_MAGIC_DEF("map", 1, js_array_every, special_map ),
- JS_CFUNC_MAGIC_DEF("filter", 1, js_array_every, special_filter ),
- JS_CFUNC_MAGIC_DEF("reduce", 1, js_array_reduce, special_reduce ),
- JS_CFUNC_MAGIC_DEF("reduceRight", 1, js_array_reduce, special_reduceRight ),
- JS_CFUNC_DEF("fill", 1, js_array_fill ),
- JS_CFUNC_MAGIC_DEF("find", 1, js_array_find, ArrayFind ),
- JS_CFUNC_MAGIC_DEF("findIndex", 1, js_array_find, ArrayFindIndex ),
- JS_CFUNC_MAGIC_DEF("findLast", 1, js_array_find, ArrayFindLast ),
- JS_CFUNC_MAGIC_DEF("findLastIndex", 1, js_array_find, ArrayFindLastIndex ),
- JS_CFUNC_DEF("indexOf", 1, js_array_indexOf ),
- JS_CFUNC_DEF("lastIndexOf", 1, js_array_lastIndexOf ),
- JS_CFUNC_DEF("includes", 1, js_array_includes ),
- JS_CFUNC_MAGIC_DEF("join", 1, js_array_join, 0 ),
- JS_CFUNC_DEF("toString", 0, js_array_toString ),
- JS_CFUNC_MAGIC_DEF("toLocaleString", 0, js_array_join, 1 ),
- JS_CFUNC_MAGIC_DEF("pop", 0, js_array_pop, 0 ),
- JS_CFUNC_MAGIC_DEF("push", 1, js_array_push, 0 ),
- JS_CFUNC_MAGIC_DEF("shift", 0, js_array_pop, 1 ),
- JS_CFUNC_MAGIC_DEF("unshift", 1, js_array_push, 1 ),
- JS_CFUNC_DEF("reverse", 0, js_array_reverse ),
- JS_CFUNC_DEF("toReversed", 0, js_array_toReversed ),
- JS_CFUNC_DEF("sort", 1, js_array_sort ),
- JS_CFUNC_DEF("toSorted", 1, js_array_toSorted ),
- JS_CFUNC_MAGIC_DEF("slice", 2, js_array_slice, 0 ),
- JS_CFUNC_MAGIC_DEF("splice", 2, js_array_slice, 1 ),
- JS_CFUNC_DEF("toSpliced", 2, js_array_toSpliced ),
- JS_CFUNC_DEF("copyWithin", 2, js_array_copyWithin ),
- JS_CFUNC_MAGIC_DEF("flatMap", 1, js_array_flatten, 1 ),
- JS_CFUNC_MAGIC_DEF("flat", 0, js_array_flatten, 0 ),
- JS_CFUNC_MAGIC_DEF("values", 0, js_create_array_iterator, JS_ITERATOR_KIND_VALUE ),
- JS_ALIAS_DEF("[Symbol.iterator]", "values" ),
- JS_CFUNC_MAGIC_DEF("keys", 0, js_create_array_iterator, JS_ITERATOR_KIND_KEY ),
- JS_CFUNC_MAGIC_DEF("entries", 0, js_create_array_iterator, JS_ITERATOR_KIND_KEY_AND_VALUE ),
- JS_OBJECT_DEF("[Symbol.unscopables]", js_array_unscopables_funcs, countof(js_array_unscopables_funcs), JS_PROP_CONFIGURABLE ),
- };
- static const JSCFunctionListEntry js_array_iterator_proto_funcs[] = {
- JS_ITERATOR_NEXT_DEF("next", 0, js_array_iterator_next, 0 ),
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "Array Iterator", JS_PROP_CONFIGURABLE ),
- };
- /* Number */
- static JSValue js_number_constructor(JSContext *ctx, JSValueConst new_target,
- int argc, JSValueConst *argv)
- {
- JSValue val, obj;
- if (argc == 0) {
- val = JS_NewInt32(ctx, 0);
- } else {
- val = JS_ToNumeric(ctx, argv[0]);
- if (JS_IsException(val))
- return val;
- switch(JS_VALUE_GET_TAG(val)) {
- case JS_TAG_SHORT_BIG_INT:
- val = JS_NewInt64(ctx, JS_VALUE_GET_SHORT_BIG_INT(val));
- if (JS_IsException(val))
- return val;
- break;
- case JS_TAG_BIG_INT:
- {
- JSBigInt *p = JS_VALUE_GET_PTR(val);
- double d;
- d = js_bigint_to_float64(ctx, p);
- JS_FreeValue(ctx, val);
- val = JS_NewFloat64(ctx, d);
- }
- break;
- default:
- break;
- }
- }
- if (!JS_IsUndefined(new_target)) {
- obj = js_create_from_ctor(ctx, new_target, JS_CLASS_NUMBER);
- if (!JS_IsException(obj))
- JS_SetObjectData(ctx, obj, val);
- return obj;
- } else {
- return val;
- }
- }
- #if 0
- static JSValue js_number___toInteger(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- return JS_ToIntegerFree(ctx, JS_DupValue(ctx, argv[0]));
- }
- static JSValue js_number___toLength(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- int64_t v;
- if (JS_ToLengthFree(ctx, &v, JS_DupValue(ctx, argv[0])))
- return JS_EXCEPTION;
- return JS_NewInt64(ctx, v);
- }
- #endif
- static JSValue js_number_isNaN(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- if (!JS_IsNumber(argv[0]))
- return JS_FALSE;
- return js_global_isNaN(ctx, this_val, argc, argv);
- }
- static JSValue js_number_isFinite(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- if (!JS_IsNumber(argv[0]))
- return JS_FALSE;
- return js_global_isFinite(ctx, this_val, argc, argv);
- }
- static JSValue js_number_isInteger(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- int ret;
- ret = JS_NumberIsInteger(ctx, argv[0]);
- if (ret < 0)
- return JS_EXCEPTION;
- else
- return JS_NewBool(ctx, ret);
- }
- static JSValue js_number_isSafeInteger(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- double d;
- if (!JS_IsNumber(argv[0]))
- return JS_FALSE;
- if (unlikely(JS_ToFloat64(ctx, &d, argv[0])))
- return JS_EXCEPTION;
- return JS_NewBool(ctx, is_safe_integer(d));
- }
- static const JSCFunctionListEntry js_number_funcs[] = {
- /* global ParseInt and parseFloat should be defined already or delayed */
- JS_ALIAS_BASE_DEF("parseInt", "parseInt", 0 ),
- JS_ALIAS_BASE_DEF("parseFloat", "parseFloat", 0 ),
- JS_CFUNC_DEF("isNaN", 1, js_number_isNaN ),
- JS_CFUNC_DEF("isFinite", 1, js_number_isFinite ),
- JS_CFUNC_DEF("isInteger", 1, js_number_isInteger ),
- JS_CFUNC_DEF("isSafeInteger", 1, js_number_isSafeInteger ),
- JS_PROP_DOUBLE_DEF("MAX_VALUE", 1.7976931348623157e+308, 0 ),
- JS_PROP_DOUBLE_DEF("MIN_VALUE", 5e-324, 0 ),
- JS_PROP_DOUBLE_DEF("NaN", NAN, 0 ),
- JS_PROP_DOUBLE_DEF("NEGATIVE_INFINITY", -INFINITY, 0 ),
- JS_PROP_DOUBLE_DEF("POSITIVE_INFINITY", INFINITY, 0 ),
- JS_PROP_DOUBLE_DEF("EPSILON", 2.220446049250313e-16, 0 ), /* ES6 */
- JS_PROP_DOUBLE_DEF("MAX_SAFE_INTEGER", 9007199254740991.0, 0 ), /* ES6 */
- JS_PROP_DOUBLE_DEF("MIN_SAFE_INTEGER", -9007199254740991.0, 0 ), /* ES6 */
- //JS_CFUNC_DEF("__toInteger", 1, js_number___toInteger ),
- //JS_CFUNC_DEF("__toLength", 1, js_number___toLength ),
- };
- static JSValue js_thisNumberValue(JSContext *ctx, JSValueConst this_val)
- {
- if (JS_IsNumber(this_val))
- return JS_DupValue(ctx, this_val);
- if (JS_VALUE_GET_TAG(this_val) == JS_TAG_OBJECT) {
- JSObject *p = JS_VALUE_GET_OBJ(this_val);
- if (p->class_id == JS_CLASS_NUMBER) {
- if (JS_IsNumber(p->u.object_data))
- return JS_DupValue(ctx, p->u.object_data);
- }
- }
- return JS_ThrowTypeError(ctx, "not a number");
- }
- static JSValue js_number_valueOf(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- return js_thisNumberValue(ctx, this_val);
- }
- static int js_get_radix(JSContext *ctx, JSValueConst val)
- {
- int radix;
- if (JS_ToInt32Sat(ctx, &radix, val))
- return -1;
- if (radix < 2 || radix > 36) {
- JS_ThrowRangeError(ctx, "radix must be between 2 and 36");
- return -1;
- }
- return radix;
- }
- static JSValue js_number_toString(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- JSValue val;
- int base, flags;
- double d;
- val = js_thisNumberValue(ctx, this_val);
- if (JS_IsException(val))
- return val;
- if (magic || JS_IsUndefined(argv[0])) {
- base = 10;
- } else {
- base = js_get_radix(ctx, argv[0]);
- if (base < 0)
- goto fail;
- }
- if (JS_VALUE_GET_TAG(val) == JS_TAG_INT) {
- char buf1[70];
- int len;
- len = i64toa_radix(buf1, JS_VALUE_GET_INT(val), base);
- return js_new_string8_len(ctx, buf1, len);
- }
- if (JS_ToFloat64Free(ctx, &d, val))
- return JS_EXCEPTION;
- flags = JS_DTOA_FORMAT_FREE;
- if (base != 10)
- flags |= JS_DTOA_EXP_DISABLED;
- return js_dtoa2(ctx, d, base, 0, flags);
- fail:
- JS_FreeValue(ctx, val);
- return JS_EXCEPTION;
- }
- static JSValue js_number_toFixed(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue val;
- int f, flags;
- double d;
- val = js_thisNumberValue(ctx, this_val);
- if (JS_IsException(val))
- return val;
- if (JS_ToFloat64Free(ctx, &d, val))
- return JS_EXCEPTION;
- if (JS_ToInt32Sat(ctx, &f, argv[0]))
- return JS_EXCEPTION;
- if (f < 0 || f > 100)
- return JS_ThrowRangeError(ctx, "invalid number of digits");
- if (fabs(d) >= 1e21)
- flags = JS_DTOA_FORMAT_FREE;
- else
- flags = JS_DTOA_FORMAT_FRAC;
- return js_dtoa2(ctx, d, 10, f, flags);
- }
- static JSValue js_number_toExponential(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue val;
- int f, flags;
- double d;
- val = js_thisNumberValue(ctx, this_val);
- if (JS_IsException(val))
- return val;
- if (JS_ToFloat64Free(ctx, &d, val))
- return JS_EXCEPTION;
- if (JS_ToInt32Sat(ctx, &f, argv[0]))
- return JS_EXCEPTION;
- if (!isfinite(d)) {
- return JS_ToStringFree(ctx, __JS_NewFloat64(ctx, d));
- }
- if (JS_IsUndefined(argv[0])) {
- flags = JS_DTOA_FORMAT_FREE;
- f = 0;
- } else {
- if (f < 0 || f > 100)
- return JS_ThrowRangeError(ctx, "invalid number of digits");
- f++;
- flags = JS_DTOA_FORMAT_FIXED;
- }
- return js_dtoa2(ctx, d, 10, f, flags | JS_DTOA_EXP_ENABLED);
- }
- static JSValue js_number_toPrecision(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue val;
- int p;
- double d;
- val = js_thisNumberValue(ctx, this_val);
- if (JS_IsException(val))
- return val;
- if (JS_ToFloat64Free(ctx, &d, val))
- return JS_EXCEPTION;
- if (JS_IsUndefined(argv[0]))
- goto to_string;
- if (JS_ToInt32Sat(ctx, &p, argv[0]))
- return JS_EXCEPTION;
- if (!isfinite(d)) {
- to_string:
- return JS_ToStringFree(ctx, __JS_NewFloat64(ctx, d));
- }
- if (p < 1 || p > 100)
- return JS_ThrowRangeError(ctx, "invalid number of digits");
- return js_dtoa2(ctx, d, 10, p, JS_DTOA_FORMAT_FIXED);
- }
- static const JSCFunctionListEntry js_number_proto_funcs[] = {
- JS_CFUNC_DEF("toExponential", 1, js_number_toExponential ),
- JS_CFUNC_DEF("toFixed", 1, js_number_toFixed ),
- JS_CFUNC_DEF("toPrecision", 1, js_number_toPrecision ),
- JS_CFUNC_MAGIC_DEF("toString", 1, js_number_toString, 0 ),
- JS_CFUNC_MAGIC_DEF("toLocaleString", 0, js_number_toString, 1 ),
- JS_CFUNC_DEF("valueOf", 0, js_number_valueOf ),
- };
- static JSValue js_parseInt(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- const char *str, *p;
- int radix, flags;
- JSValue ret;
- str = JS_ToCString(ctx, argv[0]);
- if (!str)
- return JS_EXCEPTION;
- if (JS_ToInt32(ctx, &radix, argv[1])) {
- JS_FreeCString(ctx, str);
- return JS_EXCEPTION;
- }
- if (radix != 0 && (radix < 2 || radix > 36)) {
- ret = JS_NAN;
- } else {
- p = str;
- p += skip_spaces(p);
- flags = ATOD_INT_ONLY | ATOD_ACCEPT_PREFIX_AFTER_SIGN;
- ret = js_atof(ctx, p, NULL, radix, flags);
- }
- JS_FreeCString(ctx, str);
- return ret;
- }
- static JSValue js_parseFloat(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- const char *str, *p;
- JSValue ret;
- str = JS_ToCString(ctx, argv[0]);
- if (!str)
- return JS_EXCEPTION;
- p = str;
- p += skip_spaces(p);
- ret = js_atof(ctx, p, NULL, 10, 0);
- JS_FreeCString(ctx, str);
- return ret;
- }
- /* Boolean */
- static JSValue js_boolean_constructor(JSContext *ctx, JSValueConst new_target,
- int argc, JSValueConst *argv)
- {
- JSValue val, obj;
- val = JS_NewBool(ctx, JS_ToBool(ctx, argv[0]));
- if (!JS_IsUndefined(new_target)) {
- obj = js_create_from_ctor(ctx, new_target, JS_CLASS_BOOLEAN);
- if (!JS_IsException(obj))
- JS_SetObjectData(ctx, obj, val);
- return obj;
- } else {
- return val;
- }
- }
- static JSValue js_thisBooleanValue(JSContext *ctx, JSValueConst this_val)
- {
- if (JS_VALUE_GET_TAG(this_val) == JS_TAG_BOOL)
- return JS_DupValue(ctx, this_val);
- if (JS_VALUE_GET_TAG(this_val) == JS_TAG_OBJECT) {
- JSObject *p = JS_VALUE_GET_OBJ(this_val);
- if (p->class_id == JS_CLASS_BOOLEAN) {
- if (JS_VALUE_GET_TAG(p->u.object_data) == JS_TAG_BOOL)
- return p->u.object_data;
- }
- }
- return JS_ThrowTypeError(ctx, "not a boolean");
- }
- static JSValue js_boolean_toString(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue val = js_thisBooleanValue(ctx, this_val);
- if (JS_IsException(val))
- return val;
- return JS_AtomToString(ctx, JS_VALUE_GET_BOOL(val) ?
- JS_ATOM_true : JS_ATOM_false);
- }
- static JSValue js_boolean_valueOf(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- return js_thisBooleanValue(ctx, this_val);
- }
- static const JSCFunctionListEntry js_boolean_proto_funcs[] = {
- JS_CFUNC_DEF("toString", 0, js_boolean_toString ),
- JS_CFUNC_DEF("valueOf", 0, js_boolean_valueOf ),
- };
- /* String */
- static int js_string_get_own_property(JSContext *ctx,
- JSPropertyDescriptor *desc,
- JSValueConst obj, JSAtom prop)
- {
- JSObject *p;
- JSString *p1;
- uint32_t idx, ch;
- /* This is a class exotic method: obj class_id is JS_CLASS_STRING */
- if (__JS_AtomIsTaggedInt(prop)) {
- p = JS_VALUE_GET_OBJ(obj);
- if (JS_VALUE_GET_TAG(p->u.object_data) == JS_TAG_STRING) {
- p1 = JS_VALUE_GET_STRING(p->u.object_data);
- idx = __JS_AtomToUInt32(prop);
- if (idx < p1->len) {
- if (desc) {
- ch = string_get(p1, idx);
- desc->flags = JS_PROP_ENUMERABLE;
- desc->value = js_new_string_char(ctx, ch);
- desc->getter = JS_UNDEFINED;
- desc->setter = JS_UNDEFINED;
- }
- return TRUE;
- }
- }
- }
- return FALSE;
- }
- static int js_string_define_own_property(JSContext *ctx,
- JSValueConst this_obj,
- JSAtom prop, JSValueConst val,
- JSValueConst getter,
- JSValueConst setter, int flags)
- {
- uint32_t idx;
- JSObject *p;
- JSString *p1, *p2;
- if (__JS_AtomIsTaggedInt(prop)) {
- idx = __JS_AtomToUInt32(prop);
- p = JS_VALUE_GET_OBJ(this_obj);
- if (JS_VALUE_GET_TAG(p->u.object_data) != JS_TAG_STRING)
- goto def;
- p1 = JS_VALUE_GET_STRING(p->u.object_data);
- if (idx >= p1->len)
- goto def;
- if (!check_define_prop_flags(JS_PROP_ENUMERABLE, flags))
- goto fail;
- /* check that the same value is configured */
- if (flags & JS_PROP_HAS_VALUE) {
- if (JS_VALUE_GET_TAG(val) != JS_TAG_STRING)
- goto fail;
- p2 = JS_VALUE_GET_STRING(val);
- if (p2->len != 1)
- goto fail;
- if (string_get(p1, idx) != string_get(p2, 0)) {
- fail:
- return JS_ThrowTypeErrorOrFalse(ctx, flags, "property is not configurable");
- }
- }
- return TRUE;
- } else {
- def:
- return JS_DefineProperty(ctx, this_obj, prop, val, getter, setter,
- flags | JS_PROP_NO_EXOTIC);
- }
- }
- static int js_string_delete_property(JSContext *ctx,
- JSValueConst obj, JSAtom prop)
- {
- uint32_t idx;
- if (__JS_AtomIsTaggedInt(prop)) {
- idx = __JS_AtomToUInt32(prop);
- if (idx < js_string_obj_get_length(ctx, obj)) {
- return FALSE;
- }
- }
- return TRUE;
- }
- static const JSClassExoticMethods js_string_exotic_methods = {
- .get_own_property = js_string_get_own_property,
- .define_own_property = js_string_define_own_property,
- .delete_property = js_string_delete_property,
- };
- static JSValue js_string_constructor(JSContext *ctx, JSValueConst new_target,
- int argc, JSValueConst *argv)
- {
- JSValue val, obj;
- if (argc == 0) {
- val = JS_AtomToString(ctx, JS_ATOM_empty_string);
- } else {
- if (JS_IsUndefined(new_target) && JS_IsSymbol(argv[0])) {
- JSAtomStruct *p = JS_VALUE_GET_PTR(argv[0]);
- val = JS_ConcatString3(ctx, "Symbol(", JS_AtomToString(ctx, js_get_atom_index(ctx->rt, p)), ")");
- } else {
- val = JS_ToString(ctx, argv[0]);
- }
- if (JS_IsException(val))
- return val;
- }
- if (!JS_IsUndefined(new_target)) {
- JSString *p1 = JS_VALUE_GET_STRING(val);
- obj = js_create_from_ctor(ctx, new_target, JS_CLASS_STRING);
- if (JS_IsException(obj)) {
- JS_FreeValue(ctx, val);
- } else {
- JS_SetObjectData(ctx, obj, val);
- JS_DefinePropertyValue(ctx, obj, JS_ATOM_length, JS_NewInt32(ctx, p1->len), 0);
- }
- return obj;
- } else {
- return val;
- }
- }
- static JSValue js_thisStringValue(JSContext *ctx, JSValueConst this_val)
- {
- if (JS_VALUE_GET_TAG(this_val) == JS_TAG_STRING ||
- JS_VALUE_GET_TAG(this_val) == JS_TAG_STRING_ROPE)
- return JS_DupValue(ctx, this_val);
- if (JS_VALUE_GET_TAG(this_val) == JS_TAG_OBJECT) {
- JSObject *p = JS_VALUE_GET_OBJ(this_val);
- if (p->class_id == JS_CLASS_STRING) {
- if (JS_VALUE_GET_TAG(p->u.object_data) == JS_TAG_STRING)
- return JS_DupValue(ctx, p->u.object_data);
- }
- }
- return JS_ThrowTypeError(ctx, "not a string");
- }
- static JSValue js_string_fromCharCode(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- int i;
- StringBuffer b_s, *b = &b_s;
- string_buffer_init(ctx, b, argc);
- for(i = 0; i < argc; i++) {
- int32_t c;
- if (JS_ToInt32(ctx, &c, argv[i]) || string_buffer_putc16(b, c & 0xffff)) {
- string_buffer_free(b);
- return JS_EXCEPTION;
- }
- }
- return string_buffer_end(b);
- }
- static JSValue js_string_fromCodePoint(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- double d;
- int i, c;
- StringBuffer b_s, *b = &b_s;
- /* XXX: could pre-compute string length if all arguments are JS_TAG_INT */
- if (string_buffer_init(ctx, b, argc))
- goto fail;
- for(i = 0; i < argc; i++) {
- if (JS_VALUE_GET_TAG(argv[i]) == JS_TAG_INT) {
- c = JS_VALUE_GET_INT(argv[i]);
- if (c < 0 || c > 0x10ffff)
- goto range_error;
- } else {
- if (JS_ToFloat64(ctx, &d, argv[i]))
- goto fail;
- if (isnan(d) || d < 0 || d > 0x10ffff || (c = (int)d) != d)
- goto range_error;
- }
- if (string_buffer_putc(b, c))
- goto fail;
- }
- return string_buffer_end(b);
- range_error:
- JS_ThrowRangeError(ctx, "invalid code point");
- fail:
- string_buffer_free(b);
- return JS_EXCEPTION;
- }
- static JSValue js_string_raw(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- // raw(temp,...a)
- JSValue cooked, val, raw;
- StringBuffer b_s, *b = &b_s;
- int64_t i, n;
- string_buffer_init(ctx, b, 0);
- raw = JS_UNDEFINED;
- cooked = JS_ToObject(ctx, argv[0]);
- if (JS_IsException(cooked))
- goto exception;
- raw = JS_ToObjectFree(ctx, JS_GetProperty(ctx, cooked, JS_ATOM_raw));
- if (JS_IsException(raw))
- goto exception;
- if (js_get_length64(ctx, &n, raw) < 0)
- goto exception;
- for (i = 0; i < n; i++) {
- val = JS_ToStringFree(ctx, JS_GetPropertyInt64(ctx, raw, i));
- if (JS_IsException(val))
- goto exception;
- string_buffer_concat_value_free(b, val);
- if (i < n - 1 && i + 1 < argc) {
- if (string_buffer_concat_value(b, argv[i + 1]))
- goto exception;
- }
- }
- JS_FreeValue(ctx, cooked);
- JS_FreeValue(ctx, raw);
- return string_buffer_end(b);
- exception:
- JS_FreeValue(ctx, cooked);
- JS_FreeValue(ctx, raw);
- string_buffer_free(b);
- return JS_EXCEPTION;
- }
- /* only used in test262 */
- JSValue js_string_codePointRange(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- uint32_t start, end, i, n;
- StringBuffer b_s, *b = &b_s;
- if (JS_ToUint32(ctx, &start, argv[0]) ||
- JS_ToUint32(ctx, &end, argv[1]))
- return JS_EXCEPTION;
- end = min_uint32(end, 0x10ffff + 1);
- if (start > end) {
- start = end;
- }
- n = end - start;
- if (end > 0x10000) {
- n += end - max_uint32(start, 0x10000);
- }
- if (string_buffer_init2(ctx, b, n, end >= 0x100))
- return JS_EXCEPTION;
- for(i = start; i < end; i++) {
- string_buffer_putc(b, i);
- }
- return string_buffer_end(b);
- }
- #if 0
- static JSValue js_string___isSpace(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- int c;
- if (JS_ToInt32(ctx, &c, argv[0]))
- return JS_EXCEPTION;
- return JS_NewBool(ctx, lre_is_space(c));
- }
- #endif
- static JSValue js_string_charCodeAt(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue val, ret;
- JSString *p;
- int idx, c;
- val = JS_ToStringCheckObject(ctx, this_val);
- if (JS_IsException(val))
- return val;
- p = JS_VALUE_GET_STRING(val);
- if (JS_ToInt32Sat(ctx, &idx, argv[0])) {
- JS_FreeValue(ctx, val);
- return JS_EXCEPTION;
- }
- if (idx < 0 || idx >= p->len) {
- ret = JS_NAN;
- } else {
- c = string_get(p, idx);
- ret = JS_NewInt32(ctx, c);
- }
- JS_FreeValue(ctx, val);
- return ret;
- }
- static JSValue js_string_charAt(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int is_at)
- {
- JSValue val, ret;
- JSString *p;
- int idx, c;
- val = JS_ToStringCheckObject(ctx, this_val);
- if (JS_IsException(val))
- return val;
- p = JS_VALUE_GET_STRING(val);
- if (JS_ToInt32Sat(ctx, &idx, argv[0])) {
- JS_FreeValue(ctx, val);
- return JS_EXCEPTION;
- }
- if (idx < 0 && is_at)
- idx += p->len;
- if (idx < 0 || idx >= p->len) {
- if (is_at)
- ret = JS_UNDEFINED;
- else
- ret = JS_AtomToString(ctx, JS_ATOM_empty_string);
- } else {
- c = string_get(p, idx);
- ret = js_new_string_char(ctx, c);
- }
- JS_FreeValue(ctx, val);
- return ret;
- }
- static JSValue js_string_codePointAt(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue val, ret;
- JSString *p;
- int idx, c;
- val = JS_ToStringCheckObject(ctx, this_val);
- if (JS_IsException(val))
- return val;
- p = JS_VALUE_GET_STRING(val);
- if (JS_ToInt32Sat(ctx, &idx, argv[0])) {
- JS_FreeValue(ctx, val);
- return JS_EXCEPTION;
- }
- if (idx < 0 || idx >= p->len) {
- ret = JS_UNDEFINED;
- } else {
- c = string_getc(p, &idx);
- ret = JS_NewInt32(ctx, c);
- }
- JS_FreeValue(ctx, val);
- return ret;
- }
- static JSValue js_string_concat(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue r;
- int i;
- /* XXX: Use more efficient method */
- /* XXX: This method is OK if r has a single refcount */
- /* XXX: should use string_buffer? */
- r = JS_ToStringCheckObject(ctx, this_val);
- for (i = 0; i < argc; i++) {
- if (JS_IsException(r))
- break;
- r = JS_ConcatString(ctx, r, JS_DupValue(ctx, argv[i]));
- }
- return r;
- }
- static int string_cmp(JSString *p1, JSString *p2, int x1, int x2, int len)
- {
- int i, c1, c2;
- for (i = 0; i < len; i++) {
- if ((c1 = string_get(p1, x1 + i)) != (c2 = string_get(p2, x2 + i)))
- return c1 - c2;
- }
- return 0;
- }
- static int string_indexof_char(JSString *p, int c, int from)
- {
- /* assuming 0 <= from <= p->len */
- int i, len = p->len;
- if (p->is_wide_char) {
- for (i = from; i < len; i++) {
- if (p->u.str16[i] == c)
- return i;
- }
- } else {
- if ((c & ~0xff) == 0) {
- for (i = from; i < len; i++) {
- if (p->u.str8[i] == (uint8_t)c)
- return i;
- }
- }
- }
- return -1;
- }
- static int string_indexof(JSString *p1, JSString *p2, int from)
- {
- /* assuming 0 <= from <= p1->len */
- int c, i, j, len1 = p1->len, len2 = p2->len;
- if (len2 == 0)
- return from;
- for (i = from, c = string_get(p2, 0); i + len2 <= len1; i = j + 1) {
- j = string_indexof_char(p1, c, i);
- if (j < 0 || j + len2 > len1)
- break;
- if (!string_cmp(p1, p2, j + 1, 1, len2 - 1))
- return j;
- }
- return -1;
- }
- static int64_t string_advance_index(JSString *p, int64_t index, BOOL unicode)
- {
- if (!unicode || index >= p->len || !p->is_wide_char) {
- index++;
- } else {
- int index32 = (int)index;
- string_getc(p, &index32);
- index = index32;
- }
- return index;
- }
- /* return the position of the first invalid character in the string or
- -1 if none */
- static int js_string_find_invalid_codepoint(JSString *p)
- {
- int i;
- if (!p->is_wide_char)
- return -1;
- for(i = 0; i < p->len; i++) {
- uint32_t c = p->u.str16[i];
- if (is_surrogate(c)) {
- if (is_hi_surrogate(c) && (i + 1) < p->len
- && is_lo_surrogate(p->u.str16[i + 1])) {
- i++;
- } else {
- return i;
- }
- }
- }
- return -1;
- }
- static JSValue js_string_isWellFormed(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue str;
- JSString *p;
- BOOL ret;
- str = JS_ToStringCheckObject(ctx, this_val);
- if (JS_IsException(str))
- return JS_EXCEPTION;
- p = JS_VALUE_GET_STRING(str);
- ret = (js_string_find_invalid_codepoint(p) < 0);
- JS_FreeValue(ctx, str);
- return JS_NewBool(ctx, ret);
- }
- static JSValue js_string_toWellFormed(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue str, ret;
- JSString *p;
- int i;
- str = JS_ToStringCheckObject(ctx, this_val);
- if (JS_IsException(str))
- return JS_EXCEPTION;
- p = JS_VALUE_GET_STRING(str);
- /* avoid reallocating the string if it is well-formed */
- i = js_string_find_invalid_codepoint(p);
- if (i < 0)
- return str;
- ret = js_new_string16_len(ctx, p->u.str16, p->len);
- JS_FreeValue(ctx, str);
- if (JS_IsException(ret))
- return JS_EXCEPTION;
- p = JS_VALUE_GET_STRING(ret);
- for (; i < p->len; i++) {
- uint32_t c = p->u.str16[i];
- if (is_surrogate(c)) {
- if (is_hi_surrogate(c) && (i + 1) < p->len
- && is_lo_surrogate(p->u.str16[i + 1])) {
- i++;
- } else {
- p->u.str16[i] = 0xFFFD;
- }
- }
- }
- return ret;
- }
- static JSValue js_string_indexOf(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int lastIndexOf)
- {
- JSValue str, v;
- int i, len, v_len, pos, start, stop, ret, inc;
- JSString *p;
- JSString *p1;
- str = JS_ToStringCheckObject(ctx, this_val);
- if (JS_IsException(str))
- return str;
- v = JS_ToString(ctx, argv[0]);
- if (JS_IsException(v))
- goto fail;
- p = JS_VALUE_GET_STRING(str);
- p1 = JS_VALUE_GET_STRING(v);
- len = p->len;
- v_len = p1->len;
- if (lastIndexOf) {
- pos = len - v_len;
- if (argc > 1) {
- double d;
- if (JS_ToFloat64(ctx, &d, argv[1]))
- goto fail;
- if (!isnan(d)) {
- if (d <= 0)
- pos = 0;
- else if (d < pos)
- pos = d;
- }
- }
- start = pos;
- stop = 0;
- inc = -1;
- } else {
- pos = 0;
- if (argc > 1) {
- if (JS_ToInt32Clamp(ctx, &pos, argv[1], 0, len, 0))
- goto fail;
- }
- start = pos;
- stop = len - v_len;
- inc = 1;
- }
- ret = -1;
- if (len >= v_len && inc * (stop - start) >= 0) {
- for (i = start;; i += inc) {
- if (!string_cmp(p, p1, i, 0, v_len)) {
- ret = i;
- break;
- }
- if (i == stop)
- break;
- }
- }
- JS_FreeValue(ctx, str);
- JS_FreeValue(ctx, v);
- return JS_NewInt32(ctx, ret);
- fail:
- JS_FreeValue(ctx, str);
- JS_FreeValue(ctx, v);
- return JS_EXCEPTION;
- }
- /* return < 0 if exception or TRUE/FALSE */
- static int js_is_regexp(JSContext *ctx, JSValueConst obj);
- static JSValue js_string_includes(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- JSValue str, v = JS_UNDEFINED;
- int i, len, v_len, pos, start, stop, ret;
- JSString *p;
- JSString *p1;
- str = JS_ToStringCheckObject(ctx, this_val);
- if (JS_IsException(str))
- return str;
- ret = js_is_regexp(ctx, argv[0]);
- if (ret) {
- if (ret > 0)
- JS_ThrowTypeError(ctx, "regexp not supported");
- goto fail;
- }
- v = JS_ToString(ctx, argv[0]);
- if (JS_IsException(v))
- goto fail;
- p = JS_VALUE_GET_STRING(str);
- p1 = JS_VALUE_GET_STRING(v);
- len = p->len;
- v_len = p1->len;
- pos = (magic == 2) ? len : 0;
- if (argc > 1 && !JS_IsUndefined(argv[1])) {
- if (JS_ToInt32Clamp(ctx, &pos, argv[1], 0, len, 0))
- goto fail;
- }
- len -= v_len;
- ret = 0;
- if (magic == 0) {
- start = pos;
- stop = len;
- } else {
- if (magic == 1) {
- if (pos > len)
- goto done;
- } else {
- pos -= v_len;
- }
- start = stop = pos;
- }
- if (start >= 0 && start <= stop) {
- for (i = start;; i++) {
- if (!string_cmp(p, p1, i, 0, v_len)) {
- ret = 1;
- break;
- }
- if (i == stop)
- break;
- }
- }
- done:
- JS_FreeValue(ctx, str);
- JS_FreeValue(ctx, v);
- return JS_NewBool(ctx, ret);
- fail:
- JS_FreeValue(ctx, str);
- JS_FreeValue(ctx, v);
- return JS_EXCEPTION;
- }
- static int check_regexp_g_flag(JSContext *ctx, JSValueConst regexp)
- {
- int ret;
- JSValue flags;
- ret = js_is_regexp(ctx, regexp);
- if (ret < 0)
- return -1;
- if (ret) {
- flags = JS_GetProperty(ctx, regexp, JS_ATOM_flags);
- if (JS_IsException(flags))
- return -1;
- if (JS_IsUndefined(flags) || JS_IsNull(flags)) {
- JS_ThrowTypeError(ctx, "cannot convert to object");
- return -1;
- }
- flags = JS_ToStringFree(ctx, flags);
- if (JS_IsException(flags))
- return -1;
- ret = string_indexof_char(JS_VALUE_GET_STRING(flags), 'g', 0);
- JS_FreeValue(ctx, flags);
- if (ret < 0) {
- JS_ThrowTypeError(ctx, "regexp must have the 'g' flag");
- return -1;
- }
- }
- return 0;
- }
- static JSValue js_string_match(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int atom)
- {
- // match(rx), search(rx), matchAll(rx)
- // atom is JS_ATOM_Symbol_match, JS_ATOM_Symbol_search, or JS_ATOM_Symbol_matchAll
- JSValueConst O = this_val, regexp = argv[0], args[2];
- JSValue matcher, S, rx, result, str;
- int args_len;
- if (JS_IsUndefined(O) || JS_IsNull(O))
- return JS_ThrowTypeError(ctx, "cannot convert to object");
- if (JS_IsObject(regexp)) {
- matcher = JS_GetProperty(ctx, regexp, atom);
- if (JS_IsException(matcher))
- return JS_EXCEPTION;
- if (atom == JS_ATOM_Symbol_matchAll) {
- if (check_regexp_g_flag(ctx, regexp) < 0) {
- JS_FreeValue(ctx, matcher);
- return JS_EXCEPTION;
- }
- }
- if (!JS_IsUndefined(matcher) && !JS_IsNull(matcher)) {
- return JS_CallFree(ctx, matcher, regexp, 1, &O);
- }
- }
- S = JS_ToString(ctx, O);
- if (JS_IsException(S))
- return JS_EXCEPTION;
- args_len = 1;
- args[0] = regexp;
- str = JS_UNDEFINED;
- if (atom == JS_ATOM_Symbol_matchAll) {
- str = js_new_string8(ctx, "g");
- if (JS_IsException(str))
- goto fail;
- args[args_len++] = (JSValueConst)str;
- }
- rx = JS_CallConstructor(ctx, ctx->regexp_ctor, args_len, args);
- JS_FreeValue(ctx, str);
- if (JS_IsException(rx)) {
- fail:
- JS_FreeValue(ctx, S);
- return JS_EXCEPTION;
- }
- result = JS_InvokeFree(ctx, rx, atom, 1, (JSValueConst *)&S);
- JS_FreeValue(ctx, S);
- return result;
- }
- /* if captures != NULL, captures_val and matched are ignored. Otherwise,
- captures_len is ignored */
- static int js_string_GetSubstitution(JSContext *ctx,
- StringBuffer *b,
- JSValueConst matched,
- JSString *sp,
- uint32_t position,
- JSValueConst captures_val,
- JSValueConst namedCaptures,
- JSValueConst rep,
- uint8_t **captures,
- uint32_t captures_len)
- {
- JSValue capture, name, s;
- uint32_t len, matched_len;
- int i, j, j0, k, k1, shift;
- int c, c1;
- JSString *rp;
- if (JS_VALUE_GET_TAG(rep) != JS_TAG_STRING) {
- JS_ThrowTypeError(ctx, "not a string");
- goto exception;
- }
- shift = sp->is_wide_char;
- rp = JS_VALUE_GET_STRING(rep);
- if (captures) {
- matched_len = (captures[1] - captures[0]) >> shift;
- } else {
- captures_len = 0;
- if (!JS_IsUndefined(captures_val)) {
- if (js_get_length32(ctx, &captures_len, captures_val))
- goto exception;
- }
- if (js_get_length32(ctx, &matched_len, matched))
- goto exception;
- }
- len = rp->len;
- i = 0;
- for(;;) {
- j = string_indexof_char(rp, '$', i);
- if (j < 0 || j + 1 >= len)
- break;
- string_buffer_concat(b, rp, i, j);
- j0 = j++;
- c = string_get(rp, j++);
- if (c == '$') {
- string_buffer_putc8(b, '$');
- } else if (c == '&') {
- if (captures) {
- string_buffer_concat(b, sp, position, position + matched_len);
- } else {
- if (string_buffer_concat_value(b, matched))
- goto exception;
- }
- } else if (c == '`') {
- string_buffer_concat(b, sp, 0, position);
- } else if (c == '\'') {
- string_buffer_concat(b, sp, position + matched_len, sp->len);
- } else if (c >= '0' && c <= '9') {
- k = c - '0';
- if (j < len) {
- c1 = string_get(rp, j);
- if (c1 >= '0' && c1 <= '9') {
- /* This behavior is specified in ES6 and refined in ECMA 2019 */
- /* ECMA 2019 does not have the extra test, but
- Test262 S15.5.4.11_A3_T1..3 require this behavior */
- k1 = k * 10 + c1 - '0';
- if (k1 >= 1 && k1 < captures_len) {
- k = k1;
- j++;
- }
- }
- }
- if (k >= 1 && k < captures_len) {
- if (captures) {
- int start, end;
- if (captures[2 * k] && captures[2 * k + 1]) {
- start = (captures[2 * k] - sp->u.str8) >> shift;
- end = (captures[2 * k + 1] - sp->u.str8) >> shift;
- string_buffer_concat(b, sp, start, end);
- }
- } else {
- s = JS_GetPropertyInt64(ctx, captures_val, k);
- if (JS_IsException(s))
- goto exception;
- if (!JS_IsUndefined(s)) {
- if (string_buffer_concat_value_free(b, s))
- goto exception;
- }
- }
- } else {
- goto norep;
- }
- } else if (c == '<' && !JS_IsUndefined(namedCaptures)) {
- k = string_indexof_char(rp, '>', j);
- if (k < 0)
- goto norep;
- name = js_sub_string(ctx, rp, j, k);
- if (JS_IsException(name))
- goto exception;
- capture = JS_GetPropertyValue(ctx, namedCaptures, name);
- if (JS_IsException(capture))
- goto exception;
- if (!JS_IsUndefined(capture)) {
- if (string_buffer_concat_value_free(b, capture))
- goto exception;
- }
- j = k + 1;
- } else {
- norep:
- string_buffer_concat(b, rp, j0, j);
- }
- i = j;
- }
- string_buffer_concat(b, rp, i, rp->len);
- return 0;
- exception:
- return -1;
- }
- static JSValue js_string_replace(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv,
- int is_replaceAll)
- {
- // replace(rx, rep)
- JSValueConst O = this_val, searchValue = argv[0], replaceValue = argv[1];
- JSValueConst args[3];
- JSValue str, search_str, replaceValue_str, repl_str;
- JSString *sp, *searchp;
- StringBuffer b_s, *b = &b_s;
- int pos, functionalReplace, endOfLastMatch;
- BOOL is_first;
- if (JS_IsUndefined(O) || JS_IsNull(O))
- return JS_ThrowTypeError(ctx, "cannot convert to object");
- search_str = JS_UNDEFINED;
- replaceValue_str = JS_UNDEFINED;
- repl_str = JS_UNDEFINED;
- if (JS_IsObject(searchValue)) {
- JSValue replacer;
- if (is_replaceAll) {
- if (check_regexp_g_flag(ctx, searchValue) < 0)
- return JS_EXCEPTION;
- }
- replacer = JS_GetProperty(ctx, searchValue, JS_ATOM_Symbol_replace);
- if (JS_IsException(replacer))
- return JS_EXCEPTION;
- if (!JS_IsUndefined(replacer) && !JS_IsNull(replacer)) {
- args[0] = O;
- args[1] = replaceValue;
- return JS_CallFree(ctx, replacer, searchValue, 2, args);
- }
- }
- string_buffer_init(ctx, b, 0);
- str = JS_ToString(ctx, O);
- if (JS_IsException(str))
- goto exception;
- search_str = JS_ToString(ctx, searchValue);
- if (JS_IsException(search_str))
- goto exception;
- functionalReplace = JS_IsFunction(ctx, replaceValue);
- if (!functionalReplace) {
- replaceValue_str = JS_ToString(ctx, replaceValue);
- if (JS_IsException(replaceValue_str))
- goto exception;
- }
- sp = JS_VALUE_GET_STRING(str);
- searchp = JS_VALUE_GET_STRING(search_str);
- endOfLastMatch = 0;
- is_first = TRUE;
- for(;;) {
- if (unlikely(searchp->len == 0)) {
- if (is_first)
- pos = 0;
- else if (endOfLastMatch >= sp->len)
- pos = -1;
- else
- pos = endOfLastMatch + 1;
- } else {
- pos = string_indexof(sp, searchp, endOfLastMatch);
- }
- if (pos < 0) {
- if (is_first) {
- string_buffer_free(b);
- JS_FreeValue(ctx, search_str);
- JS_FreeValue(ctx, replaceValue_str);
- return str;
- } else {
- break;
- }
- }
- string_buffer_concat(b, sp, endOfLastMatch, pos);
- if (functionalReplace) {
- args[0] = search_str;
- args[1] = JS_NewInt32(ctx, pos);
- args[2] = str;
- repl_str = JS_ToStringFree(ctx, JS_Call(ctx, replaceValue, JS_UNDEFINED, 3, args));
- if (JS_IsException(repl_str))
- goto exception;
- string_buffer_concat_value_free(b, repl_str);
- } else {
- if (js_string_GetSubstitution(ctx, b, search_str, sp, pos,
- JS_UNDEFINED, JS_UNDEFINED, replaceValue_str,
- NULL, 0)) {
- goto exception;
- }
- }
- endOfLastMatch = pos + searchp->len;
- is_first = FALSE;
- if (!is_replaceAll)
- break;
- }
- string_buffer_concat(b, sp, endOfLastMatch, sp->len);
- JS_FreeValue(ctx, search_str);
- JS_FreeValue(ctx, replaceValue_str);
- JS_FreeValue(ctx, str);
- return string_buffer_end(b);
- exception:
- string_buffer_free(b);
- JS_FreeValue(ctx, search_str);
- JS_FreeValue(ctx, replaceValue_str);
- JS_FreeValue(ctx, str);
- return JS_EXCEPTION;
- }
- static JSValue js_string_split(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- // split(sep, limit)
- JSValueConst O = this_val, separator = argv[0], limit = argv[1];
- JSValueConst args[2];
- JSValue S, A, R, T;
- uint32_t lim, lengthA;
- int64_t p, q, s, r, e;
- JSString *sp, *rp;
- if (JS_IsUndefined(O) || JS_IsNull(O))
- return JS_ThrowTypeError(ctx, "cannot convert to object");
- S = JS_UNDEFINED;
- A = JS_UNDEFINED;
- R = JS_UNDEFINED;
- if (JS_IsObject(separator)) {
- JSValue splitter;
- splitter = JS_GetProperty(ctx, separator, JS_ATOM_Symbol_split);
- if (JS_IsException(splitter))
- return JS_EXCEPTION;
- if (!JS_IsUndefined(splitter) && !JS_IsNull(splitter)) {
- args[0] = O;
- args[1] = limit;
- return JS_CallFree(ctx, splitter, separator, 2, args);
- }
- }
- S = JS_ToString(ctx, O);
- if (JS_IsException(S))
- goto exception;
- A = JS_NewArray(ctx);
- if (JS_IsException(A))
- goto exception;
- lengthA = 0;
- if (JS_IsUndefined(limit)) {
- lim = 0xffffffff;
- } else {
- if (JS_ToUint32(ctx, &lim, limit) < 0)
- goto exception;
- }
- sp = JS_VALUE_GET_STRING(S);
- s = sp->len;
- R = JS_ToString(ctx, separator);
- if (JS_IsException(R))
- goto exception;
- rp = JS_VALUE_GET_STRING(R);
- r = rp->len;
- p = 0;
- if (lim == 0)
- goto done;
- if (JS_IsUndefined(separator))
- goto add_tail;
- if (s == 0) {
- if (r != 0)
- goto add_tail;
- goto done;
- }
- for (q = p; (q += !r) <= s - r - !r; q = p = e + r) {
- e = string_indexof(sp, rp, q);
- if (e < 0)
- break;
- T = js_sub_string(ctx, sp, p, e);
- if (JS_IsException(T))
- goto exception;
- if (JS_CreateDataPropertyUint32(ctx, A, lengthA++, T, 0) < 0)
- goto exception;
- if (lengthA == lim)
- goto done;
- }
- add_tail:
- T = js_sub_string(ctx, sp, p, s);
- if (JS_IsException(T))
- goto exception;
- if (JS_CreateDataPropertyUint32(ctx, A, lengthA++, T,0 ) < 0)
- goto exception;
- done:
- JS_FreeValue(ctx, S);
- JS_FreeValue(ctx, R);
- return A;
- exception:
- JS_FreeValue(ctx, A);
- JS_FreeValue(ctx, S);
- JS_FreeValue(ctx, R);
- return JS_EXCEPTION;
- }
- static JSValue js_string_substring(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue str, ret;
- int a, b, start, end;
- JSString *p;
- str = JS_ToStringCheckObject(ctx, this_val);
- if (JS_IsException(str))
- return str;
- p = JS_VALUE_GET_STRING(str);
- if (JS_ToInt32Clamp(ctx, &a, argv[0], 0, p->len, 0)) {
- JS_FreeValue(ctx, str);
- return JS_EXCEPTION;
- }
- b = p->len;
- if (!JS_IsUndefined(argv[1])) {
- if (JS_ToInt32Clamp(ctx, &b, argv[1], 0, p->len, 0)) {
- JS_FreeValue(ctx, str);
- return JS_EXCEPTION;
- }
- }
- if (a < b) {
- start = a;
- end = b;
- } else {
- start = b;
- end = a;
- }
- ret = js_sub_string(ctx, p, start, end);
- JS_FreeValue(ctx, str);
- return ret;
- }
- static JSValue js_string_substr(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue str, ret;
- int a, len, n;
- JSString *p;
- str = JS_ToStringCheckObject(ctx, this_val);
- if (JS_IsException(str))
- return str;
- p = JS_VALUE_GET_STRING(str);
- len = p->len;
- if (JS_ToInt32Clamp(ctx, &a, argv[0], 0, len, len)) {
- JS_FreeValue(ctx, str);
- return JS_EXCEPTION;
- }
- n = len - a;
- if (!JS_IsUndefined(argv[1])) {
- if (JS_ToInt32Clamp(ctx, &n, argv[1], 0, len - a, 0)) {
- JS_FreeValue(ctx, str);
- return JS_EXCEPTION;
- }
- }
- ret = js_sub_string(ctx, p, a, a + n);
- JS_FreeValue(ctx, str);
- return ret;
- }
- static JSValue js_string_slice(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue str, ret;
- int len, start, end;
- JSString *p;
- str = JS_ToStringCheckObject(ctx, this_val);
- if (JS_IsException(str))
- return str;
- p = JS_VALUE_GET_STRING(str);
- len = p->len;
- if (JS_ToInt32Clamp(ctx, &start, argv[0], 0, len, len)) {
- JS_FreeValue(ctx, str);
- return JS_EXCEPTION;
- }
- end = len;
- if (!JS_IsUndefined(argv[1])) {
- if (JS_ToInt32Clamp(ctx, &end, argv[1], 0, len, len)) {
- JS_FreeValue(ctx, str);
- return JS_EXCEPTION;
- }
- }
- ret = js_sub_string(ctx, p, start, max_int(end, start));
- JS_FreeValue(ctx, str);
- return ret;
- }
- static JSValue js_string_pad(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int padEnd)
- {
- JSValue str, v = JS_UNDEFINED;
- StringBuffer b_s, *b = &b_s;
- JSString *p, *p1 = NULL;
- int n, len, c = ' ';
- str = JS_ToStringCheckObject(ctx, this_val);
- if (JS_IsException(str))
- goto fail1;
- if (JS_ToInt32Sat(ctx, &n, argv[0]))
- goto fail2;
- p = JS_VALUE_GET_STRING(str);
- len = p->len;
- if (len >= n)
- return str;
- if (argc > 1 && !JS_IsUndefined(argv[1])) {
- v = JS_ToString(ctx, argv[1]);
- if (JS_IsException(v))
- goto fail2;
- p1 = JS_VALUE_GET_STRING(v);
- if (p1->len == 0) {
- JS_FreeValue(ctx, v);
- return str;
- }
- if (p1->len == 1) {
- c = string_get(p1, 0);
- p1 = NULL;
- }
- }
- if (n > JS_STRING_LEN_MAX) {
- JS_ThrowRangeError(ctx, "invalid string length");
- goto fail3;
- }
- if (string_buffer_init(ctx, b, n))
- goto fail3;
- n -= len;
- if (padEnd) {
- if (string_buffer_concat(b, p, 0, len))
- goto fail;
- }
- if (p1) {
- while (n > 0) {
- int chunk = min_int(n, p1->len);
- if (string_buffer_concat(b, p1, 0, chunk))
- goto fail;
- n -= chunk;
- }
- } else {
- if (string_buffer_fill(b, c, n))
- goto fail;
- }
- if (!padEnd) {
- if (string_buffer_concat(b, p, 0, len))
- goto fail;
- }
- JS_FreeValue(ctx, v);
- JS_FreeValue(ctx, str);
- return string_buffer_end(b);
- fail:
- string_buffer_free(b);
- fail3:
- JS_FreeValue(ctx, v);
- fail2:
- JS_FreeValue(ctx, str);
- fail1:
- return JS_EXCEPTION;
- }
- static JSValue js_string_repeat(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue str;
- StringBuffer b_s, *b = &b_s;
- JSString *p;
- int64_t val;
- int n, len;
- str = JS_ToStringCheckObject(ctx, this_val);
- if (JS_IsException(str))
- goto fail;
- if (JS_ToInt64Sat(ctx, &val, argv[0]))
- goto fail;
- if (val < 0 || val > 2147483647) {
- JS_ThrowRangeError(ctx, "invalid repeat count");
- goto fail;
- }
- n = val;
- p = JS_VALUE_GET_STRING(str);
- len = p->len;
- if (len == 0 || n == 1)
- return str;
- // XXX: potential arithmetic overflow
- if (val * len > JS_STRING_LEN_MAX) {
- JS_ThrowRangeError(ctx, "invalid string length");
- goto fail;
- }
- if (string_buffer_init2(ctx, b, n * len, p->is_wide_char))
- goto fail;
- if (len == 1) {
- string_buffer_fill(b, string_get(p, 0), n);
- } else {
- while (n-- > 0) {
- string_buffer_concat(b, p, 0, len);
- }
- }
- JS_FreeValue(ctx, str);
- return string_buffer_end(b);
- fail:
- JS_FreeValue(ctx, str);
- return JS_EXCEPTION;
- }
- static JSValue js_string_trim(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- JSValue str, ret;
- int a, b, len;
- JSString *p;
- str = JS_ToStringCheckObject(ctx, this_val);
- if (JS_IsException(str))
- return str;
- p = JS_VALUE_GET_STRING(str);
- a = 0;
- b = len = p->len;
- if (magic & 1) {
- while (a < len && lre_is_space(string_get(p, a)))
- a++;
- }
- if (magic & 2) {
- while (b > a && lre_is_space(string_get(p, b - 1)))
- b--;
- }
- ret = js_sub_string(ctx, p, a, b);
- JS_FreeValue(ctx, str);
- return ret;
- }
- /* return 0 if before the first char */
- static int string_prevc(JSString *p, int *pidx)
- {
- int idx, c, c1;
- idx = *pidx;
- if (idx <= 0)
- return 0;
- idx--;
- if (p->is_wide_char) {
- c = p->u.str16[idx];
- if (is_lo_surrogate(c) && idx > 0) {
- c1 = p->u.str16[idx - 1];
- if (is_hi_surrogate(c1)) {
- c = from_surrogate(c1, c);
- idx--;
- }
- }
- } else {
- c = p->u.str8[idx];
- }
- *pidx = idx;
- return c;
- }
- static BOOL test_final_sigma(JSString *p, int sigma_pos)
- {
- int k, c1;
- /* before C: skip case ignorable chars and check there is
- a cased letter */
- k = sigma_pos;
- for(;;) {
- c1 = string_prevc(p, &k);
- if (!lre_is_case_ignorable(c1))
- break;
- }
- if (!lre_is_cased(c1))
- return FALSE;
- /* after C: skip case ignorable chars and check there is
- no cased letter */
- k = sigma_pos + 1;
- for(;;) {
- if (k >= p->len)
- return TRUE;
- c1 = string_getc(p, &k);
- if (!lre_is_case_ignorable(c1))
- break;
- }
- return !lre_is_cased(c1);
- }
- static JSValue js_string_toLowerCase(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int to_lower)
- {
- JSValue val;
- StringBuffer b_s, *b = &b_s;
- JSString *p;
- int i, c, j, l;
- uint32_t res[LRE_CC_RES_LEN_MAX];
- val = JS_ToStringCheckObject(ctx, this_val);
- if (JS_IsException(val))
- return val;
- p = JS_VALUE_GET_STRING(val);
- if (p->len == 0)
- return val;
- if (string_buffer_init(ctx, b, p->len))
- goto fail;
- for(i = 0; i < p->len;) {
- c = string_getc(p, &i);
- if (c == 0x3a3 && to_lower && test_final_sigma(p, i - 1)) {
- res[0] = 0x3c2; /* final sigma */
- l = 1;
- } else {
- l = lre_case_conv(res, c, to_lower);
- }
- for(j = 0; j < l; j++) {
- if (string_buffer_putc(b, res[j]))
- goto fail;
- }
- }
- JS_FreeValue(ctx, val);
- return string_buffer_end(b);
- fail:
- JS_FreeValue(ctx, val);
- string_buffer_free(b);
- return JS_EXCEPTION;
- }
- #ifdef CONFIG_ALL_UNICODE
- /* return (-1, NULL) if exception, otherwise (len, buf) */
- static int JS_ToUTF32String(JSContext *ctx, uint32_t **pbuf, JSValueConst val1)
- {
- JSValue val;
- JSString *p;
- uint32_t *buf;
- int i, j, len;
- val = JS_ToString(ctx, val1);
- if (JS_IsException(val))
- return -1;
- p = JS_VALUE_GET_STRING(val);
- len = p->len;
- /* UTF32 buffer length is len minus the number of correct surrogates pairs */
- buf = js_malloc(ctx, sizeof(buf[0]) * max_int(len, 1));
- if (!buf) {
- JS_FreeValue(ctx, val);
- goto fail;
- }
- for(i = j = 0; i < len;)
- buf[j++] = string_getc(p, &i);
- JS_FreeValue(ctx, val);
- *pbuf = buf;
- return j;
- fail:
- *pbuf = NULL;
- return -1;
- }
- static JSValue JS_NewUTF32String(JSContext *ctx, const uint32_t *buf, int len)
- {
- int i;
- StringBuffer b_s, *b = &b_s;
- if (string_buffer_init(ctx, b, len))
- return JS_EXCEPTION;
- for(i = 0; i < len; i++) {
- if (string_buffer_putc(b, buf[i]))
- goto fail;
- }
- return string_buffer_end(b);
- fail:
- string_buffer_free(b);
- return JS_EXCEPTION;
- }
- static int js_string_normalize1(JSContext *ctx, uint32_t **pout_buf,
- JSValueConst val,
- UnicodeNormalizationEnum n_type)
- {
- int buf_len, out_len;
- uint32_t *buf, *out_buf;
- buf_len = JS_ToUTF32String(ctx, &buf, val);
- if (buf_len < 0)
- return -1;
- out_len = unicode_normalize(&out_buf, buf, buf_len, n_type,
- ctx->rt, (DynBufReallocFunc *)js_realloc_rt);
- js_free(ctx, buf);
- if (out_len < 0)
- return -1;
- *pout_buf = out_buf;
- return out_len;
- }
- static JSValue js_string_normalize(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- const char *form, *p;
- size_t form_len;
- int is_compat, out_len;
- UnicodeNormalizationEnum n_type;
- JSValue val;
- uint32_t *out_buf;
- val = JS_ToStringCheckObject(ctx, this_val);
- if (JS_IsException(val))
- return val;
- if (argc == 0 || JS_IsUndefined(argv[0])) {
- n_type = UNICODE_NFC;
- } else {
- form = JS_ToCStringLen(ctx, &form_len, argv[0]);
- if (!form)
- goto fail1;
- p = form;
- if (p[0] != 'N' || p[1] != 'F')
- goto bad_form;
- p += 2;
- is_compat = FALSE;
- if (*p == 'K') {
- is_compat = TRUE;
- p++;
- }
- if (*p == 'C' || *p == 'D') {
- n_type = UNICODE_NFC + is_compat * 2 + (*p - 'C');
- if ((p + 1 - form) != form_len)
- goto bad_form;
- } else {
- bad_form:
- JS_FreeCString(ctx, form);
- JS_ThrowRangeError(ctx, "bad normalization form");
- fail1:
- JS_FreeValue(ctx, val);
- return JS_EXCEPTION;
- }
- JS_FreeCString(ctx, form);
- }
- out_len = js_string_normalize1(ctx, &out_buf, val, n_type);
- JS_FreeValue(ctx, val);
- if (out_len < 0)
- return JS_EXCEPTION;
- val = JS_NewUTF32String(ctx, out_buf, out_len);
- js_free(ctx, out_buf);
- return val;
- }
- /* return < 0, 0 or > 0 */
- static int js_UTF32_compare(const uint32_t *buf1, int buf1_len,
- const uint32_t *buf2, int buf2_len)
- {
- int i, len, c, res;
- len = min_int(buf1_len, buf2_len);
- for(i = 0; i < len; i++) {
- /* Note: range is limited so a subtraction is valid */
- c = buf1[i] - buf2[i];
- if (c != 0)
- return c;
- }
- if (buf1_len == buf2_len)
- res = 0;
- else if (buf1_len < buf2_len)
- res = -1;
- else
- res = 1;
- return res;
- }
- static JSValue js_string_localeCompare(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue a, b;
- int cmp, a_len, b_len;
- uint32_t *a_buf, *b_buf;
- a = JS_ToStringCheckObject(ctx, this_val);
- if (JS_IsException(a))
- return JS_EXCEPTION;
- b = JS_ToString(ctx, argv[0]);
- if (JS_IsException(b)) {
- JS_FreeValue(ctx, a);
- return JS_EXCEPTION;
- }
- a_len = js_string_normalize1(ctx, &a_buf, a, UNICODE_NFC);
- JS_FreeValue(ctx, a);
- if (a_len < 0) {
- JS_FreeValue(ctx, b);
- return JS_EXCEPTION;
- }
- b_len = js_string_normalize1(ctx, &b_buf, b, UNICODE_NFC);
- JS_FreeValue(ctx, b);
- if (b_len < 0) {
- js_free(ctx, a_buf);
- return JS_EXCEPTION;
- }
- cmp = js_UTF32_compare(a_buf, a_len, b_buf, b_len);
- js_free(ctx, a_buf);
- js_free(ctx, b_buf);
- return JS_NewInt32(ctx, cmp);
- }
- #else /* CONFIG_ALL_UNICODE */
- static JSValue js_string_localeCompare(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue a, b;
- int cmp;
- a = JS_ToStringCheckObject(ctx, this_val);
- if (JS_IsException(a))
- return JS_EXCEPTION;
- b = JS_ToString(ctx, argv[0]);
- if (JS_IsException(b)) {
- JS_FreeValue(ctx, a);
- return JS_EXCEPTION;
- }
- cmp = js_string_compare(ctx, JS_VALUE_GET_STRING(a), JS_VALUE_GET_STRING(b));
- JS_FreeValue(ctx, a);
- JS_FreeValue(ctx, b);
- return JS_NewInt32(ctx, cmp);
- }
- #endif /* !CONFIG_ALL_UNICODE */
- /* also used for String.prototype.valueOf */
- static JSValue js_string_toString(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- return js_thisStringValue(ctx, this_val);
- }
- /* String Iterator */
- static JSValue js_string_iterator_next(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv,
- BOOL *pdone, int magic)
- {
- JSArrayIteratorData *it;
- uint32_t idx, c, start;
- JSString *p;
- it = JS_GetOpaque2(ctx, this_val, JS_CLASS_STRING_ITERATOR);
- if (!it) {
- *pdone = FALSE;
- return JS_EXCEPTION;
- }
- if (JS_IsUndefined(it->obj))
- goto done;
- p = JS_VALUE_GET_STRING(it->obj);
- idx = it->idx;
- if (idx >= p->len) {
- JS_FreeValue(ctx, it->obj);
- it->obj = JS_UNDEFINED;
- done:
- *pdone = TRUE;
- return JS_UNDEFINED;
- }
- start = idx;
- c = string_getc(p, (int *)&idx);
- it->idx = idx;
- *pdone = FALSE;
- if (c <= 0xffff) {
- return js_new_string_char(ctx, c);
- } else {
- return js_new_string16_len(ctx, p->u.str16 + start, 2);
- }
- }
- /* ES6 Annex B 2.3.2 etc. */
- enum {
- magic_string_anchor,
- magic_string_big,
- magic_string_blink,
- magic_string_bold,
- magic_string_fixed,
- magic_string_fontcolor,
- magic_string_fontsize,
- magic_string_italics,
- magic_string_link,
- magic_string_small,
- magic_string_strike,
- magic_string_sub,
- magic_string_sup,
- };
- static JSValue js_string_CreateHTML(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- JSValue str;
- const JSString *p;
- StringBuffer b_s, *b = &b_s;
- static struct { const char *tag, *attr; } const defs[] = {
- { "a", "name" }, { "big", NULL }, { "blink", NULL }, { "b", NULL },
- { "tt", NULL }, { "font", "color" }, { "font", "size" }, { "i", NULL },
- { "a", "href" }, { "small", NULL }, { "strike", NULL },
- { "sub", NULL }, { "sup", NULL },
- };
- str = JS_ToStringCheckObject(ctx, this_val);
- if (JS_IsException(str))
- return JS_EXCEPTION;
- string_buffer_init(ctx, b, 7);
- string_buffer_putc8(b, '<');
- string_buffer_puts8(b, defs[magic].tag);
- if (defs[magic].attr) {
- // r += " " + attr + "=\"" + value + "\"";
- JSValue value;
- int i;
- string_buffer_putc8(b, ' ');
- string_buffer_puts8(b, defs[magic].attr);
- string_buffer_puts8(b, "=\"");
- value = JS_ToStringCheckObject(ctx, argv[0]);
- if (JS_IsException(value)) {
- JS_FreeValue(ctx, str);
- string_buffer_free(b);
- return JS_EXCEPTION;
- }
- p = JS_VALUE_GET_STRING(value);
- for (i = 0; i < p->len; i++) {
- int c = string_get(p, i);
- if (c == '"') {
- string_buffer_puts8(b, """);
- } else {
- string_buffer_putc16(b, c);
- }
- }
- JS_FreeValue(ctx, value);
- string_buffer_putc8(b, '\"');
- }
- // return r + ">" + str + "</" + tag + ">";
- string_buffer_putc8(b, '>');
- string_buffer_concat_value_free(b, str);
- string_buffer_puts8(b, "</");
- string_buffer_puts8(b, defs[magic].tag);
- string_buffer_putc8(b, '>');
- return string_buffer_end(b);
- }
- static const JSCFunctionListEntry js_string_funcs[] = {
- JS_CFUNC_DEF("fromCharCode", 1, js_string_fromCharCode ),
- JS_CFUNC_DEF("fromCodePoint", 1, js_string_fromCodePoint ),
- JS_CFUNC_DEF("raw", 1, js_string_raw ),
- };
- static const JSCFunctionListEntry js_string_proto_funcs[] = {
- JS_PROP_INT32_DEF("length", 0, JS_PROP_CONFIGURABLE ),
- JS_CFUNC_MAGIC_DEF("at", 1, js_string_charAt, 1 ),
- JS_CFUNC_DEF("charCodeAt", 1, js_string_charCodeAt ),
- JS_CFUNC_MAGIC_DEF("charAt", 1, js_string_charAt, 0 ),
- JS_CFUNC_DEF("concat", 1, js_string_concat ),
- JS_CFUNC_DEF("codePointAt", 1, js_string_codePointAt ),
- JS_CFUNC_DEF("isWellFormed", 0, js_string_isWellFormed ),
- JS_CFUNC_DEF("toWellFormed", 0, js_string_toWellFormed ),
- JS_CFUNC_MAGIC_DEF("indexOf", 1, js_string_indexOf, 0 ),
- JS_CFUNC_MAGIC_DEF("lastIndexOf", 1, js_string_indexOf, 1 ),
- JS_CFUNC_MAGIC_DEF("includes", 1, js_string_includes, 0 ),
- JS_CFUNC_MAGIC_DEF("endsWith", 1, js_string_includes, 2 ),
- JS_CFUNC_MAGIC_DEF("startsWith", 1, js_string_includes, 1 ),
- JS_CFUNC_MAGIC_DEF("match", 1, js_string_match, JS_ATOM_Symbol_match ),
- JS_CFUNC_MAGIC_DEF("matchAll", 1, js_string_match, JS_ATOM_Symbol_matchAll ),
- JS_CFUNC_MAGIC_DEF("search", 1, js_string_match, JS_ATOM_Symbol_search ),
- JS_CFUNC_DEF("split", 2, js_string_split ),
- JS_CFUNC_DEF("substring", 2, js_string_substring ),
- JS_CFUNC_DEF("substr", 2, js_string_substr ),
- JS_CFUNC_DEF("slice", 2, js_string_slice ),
- JS_CFUNC_DEF("repeat", 1, js_string_repeat ),
- JS_CFUNC_MAGIC_DEF("replace", 2, js_string_replace, 0 ),
- JS_CFUNC_MAGIC_DEF("replaceAll", 2, js_string_replace, 1 ),
- JS_CFUNC_MAGIC_DEF("padEnd", 1, js_string_pad, 1 ),
- JS_CFUNC_MAGIC_DEF("padStart", 1, js_string_pad, 0 ),
- JS_CFUNC_MAGIC_DEF("trim", 0, js_string_trim, 3 ),
- JS_CFUNC_MAGIC_DEF("trimEnd", 0, js_string_trim, 2 ),
- JS_ALIAS_DEF("trimRight", "trimEnd" ),
- JS_CFUNC_MAGIC_DEF("trimStart", 0, js_string_trim, 1 ),
- JS_ALIAS_DEF("trimLeft", "trimStart" ),
- JS_CFUNC_DEF("toString", 0, js_string_toString ),
- JS_CFUNC_DEF("valueOf", 0, js_string_toString ),
- JS_CFUNC_MAGIC_DEF("toLowerCase", 0, js_string_toLowerCase, 1 ),
- JS_CFUNC_MAGIC_DEF("toUpperCase", 0, js_string_toLowerCase, 0 ),
- JS_CFUNC_MAGIC_DEF("toLocaleLowerCase", 0, js_string_toLowerCase, 1 ),
- JS_CFUNC_MAGIC_DEF("toLocaleUpperCase", 0, js_string_toLowerCase, 0 ),
- JS_CFUNC_MAGIC_DEF("[Symbol.iterator]", 0, js_create_array_iterator, JS_ITERATOR_KIND_VALUE | 4 ),
- /* ES6 Annex B 2.3.2 etc. */
- JS_CFUNC_MAGIC_DEF("anchor", 1, js_string_CreateHTML, magic_string_anchor ),
- JS_CFUNC_MAGIC_DEF("big", 0, js_string_CreateHTML, magic_string_big ),
- JS_CFUNC_MAGIC_DEF("blink", 0, js_string_CreateHTML, magic_string_blink ),
- JS_CFUNC_MAGIC_DEF("bold", 0, js_string_CreateHTML, magic_string_bold ),
- JS_CFUNC_MAGIC_DEF("fixed", 0, js_string_CreateHTML, magic_string_fixed ),
- JS_CFUNC_MAGIC_DEF("fontcolor", 1, js_string_CreateHTML, magic_string_fontcolor ),
- JS_CFUNC_MAGIC_DEF("fontsize", 1, js_string_CreateHTML, magic_string_fontsize ),
- JS_CFUNC_MAGIC_DEF("italics", 0, js_string_CreateHTML, magic_string_italics ),
- JS_CFUNC_MAGIC_DEF("link", 1, js_string_CreateHTML, magic_string_link ),
- JS_CFUNC_MAGIC_DEF("small", 0, js_string_CreateHTML, magic_string_small ),
- JS_CFUNC_MAGIC_DEF("strike", 0, js_string_CreateHTML, magic_string_strike ),
- JS_CFUNC_MAGIC_DEF("sub", 0, js_string_CreateHTML, magic_string_sub ),
- JS_CFUNC_MAGIC_DEF("sup", 0, js_string_CreateHTML, magic_string_sup ),
- };
- static const JSCFunctionListEntry js_string_iterator_proto_funcs[] = {
- JS_ITERATOR_NEXT_DEF("next", 0, js_string_iterator_next, 0 ),
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "String Iterator", JS_PROP_CONFIGURABLE ),
- };
- static const JSCFunctionListEntry js_string_proto_normalize[] = {
- #ifdef CONFIG_ALL_UNICODE
- JS_CFUNC_DEF("normalize", 0, js_string_normalize ),
- #endif
- JS_CFUNC_DEF("localeCompare", 1, js_string_localeCompare ),
- };
- int JS_AddIntrinsicStringNormalize(JSContext *ctx)
- {
- return JS_SetPropertyFunctionList(ctx, ctx->class_proto[JS_CLASS_STRING], js_string_proto_normalize,
- countof(js_string_proto_normalize));
- }
- /* Math */
- /* precondition: a and b are not NaN */
- static double js_fmin(double a, double b)
- {
- if (a == 0 && b == 0) {
- JSFloat64Union a1, b1;
- a1.d = a;
- b1.d = b;
- a1.u64 |= b1.u64;
- return a1.d;
- } else {
- return fmin(a, b);
- }
- }
- /* precondition: a and b are not NaN */
- static double js_fmax(double a, double b)
- {
- if (a == 0 && b == 0) {
- JSFloat64Union a1, b1;
- a1.d = a;
- b1.d = b;
- a1.u64 &= b1.u64;
- return a1.d;
- } else {
- return fmax(a, b);
- }
- }
- static JSValue js_math_min_max(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- BOOL is_max = magic;
- double r, a;
- int i;
- uint32_t tag;
- if (unlikely(argc == 0)) {
- return __JS_NewFloat64(ctx, is_max ? -1.0 / 0.0 : 1.0 / 0.0);
- }
- tag = JS_VALUE_GET_TAG(argv[0]);
- if (tag == JS_TAG_INT) {
- int a1, r1 = JS_VALUE_GET_INT(argv[0]);
- for(i = 1; i < argc; i++) {
- tag = JS_VALUE_GET_TAG(argv[i]);
- if (tag != JS_TAG_INT) {
- r = r1;
- goto generic_case;
- }
- a1 = JS_VALUE_GET_INT(argv[i]);
- if (is_max)
- r1 = max_int(r1, a1);
- else
- r1 = min_int(r1, a1);
- }
- return JS_NewInt32(ctx, r1);
- } else {
- if (JS_ToFloat64(ctx, &r, argv[0]))
- return JS_EXCEPTION;
- i = 1;
- generic_case:
- while (i < argc) {
- if (JS_ToFloat64(ctx, &a, argv[i]))
- return JS_EXCEPTION;
- if (!isnan(r)) {
- if (isnan(a)) {
- r = a;
- } else {
- if (is_max)
- r = js_fmax(r, a);
- else
- r = js_fmin(r, a);
- }
- }
- i++;
- }
- return JS_NewFloat64(ctx, r);
- }
- }
- static double js_math_sign(double a)
- {
- if (isnan(a) || a == 0.0)
- return a;
- if (a < 0)
- return -1;
- else
- return 1;
- }
- static double js_math_round(double a)
- {
- JSFloat64Union u;
- uint64_t frac_mask, one;
- unsigned int e, s;
- u.d = a;
- e = (u.u64 >> 52) & 0x7ff;
- if (e < 1023) {
- /* abs(a) < 1 */
- if (e == (1023 - 1) && u.u64 != 0xbfe0000000000000) {
- /* abs(a) > 0.5 or a = 0.5: return +/-1.0 */
- u.u64 = (u.u64 & ((uint64_t)1 << 63)) | ((uint64_t)1023 << 52);
- } else {
- /* return +/-0.0 */
- u.u64 &= (uint64_t)1 << 63;
- }
- } else if (e < (1023 + 52)) {
- s = u.u64 >> 63;
- one = (uint64_t)1 << (52 - (e - 1023));
- frac_mask = one - 1;
- u.u64 += (one >> 1) - s;
- u.u64 &= ~frac_mask; /* truncate to an integer */
- }
- /* otherwise: abs(a) >= 2^52, or NaN, +/-Infinity: no change */
- return u.d;
- }
- static JSValue js_math_hypot(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- double r, a;
- int i;
- r = 0;
- if (argc > 0) {
- if (JS_ToFloat64(ctx, &r, argv[0]))
- return JS_EXCEPTION;
- if (argc == 1) {
- r = fabs(r);
- } else {
- /* use the built-in function to minimize precision loss */
- for (i = 1; i < argc; i++) {
- if (JS_ToFloat64(ctx, &a, argv[i]))
- return JS_EXCEPTION;
- r = hypot(r, a);
- }
- }
- }
- return JS_NewFloat64(ctx, r);
- }
- static double js_math_f16round(double a)
- {
- return fromfp16(tofp16(a));
- }
- static double js_math_fround(double a)
- {
- return (float)a;
- }
- static JSValue js_math_imul(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- uint32_t a, b, c;
- int32_t d;
- if (JS_ToUint32(ctx, &a, argv[0]))
- return JS_EXCEPTION;
- if (JS_ToUint32(ctx, &b, argv[1]))
- return JS_EXCEPTION;
- c = a * b;
- memcpy(&d, &c, sizeof(d));
- return JS_NewInt32(ctx, d);
- }
- static JSValue js_math_clz32(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- uint32_t a, r;
- if (JS_ToUint32(ctx, &a, argv[0]))
- return JS_EXCEPTION;
- if (a == 0)
- r = 32;
- else
- r = clz32(a);
- return JS_NewInt32(ctx, r);
- }
- typedef enum {
- SUM_PRECISE_STATE_FINITE,
- SUM_PRECISE_STATE_INFINITY,
- SUM_PRECISE_STATE_MINUS_INFINITY, /* must be after SUM_PRECISE_STATE_INFINITY */
- SUM_PRECISE_STATE_NAN, /* must be after SUM_PRECISE_STATE_MINUS_INFINITY */
- } SumPreciseStateEnum;
- #define SP_LIMB_BITS 56
- #define SP_RND_BITS (SP_LIMB_BITS - 53)
- /* we add one extra limb to avoid having to test for overflows during the sum */
- #define SUM_PRECISE_ACC_LEN 39
- #define SUM_PRECISE_COUNTER_INIT 250
- typedef struct {
- SumPreciseStateEnum state;
- uint32_t counter;
- int n_limbs; /* 'acc' contains n_limbs and is not necessarily
- acc[n_limb - 1] may be 0. 0 indicates minus zero
- result when state = SUM_PRECISE_STATE_FINITE */
- int64_t acc[SUM_PRECISE_ACC_LEN];
- } SumPreciseState;
- static void sum_precise_init(SumPreciseState *s)
- {
- memset(s->acc, 0, sizeof(s->acc));
- s->state = SUM_PRECISE_STATE_FINITE;
- s->counter = SUM_PRECISE_COUNTER_INIT;
- s->n_limbs = 0;
- }
- static void sum_precise_renorm(SumPreciseState *s)
- {
- int64_t v, carry;
- int i;
-
- carry = 0;
- for(i = 0; i < s->n_limbs; i++) {
- v = s->acc[i] + carry;
- s->acc[i] = v & (((uint64_t)1 << SP_LIMB_BITS) - 1);
- carry = v >> SP_LIMB_BITS;
- }
- /* we add a failsafe but it should be never reached in a
- reasonnable amount of time */
- if (carry != 0 && s->n_limbs < SUM_PRECISE_ACC_LEN)
- s->acc[s->n_limbs++] = carry;
- }
- static void sum_precise_add(SumPreciseState *s, double d)
- {
- uint64_t a, m, a0, a1;
- int sgn, e, p;
- unsigned int shift;
-
- a = float64_as_uint64(d);
- sgn = a >> 63;
- e = (a >> 52) & ((1 << 11) - 1);
- m = a & (((uint64_t)1 << 52) - 1);
- if (unlikely(e == 2047)) {
- if (m == 0) {
- /* +/- infinity */
- if (s->state == SUM_PRECISE_STATE_NAN ||
- (s->state == SUM_PRECISE_STATE_MINUS_INFINITY && !sgn) ||
- (s->state == SUM_PRECISE_STATE_INFINITY && sgn)) {
- s->state = SUM_PRECISE_STATE_NAN;
- } else {
- s->state = SUM_PRECISE_STATE_INFINITY + sgn;
- }
- } else {
- /* NaN */
- s->state = SUM_PRECISE_STATE_NAN;
- }
- } else if (e == 0) {
- if (likely(m == 0)) {
- /* zero */
- if (s->n_limbs == 0 && !sgn)
- s->n_limbs = 1;
- } else {
- /* subnormal */
- p = 0;
- shift = 0;
- goto add;
- }
- } else {
- /* Note: we sum even if state != SUM_PRECISE_STATE_FINITE to
- avoid tests */
- m |= (uint64_t)1 << 52;
- shift = e - 1;
- /* 'p' is the position of a0 in acc. The division is normally
- implementation as a multiplication by the compiler. */
- p = shift / SP_LIMB_BITS;
- shift %= SP_LIMB_BITS;
- add:
- a0 = (m << shift) & (((uint64_t)1 << SP_LIMB_BITS) - 1);
- a1 = m >> (SP_LIMB_BITS - shift);
- if (!sgn) {
- s->acc[p] += a0;
- s->acc[p + 1] += a1;
- } else {
- s->acc[p] -= a0;
- s->acc[p + 1] -= a1;
- }
- s->n_limbs = max_int(s->n_limbs, p + 2);
- if (unlikely(--s->counter == 0)) {
- s->counter = SUM_PRECISE_COUNTER_INIT;
- sum_precise_renorm(s);
- }
- }
- }
- static double sum_precise_get_result(SumPreciseState *s)
- {
- int n, shift, e, p, is_neg;
- uint64_t m, addend;
-
- if (s->state != SUM_PRECISE_STATE_FINITE) {
- switch(s->state) {
- default:
- case SUM_PRECISE_STATE_INFINITY:
- return INFINITY;
- case SUM_PRECISE_STATE_MINUS_INFINITY:
- return -INFINITY;
- case SUM_PRECISE_STATE_NAN:
- return NAN;
- }
- }
- sum_precise_renorm(s);
- /* extract the sign and absolute value */
- #if 0
- {
- int i;
- printf("len=%d:", s->n_limbs);
- for(i = s->n_limbs - 1; i >= 0; i--)
- printf(" %014lx", s->acc[i]);
- printf("\n");
- }
- #endif
- n = s->n_limbs;
- /* minus zero result */
- if (n == 0)
- return -0.0;
-
- /* normalize */
- while (n > 0 && s->acc[n - 1] == 0)
- n--;
- /* zero result. The spec tells it is always positive in the finite case */
- if (n == 0)
- return 0.0;
- is_neg = (s->acc[n - 1] < 0);
- if (is_neg) {
- uint64_t v, carry;
- int i;
- /* negate */
- /* XXX: do it only when needed */
- carry = 1;
- for(i = 0; i < n - 1; i++) {
- v = (((uint64_t)1 << SP_LIMB_BITS) - 1) - s->acc[i] + carry;
- carry = v >> SP_LIMB_BITS;
- s->acc[i] = v & (((uint64_t)1 << SP_LIMB_BITS) - 1);
- }
- s->acc[n - 1] = -s->acc[n - 1] + carry - 1;
- while (n > 1 && s->acc[n - 1] == 0)
- n--;
- }
- /* subnormal case */
- if (n == 1 && s->acc[0] < ((uint64_t)1 << 52))
- return uint64_as_float64(((uint64_t)is_neg << 63) | s->acc[0]);
- /* normal case */
- e = n * SP_LIMB_BITS;
- p = n - 1;
- m = s->acc[p];
- shift = clz64(m) - (64 - SP_LIMB_BITS);
- e = e - shift - 52;
- if (shift != 0) {
- m <<= shift;
- if (p > 0) {
- int shift1;
- uint64_t nz;
- p--;
- shift1 = SP_LIMB_BITS - shift;
- nz = s->acc[p] & (((uint64_t)1 << shift1) - 1);
- m = m | (s->acc[p] >> shift1) | (nz != 0);
- }
- }
- if ((m & ((1 << SP_RND_BITS) - 1)) == (1 << (SP_RND_BITS - 1))) {
- /* see if the LSB part is non zero for the final rounding */
- while (p > 0) {
- p--;
- if (s->acc[p] != 0) {
- m |= 1;
- break;
- }
- }
- }
- /* rounding to nearest with ties to even */
- addend = (1 << (SP_RND_BITS - 1)) - 1 + ((m >> SP_RND_BITS) & 1);
- m = (m + addend) >> SP_RND_BITS;
- /* handle overflow in the rounding */
- if (m == ((uint64_t)1 << 53))
- e++;
- if (unlikely(e >= 2047)) {
- /* infinity */
- return uint64_as_float64(((uint64_t)is_neg << 63) | ((uint64_t)2047 << 52));
- } else {
- m &= (((uint64_t)1 << 52) - 1);
- return uint64_as_float64(((uint64_t)is_neg << 63) | ((uint64_t)e << 52) | m);
- }
- }
- static JSValue js_math_sumPrecise(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue iter, next, item, ret;
- uint32_t tag;
- int done;
- double d;
- SumPreciseState s_s, *s = &s_s;
- iter = JS_GetIterator(ctx, argv[0], FALSE);
- if (JS_IsException(iter))
- return JS_EXCEPTION;
- ret = JS_EXCEPTION;
- next = JS_GetProperty(ctx, iter, JS_ATOM_next);
- if (JS_IsException(next))
- goto fail;
- sum_precise_init(s);
- for (;;) {
- item = JS_IteratorNext(ctx, iter, next, 0, NULL, &done);
- if (JS_IsException(item))
- goto fail;
- if (done)
- break;
- tag = JS_VALUE_GET_TAG(item);
- if (JS_TAG_IS_FLOAT64(tag)) {
- d = JS_VALUE_GET_FLOAT64(item);
- } else if (tag == JS_TAG_INT) {
- d = JS_VALUE_GET_INT(item);
- } else {
- JS_FreeValue(ctx, item);
- JS_ThrowTypeError(ctx, "not a number");
- JS_IteratorClose(ctx, iter, TRUE);
- goto fail;
- }
- sum_precise_add(s, d);
- }
- ret = __JS_NewFloat64(ctx, sum_precise_get_result(s));
- fail:
- JS_FreeValue(ctx, iter);
- JS_FreeValue(ctx, next);
- return ret;
- }
- /* xorshift* random number generator by Marsaglia */
- static uint64_t xorshift64star(uint64_t *pstate)
- {
- uint64_t x;
- x = *pstate;
- x ^= x >> 12;
- x ^= x << 25;
- x ^= x >> 27;
- *pstate = x;
- return x * 0x2545F4914F6CDD1D;
- }
- static void js_random_init(JSContext *ctx)
- {
- struct timeval tv;
- gettimeofday(&tv, NULL);
- ctx->random_state = ((int64_t)tv.tv_sec * 1000000) + tv.tv_usec;
- /* the state must be non zero */
- if (ctx->random_state == 0)
- ctx->random_state = 1;
- }
- static JSValue js_math_random(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSFloat64Union u;
- uint64_t v;
- v = xorshift64star(&ctx->random_state);
- /* 1.0 <= u.d < 2 */
- u.u64 = ((uint64_t)0x3ff << 52) | (v >> 12);
- return __JS_NewFloat64(ctx, u.d - 1.0);
- }
- static const JSCFunctionListEntry js_math_funcs[] = {
- JS_CFUNC_MAGIC_DEF("min", 2, js_math_min_max, 0 ),
- JS_CFUNC_MAGIC_DEF("max", 2, js_math_min_max, 1 ),
- JS_CFUNC_SPECIAL_DEF("abs", 1, f_f, fabs ),
- JS_CFUNC_SPECIAL_DEF("floor", 1, f_f, floor ),
- JS_CFUNC_SPECIAL_DEF("ceil", 1, f_f, ceil ),
- JS_CFUNC_SPECIAL_DEF("round", 1, f_f, js_math_round ),
- JS_CFUNC_SPECIAL_DEF("sqrt", 1, f_f, sqrt ),
- JS_CFUNC_SPECIAL_DEF("acos", 1, f_f, acos ),
- JS_CFUNC_SPECIAL_DEF("asin", 1, f_f, asin ),
- JS_CFUNC_SPECIAL_DEF("atan", 1, f_f, atan ),
- JS_CFUNC_SPECIAL_DEF("atan2", 2, f_f_f, atan2 ),
- JS_CFUNC_SPECIAL_DEF("cos", 1, f_f, cos ),
- JS_CFUNC_SPECIAL_DEF("exp", 1, f_f, exp ),
- JS_CFUNC_SPECIAL_DEF("log", 1, f_f, log ),
- JS_CFUNC_SPECIAL_DEF("pow", 2, f_f_f, js_pow ),
- JS_CFUNC_SPECIAL_DEF("sin", 1, f_f, sin ),
- JS_CFUNC_SPECIAL_DEF("tan", 1, f_f, tan ),
- /* ES6 */
- JS_CFUNC_SPECIAL_DEF("trunc", 1, f_f, trunc ),
- JS_CFUNC_SPECIAL_DEF("sign", 1, f_f, js_math_sign ),
- JS_CFUNC_SPECIAL_DEF("cosh", 1, f_f, cosh ),
- JS_CFUNC_SPECIAL_DEF("sinh", 1, f_f, sinh ),
- JS_CFUNC_SPECIAL_DEF("tanh", 1, f_f, tanh ),
- JS_CFUNC_SPECIAL_DEF("acosh", 1, f_f, acosh ),
- JS_CFUNC_SPECIAL_DEF("asinh", 1, f_f, asinh ),
- JS_CFUNC_SPECIAL_DEF("atanh", 1, f_f, atanh ),
- JS_CFUNC_SPECIAL_DEF("expm1", 1, f_f, expm1 ),
- JS_CFUNC_SPECIAL_DEF("log1p", 1, f_f, log1p ),
- JS_CFUNC_SPECIAL_DEF("log2", 1, f_f, log2 ),
- JS_CFUNC_SPECIAL_DEF("log10", 1, f_f, log10 ),
- JS_CFUNC_SPECIAL_DEF("cbrt", 1, f_f, cbrt ),
- JS_CFUNC_DEF("hypot", 2, js_math_hypot ),
- JS_CFUNC_DEF("random", 0, js_math_random ),
- JS_CFUNC_SPECIAL_DEF("f16round", 1, f_f, js_math_f16round ),
- JS_CFUNC_SPECIAL_DEF("fround", 1, f_f, js_math_fround ),
- JS_CFUNC_DEF("imul", 2, js_math_imul ),
- JS_CFUNC_DEF("clz32", 1, js_math_clz32 ),
- JS_CFUNC_DEF("sumPrecise", 1, js_math_sumPrecise ),
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "Math", JS_PROP_CONFIGURABLE ),
- JS_PROP_DOUBLE_DEF("E", 2.718281828459045, 0 ),
- JS_PROP_DOUBLE_DEF("LN10", 2.302585092994046, 0 ),
- JS_PROP_DOUBLE_DEF("LN2", 0.6931471805599453, 0 ),
- JS_PROP_DOUBLE_DEF("LOG2E", 1.4426950408889634, 0 ),
- JS_PROP_DOUBLE_DEF("LOG10E", 0.4342944819032518, 0 ),
- JS_PROP_DOUBLE_DEF("PI", 3.141592653589793, 0 ),
- JS_PROP_DOUBLE_DEF("SQRT1_2", 0.7071067811865476, 0 ),
- JS_PROP_DOUBLE_DEF("SQRT2", 1.4142135623730951, 0 ),
- };
- static const JSCFunctionListEntry js_math_obj[] = {
- JS_OBJECT_DEF("Math", js_math_funcs, countof(js_math_funcs), JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE ),
- };
- /* Date */
- /* OS dependent. d = argv[0] is in ms from 1970. Return the difference
- between UTC time and local time 'd' in minutes */
- static int getTimezoneOffset(int64_t time)
- {
- time_t ti;
- int res;
- time /= 1000; /* convert to seconds */
- if (sizeof(time_t) == 4) {
- /* on 32-bit systems, we need to clamp the time value to the
- range of `time_t`. This is better than truncating values to
- 32 bits and hopefully provides the same result as 64-bit
- implementation of localtime_r.
- */
- if ((time_t)-1 < 0) {
- if (time < INT32_MIN) {
- time = INT32_MIN;
- } else if (time > INT32_MAX) {
- time = INT32_MAX;
- }
- } else {
- if (time < 0) {
- time = 0;
- } else if (time > UINT32_MAX) {
- time = UINT32_MAX;
- }
- }
- }
- ti = time;
- #if defined(_WIN32)
- {
- struct tm *tm;
- time_t gm_ti, loc_ti;
- tm = gmtime(&ti);
- if (!tm)
- return 0;
- gm_ti = mktime(tm);
- tm = localtime(&ti);
- if (!tm)
- return 0;
- loc_ti = mktime(tm);
- res = (gm_ti - loc_ti) / 60;
- }
- #elif defined(__nebos__)
- res = 0; /* NebOS keeps the RTC in UTC */
- #else
- {
- struct tm tm;
- localtime_r(&ti, &tm);
- res = -tm.tm_gmtoff / 60;
- }
- #endif
- return res;
- }
- #if 0
- static JSValue js___date_getTimezoneOffset(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- double dd;
- if (JS_ToFloat64(ctx, &dd, argv[0]))
- return JS_EXCEPTION;
- if (isnan(dd))
- return __JS_NewFloat64(ctx, dd);
- else
- return JS_NewInt32(ctx, getTimezoneOffset((int64_t)dd));
- }
- static JSValue js_get_prototype_from_ctor(JSContext *ctx, JSValueConst ctor,
- JSValueConst def_proto)
- {
- JSValue proto;
- proto = JS_GetProperty(ctx, ctor, JS_ATOM_prototype);
- if (JS_IsException(proto))
- return proto;
- if (!JS_IsObject(proto)) {
- JS_FreeValue(ctx, proto);
- proto = JS_DupValue(ctx, def_proto);
- }
- return proto;
- }
- /* create a new date object */
- static JSValue js___date_create(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue obj, proto;
- proto = js_get_prototype_from_ctor(ctx, argv[0], argv[1]);
- if (JS_IsException(proto))
- return proto;
- obj = JS_NewObjectProtoClass(ctx, proto, JS_CLASS_DATE);
- JS_FreeValue(ctx, proto);
- if (!JS_IsException(obj))
- JS_SetObjectData(ctx, obj, JS_DupValue(ctx, argv[2]));
- return obj;
- }
- #endif
- /* RegExp */
- static void js_regexp_finalizer(JSRuntime *rt, JSValue val)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JSRegExp *re = &p->u.regexp;
- if (re->bytecode != NULL)
- JS_FreeValueRT(rt, JS_MKPTR(JS_TAG_STRING, re->bytecode));
- if (re->pattern != NULL)
- JS_FreeValueRT(rt, JS_MKPTR(JS_TAG_STRING, re->pattern));
- }
- /* create a string containing the RegExp bytecode */
- static JSValue js_compile_regexp(JSContext *ctx, JSValueConst pattern,
- JSValueConst flags)
- {
- const char *str;
- int re_flags, mask;
- uint8_t *re_bytecode_buf;
- size_t i, len;
- int re_bytecode_len;
- JSValue ret;
- char error_msg[64];
- re_flags = 0;
- if (!JS_IsUndefined(flags)) {
- str = JS_ToCStringLen(ctx, &len, flags);
- if (!str)
- return JS_EXCEPTION;
- /* XXX: re_flags = LRE_FLAG_OCTAL unless strict mode? */
- for (i = 0; i < len; i++) {
- switch(str[i]) {
- case 'd':
- mask = LRE_FLAG_INDICES;
- break;
- case 'g':
- mask = LRE_FLAG_GLOBAL;
- break;
- case 'i':
- mask = LRE_FLAG_IGNORECASE;
- break;
- case 'm':
- mask = LRE_FLAG_MULTILINE;
- break;
- case 's':
- mask = LRE_FLAG_DOTALL;
- break;
- case 'u':
- mask = LRE_FLAG_UNICODE;
- break;
- case 'v':
- mask = LRE_FLAG_UNICODE_SETS;
- break;
- case 'y':
- mask = LRE_FLAG_STICKY;
- break;
- default:
- goto bad_flags;
- }
- if ((re_flags & mask) != 0) {
- bad_flags:
- JS_FreeCString(ctx, str);
- goto bad_flags1;
- }
- re_flags |= mask;
- }
- JS_FreeCString(ctx, str);
- }
- /* 'u' and 'v' cannot be both set */
- if ((re_flags & LRE_FLAG_UNICODE_SETS) && (re_flags & LRE_FLAG_UNICODE)) {
- bad_flags1:
- return JS_ThrowSyntaxError(ctx, "invalid regular expression flags");
- }
-
- str = JS_ToCStringLen2(ctx, &len, pattern, !(re_flags & (LRE_FLAG_UNICODE | LRE_FLAG_UNICODE_SETS)));
- if (!str)
- return JS_EXCEPTION;
- re_bytecode_buf = lre_compile(&re_bytecode_len, error_msg,
- sizeof(error_msg), str, len, re_flags, ctx);
- JS_FreeCString(ctx, str);
- if (!re_bytecode_buf) {
- JS_ThrowSyntaxError(ctx, "%s", error_msg);
- return JS_EXCEPTION;
- }
- ret = js_new_string8_len(ctx, (const char *)re_bytecode_buf, re_bytecode_len);
- js_free(ctx, re_bytecode_buf);
- return ret;
- }
- /* fast regexp creation */
- static JSValue JS_NewRegexp(JSContext *ctx, JSValue pattern, JSValue bc)
- {
- JSValue obj;
- JSProperty props[1];
- JSObject *p;
- JSRegExp *re;
- /* sanity check */
- if (unlikely(JS_VALUE_GET_TAG(bc) != JS_TAG_STRING ||
- JS_VALUE_GET_TAG(pattern) != JS_TAG_STRING)) {
- JS_ThrowTypeError(ctx, "string expected");
- goto fail;
- }
- props[0].u.value = JS_NewInt32(ctx, 0); /* lastIndex */
- obj = JS_NewObjectFromShape(ctx, js_dup_shape(ctx->regexp_shape), JS_CLASS_REGEXP, props);
- if (JS_IsException(obj))
- goto fail;
- p = JS_VALUE_GET_OBJ(obj);
- re = &p->u.regexp;
- re->pattern = JS_VALUE_GET_STRING(pattern);
- re->bytecode = JS_VALUE_GET_STRING(bc);
- return obj;
- fail:
- JS_FreeValue(ctx, bc);
- JS_FreeValue(ctx, pattern);
- return JS_EXCEPTION;
- }
- /* set the RegExp fields */
- static JSValue js_regexp_set_internal(JSContext *ctx,
- JSValue obj,
- JSValue pattern, JSValue bc)
- {
- JSObject *p;
- JSRegExp *re;
- /* sanity check */
- if (unlikely(JS_VALUE_GET_TAG(bc) != JS_TAG_STRING ||
- JS_VALUE_GET_TAG(pattern) != JS_TAG_STRING)) {
- JS_ThrowTypeError(ctx, "string expected");
- JS_FreeValue(ctx, obj);
- JS_FreeValue(ctx, bc);
- JS_FreeValue(ctx, pattern);
- return JS_EXCEPTION;
- }
- p = JS_VALUE_GET_OBJ(obj);
- re = &p->u.regexp;
- re->pattern = JS_VALUE_GET_STRING(pattern);
- re->bytecode = JS_VALUE_GET_STRING(bc);
- /* Note: cannot fail because the field is preallocated */
- JS_DefinePropertyValue(ctx, obj, JS_ATOM_lastIndex, JS_NewInt32(ctx, 0),
- JS_PROP_WRITABLE);
- return obj;
- }
- static JSRegExp *js_get_regexp(JSContext *ctx, JSValueConst obj, BOOL throw_error)
- {
- if (JS_VALUE_GET_TAG(obj) == JS_TAG_OBJECT) {
- JSObject *p = JS_VALUE_GET_OBJ(obj);
- if (p->class_id == JS_CLASS_REGEXP)
- return &p->u.regexp;
- }
- if (throw_error) {
- JS_ThrowTypeErrorInvalidClass(ctx, JS_CLASS_REGEXP);
- }
- return NULL;
- }
- /* return < 0 if exception or TRUE/FALSE */
- static int js_is_regexp(JSContext *ctx, JSValueConst obj)
- {
- JSValue m;
- if (!JS_IsObject(obj))
- return FALSE;
- m = JS_GetProperty(ctx, obj, JS_ATOM_Symbol_match);
- if (JS_IsException(m))
- return -1;
- if (!JS_IsUndefined(m))
- return JS_ToBoolFree(ctx, m);
- return js_get_regexp(ctx, obj, FALSE) != NULL;
- }
- static JSValue js_regexp_constructor(JSContext *ctx, JSValueConst new_target,
- int argc, JSValueConst *argv)
- {
- JSValue pattern, flags, bc, val, obj = JS_UNDEFINED;
- JSValueConst pat, flags1;
- JSRegExp *re;
- int pat_is_regexp;
- pat = argv[0];
- flags1 = argv[1];
- pat_is_regexp = js_is_regexp(ctx, pat);
- if (pat_is_regexp < 0)
- return JS_EXCEPTION;
- if (JS_IsUndefined(new_target)) {
- /* called as a function */
- new_target = JS_GetActiveFunction(ctx);
- if (pat_is_regexp && JS_IsUndefined(flags1)) {
- JSValue ctor;
- BOOL res;
- ctor = JS_GetProperty(ctx, pat, JS_ATOM_constructor);
- if (JS_IsException(ctor))
- return ctor;
- res = js_same_value(ctx, ctor, new_target);
- JS_FreeValue(ctx, ctor);
- if (res)
- return JS_DupValue(ctx, pat);
- }
- }
- re = js_get_regexp(ctx, pat, FALSE);
- flags = JS_UNDEFINED;
- if (re) {
- pattern = JS_DupValue(ctx, JS_MKPTR(JS_TAG_STRING, re->pattern));
- if (JS_IsUndefined(flags1)) {
- bc = JS_DupValue(ctx, JS_MKPTR(JS_TAG_STRING, re->bytecode));
- obj = js_create_from_ctor(ctx, new_target, JS_CLASS_REGEXP);
- if (JS_IsException(obj))
- goto fail;
- goto no_compilation;
- } else {
- flags = JS_DupValue(ctx, flags1);
- }
- } else {
- if (pat_is_regexp) {
- pattern = JS_GetProperty(ctx, pat, JS_ATOM_source);
- if (JS_IsException(pattern))
- goto fail;
- if (JS_IsUndefined(flags1)) {
- flags = JS_GetProperty(ctx, pat, JS_ATOM_flags);
- if (JS_IsException(flags))
- goto fail;
- } else {
- flags = JS_DupValue(ctx, flags1);
- }
- } else {
- pattern = JS_DupValue(ctx, pat);
- flags = JS_DupValue(ctx, flags1);
- }
- if (JS_IsUndefined(pattern)) {
- pattern = JS_AtomToString(ctx, JS_ATOM_empty_string);
- } else {
- val = pattern;
- pattern = JS_ToString(ctx, val);
- JS_FreeValue(ctx, val);
- if (JS_IsException(pattern))
- goto fail;
- }
- }
- obj = js_create_from_ctor(ctx, new_target, JS_CLASS_REGEXP);
- if (JS_IsException(obj))
- goto fail;
- bc = js_compile_regexp(ctx, pattern, flags);
- if (JS_IsException(bc))
- goto fail;
- JS_FreeValue(ctx, flags);
- no_compilation:
- return js_regexp_set_internal(ctx, obj, pattern, bc);
- fail:
- JS_FreeValue(ctx, pattern);
- JS_FreeValue(ctx, flags);
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue js_regexp_compile(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSRegExp *re1, *re;
- JSValueConst pattern1, flags1;
- JSValue bc, pattern;
- re = js_get_regexp(ctx, this_val, TRUE);
- if (!re)
- return JS_EXCEPTION;
- pattern1 = argv[0];
- flags1 = argv[1];
- re1 = js_get_regexp(ctx, pattern1, FALSE);
- if (re1) {
- if (!JS_IsUndefined(flags1))
- return JS_ThrowTypeError(ctx, "flags must be undefined");
- pattern = JS_DupValue(ctx, JS_MKPTR(JS_TAG_STRING, re1->pattern));
- bc = JS_DupValue(ctx, JS_MKPTR(JS_TAG_STRING, re1->bytecode));
- } else {
- bc = JS_UNDEFINED;
- if (JS_IsUndefined(pattern1))
- pattern = JS_AtomToString(ctx, JS_ATOM_empty_string);
- else
- pattern = JS_ToString(ctx, pattern1);
- if (JS_IsException(pattern))
- goto fail;
- bc = js_compile_regexp(ctx, pattern, flags1);
- if (JS_IsException(bc))
- goto fail;
- }
- JS_FreeValue(ctx, JS_MKPTR(JS_TAG_STRING, re->pattern));
- JS_FreeValue(ctx, JS_MKPTR(JS_TAG_STRING, re->bytecode));
- re->pattern = JS_VALUE_GET_STRING(pattern);
- re->bytecode = JS_VALUE_GET_STRING(bc);
- if (JS_SetProperty(ctx, this_val, JS_ATOM_lastIndex,
- JS_NewInt32(ctx, 0)) < 0)
- return JS_EXCEPTION;
- return JS_DupValue(ctx, this_val);
- fail:
- JS_FreeValue(ctx, pattern);
- JS_FreeValue(ctx, bc);
- return JS_EXCEPTION;
- }
- static JSValue js_regexp_get_source(JSContext *ctx, JSValueConst this_val)
- {
- JSRegExp *re;
- JSString *p;
- StringBuffer b_s, *b = &b_s;
- int i, n, c, c2, bra;
- if (JS_VALUE_GET_TAG(this_val) != JS_TAG_OBJECT)
- return JS_ThrowTypeErrorNotAnObject(ctx);
- if (js_same_value(ctx, this_val, ctx->class_proto[JS_CLASS_REGEXP]))
- goto empty_regex;
- re = js_get_regexp(ctx, this_val, TRUE);
- if (!re)
- return JS_EXCEPTION;
- p = re->pattern;
- if (p->len == 0) {
- empty_regex:
- return js_new_string8(ctx, "(?:)");
- }
- string_buffer_init2(ctx, b, p->len, p->is_wide_char);
- /* Escape '/' and newline sequences as needed */
- bra = 0;
- for (i = 0, n = p->len; i < n;) {
- c2 = -1;
- switch (c = string_get(p, i++)) {
- case '\\':
- if (i < n)
- c2 = string_get(p, i++);
- break;
- case ']':
- bra = 0;
- break;
- case '[':
- if (!bra) {
- if (i < n && string_get(p, i) == ']')
- c2 = string_get(p, i++);
- bra = 1;
- }
- break;
- case '\n':
- c = '\\';
- c2 = 'n';
- break;
- case '\r':
- c = '\\';
- c2 = 'r';
- break;
- case '/':
- if (!bra) {
- c = '\\';
- c2 = '/';
- }
- break;
- }
- string_buffer_putc16(b, c);
- if (c2 >= 0)
- string_buffer_putc16(b, c2);
- }
- return string_buffer_end(b);
- }
- static JSValue js_regexp_get_flag(JSContext *ctx, JSValueConst this_val, int mask)
- {
- JSRegExp *re;
- int flags;
- if (JS_VALUE_GET_TAG(this_val) != JS_TAG_OBJECT)
- return JS_ThrowTypeErrorNotAnObject(ctx);
- re = js_get_regexp(ctx, this_val, FALSE);
- if (!re) {
- if (js_same_value(ctx, this_val, ctx->class_proto[JS_CLASS_REGEXP]))
- return JS_UNDEFINED;
- else
- return JS_ThrowTypeErrorInvalidClass(ctx, JS_CLASS_REGEXP);
- }
- flags = lre_get_flags(re->bytecode->u.str8);
- return JS_NewBool(ctx, flags & mask);
- }
- #define RE_FLAG_COUNT 8
- static JSValue js_regexp_get_flags(JSContext *ctx, JSValueConst this_val)
- {
- char str[RE_FLAG_COUNT], *p = str;
- int res, i;
- static const int flag_atom[RE_FLAG_COUNT] = {
- JS_ATOM_hasIndices,
- JS_ATOM_global,
- JS_ATOM_ignoreCase,
- JS_ATOM_multiline,
- JS_ATOM_dotAll,
- JS_ATOM_unicode,
- JS_ATOM_unicodeSets,
- JS_ATOM_sticky,
- };
- static const char flag_char[RE_FLAG_COUNT] = { 'd', 'g', 'i', 'm', 's', 'u', 'v', 'y' };
-
- if (JS_VALUE_GET_TAG(this_val) != JS_TAG_OBJECT)
- return JS_ThrowTypeErrorNotAnObject(ctx);
- for(i = 0; i < RE_FLAG_COUNT; i++) {
- res = JS_ToBoolFree(ctx, JS_GetProperty(ctx, this_val, flag_atom[i]));
- if (res < 0)
- goto exception;
- if (res)
- *p++ = flag_char[i];
- }
- return JS_NewStringLen(ctx, str, p - str);
- exception:
- return JS_EXCEPTION;
- }
- static JSValue js_regexp_toString(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue pattern, flags;
- StringBuffer b_s, *b = &b_s;
- if (!JS_IsObject(this_val))
- return JS_ThrowTypeErrorNotAnObject(ctx);
- string_buffer_init(ctx, b, 0);
- string_buffer_putc8(b, '/');
- pattern = JS_GetProperty(ctx, this_val, JS_ATOM_source);
- if (string_buffer_concat_value_free(b, pattern))
- goto fail;
- string_buffer_putc8(b, '/');
- flags = JS_GetProperty(ctx, this_val, JS_ATOM_flags);
- if (string_buffer_concat_value_free(b, flags))
- goto fail;
- return string_buffer_end(b);
- fail:
- string_buffer_free(b);
- return JS_EXCEPTION;
- }
- int lre_check_stack_overflow(void *opaque, size_t alloca_size)
- {
- JSContext *ctx = opaque;
- return js_check_stack_overflow(ctx->rt, alloca_size);
- }
- int lre_check_timeout(void *opaque)
- {
- JSContext *ctx = opaque;
- JSRuntime *rt = ctx->rt;
- return (rt->interrupt_handler &&
- rt->interrupt_handler(rt, rt->interrupt_opaque));
- }
- void *lre_realloc(void *opaque, void *ptr, size_t size)
- {
- JSContext *ctx = opaque;
- /* No JS exception is raised here */
- return js_realloc_rt(ctx->rt, ptr, size);
- }
- static JSValue js_regexp_escape(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue str;
- StringBuffer b_s, *b = &b_s;
- JSString *p;
- uint32_t c;
- char s[16];
- int i, i0;
-
- if (!JS_IsString(argv[0]))
- return JS_ThrowTypeError(ctx, "not a string");
- str = JS_ToString(ctx, argv[0]); /* must call it to linearlize ropes */
- if (JS_IsException(str))
- return JS_EXCEPTION;
- p = JS_VALUE_GET_STRING(str);
- string_buffer_init2(ctx, b, 0, p->is_wide_char);
- for (i = 0; i < p->len; ) {
- i0 = i;
- c = string_getc(p, &i);
- if (c < 33) {
- if (c >= 9 && c <= 13) {
- string_buffer_putc8(b, '\\');
- string_buffer_putc8(b, "tnvfr"[c - 9]);
- } else {
- goto hex2;
- }
- } else if (c < 128) {
- if ((c >= '0' && c <= '9')
- || (c >= 'A' && c <= 'Z')
- || (c >= 'a' && c <= 'z')) {
- if (i0 == 0)
- goto hex2;
- } else if (strchr(",-=<>#&!%:;@~'`\"", c)) {
- goto hex2;
- } else if (c != '_') {
- string_buffer_putc8(b, '\\');
- }
- string_buffer_putc8(b, c);
- } else if (c < 256) {
- hex2:
- snprintf(s, sizeof(s), "\\x%02x", c);
- string_buffer_puts8(b, s);
- } else if (is_surrogate(c) || lre_is_space(c)) {
- snprintf(s, sizeof(s), "\\u%04x", c);
- string_buffer_puts8(b, s);
- } else {
- string_buffer_putc(b, c);
- }
- }
- JS_FreeValue(ctx, str);
- return string_buffer_end(b);
- }
- /* this_val must be of JS_CLASS_REGEXP */
- static force_inline int js_regexp_get_lastIndex(JSContext *ctx, int64_t *plast_index,
- JSValueConst this_val)
- {
- JSObject *p = JS_VALUE_GET_OBJ(this_val);
-
- /* lastIndex is always the first property (it is not configurable) */
- if (likely(JS_VALUE_GET_TAG(p->prop[0].u.value) == JS_TAG_INT)) {
- *plast_index = max_int(JS_VALUE_GET_INT(p->prop[0].u.value), 0);
- return 0;
- } else {
- return JS_ToLengthFree(ctx, plast_index, JS_DupValue(ctx, p->prop[0].u.value));
- }
- }
- /* this_val must be of JS_CLASS_REGEXP */
- static force_inline int js_regexp_set_lastIndex(JSContext *ctx, JSValueConst this_val,
- int last_index)
- {
- JSObject *p = JS_VALUE_GET_OBJ(this_val);
-
- /* lastIndex is always the first property (it is not configurable) */
- if (likely(JS_VALUE_GET_TAG(p->prop[0].u.value) == JS_TAG_INT &&
- (get_shape_prop(p->shape)->flags & JS_PROP_WRITABLE))) {
- set_value(ctx, &p->prop[0].u.value, JS_NewInt32(ctx, last_index));
- } else {
- if (JS_SetProperty(ctx, this_val, JS_ATOM_lastIndex,
- JS_NewInt32(ctx, last_index)) < 0)
- return -1;
- }
- return 0;
- }
- static JSValue js_regexp_exec(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSRegExp *re = js_get_regexp(ctx, this_val, TRUE);
- JSString *str;
- JSValue t, ret, str_val, obj, groups;
- JSValue indices, indices_groups;
- uint8_t *re_bytecode;
- uint8_t **capture, *str_buf;
- int rc, capture_count, shift, i, re_flags, alloc_count;
- int64_t last_index;
- const char *group_name_ptr;
- JSObject *p_obj;
- JSAtom group_name;
-
- if (!re)
- return JS_EXCEPTION;
- str_val = JS_ToString(ctx, argv[0]);
- if (JS_IsException(str_val))
- return JS_EXCEPTION;
- ret = JS_EXCEPTION;
- obj = JS_NULL;
- groups = JS_UNDEFINED;
- indices = JS_UNDEFINED;
- indices_groups = JS_UNDEFINED;
- capture = NULL;
- group_name = JS_ATOM_NULL;
-
- if (js_regexp_get_lastIndex(ctx, &last_index, this_val))
- goto fail;
- re_bytecode = re->bytecode->u.str8;
- re_flags = lre_get_flags(re_bytecode);
- if ((re_flags & (LRE_FLAG_GLOBAL | LRE_FLAG_STICKY)) == 0) {
- last_index = 0;
- }
- str = JS_VALUE_GET_STRING(str_val);
- alloc_count = lre_get_alloc_count(re_bytecode);
- if (alloc_count > 0) {
- capture = js_malloc(ctx, sizeof(capture[0]) * alloc_count);
- if (!capture)
- goto fail;
- }
- capture_count = lre_get_capture_count(re_bytecode);
- shift = str->is_wide_char;
- str_buf = str->u.str8;
- if (last_index > str->len) {
- rc = 2;
- } else {
- rc = lre_exec(capture, re_bytecode,
- str_buf, last_index, str->len,
- shift, ctx);
- }
- if (rc != 1) {
- if (rc >= 0) {
- if (rc == 2 || (re_flags & (LRE_FLAG_GLOBAL | LRE_FLAG_STICKY))) {
- if (js_regexp_set_lastIndex(ctx, this_val, 0) < 0)
- goto fail;
- }
- } else {
- if (rc == LRE_RET_TIMEOUT) {
- JS_ThrowInterrupted(ctx);
- } else {
- JS_ThrowInternalError(ctx, "out of memory in regexp execution");
- }
- goto fail;
- }
- } else {
- int prop_flags;
- JSProperty props[4];
-
- if (re_flags & (LRE_FLAG_GLOBAL | LRE_FLAG_STICKY)) {
- if (js_regexp_set_lastIndex(ctx, this_val,
- (capture[1] - str_buf) >> shift) < 0)
- goto fail;
- }
- prop_flags = JS_PROP_C_W_E | JS_PROP_THROW;
- group_name_ptr = lre_get_groupnames(re_bytecode);
- if (group_name_ptr) {
- groups = JS_NewObjectProto(ctx, JS_NULL);
- if (JS_IsException(groups))
- goto fail;
- }
- if (re_flags & LRE_FLAG_INDICES) {
- indices = JS_NewArray(ctx);
- if (JS_IsException(indices))
- goto fail;
- if (group_name_ptr) {
- indices_groups = JS_NewObjectProto(ctx, JS_NULL);
- if (JS_IsException(indices_groups))
- goto fail;
- }
- }
- props[0].u.value = JS_NewInt32(ctx, capture_count); /* length */
- props[1].u.value = JS_NewInt32(ctx, (capture[0] - str_buf) >> shift); /* index */
- props[2].u.value = str_val; /* input */
- props[3].u.value = JS_DupValue(ctx, groups); /* groups */
- str_val = JS_UNDEFINED;
- obj = JS_NewObjectFromShape(ctx, js_dup_shape(ctx->regexp_result_shape),
- JS_CLASS_ARRAY, props);
- if (JS_IsException(obj))
- goto fail;
- p_obj = JS_VALUE_GET_OBJ(obj);
- if (expand_fast_array(ctx, p_obj, capture_count))
- goto fail;
-
- for(i = 0; i < capture_count; i++) {
- uint8_t **match = &capture[2 * i];
- int start = -1;
- int end = -1;
- JSValue val;
- if (group_name_ptr && i > 0) {
- if (*group_name_ptr) {
- /* XXX: slow, should create a shape when the regexp is
- compiled */
- group_name = JS_NewAtom(ctx, group_name_ptr);
- if (group_name == JS_ATOM_NULL)
- goto fail;
- }
- group_name_ptr += strlen(group_name_ptr) + LRE_GROUP_NAME_TRAILER_LEN;
- }
- if (match[0] && match[1]) {
- start = (match[0] - str_buf) >> shift;
- end = (match[1] - str_buf) >> shift;
- }
- if (!JS_IsUndefined(indices)) {
- val = JS_UNDEFINED;
- if (start != -1) {
- val = JS_NewArray(ctx);
- if (JS_IsException(val))
- goto fail;
- if (JS_DefinePropertyValueUint32(ctx, val, 0,
- JS_NewInt32(ctx, start),
- prop_flags) < 0) {
- JS_FreeValue(ctx, val);
- goto fail;
- }
- if (JS_DefinePropertyValueUint32(ctx, val, 1,
- JS_NewInt32(ctx, end),
- prop_flags) < 0) {
- JS_FreeValue(ctx, val);
- goto fail;
- }
- }
- if (group_name != JS_ATOM_NULL) {
- /* JS_HasProperty() cannot fail here */
- if (!JS_IsUndefined(val) ||
- !JS_HasProperty(ctx, indices_groups, group_name)) {
- if (JS_DefinePropertyValue(ctx, indices_groups,
- group_name, JS_DupValue(ctx, val), prop_flags) < 0) {
- JS_FreeValue(ctx, val);
- goto fail;
- }
- }
- }
- if (JS_DefinePropertyValueUint32(ctx, indices, i, val,
- prop_flags) < 0) {
- goto fail;
- }
- }
- val = JS_UNDEFINED;
- if (start != -1) {
- val = js_sub_string(ctx, str, start, end);
- if (JS_IsException(val))
- goto fail;
- }
- if (group_name != JS_ATOM_NULL) {
- /* JS_HasProperty() cannot fail here */
- if (!JS_IsUndefined(val) ||
- !JS_HasProperty(ctx, groups, group_name)) {
- if (JS_DefinePropertyValue(ctx, groups, group_name,
- JS_DupValue(ctx, val),
- prop_flags) < 0) {
- JS_FreeValue(ctx, val);
- goto fail;
- }
- }
- JS_FreeAtom(ctx, group_name);
- group_name = JS_ATOM_NULL;
- }
- p_obj->u.array.u.values[p_obj->u.array.count++] = val;
- }
- if (!JS_IsUndefined(indices)) {
- t = indices_groups, indices_groups = JS_UNDEFINED;
- if (JS_DefinePropertyValue(ctx, indices, JS_ATOM_groups,
- t, prop_flags) < 0) {
- goto fail;
- }
- t = indices, indices = JS_UNDEFINED;
- if (JS_DefinePropertyValue(ctx, obj, JS_ATOM_indices,
- t, prop_flags) < 0) {
- goto fail;
- }
- }
- }
- ret = obj;
- obj = JS_UNDEFINED;
- fail:
- JS_FreeAtom(ctx, group_name);
- JS_FreeValue(ctx, indices_groups);
- JS_FreeValue(ctx, indices);
- JS_FreeValue(ctx, str_val);
- JS_FreeValue(ctx, groups);
- JS_FreeValue(ctx, obj);
- js_free(ctx, capture);
- return ret;
- }
- /* XXX: add group names support */
- static JSValue js_regexp_replace(JSContext *ctx, JSValueConst this_val, JSValueConst arg,
- JSValueConst rep_val)
- {
- JSRegExp *re = js_get_regexp(ctx, this_val, TRUE);
- JSString *str;
- JSValue str_val;
- uint8_t *re_bytecode;
- int ret;
- uint8_t **capture, *str_buf;
- int capture_count, alloc_count, shift, re_flags;
- int next_src_pos, start, end;
- int64_t last_index;
- StringBuffer b_s, *b = &b_s;
- JSString *rp = JS_VALUE_GET_STRING(rep_val);
- const char *group_name_ptr;
- BOOL fullUnicode;
-
- if (!re)
- return JS_EXCEPTION;
- re_bytecode = re->bytecode->u.str8;
- group_name_ptr = lre_get_groupnames(re_bytecode);
- if (group_name_ptr)
- return JS_UNDEFINED; /* group names are not supported yet */
-
- string_buffer_init(ctx, b, 0);
- capture = NULL;
- str_val = JS_ToString(ctx, arg);
- if (JS_IsException(str_val))
- goto fail;
- str = JS_VALUE_GET_STRING(str_val);
- re_flags = lre_get_flags(re_bytecode);
- if (re_flags & LRE_FLAG_GLOBAL) {
- if (js_regexp_set_lastIndex(ctx, this_val, 0))
- goto fail;
- }
- if ((re_flags & (LRE_FLAG_GLOBAL | LRE_FLAG_STICKY)) == 0) {
- last_index = 0;
- } else {
- if (js_regexp_get_lastIndex(ctx, &last_index, this_val))
- goto fail;
- }
- alloc_count = lre_get_alloc_count(re_bytecode);
- if (alloc_count > 0) {
- capture = js_malloc(ctx, sizeof(capture[0]) * alloc_count);
- if (!capture)
- goto fail;
- }
- capture_count = lre_get_capture_count(re_bytecode);
- fullUnicode = ((re_flags & (LRE_FLAG_UNICODE | LRE_FLAG_UNICODE_SETS)) != 0);
- shift = str->is_wide_char;
- str_buf = str->u.str8;
- next_src_pos = 0;
- for (;;) {
- if (last_index > str->len) {
- ret = 0;
- } else {
- ret = lre_exec(capture, re_bytecode,
- str_buf, last_index, str->len, shift, ctx);
- }
- if (ret != 1) {
- if (ret >= 0) {
- if (ret == 2 || (re_flags & (LRE_FLAG_GLOBAL | LRE_FLAG_STICKY))) {
- if (js_regexp_set_lastIndex(ctx, this_val, 0) < 0)
- goto fail;
- }
- } else {
- if (ret == LRE_RET_TIMEOUT) {
- JS_ThrowInterrupted(ctx);
- } else {
- JS_ThrowInternalError(ctx, "out of memory in regexp execution");
- }
- goto fail;
- }
- break;
- }
- start = (capture[0] - str_buf) >> shift;
- end = (capture[1] - str_buf) >> shift;
- last_index = end;
- if (next_src_pos < start) {
- if (string_buffer_concat(b, str, next_src_pos, start))
- goto fail;
- }
- if (rp->len != 0) {
- if (js_string_GetSubstitution(ctx, b, JS_UNDEFINED, str, start,
- JS_UNDEFINED, JS_UNDEFINED, rep_val,
- capture, capture_count)) {
- goto fail;
- }
- }
- next_src_pos = end;
- if (!(re_flags & LRE_FLAG_GLOBAL)) {
- if (re_flags & LRE_FLAG_STICKY) {
- if (js_regexp_set_lastIndex(ctx, this_val, end) < 0)
- goto fail;
- }
- break;
- }
- if (end == start) {
- end = string_advance_index(str, end, fullUnicode);
- }
- last_index = end;
- }
- if (string_buffer_concat(b, str, next_src_pos, str->len))
- goto fail;
- JS_FreeValue(ctx, str_val);
- js_free(ctx, capture);
- return string_buffer_end(b);
- fail:
- JS_FreeValue(ctx, str_val);
- js_free(ctx, capture);
- string_buffer_free(b);
- return JS_EXCEPTION;
- }
- static JSValue JS_RegExpExec(JSContext *ctx, JSValueConst r, JSValueConst s)
- {
- JSValue method, ret;
- method = JS_GetProperty(ctx, r, JS_ATOM_exec);
- if (JS_IsException(method))
- return method;
- if (JS_IsFunction(ctx, method)) {
- ret = JS_CallFree(ctx, method, r, 1, &s);
- if (JS_IsException(ret))
- return ret;
- if (!JS_IsObject(ret) && !JS_IsNull(ret)) {
- JS_FreeValue(ctx, ret);
- return JS_ThrowTypeError(ctx, "RegExp exec method must return an object or null");
- }
- return ret;
- }
- JS_FreeValue(ctx, method);
- return js_regexp_exec(ctx, r, 1, &s);
- }
- static JSValue js_regexp_test(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue val;
- BOOL ret;
- val = JS_RegExpExec(ctx, this_val, argv[0]);
- if (JS_IsException(val))
- return JS_EXCEPTION;
- ret = !JS_IsNull(val);
- JS_FreeValue(ctx, val);
- return JS_NewBool(ctx, ret);
- }
- static JSValue js_regexp_Symbol_match(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- // [Symbol.match](str)
- JSValueConst rx = this_val;
- JSValue A, S, flags, result, matchStr;
- int global, n, fullUnicode, isEmpty;
- JSString *p;
- if (!JS_IsObject(rx))
- return JS_ThrowTypeErrorNotAnObject(ctx);
- A = JS_UNDEFINED;
- flags = JS_UNDEFINED;
- result = JS_UNDEFINED;
- matchStr = JS_UNDEFINED;
- S = JS_ToString(ctx, argv[0]);
- if (JS_IsException(S))
- goto exception;
- flags = JS_GetProperty(ctx, rx, JS_ATOM_flags);
- if (JS_IsException(flags))
- goto exception;
- flags = JS_ToStringFree(ctx, flags);
- if (JS_IsException(flags))
- goto exception;
- p = JS_VALUE_GET_STRING(flags);
- global = (-1 != string_indexof_char(p, 'g', 0));
- if (!global) {
- A = JS_RegExpExec(ctx, rx, S);
- } else {
- fullUnicode = (string_indexof_char(p, 'u', 0) >= 0 ||
- string_indexof_char(p, 'v', 0) >= 0);
- if (JS_SetProperty(ctx, rx, JS_ATOM_lastIndex, JS_NewInt32(ctx, 0)) < 0)
- goto exception;
- A = JS_NewArray(ctx);
- if (JS_IsException(A))
- goto exception;
- n = 0;
- for(;;) {
- JS_FreeValue(ctx, result);
- result = JS_RegExpExec(ctx, rx, S);
- if (JS_IsException(result))
- goto exception;
- if (JS_IsNull(result))
- break;
- matchStr = JS_ToStringFree(ctx, JS_GetPropertyInt64(ctx, result, 0));
- if (JS_IsException(matchStr))
- goto exception;
- isEmpty = JS_IsEmptyString(matchStr);
- if (JS_DefinePropertyValueInt64(ctx, A, n++, matchStr, JS_PROP_C_W_E | JS_PROP_THROW) < 0)
- goto exception;
- if (isEmpty) {
- int64_t thisIndex, nextIndex;
- if (JS_ToLengthFree(ctx, &thisIndex,
- JS_GetProperty(ctx, rx, JS_ATOM_lastIndex)) < 0)
- goto exception;
- p = JS_VALUE_GET_STRING(S);
- nextIndex = string_advance_index(p, thisIndex, fullUnicode);
- if (JS_SetProperty(ctx, rx, JS_ATOM_lastIndex, JS_NewInt64(ctx, nextIndex)) < 0)
- goto exception;
- }
- }
- if (n == 0) {
- JS_FreeValue(ctx, A);
- A = JS_NULL;
- }
- }
- JS_FreeValue(ctx, result);
- JS_FreeValue(ctx, flags);
- JS_FreeValue(ctx, S);
- return A;
- exception:
- JS_FreeValue(ctx, A);
- JS_FreeValue(ctx, result);
- JS_FreeValue(ctx, flags);
- JS_FreeValue(ctx, S);
- return JS_EXCEPTION;
- }
- typedef struct JSRegExpStringIteratorData {
- JSValue iterating_regexp;
- JSValue iterated_string;
- BOOL global;
- BOOL unicode;
- BOOL done;
- } JSRegExpStringIteratorData;
- static void js_regexp_string_iterator_finalizer(JSRuntime *rt, JSValue val)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JSRegExpStringIteratorData *it = p->u.regexp_string_iterator_data;
- if (it) {
- JS_FreeValueRT(rt, it->iterating_regexp);
- JS_FreeValueRT(rt, it->iterated_string);
- js_free_rt(rt, it);
- }
- }
- static void js_regexp_string_iterator_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JSRegExpStringIteratorData *it = p->u.regexp_string_iterator_data;
- if (it) {
- JS_MarkValue(rt, it->iterating_regexp, mark_func);
- JS_MarkValue(rt, it->iterated_string, mark_func);
- }
- }
- static JSValue js_regexp_string_iterator_next(JSContext *ctx,
- JSValueConst this_val,
- int argc, JSValueConst *argv,
- BOOL *pdone, int magic)
- {
- JSRegExpStringIteratorData *it;
- JSValueConst R, S;
- JSValue matchStr = JS_UNDEFINED, match = JS_UNDEFINED;
- JSString *sp;
- it = JS_GetOpaque2(ctx, this_val, JS_CLASS_REGEXP_STRING_ITERATOR);
- if (!it)
- goto exception;
- if (it->done) {
- *pdone = TRUE;
- return JS_UNDEFINED;
- }
- R = it->iterating_regexp;
- S = it->iterated_string;
- match = JS_RegExpExec(ctx, R, S);
- if (JS_IsException(match))
- goto exception;
- if (JS_IsNull(match)) {
- it->done = TRUE;
- *pdone = TRUE;
- return JS_UNDEFINED;
- } else if (it->global) {
- matchStr = JS_ToStringFree(ctx, JS_GetPropertyInt64(ctx, match, 0));
- if (JS_IsException(matchStr))
- goto exception;
- if (JS_IsEmptyString(matchStr)) {
- int64_t thisIndex, nextIndex;
- if (JS_ToLengthFree(ctx, &thisIndex,
- JS_GetProperty(ctx, R, JS_ATOM_lastIndex)) < 0)
- goto exception;
- sp = JS_VALUE_GET_STRING(S);
- nextIndex = string_advance_index(sp, thisIndex, it->unicode);
- if (JS_SetProperty(ctx, R, JS_ATOM_lastIndex,
- JS_NewInt64(ctx, nextIndex)) < 0)
- goto exception;
- }
- JS_FreeValue(ctx, matchStr);
- } else {
- it->done = TRUE;
- }
- *pdone = FALSE;
- return match;
- exception:
- JS_FreeValue(ctx, match);
- JS_FreeValue(ctx, matchStr);
- *pdone = FALSE;
- return JS_EXCEPTION;
- }
- static JSValue js_regexp_Symbol_matchAll(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- // [Symbol.matchAll](str)
- JSValueConst R = this_val;
- JSValue S, C, flags, matcher, iter;
- JSValueConst args[2];
- JSString *strp;
- int64_t lastIndex;
- JSRegExpStringIteratorData *it;
- if (!JS_IsObject(R))
- return JS_ThrowTypeErrorNotAnObject(ctx);
- C = JS_UNDEFINED;
- flags = JS_UNDEFINED;
- matcher = JS_UNDEFINED;
- iter = JS_UNDEFINED;
- S = JS_ToString(ctx, argv[0]);
- if (JS_IsException(S))
- goto exception;
- C = JS_SpeciesConstructor(ctx, R, ctx->regexp_ctor);
- if (JS_IsException(C))
- goto exception;
- flags = JS_ToStringFree(ctx, JS_GetProperty(ctx, R, JS_ATOM_flags));
- if (JS_IsException(flags))
- goto exception;
- args[0] = R;
- args[1] = flags;
- matcher = JS_CallConstructor(ctx, C, 2, args);
- if (JS_IsException(matcher))
- goto exception;
- if (JS_ToLengthFree(ctx, &lastIndex,
- JS_GetProperty(ctx, R, JS_ATOM_lastIndex)))
- goto exception;
- if (JS_SetProperty(ctx, matcher, JS_ATOM_lastIndex,
- JS_NewInt64(ctx, lastIndex)) < 0)
- goto exception;
- iter = JS_NewObjectClass(ctx, JS_CLASS_REGEXP_STRING_ITERATOR);
- if (JS_IsException(iter))
- goto exception;
- it = js_malloc(ctx, sizeof(*it));
- if (!it)
- goto exception;
- it->iterating_regexp = matcher;
- it->iterated_string = S;
- strp = JS_VALUE_GET_STRING(flags);
- it->global = string_indexof_char(strp, 'g', 0) >= 0;
- it->unicode = (string_indexof_char(strp, 'u', 0) >= 0 ||
- string_indexof_char(strp, 'v', 0) >= 0);
- it->done = FALSE;
- JS_SetOpaque(iter, it);
- JS_FreeValue(ctx, C);
- JS_FreeValue(ctx, flags);
- return iter;
- exception:
- JS_FreeValue(ctx, S);
- JS_FreeValue(ctx, C);
- JS_FreeValue(ctx, flags);
- JS_FreeValue(ctx, matcher);
- JS_FreeValue(ctx, iter);
- return JS_EXCEPTION;
- }
- typedef struct ValueBuffer {
- JSContext *ctx;
- JSValue *arr;
- JSValue def[4];
- int len;
- int size;
- int error_status;
- } ValueBuffer;
- static int value_buffer_init(JSContext *ctx, ValueBuffer *b)
- {
- b->ctx = ctx;
- b->len = 0;
- b->size = 4;
- b->error_status = 0;
- b->arr = b->def;
- return 0;
- }
- static void value_buffer_free(ValueBuffer *b)
- {
- while (b->len > 0)
- JS_FreeValue(b->ctx, b->arr[--b->len]);
- if (b->arr != b->def)
- js_free(b->ctx, b->arr);
- b->arr = b->def;
- b->size = 4;
- }
- static int value_buffer_append(ValueBuffer *b, JSValue val)
- {
- if (b->error_status)
- return -1;
- if (b->len >= b->size) {
- int new_size = (b->len + (b->len >> 1) + 31) & ~16;
- size_t slack;
- JSValue *new_arr;
- if (b->arr == b->def) {
- new_arr = js_realloc2(b->ctx, NULL, sizeof(*b->arr) * new_size, &slack);
- if (new_arr)
- memcpy(new_arr, b->def, sizeof b->def);
- } else {
- new_arr = js_realloc2(b->ctx, b->arr, sizeof(*b->arr) * new_size, &slack);
- }
- if (!new_arr) {
- value_buffer_free(b);
- JS_FreeValue(b->ctx, val);
- b->error_status = -1;
- return -1;
- }
- new_size += slack / sizeof(*new_arr);
- b->arr = new_arr;
- b->size = new_size;
- }
- b->arr[b->len++] = val;
- return 0;
- }
- /* find in 'p' or its prototypes */
- static JSShapeProperty *find_property_regexp(JSProperty **ppr,
- JSObject *p, JSAtom atom)
- {
- JSShapeProperty *prs;
- for(;;) {
- prs = find_own_property(ppr, p, atom);
- if (prs)
- return prs;
- p = p->shape->proto;
- if (!p)
- return NULL;
- if (p->is_exotic)
- return NULL;
- }
- }
- static BOOL check_regexp_getter(JSContext *ctx,
- JSObject *p, JSAtom atom,
- JSCFunction *func, int magic)
- {
- JSProperty *pr;
- JSShapeProperty *prs;
- prs = find_property_regexp(&pr, p, atom);
- if (!prs)
- return FALSE;
- if ((prs->flags & JS_PROP_TMASK) != JS_PROP_GETSET)
- return FALSE;
- if (!pr->u.getset.getter)
- return FALSE;
- return JS_IsCFunction(ctx, JS_MKPTR(JS_TAG_OBJECT, pr->u.getset.getter),
- func, magic);
- }
- static BOOL js_is_standard_regexp(JSContext *ctx, JSValueConst obj)
- {
- JSObject *p;
- JSProperty *pr;
- JSShapeProperty *prs;
- JSCFunctionType ft;
-
- if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)
- return FALSE;
- p = JS_VALUE_GET_OBJ(obj);
- if (p->class_id != JS_CLASS_REGEXP)
- return FALSE;
- /* check that the lastIndex is a number (no side effect while getting it) */
- prs = find_own_property(&pr, p, JS_ATOM_lastIndex);
- if (!prs)
- return FALSE;
- if (!JS_IsNumber(pr->u.value))
- return FALSE;
- /* check the 'exec' method. */
- prs = find_property_regexp(&pr, p, JS_ATOM_exec);
- if (!prs)
- return FALSE;
- if ((prs->flags & JS_PROP_TMASK) != JS_PROP_NORMAL)
- return FALSE;
- if (!JS_IsCFunction(ctx, pr->u.value, js_regexp_exec, 0))
- return FALSE;
- /* check the flag getters */
- ft.getter = js_regexp_get_flags;
- if (!check_regexp_getter(ctx, p, JS_ATOM_flags, ft.generic, 0))
- return FALSE;
- ft.getter_magic = js_regexp_get_flag;
- if (!check_regexp_getter(ctx, p, JS_ATOM_global, ft.generic, LRE_FLAG_GLOBAL))
- return FALSE;
- if (!check_regexp_getter(ctx, p, JS_ATOM_unicode, ft.generic, LRE_FLAG_UNICODE))
- return FALSE;
- /* XXX: need to check all accessors, need a faster way. */
- return TRUE;
- }
- static JSValue js_regexp_Symbol_replace(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- // [Symbol.replace](str, rep)
- JSValueConst rx = this_val, rep = argv[1];
- JSValueConst args[6];
- JSValue flags, str, rep_val, matched, tab, rep_str, namedCaptures, res;
- JSString *p, *sp;
- StringBuffer b_s, *b = &b_s;
- ValueBuffer v_b, *results = &v_b;
- int nextSourcePosition, n, j, functionalReplace, is_global, fullUnicode;
- uint32_t nCaptures;
- int64_t position;
- if (!JS_IsObject(rx))
- return JS_ThrowTypeErrorNotAnObject(ctx);
- string_buffer_init(ctx, b, 0);
- value_buffer_init(ctx, results);
- rep_val = JS_UNDEFINED;
- matched = JS_UNDEFINED;
- tab = JS_UNDEFINED;
- flags = JS_UNDEFINED;
- rep_str = JS_UNDEFINED;
- namedCaptures = JS_UNDEFINED;
- str = JS_ToString(ctx, argv[0]);
- if (JS_IsException(str))
- goto exception;
- sp = JS_VALUE_GET_STRING(str);
- functionalReplace = JS_IsFunction(ctx, rep);
- if (!functionalReplace) {
- rep_val = JS_ToString(ctx, rep);
- if (JS_IsException(rep_val))
- goto exception;
- }
- if (!functionalReplace && js_is_standard_regexp(ctx, rx)) {
- /* use faster version for simple cases */
- res = js_regexp_replace(ctx, rx, str, rep_val);
- if (!JS_IsUndefined(res))
- goto done;
- }
-
- flags = JS_GetProperty(ctx, rx, JS_ATOM_flags);
- if (JS_IsException(flags))
- goto exception;
- flags = JS_ToStringFree(ctx, flags);
- if (JS_IsException(flags))
- goto exception;
- p = JS_VALUE_GET_STRING(flags);
- fullUnicode = 0;
- is_global = (-1 != string_indexof_char(p, 'g', 0));
- if (is_global) {
- fullUnicode = (string_indexof_char(p, 'u', 0) >= 0 ||
- string_indexof_char(p, 'v', 0) >= 0);
- if (JS_SetProperty(ctx, rx, JS_ATOM_lastIndex, JS_NewInt32(ctx, 0)) < 0)
- goto exception;
- }
- for(;;) {
- JSValue result;
- result = JS_RegExpExec(ctx, rx, str);
- if (JS_IsException(result))
- goto exception;
- if (JS_IsNull(result))
- break;
- if (value_buffer_append(results, result) < 0)
- goto exception;
- if (!is_global)
- break;
- JS_FreeValue(ctx, matched);
- matched = JS_ToStringFree(ctx, JS_GetPropertyInt64(ctx, result, 0));
- if (JS_IsException(matched))
- goto exception;
- if (JS_IsEmptyString(matched)) {
- /* always advance of at least one char */
- int64_t thisIndex, nextIndex;
- if (JS_ToLengthFree(ctx, &thisIndex, JS_GetProperty(ctx, rx, JS_ATOM_lastIndex)) < 0)
- goto exception;
- nextIndex = string_advance_index(sp, thisIndex, fullUnicode);
- if (JS_SetProperty(ctx, rx, JS_ATOM_lastIndex, JS_NewInt64(ctx, nextIndex)) < 0)
- goto exception;
- }
- }
- nextSourcePosition = 0;
- for(j = 0; j < results->len; j++) {
- JSValueConst result;
- result = results->arr[j];
- if (js_get_length32(ctx, &nCaptures, result) < 0)
- goto exception;
- JS_FreeValue(ctx, matched);
- matched = JS_ToStringFree(ctx, JS_GetPropertyInt64(ctx, result, 0));
- if (JS_IsException(matched))
- goto exception;
- if (JS_ToLengthFree(ctx, &position, JS_GetProperty(ctx, result, JS_ATOM_index)))
- goto exception;
- if (position > sp->len)
- position = sp->len;
- else if (position < 0)
- position = 0;
- /* ignore substition if going backward (can happen
- with custom regexp object) */
- JS_FreeValue(ctx, tab);
- tab = JS_NewArray(ctx);
- if (JS_IsException(tab))
- goto exception;
- if (JS_DefinePropertyValueInt64(ctx, tab, 0, JS_DupValue(ctx, matched),
- JS_PROP_C_W_E | JS_PROP_THROW) < 0)
- goto exception;
- for(n = 1; n < nCaptures; n++) {
- JSValue capN;
- capN = JS_GetPropertyInt64(ctx, result, n);
- if (JS_IsException(capN))
- goto exception;
- if (!JS_IsUndefined(capN)) {
- capN = JS_ToStringFree(ctx, capN);
- if (JS_IsException(capN))
- goto exception;
- }
- if (JS_DefinePropertyValueInt64(ctx, tab, n, capN,
- JS_PROP_C_W_E | JS_PROP_THROW) < 0)
- goto exception;
- }
- JS_FreeValue(ctx, namedCaptures);
- namedCaptures = JS_GetProperty(ctx, result, JS_ATOM_groups);
- if (JS_IsException(namedCaptures))
- goto exception;
- if (functionalReplace) {
- if (JS_DefinePropertyValueInt64(ctx, tab, n++, JS_NewInt32(ctx, position), JS_PROP_C_W_E | JS_PROP_THROW) < 0)
- goto exception;
- if (JS_DefinePropertyValueInt64(ctx, tab, n++, JS_DupValue(ctx, str), JS_PROP_C_W_E | JS_PROP_THROW) < 0)
- goto exception;
- if (!JS_IsUndefined(namedCaptures)) {
- if (JS_DefinePropertyValueInt64(ctx, tab, n++, JS_DupValue(ctx, namedCaptures), JS_PROP_C_W_E | JS_PROP_THROW) < 0)
- goto exception;
- }
- args[0] = JS_UNDEFINED;
- args[1] = tab;
- JS_FreeValue(ctx, rep_str);
- rep_str = JS_ToStringFree(ctx, js_function_apply(ctx, rep, 2, args, 0));
- } else {
- JSValue namedCaptures1;
- StringBuffer b1_s, *b1 = &b1_s;
- int ret;
-
- if (!JS_IsUndefined(namedCaptures)) {
- namedCaptures1 = JS_ToObject(ctx, namedCaptures);
- if (JS_IsException(namedCaptures1))
- goto exception;
- } else {
- namedCaptures1 = JS_UNDEFINED;
- }
- JS_FreeValue(ctx, rep_str);
-
- string_buffer_init(ctx, b1, 0);
- ret = js_string_GetSubstitution(ctx, b1, matched, sp, position,
- tab, namedCaptures1, rep_val,
- NULL, 0);
- rep_str = string_buffer_end(b1);
- JS_FreeValue(ctx, namedCaptures1);
- if (ret)
- goto exception;
- }
- if (JS_IsException(rep_str))
- goto exception;
- if (position >= nextSourcePosition) {
- string_buffer_concat(b, sp, nextSourcePosition, position);
- string_buffer_concat_value(b, rep_str);
- nextSourcePosition = position + JS_VALUE_GET_STRING(matched)->len;
- }
- }
- string_buffer_concat(b, sp, nextSourcePosition, sp->len);
- res = string_buffer_end(b);
- goto done1;
- exception:
- res = JS_EXCEPTION;
- done:
- string_buffer_free(b);
- done1:
- value_buffer_free(results);
- JS_FreeValue(ctx, rep_val);
- JS_FreeValue(ctx, matched);
- JS_FreeValue(ctx, flags);
- JS_FreeValue(ctx, tab);
- JS_FreeValue(ctx, rep_str);
- JS_FreeValue(ctx, namedCaptures);
- JS_FreeValue(ctx, str);
- return res;
- }
- static JSValue js_regexp_Symbol_search(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValueConst rx = this_val;
- JSValue str, previousLastIndex, currentLastIndex, result, index;
- if (!JS_IsObject(rx))
- return JS_ThrowTypeErrorNotAnObject(ctx);
- result = JS_UNDEFINED;
- currentLastIndex = JS_UNDEFINED;
- previousLastIndex = JS_UNDEFINED;
- str = JS_ToString(ctx, argv[0]);
- if (JS_IsException(str))
- goto exception;
- previousLastIndex = JS_GetProperty(ctx, rx, JS_ATOM_lastIndex);
- if (JS_IsException(previousLastIndex))
- goto exception;
- if (!js_same_value(ctx, previousLastIndex, JS_NewInt32(ctx, 0))) {
- if (JS_SetProperty(ctx, rx, JS_ATOM_lastIndex, JS_NewInt32(ctx, 0)) < 0) {
- goto exception;
- }
- }
- result = JS_RegExpExec(ctx, rx, str);
- if (JS_IsException(result))
- goto exception;
- currentLastIndex = JS_GetProperty(ctx, rx, JS_ATOM_lastIndex);
- if (JS_IsException(currentLastIndex))
- goto exception;
- if (js_same_value(ctx, currentLastIndex, previousLastIndex)) {
- JS_FreeValue(ctx, previousLastIndex);
- } else {
- if (JS_SetProperty(ctx, rx, JS_ATOM_lastIndex, previousLastIndex) < 0) {
- previousLastIndex = JS_UNDEFINED;
- goto exception;
- }
- }
- JS_FreeValue(ctx, str);
- JS_FreeValue(ctx, currentLastIndex);
- if (JS_IsNull(result)) {
- return JS_NewInt32(ctx, -1);
- } else {
- index = JS_GetProperty(ctx, result, JS_ATOM_index);
- JS_FreeValue(ctx, result);
- return index;
- }
- exception:
- JS_FreeValue(ctx, result);
- JS_FreeValue(ctx, str);
- JS_FreeValue(ctx, currentLastIndex);
- JS_FreeValue(ctx, previousLastIndex);
- return JS_EXCEPTION;
- }
- static JSValue js_regexp_Symbol_split(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- // [Symbol.split](str, limit)
- JSValueConst rx = this_val;
- JSValueConst args[2];
- JSValue str, ctor, splitter, A, flags, z, sub;
- JSString *strp;
- uint32_t lim, size, p, q;
- int unicodeMatching;
- int64_t lengthA, e, numberOfCaptures, i;
- if (!JS_IsObject(rx))
- return JS_ThrowTypeErrorNotAnObject(ctx);
- ctor = JS_UNDEFINED;
- splitter = JS_UNDEFINED;
- A = JS_UNDEFINED;
- flags = JS_UNDEFINED;
- z = JS_UNDEFINED;
- str = JS_ToString(ctx, argv[0]);
- if (JS_IsException(str))
- goto exception;
- ctor = JS_SpeciesConstructor(ctx, rx, ctx->regexp_ctor);
- if (JS_IsException(ctor))
- goto exception;
- flags = JS_ToStringFree(ctx, JS_GetProperty(ctx, rx, JS_ATOM_flags));
- if (JS_IsException(flags))
- goto exception;
- strp = JS_VALUE_GET_STRING(flags);
- unicodeMatching = (string_indexof_char(strp, 'u', 0) >= 0 ||
- string_indexof_char(strp, 'v', 0) >= 0);
- if (string_indexof_char(strp, 'y', 0) < 0) {
- flags = JS_ConcatString3(ctx, "", flags, "y");
- if (JS_IsException(flags))
- goto exception;
- }
- args[0] = rx;
- args[1] = flags;
- splitter = JS_CallConstructor(ctx, ctor, 2, args);
- if (JS_IsException(splitter))
- goto exception;
- A = JS_NewArray(ctx);
- if (JS_IsException(A))
- goto exception;
- lengthA = 0;
- if (JS_IsUndefined(argv[1])) {
- lim = 0xffffffff;
- } else {
- if (JS_ToUint32(ctx, &lim, argv[1]) < 0)
- goto exception;
- if (lim == 0)
- goto done;
- }
- strp = JS_VALUE_GET_STRING(str);
- p = q = 0;
- size = strp->len;
- if (size == 0) {
- z = JS_RegExpExec(ctx, splitter, str);
- if (JS_IsException(z))
- goto exception;
- if (JS_IsNull(z))
- goto add_tail;
- goto done;
- }
- while (q < size) {
- if (JS_SetProperty(ctx, splitter, JS_ATOM_lastIndex, JS_NewInt32(ctx, q)) < 0)
- goto exception;
- JS_FreeValue(ctx, z);
- z = JS_RegExpExec(ctx, splitter, str);
- if (JS_IsException(z))
- goto exception;
- if (JS_IsNull(z)) {
- q = string_advance_index(strp, q, unicodeMatching);
- } else {
- if (JS_ToLengthFree(ctx, &e, JS_GetProperty(ctx, splitter, JS_ATOM_lastIndex)))
- goto exception;
- if (e > size)
- e = size;
- if (e == p) {
- q = string_advance_index(strp, q, unicodeMatching);
- } else {
- sub = js_sub_string(ctx, strp, p, q);
- if (JS_IsException(sub))
- goto exception;
- if (JS_DefinePropertyValueInt64(ctx, A, lengthA++, sub,
- JS_PROP_C_W_E | JS_PROP_THROW) < 0)
- goto exception;
- if (lengthA == lim)
- goto done;
- p = e;
- if (js_get_length64(ctx, &numberOfCaptures, z))
- goto exception;
- for(i = 1; i < numberOfCaptures; i++) {
- sub = JS_GetPropertyInt64(ctx, z, i);
- if (JS_IsException(sub))
- goto exception;
- if (JS_DefinePropertyValueInt64(ctx, A, lengthA++, sub, JS_PROP_C_W_E | JS_PROP_THROW) < 0)
- goto exception;
- if (lengthA == lim)
- goto done;
- }
- q = p;
- }
- }
- }
- add_tail:
- if (p > size)
- p = size;
- sub = js_sub_string(ctx, strp, p, size);
- if (JS_IsException(sub))
- goto exception;
- if (JS_DefinePropertyValueInt64(ctx, A, lengthA++, sub, JS_PROP_C_W_E | JS_PROP_THROW) < 0)
- goto exception;
- goto done;
- exception:
- JS_FreeValue(ctx, A);
- A = JS_EXCEPTION;
- done:
- JS_FreeValue(ctx, str);
- JS_FreeValue(ctx, ctor);
- JS_FreeValue(ctx, splitter);
- JS_FreeValue(ctx, flags);
- JS_FreeValue(ctx, z);
- return A;
- }
- static const JSCFunctionListEntry js_regexp_funcs[] = {
- JS_CFUNC_DEF("escape", 1, js_regexp_escape ),
- JS_CGETSET_DEF("[Symbol.species]", js_get_this, NULL ),
- };
- static const JSCFunctionListEntry js_regexp_proto_funcs[] = {
- JS_CGETSET_DEF("flags", js_regexp_get_flags, NULL ),
- JS_CGETSET_DEF("source", js_regexp_get_source, NULL ),
- JS_CGETSET_MAGIC_DEF("global", js_regexp_get_flag, NULL, LRE_FLAG_GLOBAL ),
- JS_CGETSET_MAGIC_DEF("ignoreCase", js_regexp_get_flag, NULL, LRE_FLAG_IGNORECASE ),
- JS_CGETSET_MAGIC_DEF("multiline", js_regexp_get_flag, NULL, LRE_FLAG_MULTILINE ),
- JS_CGETSET_MAGIC_DEF("dotAll", js_regexp_get_flag, NULL, LRE_FLAG_DOTALL ),
- JS_CGETSET_MAGIC_DEF("unicode", js_regexp_get_flag, NULL, LRE_FLAG_UNICODE ),
- JS_CGETSET_MAGIC_DEF("unicodeSets", js_regexp_get_flag, NULL, LRE_FLAG_UNICODE_SETS ),
- JS_CGETSET_MAGIC_DEF("sticky", js_regexp_get_flag, NULL, LRE_FLAG_STICKY ),
- JS_CGETSET_MAGIC_DEF("hasIndices", js_regexp_get_flag, NULL, LRE_FLAG_INDICES ),
- JS_CFUNC_DEF("exec", 1, js_regexp_exec ),
- JS_CFUNC_DEF("compile", 2, js_regexp_compile ),
- JS_CFUNC_DEF("test", 1, js_regexp_test ),
- JS_CFUNC_DEF("toString", 0, js_regexp_toString ),
- JS_CFUNC_DEF("[Symbol.replace]", 2, js_regexp_Symbol_replace ),
- JS_CFUNC_DEF("[Symbol.match]", 1, js_regexp_Symbol_match ),
- JS_CFUNC_DEF("[Symbol.matchAll]", 1, js_regexp_Symbol_matchAll ),
- JS_CFUNC_DEF("[Symbol.search]", 1, js_regexp_Symbol_search ),
- JS_CFUNC_DEF("[Symbol.split]", 2, js_regexp_Symbol_split ),
- };
- static const JSCFunctionListEntry js_regexp_string_iterator_proto_funcs[] = {
- JS_ITERATOR_NEXT_DEF("next", 0, js_regexp_string_iterator_next, 0 ),
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "RegExp String Iterator", JS_PROP_CONFIGURABLE ),
- };
- void JS_AddIntrinsicRegExpCompiler(JSContext *ctx)
- {
- ctx->compile_regexp = js_compile_regexp;
- }
- int JS_AddIntrinsicRegExp(JSContext *ctx)
- {
- JSValue obj;
- JS_AddIntrinsicRegExpCompiler(ctx);
- obj = JS_NewCConstructor(ctx, JS_CLASS_REGEXP, "RegExp",
- js_regexp_constructor, 2, JS_CFUNC_constructor_or_func, 0,
- JS_UNDEFINED,
- js_regexp_funcs, countof(js_regexp_funcs),
- js_regexp_proto_funcs, countof(js_regexp_proto_funcs),
- 0);
- if (JS_IsException(obj))
- return -1;
- ctx->regexp_ctor = obj;
-
- ctx->class_proto[JS_CLASS_REGEXP_STRING_ITERATOR] =
- JS_NewObjectProtoList(ctx, ctx->class_proto[JS_CLASS_ITERATOR],
- js_regexp_string_iterator_proto_funcs,
- countof(js_regexp_string_iterator_proto_funcs));
- if (JS_IsException(ctx->class_proto[JS_CLASS_REGEXP_STRING_ITERATOR]))
- return -1;
- ctx->regexp_shape = js_new_shape2(ctx, get_proto_obj(ctx->class_proto[JS_CLASS_REGEXP]),
- JS_PROP_INITIAL_HASH_SIZE, 1);
- if (!ctx->regexp_shape)
- return -1;
- if (add_shape_property(ctx, &ctx->regexp_shape, NULL,
- JS_ATOM_lastIndex, JS_PROP_WRITABLE))
- return -1;
- ctx->regexp_result_shape = js_new_shape2(ctx, get_proto_obj(ctx->class_proto[JS_CLASS_ARRAY]),
- JS_PROP_INITIAL_HASH_SIZE, 4);
- if (!ctx->regexp_result_shape)
- return -1;
- if (add_shape_property(ctx, &ctx->regexp_result_shape, NULL,
- JS_ATOM_length, JS_PROP_WRITABLE | JS_PROP_LENGTH))
- return -1;
- if (add_shape_property(ctx, &ctx->regexp_result_shape, NULL,
- JS_ATOM_index, JS_PROP_C_W_E))
- return -1;
- if (add_shape_property(ctx, &ctx->regexp_result_shape, NULL,
- JS_ATOM_input, JS_PROP_C_W_E))
- return -1;
- if (add_shape_property(ctx, &ctx->regexp_result_shape, NULL,
- JS_ATOM_groups, JS_PROP_C_W_E))
- return -1;
- return 0;
- }
- /* JSON */
- static int json_parse_expect(JSParseState *s, int tok)
- {
- if (s->token.val != tok) {
- /* XXX: dump token correctly in all cases */
- return js_parse_error(s, "expecting '%c'", tok);
- }
- return json_next_token(s);
- }
- typedef struct {
- int count;
- uint32_t hash_size;
- struct JSONParseRecordEntry *entries;
- uint32_t *hash_table;
- } JSONParseRecordObject;
-
- typedef struct JSONParseRecord {
- JSValue value;
- union {
- JSONParseRecordObject obj;
- struct {
- int count;
- struct JSONParseRecord *elements;
- } array;
- struct {
- uint32_t source_pos;
- uint32_t source_len;
- } primitive;
- } u;
- } JSONParseRecord;
- typedef struct JSONParseRecordEntry {
- JSAtom atom;
- uint32_t hash_next;
- JSONParseRecord parse_record;
- } JSONParseRecordEntry;
- static void json_parse_record_init_obj(JSContext *ctx, JSONParseRecord *pr, JSValueConst val)
- {
- pr->value = JS_DupValue(ctx, val);
- pr->u.obj.count = 0;
- pr->u.obj.entries = NULL;
- pr->u.obj.hash_table = NULL;
- pr->u.obj.hash_size = 0;
- }
- static void json_parse_record_init_array(JSContext *ctx, JSONParseRecord *pr, JSValueConst val)
- {
- pr->value = JS_DupValue(ctx, val);
- pr->u.array.count = 0;
- pr->u.array.elements = NULL;
- }
- static void json_parse_record_init_primitive(JSContext *ctx, JSONParseRecord *pr, JSValueConst val,
- uint32_t source_pos, uint32_t source_len)
- {
- pr->value = JS_DupValue(ctx, val);
- pr->u.primitive.source_pos = source_pos;
- pr->u.primitive.source_len = source_len;
- }
- static int json_parse_record_resize_hash(JSContext *ctx, JSONParseRecordObject *po, uint32_t new_hash_size)
- {
- uint32_t i, h, *new_hash_table;
- JSONParseRecordEntry *e;
- new_hash_table = js_malloc(ctx, sizeof(new_hash_table[0]) * new_hash_size);
- if (!new_hash_table)
- return -1;
- js_free(ctx, po->hash_table);
- po->hash_table = new_hash_table;
- po->hash_size = new_hash_size;
- for(i = 0; i < po->hash_size; i++) {
- po->hash_table[i] = -1;
- }
- for(i = 0; i < po->count; i++) {
- e = &po->entries[i];
- h = e->atom & (po->hash_size - 1);
- e->hash_next = po->hash_table[h];
- po->hash_table[h] = i;
- }
- return 0;
- }
- static JSONParseRecord *json_parse_record_add(JSContext *ctx, JSONParseRecord *pr, JSAtom key, int *psize)
- {
- JSONParseRecordObject *po = &pr->u.obj;
- JSONParseRecordEntry *e;
- JSONParseRecord *pr1;
- uint32_t h;
-
- if (js_resize_array(ctx, (void **)&po->entries, sizeof(po->entries[0]),
- psize, po->count + 1)) {
- return NULL;
- }
- /* don't use a hash table when the number of entries is small */
- if (po->count >= 8 && (po->count + 1) > po->hash_size) {
- int hash_bits = 32 - clz32(po->count);
- if (json_parse_record_resize_hash(ctx, po, 1 << hash_bits))
- return NULL;
- }
- e = &po->entries[po->count++];
- e->atom = JS_DupAtom(ctx, key);
- pr1 = &e->parse_record;
- pr1->value = JS_UNDEFINED;
- if (po->hash_size != 0) {
- h = key & (po->hash_size - 1);
- e->hash_next = po->hash_table[h];
- po->hash_table[h] = po->count - 1;
- }
- return pr1;
- }
- static JSONParseRecord *json_parse_record_find(JSONParseRecord *pr, JSAtom key)
- {
- JSONParseRecordObject *po = &pr->u.obj;
- JSONParseRecordEntry *e;
- uint32_t h, i;
-
- if (po->hash_size == 0) {
- for(i = 0; i < po->count; i++) {
- if (po->entries[i].atom == key)
- return &po->entries[i].parse_record;
- }
- } else {
- h = key & (po->hash_size - 1);
- i = po->hash_table[h];
- while (i != -1) {
- e = &po->entries[i];
- if (e->atom == key)
- return &e->parse_record;
- i = e->hash_next;
- }
- }
- return NULL;
- }
- static void json_free_parse_record(JSContext *ctx, JSONParseRecord *pr)
- {
- int i;
- if (!pr)
- return;
- if (JS_IsObject(pr->value)) {
- if (JS_IsArray(ctx, pr->value)) {
- for(i = 0; i < pr->u.array.count; i++) {
- json_free_parse_record(ctx, &pr->u.array.elements[i]);
- }
- js_free(ctx, pr->u.array.elements);
- } else {
- for(i = 0; i < pr->u.obj.count; i++) {
- JS_FreeAtom(ctx, pr->u.obj.entries[i].atom);
- json_free_parse_record(ctx, &pr->u.obj.entries[i].parse_record);
- }
- js_free(ctx, pr->u.obj.entries);
- js_free(ctx, pr->u.obj.hash_table);
- }
- }
- JS_FreeValue(ctx, pr->value);
- pr->value = JS_UNDEFINED; /* fail safe */
- }
- /* 'pr' can be NULL */
- static JSValue json_parse_value(JSParseState *s, JSONParseRecord *pr)
- {
- JSContext *ctx = s->ctx;
- JSValue val = JS_NULL;
- int ret;
- if (pr) {
- pr->value = JS_UNDEFINED;
- }
-
- switch(s->token.val) {
- case '{':
- {
- JSValue prop_val;
- JSAtom prop_name;
- JSONParseRecord *pr1;
- int pr_size;
-
- if (json_next_token(s))
- goto fail;
- val = JS_NewObject(ctx);
- if (JS_IsException(val))
- goto fail;
- if (pr) {
- json_parse_record_init_obj(ctx, pr, val);
- pr_size = 0;
- }
- if (s->token.val != '}') {
- for(;;) {
- if (s->token.val == TOK_STRING) {
- prop_name = JS_ValueToAtom(ctx, s->token.u.str.str);
- if (prop_name == JS_ATOM_NULL)
- goto fail;
- } else if (s->ext_json && s->token.val == TOK_IDENT) {
- prop_name = JS_DupAtom(ctx, s->token.u.ident.atom);
- } else {
- js_parse_error(s, "expecting property name");
- goto fail;
- }
- if (json_next_token(s))
- goto fail1;
- if (json_parse_expect(s, ':'))
- goto fail1;
- if (pr) {
- pr1 = json_parse_record_add(ctx, pr, prop_name, &pr_size);
- if (!pr1)
- goto fail1;
- } else {
- pr1 = NULL;
- }
- prop_val = json_parse_value(s, pr1);
- if (JS_IsException(prop_val)) {
- fail1:
- JS_FreeAtom(ctx, prop_name);
- goto fail;
- }
- ret = JS_DefinePropertyValue(ctx, val, prop_name,
- prop_val, JS_PROP_C_W_E);
- JS_FreeAtom(ctx, prop_name);
- if (ret < 0)
- goto fail;
- if (s->token.val != ',')
- break;
- if (json_next_token(s))
- goto fail;
- if (s->ext_json && s->token.val == '}')
- break;
- }
- }
- if (json_parse_expect(s, '}'))
- goto fail;
- }
- break;
- case '[':
- {
- JSValue el;
- uint32_t idx;
- JSONParseRecord *pr1;
- int pr_size;
-
- if (json_next_token(s))
- goto fail;
- val = JS_NewArray(ctx);
- if (JS_IsException(val))
- goto fail;
- if (pr) {
- json_parse_record_init_array(ctx, pr, val);
- pr_size = 0;
- }
- if (s->token.val != ']') {
- idx = 0;
- for(;;) {
- if (pr) {
- if (js_resize_array(ctx, (void **)&pr->u.array.elements, sizeof(pr->u.array.elements[0]),
- &pr_size, pr->u.array.count + 1))
- goto fail;
- pr1 = &pr->u.array.elements[pr->u.array.count++];
- pr1->value = JS_UNDEFINED;
- } else {
- pr1 = NULL;
- }
- el = json_parse_value(s, pr1);
- if (JS_IsException(el))
- goto fail;
- ret = JS_DefinePropertyValueUint32(ctx, val, idx, el, JS_PROP_C_W_E);
- if (ret < 0)
- goto fail;
- if (s->token.val != ',')
- break;
- if (json_next_token(s))
- goto fail;
- idx++;
- if (s->ext_json && s->token.val == ']')
- break;
- }
- }
- if (json_parse_expect(s, ']'))
- goto fail;
- }
- break;
- case TOK_STRING:
- val = JS_DupValue(ctx, s->token.u.str.str);
- if (pr) {
- json_parse_record_init_primitive(ctx, pr, val,
- s->token.ptr - s->buf_start,
- s->buf_ptr - s->token.ptr);
- }
- if (json_next_token(s))
- goto fail;
- break;
- case TOK_NUMBER:
- val = s->token.u.num.val;
- if (pr) {
- json_parse_record_init_primitive(ctx, pr, val,
- s->token.ptr - s->buf_start,
- s->buf_ptr - s->token.ptr);
- }
- if (json_next_token(s))
- goto fail;
- break;
- case TOK_IDENT:
- if (s->token.u.ident.atom == JS_ATOM_false ||
- s->token.u.ident.atom == JS_ATOM_true) {
- val = JS_NewBool(ctx, s->token.u.ident.atom == JS_ATOM_true);
- if (pr) {
- json_parse_record_init_primitive(ctx, pr, val,
- s->token.ptr - s->buf_start,
- s->buf_ptr - s->token.ptr);
- }
- } else if (s->token.u.ident.atom == JS_ATOM_null) {
- val = JS_NULL;
- if (pr) {
- json_parse_record_init_primitive(ctx, pr, val,
- s->token.ptr - s->buf_start,
- s->buf_ptr - s->token.ptr);
- }
- } else if (s->token.u.ident.atom == JS_ATOM_NaN && s->ext_json) {
- /* Note: json5 identifier handling is ambiguous e.g. is
- '{ NaN: 1 }' a valid JSON5 production ? */
- val = JS_NewFloat64(s->ctx, NAN);
- } else if (s->token.u.ident.atom == JS_ATOM_Infinity && s->ext_json) {
- val = JS_NewFloat64(s->ctx, INFINITY);
- } else {
- goto def_token;
- }
- if (json_next_token(s))
- goto fail;
- break;
- default:
- def_token:
- if (s->token.val == TOK_EOF) {
- js_parse_error(s, "Unexpected end of JSON input");
- } else {
- js_parse_error(s, "unexpected token: '%.*s'",
- (int)(s->buf_ptr - s->token.ptr), s->token.ptr);
- }
- goto fail;
- }
- return val;
- fail:
- json_free_parse_record(ctx, pr);
- JS_FreeValue(ctx, val);
- return JS_EXCEPTION;
- }
- JSValue JS_ParseJSON3(JSContext *ctx, const char *buf, size_t buf_len,
- const char *filename, int flags, JSONParseRecord *pr)
- {
- JSParseState s1, *s = &s1;
- JSValue val = JS_UNDEFINED;
- js_parse_init(ctx, s, buf, buf_len, filename);
- s->ext_json = ((flags & JS_PARSE_JSON_EXT) != 0);
- if (json_next_token(s))
- goto fail;
- val = json_parse_value(s, pr);
- if (JS_IsException(val))
- goto fail;
- if (s->token.val != TOK_EOF) {
- if (js_parse_error(s, "unexpected data at the end")) {
- json_free_parse_record(ctx, pr);
- goto fail;
- }
- }
- return val;
- fail:
- JS_FreeValue(ctx, val);
- free_token(s, &s->token);
- return JS_EXCEPTION;
- }
- JSValue JS_ParseJSON2(JSContext *ctx, const char *buf, size_t buf_len,
- const char *filename, int flags)
- {
- return JS_ParseJSON3(ctx, buf, buf_len, filename, flags, NULL);
- }
- JSValue JS_ParseJSON(JSContext *ctx, const char *buf, size_t buf_len,
- const char *filename)
- {
- return JS_ParseJSON3(ctx, buf, buf_len, filename, 0, NULL);
- }
- /* if pr != NULL, then pr->value = holder by construction */
- static JSValue internalize_json_property(JSContext *ctx, JSValueConst holder,
- JSAtom name, JSValueConst reviver,
- const char *text_str, JSONParseRecord *pr)
- {
- JSValue val, new_el, name_val, res, context;
- JSValueConst args[3];
- int ret, is_array;
- uint32_t i, len = 0;
- JSAtom prop;
- JSPropertyEnum *atoms = NULL;
- if (js_check_stack_overflow(ctx->rt, 0)) {
- return JS_ThrowStackOverflow(ctx);
- }
- val = JS_GetProperty(ctx, holder, name);
- if (JS_IsException(val))
- return val;
- if (pr) {
- if (JS_IsArray(ctx, pr->value)) {
- if (__JS_AtomIsTaggedInt(name)) {
- uint32_t idx = __JS_AtomToUInt32(name);
- if (idx < pr->u.array.count) {
- pr = &pr->u.array.elements[idx];
- } else {
- pr = NULL;
- }
- }
- } else {
- pr = json_parse_record_find(pr, name);
- }
- if (pr && !js_same_value(ctx, pr->value, val)) {
- pr = NULL;
- }
- }
- context = JS_NewObject(ctx);
- if (JS_IsException(context))
- goto fail;
-
- if (JS_IsObject(val)) {
- is_array = JS_IsArray(ctx, val);
- if (is_array < 0)
- goto fail;
- if (is_array) {
- if (js_get_length32(ctx, &len, val))
- goto fail;
- } else {
- ret = JS_GetOwnPropertyNamesInternal(ctx, &atoms, &len, JS_VALUE_GET_OBJ(val), JS_GPN_ENUM_ONLY | JS_GPN_STRING_MASK);
- if (ret < 0)
- goto fail;
- }
- for(i = 0; i < len; i++) {
- if (is_array) {
- prop = JS_NewAtomUInt32(ctx, i);
- if (prop == JS_ATOM_NULL)
- goto fail;
- } else {
- prop = JS_DupAtom(ctx, atoms[i].atom);
- }
- new_el = internalize_json_property(ctx, val, prop, reviver, text_str, pr);
- if (JS_IsException(new_el)) {
- JS_FreeAtom(ctx, prop);
- goto fail;
- }
- if (JS_IsUndefined(new_el)) {
- ret = JS_DeleteProperty(ctx, val, prop, 0);
- } else {
- ret = JS_DefinePropertyValue(ctx, val, prop, new_el, JS_PROP_C_W_E);
- }
- JS_FreeAtom(ctx, prop);
- if (ret < 0)
- goto fail;
- }
- } else {
- if (pr) {
- new_el = JS_NewStringLen(ctx, text_str + pr->u.primitive.source_pos,
- pr->u.primitive.source_len);
- if (JS_IsException(new_el))
- goto fail;
- if (JS_DefinePropertyValue(ctx, context, JS_ATOM_source, new_el, JS_PROP_C_W_E) < 0)
- goto fail;
- }
- }
- JS_FreePropertyEnum(ctx, atoms, len);
- atoms = NULL;
- name_val = JS_AtomToValue(ctx, name);
- if (JS_IsException(name_val))
- goto fail;
- args[0] = name_val;
- args[1] = val;
- args[2] = context;
- res = JS_Call(ctx, reviver, holder, 3, args);
- JS_FreeValue(ctx, name_val);
- JS_FreeValue(ctx, val);
- JS_FreeValue(ctx, context);
- return res;
- fail:
- JS_FreePropertyEnum(ctx, atoms, len);
- JS_FreeValue(ctx, context);
- JS_FreeValue(ctx, val);
- return JS_EXCEPTION;
- }
- static JSValue js_json_parse(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue obj;
- const char *str;
- size_t len;
-
- str = JS_ToCStringLen(ctx, &len, argv[0]);
- if (!str)
- return JS_EXCEPTION;
- if (argc > 1 && JS_IsFunction(ctx, argv[1])) {
- JSONParseRecord pr_s, *pr = &pr_s, *pr1;
- JSValue root;
- JSValueConst reviver;
- int size;
-
- reviver = argv[1];
- root = JS_NewObject(ctx);
- if (JS_IsException(root))
- goto fail;
- json_parse_record_init_obj(ctx, pr, root);
- size = 0;
- pr1 = json_parse_record_add(ctx, pr, JS_ATOM_empty_string, &size);
- if (!pr1)
- goto fail1;
- obj = JS_ParseJSON3(ctx, str, len, "<input>", 0, pr1);
- if (JS_IsException(obj))
- goto fail1;
-
- if (JS_DefinePropertyValue(ctx, root, JS_ATOM_empty_string, obj,
- JS_PROP_C_W_E) < 0) {
- JS_FreeValue(ctx, obj);
- fail1:
- json_free_parse_record(ctx, pr);
- JS_FreeValue(ctx, root);
- goto fail;
- }
-
- obj = internalize_json_property(ctx, root, JS_ATOM_empty_string,
- reviver, str, pr);
- json_free_parse_record(ctx, pr);
- JS_FreeValue(ctx, root);
- } else {
- obj = JS_ParseJSON3(ctx, str, len, "<input>", 0, NULL);
- }
- JS_FreeCString(ctx, str);
- return obj;
- fail:
- JS_FreeCString(ctx, str);
- return JS_EXCEPTION;
- }
- static JSValue js_json_isRawJSON(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValueConst obj = argv[0];
- if (JS_VALUE_GET_TAG(obj) == JS_TAG_OBJECT) {
- JSObject *p = JS_VALUE_GET_OBJ(obj);
- return JS_NewBool(ctx, p->class_id == JS_CLASS_RAWJSON);
- } else {
- return JS_FALSE;
- }
- }
- static BOOL is_valid_raw_json_char(int c)
- {
- return ((c >= 'a' && c <= 'z') ||
- (c >= '0' && c <= '9') ||
- c == '-' ||
- c == '"');
- }
- static JSValue js_json_rawJSON(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue str, res, obj;
- JSString *p;
- str = JS_ToString(ctx, argv[0]);
- if (JS_IsException(str))
- return str;
- p = JS_VALUE_GET_STRING(str);
- if (p->len == 0 ||
- !is_valid_raw_json_char(string_get(p, 0)) ||
- !is_valid_raw_json_char(string_get(p, p->len - 1))) {
- goto syntax_error;
- }
- res = js_json_parse(ctx, JS_UNDEFINED, 1, (JSValueConst *)&str);
- if (JS_IsException(res)) {
- syntax_error:
- JS_ThrowSyntaxError(ctx, "invalid rawJSON string");
- goto fail;
- }
- JS_FreeValue(ctx, res);
-
- obj = JS_NewObjectProtoClass(ctx, JS_NULL, JS_CLASS_RAWJSON);
- if (JS_IsException(obj))
- goto fail;
- if (JS_DefinePropertyValue(ctx, obj, JS_ATOM_rawJSON, str, JS_PROP_ENUMERABLE) < 0) {
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- JS_PreventExtensions(ctx, obj);
- return obj;
- fail:
- JS_FreeValue(ctx, str);
- return JS_EXCEPTION;
- }
- typedef struct JSONStringifyContext {
- JSValueConst replacer_func;
- JSValue stack;
- JSValue property_list;
- JSValue gap;
- JSValue empty;
- StringBuffer *b;
- } JSONStringifyContext;
- static int JS_ToQuotedString(JSContext *ctx, StringBuffer *b, JSValueConst val1)
- {
- JSValue val;
- JSString *p;
- int i;
- uint32_t c;
- char buf[16];
- val = JS_ToStringCheckObject(ctx, val1);
- if (JS_IsException(val))
- return -1;
- p = JS_VALUE_GET_STRING(val);
- if (string_buffer_putc8(b, '\"'))
- goto fail;
- for(i = 0; i < p->len; ) {
- c = string_getc(p, &i);
- switch(c) {
- case '\t':
- c = 't';
- goto quote;
- case '\r':
- c = 'r';
- goto quote;
- case '\n':
- c = 'n';
- goto quote;
- case '\b':
- c = 'b';
- goto quote;
- case '\f':
- c = 'f';
- goto quote;
- case '\"':
- case '\\':
- quote:
- if (string_buffer_putc8(b, '\\'))
- goto fail;
- if (string_buffer_putc8(b, c))
- goto fail;
- break;
- default:
- if (c < 32 || is_surrogate(c)) {
- snprintf(buf, sizeof(buf), "\\u%04x", c);
- if (string_buffer_puts8(b, buf))
- goto fail;
- } else {
- if (string_buffer_putc(b, c))
- goto fail;
- }
- break;
- }
- }
- if (string_buffer_putc8(b, '\"'))
- goto fail;
- JS_FreeValue(ctx, val);
- return 0;
- fail:
- JS_FreeValue(ctx, val);
- return -1;
- }
- static int JS_ToQuotedStringFree(JSContext *ctx, StringBuffer *b, JSValue val) {
- int ret = JS_ToQuotedString(ctx, b, val);
- JS_FreeValue(ctx, val);
- return ret;
- }
- static JSValue js_json_check(JSContext *ctx, JSONStringifyContext *jsc,
- JSValueConst holder, JSValue val, JSValueConst key)
- {
- JSValue v;
- JSValueConst args[2];
- /* check for object.toJSON method */
- /* ECMA specifies this is done only for Object and BigInt */
- if (JS_IsObject(val) || JS_IsBigInt(ctx, val)) {
- JSValue f = JS_GetProperty(ctx, val, JS_ATOM_toJSON);
- if (JS_IsException(f))
- goto exception;
- if (JS_IsFunction(ctx, f)) {
- v = JS_CallFree(ctx, f, val, 1, &key);
- JS_FreeValue(ctx, val);
- val = v;
- if (JS_IsException(val))
- goto exception;
- } else {
- JS_FreeValue(ctx, f);
- }
- }
- if (!JS_IsUndefined(jsc->replacer_func)) {
- args[0] = key;
- args[1] = val;
- v = JS_Call(ctx, jsc->replacer_func, holder, 2, args);
- JS_FreeValue(ctx, val);
- val = v;
- if (JS_IsException(val))
- goto exception;
- }
- switch (JS_VALUE_GET_NORM_TAG(val)) {
- case JS_TAG_OBJECT:
- if (JS_IsFunction(ctx, val))
- break;
- case JS_TAG_STRING:
- case JS_TAG_STRING_ROPE:
- case JS_TAG_INT:
- case JS_TAG_FLOAT64:
- case JS_TAG_BOOL:
- case JS_TAG_NULL:
- case JS_TAG_SHORT_BIG_INT:
- case JS_TAG_BIG_INT:
- case JS_TAG_EXCEPTION:
- return val;
- default:
- break;
- }
- JS_FreeValue(ctx, val);
- return JS_UNDEFINED;
- exception:
- JS_FreeValue(ctx, val);
- return JS_EXCEPTION;
- }
- static int js_json_to_str(JSContext *ctx, JSONStringifyContext *jsc,
- JSValueConst holder, JSValue val,
- JSValueConst indent)
- {
- JSValue indent1, sep, sep1, tab, v, prop;
- JSObject *p;
- int64_t i, len;
- int cl, ret;
- BOOL has_content;
- indent1 = JS_UNDEFINED;
- sep = JS_UNDEFINED;
- sep1 = JS_UNDEFINED;
- tab = JS_UNDEFINED;
- prop = JS_UNDEFINED;
- if (js_check_stack_overflow(ctx->rt, 0)) {
- JS_ThrowStackOverflow(ctx);
- goto exception;
- }
- if (JS_IsObject(val)) {
- p = JS_VALUE_GET_OBJ(val);
- cl = p->class_id;
- if (cl == JS_CLASS_STRING) {
- val = JS_ToStringFree(ctx, val);
- if (JS_IsException(val))
- goto exception;
- goto concat_primitive;
- } else if (cl == JS_CLASS_NUMBER) {
- val = JS_ToNumberFree(ctx, val);
- if (JS_IsException(val))
- goto exception;
- goto concat_primitive;
- } else if (cl == JS_CLASS_BOOLEAN || cl == JS_CLASS_BIG_INT) {
- /* This will thow the same error as for the primitive object */
- set_value(ctx, &val, JS_DupValue(ctx, p->u.object_data));
- goto concat_primitive;
- } else if (cl == JS_CLASS_RAWJSON) {
- JSValue val1;
- val1 = JS_GetProperty(ctx, val, JS_ATOM_rawJSON);
- if (JS_IsException(val1))
- goto exception;
- JS_FreeValue(ctx, val);
- val = val1;
- goto concat_value;
- }
- v = js_array_includes(ctx, jsc->stack, 1, (JSValueConst *)&val);
- if (JS_IsException(v))
- goto exception;
- if (JS_ToBoolFree(ctx, v)) {
- JS_ThrowTypeError(ctx, "circular reference");
- goto exception;
- }
- indent1 = JS_ConcatString(ctx, JS_DupValue(ctx, indent), JS_DupValue(ctx, jsc->gap));
- if (JS_IsException(indent1))
- goto exception;
- if (!JS_IsEmptyString(jsc->gap)) {
- sep = JS_ConcatString3(ctx, "\n", JS_DupValue(ctx, indent1), "");
- if (JS_IsException(sep))
- goto exception;
- sep1 = js_new_string8(ctx, " ");
- if (JS_IsException(sep1))
- goto exception;
- } else {
- sep = JS_DupValue(ctx, jsc->empty);
- sep1 = JS_DupValue(ctx, jsc->empty);
- }
- v = js_array_push(ctx, jsc->stack, 1, (JSValueConst *)&val, 0);
- if (check_exception_free(ctx, v))
- goto exception;
- ret = JS_IsArray(ctx, val);
- if (ret < 0)
- goto exception;
- if (ret) {
- if (js_get_length64(ctx, &len, val))
- goto exception;
- string_buffer_putc8(jsc->b, '[');
- for(i = 0; i < len; i++) {
- if (i > 0)
- string_buffer_putc8(jsc->b, ',');
- string_buffer_concat_value(jsc->b, sep);
- v = JS_GetPropertyInt64(ctx, val, i);
- if (JS_IsException(v))
- goto exception;
- /* XXX: could do this string conversion only when needed */
- prop = JS_ToStringFree(ctx, JS_NewInt64(ctx, i));
- if (JS_IsException(prop))
- goto exception;
- v = js_json_check(ctx, jsc, val, v, prop);
- JS_FreeValue(ctx, prop);
- prop = JS_UNDEFINED;
- if (JS_IsException(v))
- goto exception;
- if (JS_IsUndefined(v))
- v = JS_NULL;
- if (js_json_to_str(ctx, jsc, val, v, indent1))
- goto exception;
- }
- if (len > 0 && !JS_IsEmptyString(jsc->gap)) {
- string_buffer_putc8(jsc->b, '\n');
- string_buffer_concat_value(jsc->b, indent);
- }
- string_buffer_putc8(jsc->b, ']');
- } else {
- if (!JS_IsUndefined(jsc->property_list))
- tab = JS_DupValue(ctx, jsc->property_list);
- else
- tab = js_object_keys(ctx, JS_UNDEFINED, 1, (JSValueConst *)&val, JS_ITERATOR_KIND_KEY);
- if (JS_IsException(tab))
- goto exception;
- if (js_get_length64(ctx, &len, tab))
- goto exception;
- string_buffer_putc8(jsc->b, '{');
- has_content = FALSE;
- for(i = 0; i < len; i++) {
- JS_FreeValue(ctx, prop);
- prop = JS_GetPropertyInt64(ctx, tab, i);
- if (JS_IsException(prop))
- goto exception;
- v = JS_GetPropertyValue(ctx, val, JS_DupValue(ctx, prop));
- if (JS_IsException(v))
- goto exception;
- v = js_json_check(ctx, jsc, val, v, prop);
- if (JS_IsException(v))
- goto exception;
- if (!JS_IsUndefined(v)) {
- if (has_content)
- string_buffer_putc8(jsc->b, ',');
- string_buffer_concat_value(jsc->b, sep);
- if (JS_ToQuotedString(ctx, jsc->b, prop)) {
- JS_FreeValue(ctx, v);
- goto exception;
- }
- string_buffer_putc8(jsc->b, ':');
- string_buffer_concat_value(jsc->b, sep1);
- if (js_json_to_str(ctx, jsc, val, v, indent1))
- goto exception;
- has_content = TRUE;
- }
- }
- if (has_content && !JS_IsEmptyString(jsc->gap)) {
- string_buffer_putc8(jsc->b, '\n');
- string_buffer_concat_value(jsc->b, indent);
- }
- string_buffer_putc8(jsc->b, '}');
- }
- if (check_exception_free(ctx, js_array_pop(ctx, jsc->stack, 0, NULL, 0)))
- goto exception;
- JS_FreeValue(ctx, val);
- JS_FreeValue(ctx, tab);
- JS_FreeValue(ctx, sep);
- JS_FreeValue(ctx, sep1);
- JS_FreeValue(ctx, indent1);
- JS_FreeValue(ctx, prop);
- return 0;
- }
- concat_primitive:
- switch (JS_VALUE_GET_NORM_TAG(val)) {
- case JS_TAG_STRING:
- case JS_TAG_STRING_ROPE:
- return JS_ToQuotedStringFree(ctx, jsc->b, val);
- case JS_TAG_FLOAT64:
- if (!isfinite(JS_VALUE_GET_FLOAT64(val))) {
- val = JS_NULL;
- }
- goto concat_value;
- case JS_TAG_INT:
- case JS_TAG_BOOL:
- case JS_TAG_NULL:
- concat_value:
- return string_buffer_concat_value_free(jsc->b, val);
- case JS_TAG_SHORT_BIG_INT:
- case JS_TAG_BIG_INT:
- /* reject big numbers: use toJSON method to override */
- JS_ThrowTypeError(ctx, "Do not know how to serialize a BigInt");
- goto exception;
- default:
- JS_FreeValue(ctx, val);
- return 0;
- }
- exception:
- JS_FreeValue(ctx, val);
- JS_FreeValue(ctx, tab);
- JS_FreeValue(ctx, sep);
- JS_FreeValue(ctx, sep1);
- JS_FreeValue(ctx, indent1);
- JS_FreeValue(ctx, prop);
- return -1;
- }
- JSValue JS_JSONStringify(JSContext *ctx, JSValueConst obj,
- JSValueConst replacer, JSValueConst space0)
- {
- StringBuffer b_s;
- JSONStringifyContext jsc_s, *jsc = &jsc_s;
- JSValue val, v, space, ret, wrapper;
- int res;
- int64_t i, j, n;
- jsc->replacer_func = JS_UNDEFINED;
- jsc->stack = JS_UNDEFINED;
- jsc->property_list = JS_UNDEFINED;
- jsc->gap = JS_UNDEFINED;
- jsc->b = &b_s;
- jsc->empty = JS_AtomToString(ctx, JS_ATOM_empty_string);
- ret = JS_UNDEFINED;
- wrapper = JS_UNDEFINED;
- string_buffer_init(ctx, jsc->b, 0);
- jsc->stack = JS_NewArray(ctx);
- if (JS_IsException(jsc->stack))
- goto exception;
- if (JS_IsFunction(ctx, replacer)) {
- jsc->replacer_func = replacer;
- } else {
- res = JS_IsArray(ctx, replacer);
- if (res < 0)
- goto exception;
- if (res) {
- /* XXX: enumeration is not fully correct */
- jsc->property_list = JS_NewArray(ctx);
- if (JS_IsException(jsc->property_list))
- goto exception;
- if (js_get_length64(ctx, &n, replacer))
- goto exception;
- for (i = j = 0; i < n; i++) {
- JSValue present;
- v = JS_GetPropertyInt64(ctx, replacer, i);
- if (JS_IsException(v))
- goto exception;
- if (JS_IsObject(v)) {
- JSObject *p = JS_VALUE_GET_OBJ(v);
- if (p->class_id == JS_CLASS_STRING ||
- p->class_id == JS_CLASS_NUMBER) {
- v = JS_ToStringFree(ctx, v);
- if (JS_IsException(v))
- goto exception;
- } else {
- JS_FreeValue(ctx, v);
- continue;
- }
- } else if (JS_IsNumber(v)) {
- v = JS_ToStringFree(ctx, v);
- if (JS_IsException(v))
- goto exception;
- } else if (!JS_IsString(v)) {
- JS_FreeValue(ctx, v);
- continue;
- }
- present = js_array_includes(ctx, jsc->property_list,
- 1, (JSValueConst *)&v);
- if (JS_IsException(present)) {
- JS_FreeValue(ctx, v);
- goto exception;
- }
- if (!JS_ToBoolFree(ctx, present)) {
- JS_SetPropertyInt64(ctx, jsc->property_list, j++, v);
- } else {
- JS_FreeValue(ctx, v);
- }
- }
- }
- }
- space = JS_DupValue(ctx, space0);
- if (JS_IsObject(space)) {
- JSObject *p = JS_VALUE_GET_OBJ(space);
- if (p->class_id == JS_CLASS_NUMBER) {
- space = JS_ToNumberFree(ctx, space);
- } else if (p->class_id == JS_CLASS_STRING) {
- space = JS_ToStringFree(ctx, space);
- }
- if (JS_IsException(space)) {
- JS_FreeValue(ctx, space);
- goto exception;
- }
- }
- if (JS_IsNumber(space)) {
- int n;
- if (JS_ToInt32Clamp(ctx, &n, space, 0, 10, 0))
- goto exception;
- jsc->gap = js_new_string8_len(ctx, " ", n);
- } else if (JS_IsString(space)) {
- JSString *p = JS_VALUE_GET_STRING(space);
- jsc->gap = js_sub_string(ctx, p, 0, min_int(p->len, 10));
- } else {
- jsc->gap = JS_DupValue(ctx, jsc->empty);
- }
- JS_FreeValue(ctx, space);
- if (JS_IsException(jsc->gap))
- goto exception;
- wrapper = JS_NewObject(ctx);
- if (JS_IsException(wrapper))
- goto exception;
- if (JS_DefinePropertyValue(ctx, wrapper, JS_ATOM_empty_string,
- JS_DupValue(ctx, obj), JS_PROP_C_W_E) < 0)
- goto exception;
- val = JS_DupValue(ctx, obj);
- val = js_json_check(ctx, jsc, wrapper, val, jsc->empty);
- if (JS_IsException(val))
- goto exception;
- if (JS_IsUndefined(val)) {
- ret = JS_UNDEFINED;
- goto done1;
- }
- if (js_json_to_str(ctx, jsc, wrapper, val, jsc->empty))
- goto exception;
- ret = string_buffer_end(jsc->b);
- goto done;
- exception:
- ret = JS_EXCEPTION;
- done1:
- string_buffer_free(jsc->b);
- done:
- JS_FreeValue(ctx, wrapper);
- JS_FreeValue(ctx, jsc->empty);
- JS_FreeValue(ctx, jsc->gap);
- JS_FreeValue(ctx, jsc->property_list);
- JS_FreeValue(ctx, jsc->stack);
- return ret;
- }
- static JSValue js_json_stringify(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- // stringify(val, replacer, space)
- return JS_JSONStringify(ctx, argv[0], argv[1], argv[2]);
- }
- static const JSCFunctionListEntry js_json_funcs[] = {
- JS_CFUNC_DEF("isRawJSON", 1, js_json_isRawJSON ),
- JS_CFUNC_DEF("parse", 2, js_json_parse ),
- JS_CFUNC_DEF("rawJSON", 1, js_json_rawJSON ),
- JS_CFUNC_DEF("stringify", 3, js_json_stringify ),
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "JSON", JS_PROP_CONFIGURABLE ),
- };
- static const JSCFunctionListEntry js_json_obj[] = {
- JS_OBJECT_DEF("JSON", js_json_funcs, countof(js_json_funcs), JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE ),
- };
- int JS_AddIntrinsicJSON(JSContext *ctx)
- {
- /* add JSON as autoinit object */
- return JS_SetPropertyFunctionList(ctx, ctx->global_obj, js_json_obj, countof(js_json_obj));
- }
- /* Reflect */
- static JSValue js_reflect_apply(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- return js_function_apply(ctx, argv[0], max_int(0, argc - 1), argv + 1, 2);
- }
- static JSValue js_reflect_construct(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValueConst func, array_arg, new_target;
- JSValue *tab, ret;
- uint32_t len;
- func = argv[0];
- array_arg = argv[1];
- if (argc > 2) {
- new_target = argv[2];
- if (!JS_IsConstructor(ctx, new_target))
- return JS_ThrowTypeErrorNotAConstructor(ctx, new_target);
- } else {
- new_target = func;
- }
- tab = build_arg_list(ctx, &len, array_arg);
- if (!tab)
- return JS_EXCEPTION;
- ret = JS_CallConstructor2(ctx, func, new_target, len, (JSValueConst *)tab);
- free_arg_list(ctx, tab, len);
- return ret;
- }
- static JSValue js_reflect_deleteProperty(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValueConst obj;
- JSAtom atom;
- int ret;
- obj = argv[0];
- if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)
- return JS_ThrowTypeErrorNotAnObject(ctx);
- atom = JS_ValueToAtom(ctx, argv[1]);
- if (unlikely(atom == JS_ATOM_NULL))
- return JS_EXCEPTION;
- ret = JS_DeleteProperty(ctx, obj, atom, 0);
- JS_FreeAtom(ctx, atom);
- if (ret < 0)
- return JS_EXCEPTION;
- else
- return JS_NewBool(ctx, ret);
- }
- static JSValue js_reflect_get(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValueConst obj, prop, receiver;
- JSAtom atom;
- JSValue ret;
- obj = argv[0];
- prop = argv[1];
- if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)
- return JS_ThrowTypeErrorNotAnObject(ctx);
- if (argc > 2)
- receiver = argv[2];
- else
- receiver = obj;
- atom = JS_ValueToAtom(ctx, prop);
- if (unlikely(atom == JS_ATOM_NULL))
- return JS_EXCEPTION;
- ret = JS_GetPropertyInternal(ctx, obj, atom, receiver, FALSE);
- JS_FreeAtom(ctx, atom);
- return ret;
- }
- static JSValue js_reflect_has(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValueConst obj, prop;
- JSAtom atom;
- int ret;
- obj = argv[0];
- prop = argv[1];
- if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)
- return JS_ThrowTypeErrorNotAnObject(ctx);
- atom = JS_ValueToAtom(ctx, prop);
- if (unlikely(atom == JS_ATOM_NULL))
- return JS_EXCEPTION;
- ret = JS_HasProperty(ctx, obj, atom);
- JS_FreeAtom(ctx, atom);
- if (ret < 0)
- return JS_EXCEPTION;
- else
- return JS_NewBool(ctx, ret);
- }
- static JSValue js_reflect_set(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValueConst obj, prop, val, receiver;
- int ret;
- JSAtom atom;
- obj = argv[0];
- prop = argv[1];
- val = argv[2];
- if (argc > 3)
- receiver = argv[3];
- else
- receiver = obj;
- if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)
- return JS_ThrowTypeErrorNotAnObject(ctx);
- atom = JS_ValueToAtom(ctx, prop);
- if (unlikely(atom == JS_ATOM_NULL))
- return JS_EXCEPTION;
- ret = JS_SetPropertyInternal(ctx, obj, atom,
- JS_DupValue(ctx, val), receiver, 0);
- JS_FreeAtom(ctx, atom);
- if (ret < 0)
- return JS_EXCEPTION;
- else
- return JS_NewBool(ctx, ret);
- }
- static JSValue js_reflect_setPrototypeOf(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- int ret;
- ret = JS_SetPrototypeInternal(ctx, argv[0], argv[1], FALSE);
- if (ret < 0)
- return JS_EXCEPTION;
- else
- return JS_NewBool(ctx, ret);
- }
- static JSValue js_reflect_ownKeys(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- if (JS_VALUE_GET_TAG(argv[0]) != JS_TAG_OBJECT)
- return JS_ThrowTypeErrorNotAnObject(ctx);
- return JS_GetOwnPropertyNames2(ctx, argv[0],
- JS_GPN_STRING_MASK | JS_GPN_SYMBOL_MASK,
- JS_ITERATOR_KIND_KEY);
- }
- static const JSCFunctionListEntry js_reflect_funcs[] = {
- JS_CFUNC_DEF("apply", 3, js_reflect_apply ),
- JS_CFUNC_DEF("construct", 2, js_reflect_construct ),
- JS_CFUNC_MAGIC_DEF("defineProperty", 3, js_object_defineProperty, 1 ),
- JS_CFUNC_DEF("deleteProperty", 2, js_reflect_deleteProperty ),
- JS_CFUNC_DEF("get", 2, js_reflect_get ),
- JS_CFUNC_MAGIC_DEF("getOwnPropertyDescriptor", 2, js_object_getOwnPropertyDescriptor, 1 ),
- JS_CFUNC_MAGIC_DEF("getPrototypeOf", 1, js_object_getPrototypeOf, 1 ),
- JS_CFUNC_DEF("has", 2, js_reflect_has ),
- JS_CFUNC_MAGIC_DEF("isExtensible", 1, js_object_isExtensible, 1 ),
- JS_CFUNC_DEF("ownKeys", 1, js_reflect_ownKeys ),
- JS_CFUNC_MAGIC_DEF("preventExtensions", 1, js_object_preventExtensions, 1 ),
- JS_CFUNC_DEF("set", 3, js_reflect_set ),
- JS_CFUNC_DEF("setPrototypeOf", 2, js_reflect_setPrototypeOf ),
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "Reflect", JS_PROP_CONFIGURABLE ),
- };
- static const JSCFunctionListEntry js_reflect_obj[] = {
- JS_OBJECT_DEF("Reflect", js_reflect_funcs, countof(js_reflect_funcs), JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE ),
- };
- /* Proxy */
- static void js_proxy_finalizer(JSRuntime *rt, JSValue val)
- {
- JSProxyData *s = JS_GetOpaque(val, JS_CLASS_PROXY);
- if (s) {
- JS_FreeValueRT(rt, s->target);
- JS_FreeValueRT(rt, s->handler);
- js_free_rt(rt, s);
- }
- }
- static void js_proxy_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func)
- {
- JSProxyData *s = JS_GetOpaque(val, JS_CLASS_PROXY);
- if (s) {
- JS_MarkValue(rt, s->target, mark_func);
- JS_MarkValue(rt, s->handler, mark_func);
- }
- }
- static JSValue JS_ThrowTypeErrorRevokedProxy(JSContext *ctx)
- {
- return JS_ThrowTypeError(ctx, "revoked proxy");
- }
- static JSProxyData *get_proxy_method(JSContext *ctx, JSValue *pmethod,
- JSValueConst obj, JSAtom name)
- {
- JSProxyData *s = JS_GetOpaque(obj, JS_CLASS_PROXY);
- JSValue method;
- /* safer to test recursion in all proxy methods */
- if (js_check_stack_overflow(ctx->rt, 0)) {
- JS_ThrowStackOverflow(ctx);
- return NULL;
- }
- /* 's' should never be NULL */
- if (s->is_revoked) {
- JS_ThrowTypeErrorRevokedProxy(ctx);
- return NULL;
- }
- method = JS_GetProperty(ctx, s->handler, name);
- if (JS_IsException(method))
- return NULL;
- if (JS_IsNull(method))
- method = JS_UNDEFINED;
- *pmethod = method;
- return s;
- }
- static JSValue js_proxy_get_prototype(JSContext *ctx, JSValueConst obj)
- {
- JSProxyData *s;
- JSValue method, ret, proto1;
- int res;
- s = get_proxy_method(ctx, &method, obj, JS_ATOM_getPrototypeOf);
- if (!s)
- return JS_EXCEPTION;
- if (JS_IsUndefined(method))
- return JS_GetPrototype(ctx, s->target);
- ret = JS_CallFree(ctx, method, s->handler, 1, (JSValueConst *)&s->target);
- if (JS_IsException(ret))
- return ret;
- if (JS_VALUE_GET_TAG(ret) != JS_TAG_NULL &&
- JS_VALUE_GET_TAG(ret) != JS_TAG_OBJECT) {
- goto fail;
- }
- res = JS_IsExtensible(ctx, s->target);
- if (res < 0) {
- JS_FreeValue(ctx, ret);
- return JS_EXCEPTION;
- }
- if (!res) {
- /* check invariant */
- proto1 = JS_GetPrototype(ctx, s->target);
- if (JS_IsException(proto1)) {
- JS_FreeValue(ctx, ret);
- return JS_EXCEPTION;
- }
- if (!js_same_value(ctx, proto1, ret)) {
- JS_FreeValue(ctx, proto1);
- fail:
- JS_FreeValue(ctx, ret);
- return JS_ThrowTypeError(ctx, "proxy: inconsistent prototype");
- }
- JS_FreeValue(ctx, proto1);
- }
- return ret;
- }
- static int js_proxy_set_prototype(JSContext *ctx, JSValueConst obj,
- JSValueConst proto_val)
- {
- JSProxyData *s;
- JSValue method, ret, proto1;
- JSValueConst args[2];
- BOOL res;
- int res2;
- s = get_proxy_method(ctx, &method, obj, JS_ATOM_setPrototypeOf);
- if (!s)
- return -1;
- if (JS_IsUndefined(method))
- return JS_SetPrototypeInternal(ctx, s->target, proto_val, FALSE);
- args[0] = s->target;
- args[1] = proto_val;
- ret = JS_CallFree(ctx, method, s->handler, 2, args);
- if (JS_IsException(ret))
- return -1;
- res = JS_ToBoolFree(ctx, ret);
- if (!res)
- return FALSE;
- res2 = JS_IsExtensible(ctx, s->target);
- if (res2 < 0)
- return -1;
- if (!res2) {
- proto1 = JS_GetPrototype(ctx, s->target);
- if (JS_IsException(proto1))
- return -1;
- if (!js_same_value(ctx, proto_val, proto1)) {
- JS_FreeValue(ctx, proto1);
- JS_ThrowTypeError(ctx, "proxy: inconsistent prototype");
- return -1;
- }
- JS_FreeValue(ctx, proto1);
- }
- return TRUE;
- }
- static int js_proxy_is_extensible(JSContext *ctx, JSValueConst obj)
- {
- JSProxyData *s;
- JSValue method, ret;
- BOOL res;
- int res2;
- s = get_proxy_method(ctx, &method, obj, JS_ATOM_isExtensible);
- if (!s)
- return -1;
- if (JS_IsUndefined(method))
- return JS_IsExtensible(ctx, s->target);
- ret = JS_CallFree(ctx, method, s->handler, 1, (JSValueConst *)&s->target);
- if (JS_IsException(ret))
- return -1;
- res = JS_ToBoolFree(ctx, ret);
- res2 = JS_IsExtensible(ctx, s->target);
- if (res2 < 0)
- return res2;
- if (res != res2) {
- JS_ThrowTypeError(ctx, "proxy: inconsistent isExtensible");
- return -1;
- }
- return res;
- }
- static int js_proxy_prevent_extensions(JSContext *ctx, JSValueConst obj)
- {
- JSProxyData *s;
- JSValue method, ret;
- BOOL res;
- int res2;
- s = get_proxy_method(ctx, &method, obj, JS_ATOM_preventExtensions);
- if (!s)
- return -1;
- if (JS_IsUndefined(method))
- return JS_PreventExtensions(ctx, s->target);
- ret = JS_CallFree(ctx, method, s->handler, 1, (JSValueConst *)&s->target);
- if (JS_IsException(ret))
- return -1;
- res = JS_ToBoolFree(ctx, ret);
- if (res) {
- res2 = JS_IsExtensible(ctx, s->target);
- if (res2 < 0)
- return res2;
- if (res2) {
- JS_ThrowTypeError(ctx, "proxy: inconsistent preventExtensions");
- return -1;
- }
- }
- return res;
- }
- static int js_proxy_has(JSContext *ctx, JSValueConst obj, JSAtom atom)
- {
- JSProxyData *s;
- JSValue method, ret1, atom_val;
- int ret, res;
- JSObject *p;
- JSValueConst args[2];
- BOOL res2;
- s = get_proxy_method(ctx, &method, obj, JS_ATOM_has);
- if (!s)
- return -1;
- if (JS_IsUndefined(method))
- return JS_HasProperty(ctx, s->target, atom);
- atom_val = JS_AtomToValue(ctx, atom);
- if (JS_IsException(atom_val)) {
- JS_FreeValue(ctx, method);
- return -1;
- }
- args[0] = s->target;
- args[1] = atom_val;
- ret1 = JS_CallFree(ctx, method, s->handler, 2, args);
- JS_FreeValue(ctx, atom_val);
- if (JS_IsException(ret1))
- return -1;
- ret = JS_ToBoolFree(ctx, ret1);
- if (!ret) {
- JSPropertyDescriptor desc;
- p = JS_VALUE_GET_OBJ(s->target);
- res = JS_GetOwnPropertyInternal(ctx, &desc, p, atom);
- if (res < 0)
- return -1;
- if (res) {
- res2 = !(desc.flags & JS_PROP_CONFIGURABLE);
- js_free_desc(ctx, &desc);
- if (res2 || !p->extensible) {
- JS_ThrowTypeError(ctx, "proxy: inconsistent has");
- return -1;
- }
- }
- }
- return ret;
- }
- static JSValue js_proxy_get(JSContext *ctx, JSValueConst obj, JSAtom atom,
- JSValueConst receiver)
- {
- JSProxyData *s;
- JSValue method, ret, atom_val;
- int res;
- JSValueConst args[3];
- JSPropertyDescriptor desc;
- s = get_proxy_method(ctx, &method, obj, JS_ATOM_get);
- if (!s)
- return JS_EXCEPTION;
- /* Note: recursion is possible thru the prototype of s->target */
- if (JS_IsUndefined(method))
- return JS_GetPropertyInternal(ctx, s->target, atom, receiver, FALSE);
- atom_val = JS_AtomToValue(ctx, atom);
- if (JS_IsException(atom_val)) {
- JS_FreeValue(ctx, method);
- return JS_EXCEPTION;
- }
- args[0] = s->target;
- args[1] = atom_val;
- args[2] = receiver;
- ret = JS_CallFree(ctx, method, s->handler, 3, args);
- JS_FreeValue(ctx, atom_val);
- if (JS_IsException(ret))
- return JS_EXCEPTION;
- res = JS_GetOwnPropertyInternal(ctx, &desc, JS_VALUE_GET_OBJ(s->target), atom);
- if (res < 0) {
- JS_FreeValue(ctx, ret);
- return JS_EXCEPTION;
- }
- if (res) {
- if ((desc.flags & (JS_PROP_GETSET | JS_PROP_CONFIGURABLE | JS_PROP_WRITABLE)) == 0) {
- if (!js_same_value(ctx, desc.value, ret)) {
- goto fail;
- }
- } else if ((desc.flags & (JS_PROP_GETSET | JS_PROP_CONFIGURABLE)) == JS_PROP_GETSET) {
- if (JS_IsUndefined(desc.getter) && !JS_IsUndefined(ret)) {
- fail:
- js_free_desc(ctx, &desc);
- JS_FreeValue(ctx, ret);
- return JS_ThrowTypeError(ctx, "proxy: inconsistent get");
- }
- }
- js_free_desc(ctx, &desc);
- }
- return ret;
- }
- static int js_proxy_set(JSContext *ctx, JSValueConst obj, JSAtom atom,
- JSValueConst value, JSValueConst receiver, int flags)
- {
- JSProxyData *s;
- JSValue method, ret1, atom_val;
- int ret, res;
- JSValueConst args[4];
- s = get_proxy_method(ctx, &method, obj, JS_ATOM_set);
- if (!s)
- return -1;
- if (JS_IsUndefined(method)) {
- return JS_SetPropertyInternal(ctx, s->target, atom,
- JS_DupValue(ctx, value), receiver,
- flags);
- }
- atom_val = JS_AtomToValue(ctx, atom);
- if (JS_IsException(atom_val)) {
- JS_FreeValue(ctx, method);
- return -1;
- }
- args[0] = s->target;
- args[1] = atom_val;
- args[2] = value;
- args[3] = receiver;
- ret1 = JS_CallFree(ctx, method, s->handler, 4, args);
- JS_FreeValue(ctx, atom_val);
- if (JS_IsException(ret1))
- return -1;
- ret = JS_ToBoolFree(ctx, ret1);
- if (ret) {
- JSPropertyDescriptor desc;
- res = JS_GetOwnPropertyInternal(ctx, &desc, JS_VALUE_GET_OBJ(s->target), atom);
- if (res < 0)
- return -1;
- if (res) {
- if ((desc.flags & (JS_PROP_GETSET | JS_PROP_CONFIGURABLE | JS_PROP_WRITABLE)) == 0) {
- if (!js_same_value(ctx, desc.value, value)) {
- goto fail;
- }
- } else if ((desc.flags & (JS_PROP_GETSET | JS_PROP_CONFIGURABLE)) == JS_PROP_GETSET && JS_IsUndefined(desc.setter)) {
- fail:
- js_free_desc(ctx, &desc);
- JS_ThrowTypeError(ctx, "proxy: inconsistent set");
- return -1;
- }
- js_free_desc(ctx, &desc);
- }
- } else {
- if ((flags & JS_PROP_THROW) ||
- ((flags & JS_PROP_THROW_STRICT) && is_strict_mode(ctx))) {
- JS_ThrowTypeError(ctx, "proxy: cannot set property");
- return -1;
- }
- }
- return ret;
- }
- static JSValue js_create_desc(JSContext *ctx, JSValueConst val,
- JSValueConst getter, JSValueConst setter,
- int flags)
- {
- JSValue ret;
- ret = JS_NewObject(ctx);
- if (JS_IsException(ret))
- return ret;
- if (flags & JS_PROP_HAS_GET) {
- JS_DefinePropertyValue(ctx, ret, JS_ATOM_get, JS_DupValue(ctx, getter),
- JS_PROP_C_W_E);
- }
- if (flags & JS_PROP_HAS_SET) {
- JS_DefinePropertyValue(ctx, ret, JS_ATOM_set, JS_DupValue(ctx, setter),
- JS_PROP_C_W_E);
- }
- if (flags & JS_PROP_HAS_VALUE) {
- JS_DefinePropertyValue(ctx, ret, JS_ATOM_value, JS_DupValue(ctx, val),
- JS_PROP_C_W_E);
- }
- if (flags & JS_PROP_HAS_WRITABLE) {
- JS_DefinePropertyValue(ctx, ret, JS_ATOM_writable,
- JS_NewBool(ctx, flags & JS_PROP_WRITABLE),
- JS_PROP_C_W_E);
- }
- if (flags & JS_PROP_HAS_ENUMERABLE) {
- JS_DefinePropertyValue(ctx, ret, JS_ATOM_enumerable,
- JS_NewBool(ctx, flags & JS_PROP_ENUMERABLE),
- JS_PROP_C_W_E);
- }
- if (flags & JS_PROP_HAS_CONFIGURABLE) {
- JS_DefinePropertyValue(ctx, ret, JS_ATOM_configurable,
- JS_NewBool(ctx, flags & JS_PROP_CONFIGURABLE),
- JS_PROP_C_W_E);
- }
- return ret;
- }
- static int js_proxy_get_own_property(JSContext *ctx, JSPropertyDescriptor *pdesc,
- JSValueConst obj, JSAtom prop)
- {
- JSProxyData *s;
- JSValue method, trap_result_obj, prop_val;
- int res, target_desc_ret, ret;
- JSObject *p;
- JSValueConst args[2];
- JSPropertyDescriptor result_desc, target_desc;
- s = get_proxy_method(ctx, &method, obj, JS_ATOM_getOwnPropertyDescriptor);
- if (!s)
- return -1;
- p = JS_VALUE_GET_OBJ(s->target);
- if (JS_IsUndefined(method)) {
- return JS_GetOwnPropertyInternal(ctx, pdesc, p, prop);
- }
- prop_val = JS_AtomToValue(ctx, prop);
- if (JS_IsException(prop_val)) {
- JS_FreeValue(ctx, method);
- return -1;
- }
- args[0] = s->target;
- args[1] = prop_val;
- trap_result_obj = JS_CallFree(ctx, method, s->handler, 2, args);
- JS_FreeValue(ctx, prop_val);
- if (JS_IsException(trap_result_obj))
- return -1;
- if (!JS_IsObject(trap_result_obj) && !JS_IsUndefined(trap_result_obj)) {
- JS_FreeValue(ctx, trap_result_obj);
- goto fail;
- }
- target_desc_ret = JS_GetOwnPropertyInternal(ctx, &target_desc, p, prop);
- if (target_desc_ret < 0) {
- JS_FreeValue(ctx, trap_result_obj);
- return -1;
- }
- if (target_desc_ret)
- js_free_desc(ctx, &target_desc);
- if (JS_IsUndefined(trap_result_obj)) {
- if (target_desc_ret) {
- if (!(target_desc.flags & JS_PROP_CONFIGURABLE) || !p->extensible)
- goto fail;
- }
- ret = FALSE;
- } else {
- int flags1, extensible_target;
- extensible_target = JS_IsExtensible(ctx, s->target);
- if (extensible_target < 0) {
- JS_FreeValue(ctx, trap_result_obj);
- return -1;
- }
- res = js_obj_to_desc(ctx, &result_desc, trap_result_obj);
- JS_FreeValue(ctx, trap_result_obj);
- if (res < 0)
- return -1;
- /* convert the result_desc.flags to property flags */
- if (result_desc.flags & (JS_PROP_HAS_GET | JS_PROP_HAS_SET)) {
- result_desc.flags |= JS_PROP_GETSET;
- } else {
- result_desc.flags |= JS_PROP_NORMAL;
- }
- result_desc.flags &= (JS_PROP_C_W_E | JS_PROP_TMASK);
-
- if (target_desc_ret) {
- /* convert result_desc.flags to defineProperty flags */
- flags1 = result_desc.flags | JS_PROP_HAS_CONFIGURABLE | JS_PROP_HAS_ENUMERABLE;
- if (result_desc.flags & JS_PROP_GETSET)
- flags1 |= JS_PROP_HAS_GET | JS_PROP_HAS_SET;
- else
- flags1 |= JS_PROP_HAS_VALUE | JS_PROP_HAS_WRITABLE;
- /* XXX: not complete check: need to compare value &
- getter/setter as in defineproperty */
- if (!check_define_prop_flags(target_desc.flags, flags1))
- goto fail1;
- } else {
- if (!extensible_target)
- goto fail1;
- }
- if (!(result_desc.flags & JS_PROP_CONFIGURABLE)) {
- if (!target_desc_ret || (target_desc.flags & JS_PROP_CONFIGURABLE))
- goto fail1;
- if ((result_desc.flags &
- (JS_PROP_GETSET | JS_PROP_WRITABLE)) == 0 &&
- target_desc_ret &&
- (target_desc.flags & JS_PROP_WRITABLE) != 0) {
- /* proxy-missing-checks */
- fail1:
- js_free_desc(ctx, &result_desc);
- fail:
- JS_ThrowTypeError(ctx, "proxy: inconsistent getOwnPropertyDescriptor");
- return -1;
- }
- }
- ret = TRUE;
- if (pdesc) {
- *pdesc = result_desc;
- } else {
- js_free_desc(ctx, &result_desc);
- }
- }
- return ret;
- }
- static int js_proxy_define_own_property(JSContext *ctx, JSValueConst obj,
- JSAtom prop, JSValueConst val,
- JSValueConst getter, JSValueConst setter,
- int flags)
- {
- JSProxyData *s;
- JSValue method, ret1, prop_val, desc_val;
- int res, ret;
- JSObject *p;
- JSValueConst args[3];
- JSPropertyDescriptor desc;
- BOOL setting_not_configurable;
- s = get_proxy_method(ctx, &method, obj, JS_ATOM_defineProperty);
- if (!s)
- return -1;
- if (JS_IsUndefined(method)) {
- return JS_DefineProperty(ctx, s->target, prop, val, getter, setter, flags);
- }
- prop_val = JS_AtomToValue(ctx, prop);
- if (JS_IsException(prop_val)) {
- JS_FreeValue(ctx, method);
- return -1;
- }
- desc_val = js_create_desc(ctx, val, getter, setter, flags);
- if (JS_IsException(desc_val)) {
- JS_FreeValue(ctx, prop_val);
- JS_FreeValue(ctx, method);
- return -1;
- }
- args[0] = s->target;
- args[1] = prop_val;
- args[2] = desc_val;
- ret1 = JS_CallFree(ctx, method, s->handler, 3, args);
- JS_FreeValue(ctx, prop_val);
- JS_FreeValue(ctx, desc_val);
- if (JS_IsException(ret1))
- return -1;
- ret = JS_ToBoolFree(ctx, ret1);
- if (!ret) {
- if (flags & JS_PROP_THROW) {
- JS_ThrowTypeError(ctx, "proxy: defineProperty exception");
- return -1;
- } else {
- return 0;
- }
- }
- p = JS_VALUE_GET_OBJ(s->target);
- res = JS_GetOwnPropertyInternal(ctx, &desc, p, prop);
- if (res < 0)
- return -1;
- setting_not_configurable = ((flags & (JS_PROP_HAS_CONFIGURABLE |
- JS_PROP_CONFIGURABLE)) ==
- JS_PROP_HAS_CONFIGURABLE);
- if (!res) {
- if (!p->extensible || setting_not_configurable)
- goto fail;
- } else {
- if (!check_define_prop_flags(desc.flags, flags))
- goto fail1;
- /* do the missing check from check_define_prop_flags() */
- if (!(desc.flags & JS_PROP_CONFIGURABLE)) {
- if ((desc.flags & JS_PROP_TMASK) == JS_PROP_GETSET) {
- if ((flags & JS_PROP_HAS_GET) &&
- !js_same_value(ctx, getter, desc.getter)) {
- goto fail1;
- }
- if ((flags & JS_PROP_HAS_SET) &&
- !js_same_value(ctx, setter, desc.setter)) {
- goto fail1;
- }
- } else if (!(desc.flags & JS_PROP_WRITABLE)) {
- if ((flags & JS_PROP_HAS_VALUE) &&
- !js_same_value(ctx, val, desc.value)) {
- goto fail1;
- }
- }
- }
- /* additional checks */
- if ((desc.flags & JS_PROP_CONFIGURABLE) && setting_not_configurable)
- goto fail1;
- if ((desc.flags & JS_PROP_TMASK) != JS_PROP_GETSET &&
- (desc.flags & (JS_PROP_CONFIGURABLE | JS_PROP_WRITABLE)) == JS_PROP_WRITABLE &&
- (flags & (JS_PROP_HAS_WRITABLE | JS_PROP_WRITABLE)) == JS_PROP_HAS_WRITABLE) {
- fail1:
- js_free_desc(ctx, &desc);
- fail:
- JS_ThrowTypeError(ctx, "proxy: inconsistent defineProperty");
- return -1;
- }
- js_free_desc(ctx, &desc);
- }
- return 1;
- }
- static int js_proxy_delete_property(JSContext *ctx, JSValueConst obj,
- JSAtom atom)
- {
- JSProxyData *s;
- JSValue method, ret, atom_val;
- int res, res2, is_extensible;
- JSValueConst args[2];
- s = get_proxy_method(ctx, &method, obj, JS_ATOM_deleteProperty);
- if (!s)
- return -1;
- if (JS_IsUndefined(method)) {
- return JS_DeleteProperty(ctx, s->target, atom, 0);
- }
- atom_val = JS_AtomToValue(ctx, atom);;
- if (JS_IsException(atom_val)) {
- JS_FreeValue(ctx, method);
- return -1;
- }
- args[0] = s->target;
- args[1] = atom_val;
- ret = JS_CallFree(ctx, method, s->handler, 2, args);
- JS_FreeValue(ctx, atom_val);
- if (JS_IsException(ret))
- return -1;
- res = JS_ToBoolFree(ctx, ret);
- if (res) {
- JSPropertyDescriptor desc;
- res2 = JS_GetOwnPropertyInternal(ctx, &desc, JS_VALUE_GET_OBJ(s->target), atom);
- if (res2 < 0)
- return -1;
- if (res2) {
- if (!(desc.flags & JS_PROP_CONFIGURABLE))
- goto fail;
- is_extensible = JS_IsExtensible(ctx, s->target);
- if (is_extensible < 0)
- goto fail1;
- if (!is_extensible) {
- /* proxy-missing-checks */
- fail:
- JS_ThrowTypeError(ctx, "proxy: inconsistent deleteProperty");
- fail1:
- js_free_desc(ctx, &desc);
- return -1;
- }
- js_free_desc(ctx, &desc);
- }
- }
- return res;
- }
- /* return the index of the property or -1 if not found */
- static int find_prop_key(const JSPropertyEnum *tab, int n, JSAtom atom)
- {
- int i;
- for(i = 0; i < n; i++) {
- if (tab[i].atom == atom)
- return i;
- }
- return -1;
- }
- static int js_proxy_get_own_property_names(JSContext *ctx,
- JSPropertyEnum **ptab,
- uint32_t *plen,
- JSValueConst obj)
- {
- JSProxyData *s;
- JSValue method, prop_array, val;
- uint32_t len, i, len2;
- JSPropertyEnum *tab, *tab2;
- JSAtom atom;
- JSPropertyDescriptor desc;
- int res, is_extensible, idx;
- s = get_proxy_method(ctx, &method, obj, JS_ATOM_ownKeys);
- if (!s)
- return -1;
- if (JS_IsUndefined(method)) {
- return JS_GetOwnPropertyNamesInternal(ctx, ptab, plen,
- JS_VALUE_GET_OBJ(s->target),
- JS_GPN_STRING_MASK | JS_GPN_SYMBOL_MASK);
- }
- prop_array = JS_CallFree(ctx, method, s->handler, 1, (JSValueConst *)&s->target);
- if (JS_IsException(prop_array))
- return -1;
- tab = NULL;
- len = 0;
- tab2 = NULL;
- len2 = 0;
- if (js_get_length32(ctx, &len, prop_array))
- goto fail;
- if (len > 0) {
- tab = js_mallocz(ctx, sizeof(tab[0]) * len);
- if (!tab)
- goto fail;
- }
- for(i = 0; i < len; i++) {
- val = JS_GetPropertyUint32(ctx, prop_array, i);
- if (JS_IsException(val))
- goto fail;
- if (!JS_IsString(val) && !JS_IsSymbol(val)) {
- JS_FreeValue(ctx, val);
- JS_ThrowTypeError(ctx, "proxy: properties must be strings or symbols");
- goto fail;
- }
- atom = JS_ValueToAtom(ctx, val);
- JS_FreeValue(ctx, val);
- if (atom == JS_ATOM_NULL)
- goto fail;
- tab[i].atom = atom;
- tab[i].is_enumerable = FALSE; /* XXX: redundant? */
- }
- /* check duplicate properties (XXX: inefficient, could store the
- * properties an a temporary object to use the hash) */
- for(i = 1; i < len; i++) {
- if (find_prop_key(tab, i, tab[i].atom) >= 0) {
- JS_ThrowTypeError(ctx, "proxy: duplicate property");
- goto fail;
- }
- }
- is_extensible = JS_IsExtensible(ctx, s->target);
- if (is_extensible < 0)
- goto fail;
- /* check if there are non configurable properties */
- if (s->is_revoked) {
- JS_ThrowTypeErrorRevokedProxy(ctx);
- goto fail;
- }
- if (JS_GetOwnPropertyNamesInternal(ctx, &tab2, &len2, JS_VALUE_GET_OBJ(s->target),
- JS_GPN_STRING_MASK | JS_GPN_SYMBOL_MASK))
- goto fail;
- for(i = 0; i < len2; i++) {
- if (s->is_revoked) {
- JS_ThrowTypeErrorRevokedProxy(ctx);
- goto fail;
- }
- res = JS_GetOwnPropertyInternal(ctx, &desc, JS_VALUE_GET_OBJ(s->target),
- tab2[i].atom);
- if (res < 0)
- goto fail;
- if (res) { /* safety, property should be found */
- js_free_desc(ctx, &desc);
- if (!(desc.flags & JS_PROP_CONFIGURABLE) || !is_extensible) {
- idx = find_prop_key(tab, len, tab2[i].atom);
- if (idx < 0) {
- JS_ThrowTypeError(ctx, "proxy: target property must be present in proxy ownKeys");
- goto fail;
- }
- /* mark the property as found */
- if (!is_extensible)
- tab[idx].is_enumerable = TRUE;
- }
- }
- }
- if (!is_extensible) {
- /* check that all property in 'tab' were checked */
- for(i = 0; i < len; i++) {
- if (!tab[i].is_enumerable) {
- JS_ThrowTypeError(ctx, "proxy: property not present in target were returned by non extensible proxy");
- goto fail;
- }
- }
- }
- JS_FreePropertyEnum(ctx, tab2, len2);
- JS_FreeValue(ctx, prop_array);
- *ptab = tab;
- *plen = len;
- return 0;
- fail:
- JS_FreePropertyEnum(ctx, tab2, len2);
- JS_FreePropertyEnum(ctx, tab, len);
- JS_FreeValue(ctx, prop_array);
- return -1;
- }
- static JSValue js_proxy_call_constructor(JSContext *ctx, JSValueConst func_obj,
- JSValueConst new_target,
- int argc, JSValueConst *argv)
- {
- JSProxyData *s;
- JSValue method, arg_array, ret;
- JSValueConst args[3];
- s = get_proxy_method(ctx, &method, func_obj, JS_ATOM_construct);
- if (!s)
- return JS_EXCEPTION;
- if (!JS_IsConstructor(ctx, s->target))
- return JS_ThrowTypeErrorNotAConstructor(ctx, s->target);
- if (JS_IsUndefined(method))
- return JS_CallConstructor2(ctx, s->target, new_target, argc, argv);
- arg_array = js_create_array(ctx, argc, argv);
- if (JS_IsException(arg_array)) {
- ret = JS_EXCEPTION;
- goto fail;
- }
- args[0] = s->target;
- args[1] = arg_array;
- args[2] = new_target;
- ret = JS_Call(ctx, method, s->handler, 3, args);
- if (!JS_IsException(ret) && JS_VALUE_GET_TAG(ret) != JS_TAG_OBJECT) {
- JS_FreeValue(ctx, ret);
- ret = JS_ThrowTypeErrorNotAnObject(ctx);
- }
- fail:
- JS_FreeValue(ctx, method);
- JS_FreeValue(ctx, arg_array);
- return ret;
- }
- static JSValue js_proxy_call(JSContext *ctx, JSValueConst func_obj,
- JSValueConst this_obj,
- int argc, JSValueConst *argv, int flags)
- {
- JSProxyData *s;
- JSValue method, arg_array, ret;
- JSValueConst args[3];
- if (flags & JS_CALL_FLAG_CONSTRUCTOR)
- return js_proxy_call_constructor(ctx, func_obj, this_obj, argc, argv);
- s = get_proxy_method(ctx, &method, func_obj, JS_ATOM_apply);
- if (!s)
- return JS_EXCEPTION;
- if (!s->is_func) {
- JS_FreeValue(ctx, method);
- return JS_ThrowTypeError(ctx, "not a function");
- }
- if (JS_IsUndefined(method))
- return JS_Call(ctx, s->target, this_obj, argc, argv);
- arg_array = js_create_array(ctx, argc, argv);
- if (JS_IsException(arg_array)) {
- ret = JS_EXCEPTION;
- goto fail;
- }
- args[0] = s->target;
- args[1] = this_obj;
- args[2] = arg_array;
- ret = JS_Call(ctx, method, s->handler, 3, args);
- fail:
- JS_FreeValue(ctx, method);
- JS_FreeValue(ctx, arg_array);
- return ret;
- }
- /* `js_resolve_proxy`: resolve the proxy chain
- `*pval` is updated with to ultimate proxy target
- `throw_exception` controls whether exceptions are thown or not
- - return -1 in case of error
- - otherwise return 0
- */
- static int js_resolve_proxy(JSContext *ctx, JSValueConst *pval, BOOL throw_exception) {
- int depth = 0;
- JSObject *p;
- JSProxyData *s;
- while (JS_VALUE_GET_TAG(*pval) == JS_TAG_OBJECT) {
- p = JS_VALUE_GET_OBJ(*pval);
- if (p->class_id != JS_CLASS_PROXY)
- break;
- if (depth++ > 1000) {
- if (throw_exception)
- JS_ThrowStackOverflow(ctx);
- return -1;
- }
- s = p->u.opaque;
- if (s->is_revoked) {
- if (throw_exception)
- JS_ThrowTypeErrorRevokedProxy(ctx);
- return -1;
- }
- *pval = s->target;
- }
- return 0;
- }
- static const JSClassExoticMethods js_proxy_exotic_methods = {
- .get_own_property = js_proxy_get_own_property,
- .define_own_property = js_proxy_define_own_property,
- .delete_property = js_proxy_delete_property,
- .get_own_property_names = js_proxy_get_own_property_names,
- .has_property = js_proxy_has,
- .get_property = js_proxy_get,
- .set_property = js_proxy_set,
- .get_prototype = js_proxy_get_prototype,
- .set_prototype = js_proxy_set_prototype,
- .is_extensible = js_proxy_is_extensible,
- .prevent_extensions = js_proxy_prevent_extensions,
- };
- static JSValue js_proxy_constructor(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValueConst target, handler;
- JSValue obj;
- JSProxyData *s;
- target = argv[0];
- handler = argv[1];
- if (JS_VALUE_GET_TAG(target) != JS_TAG_OBJECT ||
- JS_VALUE_GET_TAG(handler) != JS_TAG_OBJECT)
- return JS_ThrowTypeErrorNotAnObject(ctx);
- obj = JS_NewObjectProtoClass(ctx, JS_NULL, JS_CLASS_PROXY);
- if (JS_IsException(obj))
- return obj;
- s = js_malloc(ctx, sizeof(JSProxyData));
- if (!s) {
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- s->target = JS_DupValue(ctx, target);
- s->handler = JS_DupValue(ctx, handler);
- s->is_func = JS_IsFunction(ctx, target);
- s->is_revoked = FALSE;
- JS_SetOpaque(obj, s);
- JS_SetConstructorBit(ctx, obj, JS_IsConstructor(ctx, target));
- return obj;
- }
- static JSValue js_proxy_revoke(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic,
- JSValue *func_data)
- {
- JSProxyData *s = JS_GetOpaque(func_data[0], JS_CLASS_PROXY);
- if (s) {
- /* We do not free the handler and target in case they are
- referenced as constants in the C call stack */
- s->is_revoked = TRUE;
- JS_FreeValue(ctx, func_data[0]);
- func_data[0] = JS_NULL;
- }
- return JS_UNDEFINED;
- }
- static JSValue js_proxy_revoke_constructor(JSContext *ctx,
- JSValueConst proxy_obj)
- {
- return JS_NewCFunctionData(ctx, js_proxy_revoke, 0, 0, 1, &proxy_obj);
- }
- static JSValue js_proxy_revocable(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue proxy_obj, revoke_obj = JS_UNDEFINED, obj;
- proxy_obj = js_proxy_constructor(ctx, JS_UNDEFINED, argc, argv);
- if (JS_IsException(proxy_obj))
- goto fail;
- revoke_obj = js_proxy_revoke_constructor(ctx, proxy_obj);
- if (JS_IsException(revoke_obj))
- goto fail;
- obj = JS_NewObject(ctx);
- if (JS_IsException(obj))
- goto fail;
- // XXX: exceptions?
- JS_DefinePropertyValue(ctx, obj, JS_ATOM_proxy, proxy_obj, JS_PROP_C_W_E);
- JS_DefinePropertyValue(ctx, obj, JS_ATOM_revoke, revoke_obj, JS_PROP_C_W_E);
- return obj;
- fail:
- JS_FreeValue(ctx, proxy_obj);
- JS_FreeValue(ctx, revoke_obj);
- return JS_EXCEPTION;
- }
- static const JSCFunctionListEntry js_proxy_funcs[] = {
- JS_CFUNC_DEF("revocable", 2, js_proxy_revocable ),
- };
- static const JSClassShortDef js_proxy_class_def[] = {
- { JS_ATOM_Object, js_proxy_finalizer, js_proxy_mark }, /* JS_CLASS_PROXY */
- };
- int JS_AddIntrinsicProxy(JSContext *ctx)
- {
- JSRuntime *rt = ctx->rt;
- JSValue obj1;
- if (!JS_IsRegisteredClass(rt, JS_CLASS_PROXY)) {
- if (init_class_range(rt, js_proxy_class_def, JS_CLASS_PROXY,
- countof(js_proxy_class_def)))
- return -1;
- rt->class_array[JS_CLASS_PROXY].exotic = &js_proxy_exotic_methods;
- rt->class_array[JS_CLASS_PROXY].call = js_proxy_call;
- }
- /* additional fields: name, length */
- obj1 = JS_NewCFunction3(ctx, js_proxy_constructor, "Proxy", 2,
- JS_CFUNC_constructor, 0,
- ctx->function_proto, countof(js_proxy_funcs) + 2);
- if (JS_IsException(obj1))
- return -1;
- JS_SetConstructorBit(ctx, obj1, TRUE);
- if (JS_SetPropertyFunctionList(ctx, obj1, js_proxy_funcs,
- countof(js_proxy_funcs)))
- goto fail;
- if (JS_DefinePropertyValueStr(ctx, ctx->global_obj, "Proxy",
- obj1, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE) < 0)
- goto fail;
- return 0;
- fail:
- JS_FreeValue(ctx, obj1);
- return -1;
- }
- /* Symbol */
- static JSValue js_symbol_constructor(JSContext *ctx, JSValueConst new_target,
- int argc, JSValueConst *argv)
- {
- JSValue str;
- JSString *p;
- if (!JS_IsUndefined(new_target))
- return JS_ThrowTypeErrorNotAConstructor(ctx, new_target);
- if (argc == 0 || JS_IsUndefined(argv[0])) {
- p = NULL;
- } else {
- str = JS_ToString(ctx, argv[0]);
- if (JS_IsException(str))
- return JS_EXCEPTION;
- p = JS_VALUE_GET_STRING(str);
- }
- return JS_NewSymbol(ctx, p, JS_ATOM_TYPE_SYMBOL);
- }
- static JSValue js_thisSymbolValue(JSContext *ctx, JSValueConst this_val)
- {
- if (JS_VALUE_GET_TAG(this_val) == JS_TAG_SYMBOL)
- return JS_DupValue(ctx, this_val);
- if (JS_VALUE_GET_TAG(this_val) == JS_TAG_OBJECT) {
- JSObject *p = JS_VALUE_GET_OBJ(this_val);
- if (p->class_id == JS_CLASS_SYMBOL) {
- if (JS_VALUE_GET_TAG(p->u.object_data) == JS_TAG_SYMBOL)
- return JS_DupValue(ctx, p->u.object_data);
- }
- }
- return JS_ThrowTypeError(ctx, "not a symbol");
- }
- static JSValue js_symbol_toString(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue val, ret;
- val = js_thisSymbolValue(ctx, this_val);
- if (JS_IsException(val))
- return val;
- /* XXX: use JS_ToStringInternal() with a flags */
- ret = js_string_constructor(ctx, JS_UNDEFINED, 1, (JSValueConst *)&val);
- JS_FreeValue(ctx, val);
- return ret;
- }
- static JSValue js_symbol_valueOf(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- return js_thisSymbolValue(ctx, this_val);
- }
- static JSValue js_symbol_get_description(JSContext *ctx, JSValueConst this_val)
- {
- JSValue val, ret;
- JSAtomStruct *p;
- val = js_thisSymbolValue(ctx, this_val);
- if (JS_IsException(val))
- return val;
- p = JS_VALUE_GET_PTR(val);
- if (p->len == 0 && p->is_wide_char != 0) {
- ret = JS_UNDEFINED;
- } else {
- ret = JS_AtomToString(ctx, js_get_atom_index(ctx->rt, p));
- }
- JS_FreeValue(ctx, val);
- return ret;
- }
- static const JSCFunctionListEntry js_symbol_proto_funcs[] = {
- JS_CFUNC_DEF("toString", 0, js_symbol_toString ),
- JS_CFUNC_DEF("valueOf", 0, js_symbol_valueOf ),
- // XXX: should have writable: false
- JS_CFUNC_DEF("[Symbol.toPrimitive]", 1, js_symbol_valueOf ),
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "Symbol", JS_PROP_CONFIGURABLE ),
- JS_CGETSET_DEF("description", js_symbol_get_description, NULL ),
- };
- static JSValue js_symbol_for(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue str;
- str = JS_ToString(ctx, argv[0]);
- if (JS_IsException(str))
- return JS_EXCEPTION;
- return JS_NewSymbol(ctx, JS_VALUE_GET_STRING(str), JS_ATOM_TYPE_GLOBAL_SYMBOL);
- }
- static JSValue js_symbol_keyFor(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSAtomStruct *p;
- if (!JS_IsSymbol(argv[0]))
- return JS_ThrowTypeError(ctx, "not a symbol");
- p = JS_VALUE_GET_PTR(argv[0]);
- if (p->atom_type != JS_ATOM_TYPE_GLOBAL_SYMBOL)
- return JS_UNDEFINED;
- return JS_DupValue(ctx, JS_MKPTR(JS_TAG_STRING, p));
- }
- static const JSCFunctionListEntry js_symbol_funcs[] = {
- JS_CFUNC_DEF("for", 1, js_symbol_for ),
- JS_CFUNC_DEF("keyFor", 1, js_symbol_keyFor ),
- JS_PROP_ATOM_DEF("toPrimitive", JS_ATOM_Symbol_toPrimitive, 0),
- JS_PROP_ATOM_DEF("iterator", JS_ATOM_Symbol_iterator, 0),
- JS_PROP_ATOM_DEF("match", JS_ATOM_Symbol_match, 0),
- JS_PROP_ATOM_DEF("matchAll", JS_ATOM_Symbol_matchAll, 0),
- JS_PROP_ATOM_DEF("replace", JS_ATOM_Symbol_replace, 0),
- JS_PROP_ATOM_DEF("search", JS_ATOM_Symbol_search, 0),
- JS_PROP_ATOM_DEF("split", JS_ATOM_Symbol_split, 0),
- JS_PROP_ATOM_DEF("toStringTag", JS_ATOM_Symbol_toStringTag, 0),
- JS_PROP_ATOM_DEF("isConcatSpreadable", JS_ATOM_Symbol_isConcatSpreadable, 0),
- JS_PROP_ATOM_DEF("hasInstance", JS_ATOM_Symbol_hasInstance, 0),
- JS_PROP_ATOM_DEF("species", JS_ATOM_Symbol_species, 0),
- JS_PROP_ATOM_DEF("unscopables", JS_ATOM_Symbol_unscopables, 0),
- JS_PROP_ATOM_DEF("asyncIterator", JS_ATOM_Symbol_asyncIterator, 0),
- };
- /* Set/Map/WeakSet/WeakMap */
- static BOOL js_weakref_is_target(JSValueConst val)
- {
- switch (JS_VALUE_GET_TAG(val)) {
- case JS_TAG_OBJECT:
- return TRUE;
- case JS_TAG_SYMBOL:
- {
- JSAtomStruct *p = JS_VALUE_GET_PTR(val);
- if (p->atom_type == JS_ATOM_TYPE_SYMBOL &&
- p->hash != JS_ATOM_HASH_PRIVATE)
- return TRUE;
- }
- break;
- default:
- break;
- }
- return FALSE;
- }
- /* JS_UNDEFINED is considered as a live weakref */
- /* XXX: add a specific JSWeakRef value type ? */
- static BOOL js_weakref_is_live(JSValueConst val)
- {
- void *p;
- if (JS_IsUndefined(val))
- return TRUE;
- p = JS_VALUE_GET_PTR(val);
- return (js_rc(p)->ref_count != 0);
- }
- /* 'val' can be JS_UNDEFINED */
- static void js_weakref_free(JSRuntime *rt, JSValue val)
- {
- if (JS_VALUE_GET_TAG(val) == JS_TAG_OBJECT) {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- assert(p->weakref_count >= 1);
- p->weakref_count--;
- /* 'mark' is tested to avoid freeing the object structure when
- it is about to be freed in a cycle or in
- free_zero_refcount() */
- if (p->weakref_count == 0 && js_rc(p)->ref_count == 0 &&
- js_rc(p)->mark == 0) {
- js_free_rt(rt, p);
- }
- } else if (JS_VALUE_GET_TAG(val) == JS_TAG_SYMBOL) {
- JSString *p = JS_VALUE_GET_STRING(val);
- assert(p->hash >= 1);
- p->hash--;
- if (p->hash == 0 && js_rc(p)->ref_count == 0) {
- /* can remove the dummy structure */
- js_free_rt(rt, p);
- }
- }
- }
- /* val must be an object, a symbol or undefined (see
- js_weakref_is_target). */
- static JSValue js_weakref_new(JSContext *ctx, JSValueConst val)
- {
- if (JS_VALUE_GET_TAG(val) == JS_TAG_OBJECT) {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- p->weakref_count++;
- } else if (JS_VALUE_GET_TAG(val) == JS_TAG_SYMBOL) {
- JSString *p = JS_VALUE_GET_STRING(val);
- /* XXX: could return an exception if too many references */
- assert(p->hash < JS_ATOM_HASH_MASK - 2);
- p->hash++;
- } else {
- assert(JS_IsUndefined(val));
- }
- return (JSValue)val;
- }
- #define MAGIC_SET (1 << 0)
- #define MAGIC_WEAK (1 << 1)
- static JSValue js_map_constructor(JSContext *ctx, JSValueConst new_target,
- int argc, JSValueConst *argv, int magic)
- {
- JSMapState *s;
- JSValue obj, adder = JS_UNDEFINED, iter = JS_UNDEFINED, next_method = JS_UNDEFINED;
- JSValueConst arr;
- BOOL is_set, is_weak;
- is_set = magic & MAGIC_SET;
- is_weak = ((magic & MAGIC_WEAK) != 0);
- obj = js_create_from_ctor(ctx, new_target, JS_CLASS_MAP + magic);
- if (JS_IsException(obj))
- return JS_EXCEPTION;
- s = js_mallocz(ctx, sizeof(*s));
- if (!s)
- goto fail;
- init_list_head(&s->records);
- s->is_weak = is_weak;
- if (is_weak) {
- s->weakref_header.weakref_type = JS_WEAKREF_TYPE_MAP;
- list_add_tail(&s->weakref_header.link, &ctx->rt->weakref_list);
- }
- JS_SetOpaque(obj, s);
- s->hash_bits = 1;
- s->hash_size = 1U << s->hash_bits;
- s->hash_table = js_mallocz(ctx, sizeof(s->hash_table[0]) * s->hash_size);
- if (!s->hash_table)
- goto fail;
- s->record_count_threshold = 4;
- arr = JS_UNDEFINED;
- if (argc > 0)
- arr = argv[0];
- if (!JS_IsUndefined(arr) && !JS_IsNull(arr)) {
- JSValue item, ret;
- BOOL done;
- adder = JS_GetProperty(ctx, obj, is_set ? JS_ATOM_add : JS_ATOM_set);
- if (JS_IsException(adder))
- goto fail;
- if (!JS_IsFunction(ctx, adder)) {
- JS_ThrowTypeError(ctx, "set/add is not a function");
- goto fail;
- }
- iter = JS_GetIterator(ctx, arr, FALSE);
- if (JS_IsException(iter))
- goto fail;
- next_method = JS_GetProperty(ctx, iter, JS_ATOM_next);
- if (JS_IsException(next_method))
- goto fail;
- for(;;) {
- item = JS_IteratorNext(ctx, iter, next_method, 0, NULL, &done);
- if (JS_IsException(item))
- goto fail;
- if (done)
- break;
- if (is_set) {
- ret = JS_Call(ctx, adder, obj, 1, (JSValueConst *)&item);
- if (JS_IsException(ret)) {
- JS_FreeValue(ctx, item);
- goto fail_close;
- }
- } else {
- JSValue key, value;
- JSValueConst args[2];
- key = JS_UNDEFINED;
- value = JS_UNDEFINED;
- if (!JS_IsObject(item)) {
- JS_ThrowTypeErrorNotAnObject(ctx);
- goto fail1;
- }
- key = JS_GetPropertyUint32(ctx, item, 0);
- if (JS_IsException(key))
- goto fail1;
- value = JS_GetPropertyUint32(ctx, item, 1);
- if (JS_IsException(value))
- goto fail1;
- args[0] = key;
- args[1] = value;
- ret = JS_Call(ctx, adder, obj, 2, args);
- if (JS_IsException(ret)) {
- fail1:
- JS_FreeValue(ctx, item);
- JS_FreeValue(ctx, key);
- JS_FreeValue(ctx, value);
- goto fail_close;
- }
- JS_FreeValue(ctx, key);
- JS_FreeValue(ctx, value);
- }
- JS_FreeValue(ctx, ret);
- JS_FreeValue(ctx, item);
- }
- JS_FreeValue(ctx, next_method);
- JS_FreeValue(ctx, iter);
- JS_FreeValue(ctx, adder);
- }
- return obj;
- fail_close:
- /* close the iterator object, preserving pending exception */
- JS_IteratorClose(ctx, iter, TRUE);
- fail:
- JS_FreeValue(ctx, next_method);
- JS_FreeValue(ctx, iter);
- JS_FreeValue(ctx, adder);
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- /* XXX: could normalize strings to speed up comparison */
- static JSValue map_normalize_key(JSContext *ctx, JSValue key)
- {
- uint32_t tag = JS_VALUE_GET_TAG(key);
- /* convert -0.0 to +0.0 */
- if (JS_TAG_IS_FLOAT64(tag) && JS_VALUE_GET_FLOAT64(key) == 0.0) {
- key = JS_NewInt32(ctx, 0);
- }
- return key;
- }
- static JSValueConst map_normalize_key_const(JSContext *ctx, JSValueConst key)
- {
- return (JSValueConst)map_normalize_key(ctx, (JSValue)key);
- }
- /* hash multipliers, same as the Linux kernel (see Knuth vol 3,
- section 6.4, exercise 9) */
- #define HASH_MUL32 0x61C88647
- #define HASH_MUL64 UINT64_C(0x61C8864680B583EB)
- static uint32_t map_hash32(uint32_t a, int hash_bits)
- {
- return (a * HASH_MUL32) >> (32 - hash_bits);
- }
- static uint32_t map_hash64(uint64_t a, int hash_bits)
- {
- return (a * HASH_MUL64) >> (64 - hash_bits);
- }
- static uint32_t map_hash_pointer(uintptr_t a, int hash_bits)
- {
- #ifdef JS_PTR64
- return map_hash64(a, hash_bits);
- #else
- return map_hash32(a, hash_bits);
- #endif
- }
- /* XXX: better hash ? */
- /* precondition: 1 <= hash_bits <= 32 */
- static uint32_t map_hash_key(JSValueConst key, int hash_bits)
- {
- uint32_t tag = JS_VALUE_GET_NORM_TAG(key);
- uint32_t h;
- double d;
- JSBigInt *p;
- JSBigIntBuf buf;
-
- switch(tag) {
- case JS_TAG_BOOL:
- h = map_hash32(JS_VALUE_GET_INT(key) ^ JS_TAG_BOOL, hash_bits);
- break;
- case JS_TAG_STRING:
- h = map_hash32(hash_string(JS_VALUE_GET_STRING(key), 0) ^ JS_TAG_STRING, hash_bits);
- break;
- case JS_TAG_STRING_ROPE:
- h = map_hash32(hash_string_rope(key, 0) ^ JS_TAG_STRING, hash_bits);
- break;
- case JS_TAG_OBJECT:
- case JS_TAG_SYMBOL:
- h = map_hash_pointer((uintptr_t)JS_VALUE_GET_PTR(key) ^ tag, hash_bits);
- break;
- case JS_TAG_INT:
- d = JS_VALUE_GET_INT(key);
- goto hash_float64;
- case JS_TAG_FLOAT64:
- d = JS_VALUE_GET_FLOAT64(key);
- /* normalize the NaN */
- if (isnan(d))
- d = JS_FLOAT64_NAN;
- hash_float64:
- h = map_hash64(float64_as_uint64(d) ^ JS_TAG_FLOAT64, hash_bits);
- break;
- case JS_TAG_SHORT_BIG_INT:
- p = js_bigint_set_short(&buf, key);
- goto hash_bigint;
- case JS_TAG_BIG_INT:
- p = JS_VALUE_GET_PTR(key);
- hash_bigint:
- {
- int i;
- h = 1;
- for(i = p->len - 1; i >= 0; i--) {
- h = h * 263 + p->tab[i];
- }
- /* the final step is necessary otherwise h mod n only
- depends of p->tab[i] mod n */
- h = map_hash32(h ^ JS_TAG_BIG_INT, hash_bits);
- }
- break;
- default:
- h = 0;
- break;
- }
- return h;
- }
- static JSMapRecord *map_find_record(JSContext *ctx, JSMapState *s,
- JSValueConst key)
- {
- JSMapRecord *mr;
- uint32_t h;
- h = map_hash_key(key, s->hash_bits);
- for(mr = s->hash_table[h]; mr != NULL; mr = mr->hash_next) {
- if (mr->empty || (s->is_weak && !js_weakref_is_live(mr->key))) {
- /* cannot match */
- } else {
- if (js_same_value_zero(ctx, mr->key, key))
- return mr;
- }
- }
- return NULL;
- }
- static void map_hash_resize(JSContext *ctx, JSMapState *s)
- {
- uint32_t new_hash_size, h;
- int new_hash_bits;
- struct list_head *el;
- JSMapRecord *mr, **new_hash_table;
- /* XXX: no reporting of memory allocation failure */
- new_hash_bits = min_int(s->hash_bits + 1, 31);
- new_hash_size = 1U << new_hash_bits;
- new_hash_table = js_realloc(ctx, s->hash_table,
- sizeof(new_hash_table[0]) * new_hash_size);
- if (!new_hash_table)
- return;
- memset(new_hash_table, 0, sizeof(new_hash_table[0]) * new_hash_size);
- list_for_each(el, &s->records) {
- mr = list_entry(el, JSMapRecord, link);
- if (mr->empty || (s->is_weak && !js_weakref_is_live(mr->key))) {
- } else {
- h = map_hash_key(mr->key, new_hash_bits);
- mr->hash_next = new_hash_table[h];
- new_hash_table[h] = mr;
- }
- }
- s->hash_table = new_hash_table;
- s->hash_bits = new_hash_bits;
- s->hash_size = new_hash_size;
- s->record_count_threshold = new_hash_size * 2;
- }
- static JSMapRecord *map_add_record(JSContext *ctx, JSMapState *s,
- JSValueConst key)
- {
- uint32_t h;
- JSMapRecord *mr;
- mr = js_malloc(ctx, sizeof(*mr));
- if (!mr)
- return NULL;
- mr->ref_count = 1;
- mr->empty = FALSE;
- if (s->is_weak) {
- mr->key = js_weakref_new(ctx, key);
- } else {
- mr->key = JS_DupValue(ctx, key);
- }
- h = map_hash_key(key, s->hash_bits);
- mr->hash_next = s->hash_table[h];
- s->hash_table[h] = mr;
- list_add_tail(&mr->link, &s->records);
- s->record_count++;
- if (s->record_count >= s->record_count_threshold) {
- map_hash_resize(ctx, s);
- }
- return mr;
- }
- static JSMapRecord *set_add_record(JSContext *ctx, JSMapState *s,
- JSValueConst key)
- {
- JSMapRecord *mr;
- mr = map_add_record(ctx, s, key);
- if (!mr)
- return NULL;
- mr->value = JS_UNDEFINED;
- return mr;
- }
- /* warning: the record must be removed from the hash table before */
- static void map_delete_record_internal(JSRuntime *rt, JSMapState *s, JSMapRecord *mr)
- {
- if (mr->empty)
- return;
-
- if (s->is_weak) {
- js_weakref_free(rt, mr->key);
- } else {
- JS_FreeValueRT(rt, mr->key);
- }
- JS_FreeValueRT(rt, mr->value);
- if (--mr->ref_count == 0) {
- list_del(&mr->link);
- js_free_rt(rt, mr);
- } else {
- /* keep a zombie record for iterators */
- mr->empty = TRUE;
- mr->key = JS_UNDEFINED;
- mr->value = JS_UNDEFINED;
- }
- s->record_count--;
- }
- static void map_decref_record(JSRuntime *rt, JSMapRecord *mr)
- {
- if (--mr->ref_count == 0) {
- /* the record can be safely removed */
- assert(mr->empty);
- list_del(&mr->link);
- js_free_rt(rt, mr);
- }
- }
- static void map_delete_weakrefs(JSRuntime *rt, JSWeakRefHeader *wh)
- {
- JSMapState *s = container_of(wh, JSMapState, weakref_header);
- struct list_head *el, *el1;
- JSMapRecord *mr1, **pmr;
- uint32_t h;
- list_for_each_safe(el, el1, &s->records) {
- JSMapRecord *mr = list_entry(el, JSMapRecord, link);
- if (!js_weakref_is_live(mr->key)) {
- /* even if key is not live it can be hashed as a pointer */
- h = map_hash_key(mr->key, s->hash_bits);
- pmr = &s->hash_table[h];
- for(;;) {
- mr1 = *pmr;
- /* the entry may already be removed from the hash
- table if the map was resized */
- if (mr1 == NULL)
- goto done;
- if (mr1 == mr)
- break;
- pmr = &mr1->hash_next;
- }
- /* remove from the hash table */
- *pmr = mr1->hash_next;
- done:
- map_delete_record_internal(rt, s, mr);
- }
- }
- }
- static JSValue js_map_set(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- JSMapState *s = JS_GetOpaque2(ctx, this_val, JS_CLASS_MAP + magic);
- JSMapRecord *mr;
- JSValueConst key, value;
- if (!s)
- return JS_EXCEPTION;
- key = map_normalize_key_const(ctx, argv[0]);
- if (s->is_weak && !js_weakref_is_target(key))
- return JS_ThrowTypeError(ctx, "invalid value used as %s key", (magic & MAGIC_SET) ? "WeakSet" : "WeakMap");
- if (magic & MAGIC_SET)
- value = JS_UNDEFINED;
- else
- value = argv[1];
- mr = map_find_record(ctx, s, key);
- if (mr) {
- JS_FreeValue(ctx, mr->value);
- } else {
- mr = map_add_record(ctx, s, key);
- if (!mr)
- return JS_EXCEPTION;
- }
- mr->value = JS_DupValue(ctx, value);
- return JS_DupValue(ctx, this_val);
- }
- static JSValue js_map_get(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- JSMapState *s = JS_GetOpaque2(ctx, this_val, JS_CLASS_MAP + magic);
- JSMapRecord *mr;
- JSValueConst key;
- if (!s)
- return JS_EXCEPTION;
- key = map_normalize_key_const(ctx, argv[0]);
- mr = map_find_record(ctx, s, key);
- if (!mr)
- return JS_UNDEFINED;
- else
- return JS_DupValue(ctx, mr->value);
- }
- /* return JS_TRUE or JS_FALSE */
- static JSValue map_delete_record(JSContext *ctx, JSMapState *s, JSValueConst key)
- {
- JSMapRecord *mr, **pmr;
- uint32_t h;
- key = map_normalize_key_const(ctx, key);
-
- h = map_hash_key(key, s->hash_bits);
- pmr = &s->hash_table[h];
- for(;;) {
- mr = *pmr;
- if (mr == NULL)
- return JS_FALSE;
- if (mr->empty || (s->is_weak && !js_weakref_is_live(mr->key))) {
- /* not valid */
- } else {
- if (js_same_value_zero(ctx, mr->key, key))
- break;
- }
- pmr = &mr->hash_next;
- }
- /* remove from the hash table */
- *pmr = mr->hash_next;
-
- map_delete_record_internal(ctx->rt, s, mr);
- return JS_TRUE;
- }
- static JSValue js_map_getOrInsert(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- BOOL computed = magic & 1;
- JSClassID class_id = magic >> 1;
- JSMapState *s = JS_GetOpaque2(ctx, this_val, class_id);
- JSMapRecord *mr;
- JSValueConst key;
- JSValue value;
- if (!s)
- return JS_EXCEPTION;
- if (computed && !JS_IsFunction(ctx, argv[1]))
- return JS_ThrowTypeError(ctx, "not a function");
- key = map_normalize_key_const(ctx, argv[0]);
- if (s->is_weak && !js_weakref_is_target(key))
- return JS_ThrowTypeError(ctx, "invalid value used as WeakMap key");
- mr = map_find_record(ctx, s, key);
- if (!mr) {
- if (computed) {
- value = JS_Call(ctx, argv[1], JS_UNDEFINED, 1, &key);
- if (JS_IsException(value))
- return JS_EXCEPTION;
- map_delete_record(ctx, s, key);
- } else {
- value = JS_DupValue(ctx, argv[1]);
- }
- mr = map_add_record(ctx, s, key);
- if (!mr) {
- JS_FreeValue(ctx, value);
- return JS_EXCEPTION;
- }
- mr->value = value;
- }
- return JS_DupValue(ctx, mr->value);
- }
- static JSValue js_map_has(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- JSMapState *s = JS_GetOpaque2(ctx, this_val, JS_CLASS_MAP + magic);
- JSMapRecord *mr;
- JSValueConst key;
- if (!s)
- return JS_EXCEPTION;
- key = map_normalize_key_const(ctx, argv[0]);
- mr = map_find_record(ctx, s, key);
- return JS_NewBool(ctx, mr != NULL);
- }
- static JSValue js_map_delete(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- JSMapState *s = JS_GetOpaque2(ctx, this_val, JS_CLASS_MAP + magic);
- if (!s)
- return JS_EXCEPTION;
- return map_delete_record(ctx, s, argv[0]);
- }
- static JSValue js_map_clear(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- JSMapState *s = JS_GetOpaque2(ctx, this_val, JS_CLASS_MAP + magic);
- struct list_head *el, *el1;
- JSMapRecord *mr;
- if (!s)
- return JS_EXCEPTION;
- /* remove from the hash table */
- memset(s->hash_table, 0, sizeof(s->hash_table[0]) * s->hash_size);
-
- list_for_each_safe(el, el1, &s->records) {
- mr = list_entry(el, JSMapRecord, link);
- map_delete_record_internal(ctx->rt, s, mr);
- }
- return JS_UNDEFINED;
- }
- static JSValue js_map_get_size(JSContext *ctx, JSValueConst this_val, int magic)
- {
- JSMapState *s = JS_GetOpaque2(ctx, this_val, JS_CLASS_MAP + magic);
- if (!s)
- return JS_EXCEPTION;
- return JS_NewUint32(ctx, s->record_count);
- }
- static JSValue js_map_forEach(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- JSMapState *s = JS_GetOpaque2(ctx, this_val, JS_CLASS_MAP + magic);
- JSValueConst func, this_arg;
- JSValue ret, args[3];
- struct list_head *el;
- JSMapRecord *mr;
- if (!s)
- return JS_EXCEPTION;
- func = argv[0];
- if (argc > 1)
- this_arg = argv[1];
- else
- this_arg = JS_UNDEFINED;
- if (check_function(ctx, func))
- return JS_EXCEPTION;
- /* Note: the list can be modified while traversing it, but the
- current element is locked */
- el = s->records.next;
- while (el != &s->records) {
- mr = list_entry(el, JSMapRecord, link);
- if (!mr->empty) {
- mr->ref_count++;
- /* must duplicate in case the record is deleted */
- args[1] = JS_DupValue(ctx, mr->key);
- if (magic)
- args[0] = args[1];
- else
- args[0] = JS_DupValue(ctx, mr->value);
- args[2] = (JSValue)this_val;
- ret = JS_Call(ctx, func, this_arg, 3, (JSValueConst *)args);
- JS_FreeValue(ctx, args[0]);
- if (!magic)
- JS_FreeValue(ctx, args[1]);
- el = el->next;
- map_decref_record(ctx->rt, mr);
- if (JS_IsException(ret))
- return ret;
- JS_FreeValue(ctx, ret);
- } else {
- el = el->next;
- }
- }
- return JS_UNDEFINED;
- }
- static JSValue js_object_groupBy(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int is_map)
- {
- JSValueConst cb, args[2];
- JSValue res, iter, next, groups, key, v, prop;
- JSAtom key_atom = JS_ATOM_NULL;
- int64_t idx;
- BOOL done;
- // "is function?" check must be observed before argv[0] is accessed
- cb = argv[1];
- if (check_function(ctx, cb))
- return JS_EXCEPTION;
- iter = JS_GetIterator(ctx, argv[0], /*is_async*/FALSE);
- if (JS_IsException(iter))
- return JS_EXCEPTION;
- key = JS_UNDEFINED;
- key_atom = JS_ATOM_NULL;
- v = JS_UNDEFINED;
- prop = JS_UNDEFINED;
- groups = JS_UNDEFINED;
- next = JS_GetProperty(ctx, iter, JS_ATOM_next);
- if (JS_IsException(next))
- goto exception;
- if (is_map) {
- groups = js_map_constructor(ctx, JS_UNDEFINED, 0, NULL, 0);
- } else {
- groups = JS_NewObjectProto(ctx, JS_NULL);
- }
- if (JS_IsException(groups))
- goto exception;
- for (idx = 0; ; idx++) {
- if (idx >= MAX_SAFE_INTEGER) {
- JS_ThrowTypeError(ctx, "too many elements");
- goto iterator_close_exception;
- }
- v = JS_IteratorNext(ctx, iter, next, 0, NULL, &done);
- if (JS_IsException(v))
- goto exception;
- if (done)
- break; // v is JS_UNDEFINED
- args[0] = v;
- args[1] = JS_NewInt64(ctx, idx);
- key = JS_Call(ctx, cb, ctx->global_obj, 2, args);
- if (JS_IsException(key))
- goto iterator_close_exception;
- if (is_map) {
- prop = js_map_get(ctx, groups, 1, (JSValueConst *)&key, 0);
- } else {
- key_atom = JS_ValueToAtom(ctx, key);
- JS_FreeValue(ctx, key);
- key = JS_UNDEFINED;
- if (key_atom == JS_ATOM_NULL)
- goto iterator_close_exception;
- prop = JS_GetProperty(ctx, groups, key_atom);
- }
- if (JS_IsException(prop))
- goto exception;
- if (JS_IsUndefined(prop)) {
- prop = JS_NewArray(ctx);
- if (JS_IsException(prop))
- goto exception;
- if (is_map) {
- args[0] = key;
- args[1] = prop;
- res = js_map_set(ctx, groups, 2, args, 0);
- if (JS_IsException(res))
- goto exception;
- JS_FreeValue(ctx, res);
- } else {
- prop = JS_DupValue(ctx, prop);
- if (JS_DefinePropertyValue(ctx, groups, key_atom, prop,
- JS_PROP_C_W_E) < 0) {
- goto exception;
- }
- }
- }
- res = js_array_push(ctx, prop, 1, (JSValueConst *)&v, /*unshift*/0);
- if (JS_IsException(res))
- goto exception;
- // res is an int64
- JS_FreeValue(ctx, prop);
- JS_FreeValue(ctx, key);
- JS_FreeAtom(ctx, key_atom);
- JS_FreeValue(ctx, v);
- prop = JS_UNDEFINED;
- key = JS_UNDEFINED;
- key_atom = JS_ATOM_NULL;
- v = JS_UNDEFINED;
- }
- JS_FreeValue(ctx, iter);
- JS_FreeValue(ctx, next);
- return groups;
- iterator_close_exception:
- JS_IteratorClose(ctx, iter, TRUE);
- exception:
- JS_FreeAtom(ctx, key_atom);
- JS_FreeValue(ctx, prop);
- JS_FreeValue(ctx, key);
- JS_FreeValue(ctx, v);
- JS_FreeValue(ctx, groups);
- JS_FreeValue(ctx, iter);
- JS_FreeValue(ctx, next);
- return JS_EXCEPTION;
- }
- static void js_map_finalizer(JSRuntime *rt, JSValue val)
- {
- JSObject *p;
- JSMapState *s;
- struct list_head *el, *el1;
- JSMapRecord *mr;
- p = JS_VALUE_GET_OBJ(val);
- s = p->u.map_state;
- if (s) {
- /* if the object is deleted we are sure that no iterator is
- using it */
- list_for_each_safe(el, el1, &s->records) {
- mr = list_entry(el, JSMapRecord, link);
- if (!mr->empty) {
- if (s->is_weak)
- js_weakref_free(rt, mr->key);
- else
- JS_FreeValueRT(rt, mr->key);
- JS_FreeValueRT(rt, mr->value);
- }
- js_free_rt(rt, mr);
- }
- js_free_rt(rt, s->hash_table);
- if (s->is_weak) {
- list_del(&s->weakref_header.link);
- }
- js_free_rt(rt, s);
- }
- }
- static void js_map_mark(JSRuntime *rt, JSValueConst val, JS_MarkFunc *mark_func)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JSMapState *s;
- struct list_head *el;
- JSMapRecord *mr;
- s = p->u.map_state;
- if (s) {
- list_for_each(el, &s->records) {
- mr = list_entry(el, JSMapRecord, link);
- if (!s->is_weak)
- JS_MarkValue(rt, mr->key, mark_func);
- JS_MarkValue(rt, mr->value, mark_func);
- }
- }
- }
- /* Map Iterator */
- typedef struct JSMapIteratorData {
- JSValue obj;
- JSIteratorKindEnum kind;
- JSMapRecord *cur_record;
- } JSMapIteratorData;
- static void js_map_iterator_finalizer(JSRuntime *rt, JSValue val)
- {
- JSObject *p;
- JSMapIteratorData *it;
- p = JS_VALUE_GET_OBJ(val);
- it = p->u.map_iterator_data;
- if (it) {
- /* During the GC sweep phase the Map finalizer may be
- called before the Map iterator finalizer */
- if (JS_IsLiveObject(rt, it->obj) && it->cur_record) {
- map_decref_record(rt, it->cur_record);
- }
- JS_FreeValueRT(rt, it->obj);
- js_free_rt(rt, it);
- }
- }
- static void js_map_iterator_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JSMapIteratorData *it;
- it = p->u.map_iterator_data;
- if (it) {
- /* the record is already marked by the object */
- JS_MarkValue(rt, it->obj, mark_func);
- }
- }
- static JSValue js_create_map_iterator(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- JSIteratorKindEnum kind;
- JSMapState *s;
- JSMapIteratorData *it;
- JSValue enum_obj;
- kind = magic >> 2;
- magic &= 3;
- s = JS_GetOpaque2(ctx, this_val, JS_CLASS_MAP + magic);
- if (!s)
- return JS_EXCEPTION;
- enum_obj = JS_NewObjectClass(ctx, JS_CLASS_MAP_ITERATOR + magic);
- if (JS_IsException(enum_obj))
- goto fail;
- it = js_malloc(ctx, sizeof(*it));
- if (!it) {
- JS_FreeValue(ctx, enum_obj);
- goto fail;
- }
- it->obj = JS_DupValue(ctx, this_val);
- it->kind = kind;
- it->cur_record = NULL;
- JS_SetOpaque(enum_obj, it);
- return enum_obj;
- fail:
- return JS_EXCEPTION;
- }
- static JSValue js_map_iterator_next(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv,
- BOOL *pdone, int magic)
- {
- JSMapIteratorData *it;
- JSMapState *s;
- JSMapRecord *mr;
- struct list_head *el;
- it = JS_GetOpaque2(ctx, this_val, JS_CLASS_MAP_ITERATOR + magic);
- if (!it) {
- *pdone = FALSE;
- return JS_EXCEPTION;
- }
- if (JS_IsUndefined(it->obj))
- goto done;
- s = JS_GetOpaque(it->obj, JS_CLASS_MAP + magic);
- assert(s != NULL);
- if (!it->cur_record) {
- el = s->records.next;
- } else {
- mr = it->cur_record;
- el = mr->link.next;
- map_decref_record(ctx->rt, mr); /* the record can be freed here */
- }
- for(;;) {
- if (el == &s->records) {
- /* no more record */
- it->cur_record = NULL;
- JS_FreeValue(ctx, it->obj);
- it->obj = JS_UNDEFINED;
- done:
- /* end of enumeration */
- *pdone = TRUE;
- return JS_UNDEFINED;
- }
- mr = list_entry(el, JSMapRecord, link);
- if (!mr->empty)
- break;
- /* get the next record */
- el = mr->link.next;
- }
- /* lock the record so that it won't be freed */
- mr->ref_count++;
- it->cur_record = mr;
- *pdone = FALSE;
- if (it->kind == JS_ITERATOR_KIND_KEY) {
- return JS_DupValue(ctx, mr->key);
- } else {
- JSValueConst args[2];
- args[0] = mr->key;
- if (magic)
- args[1] = mr->key;
- else
- args[1] = mr->value;
- if (it->kind == JS_ITERATOR_KIND_VALUE) {
- return JS_DupValue(ctx, args[1]);
- } else {
- return js_create_array(ctx, 2, args);
- }
- }
- }
- static int get_set_record(JSContext *ctx, JSValueConst obj,
- int64_t *psize, JSValue *phas, JSValue *pkeys)
- {
- JSMapState *s;
- int64_t size;
- JSValue has = JS_UNDEFINED, keys = JS_UNDEFINED;
-
- s = JS_GetOpaque(obj, JS_CLASS_SET);
- if (s) {
- size = s->record_count;
- } else {
- JSValue v;
- double d;
- v = JS_GetProperty(ctx, obj, JS_ATOM_size);
- if (JS_IsException(v))
- goto exception;
- if (JS_ToFloat64Free(ctx, &d, v) < 0)
- goto exception;
- if (isnan(d)) {
- JS_ThrowTypeError(ctx, ".size is not a number");
- goto exception;
- }
- if (d < INT64_MIN)
- size = INT64_MIN;
- else if (d >= 0x1p63) /* must use INT64_MAX + 1 because INT64_MAX cannot be exactly represented as a double */
- size = INT64_MAX;
- else
- size = (int64_t)d;
- if (size < 0) {
- JS_ThrowRangeError(ctx, ".size must be positive");
- goto exception;
- }
- }
- has = JS_GetProperty(ctx, obj, JS_ATOM_has);
- if (JS_IsException(has))
- goto exception;
- if (JS_IsUndefined(has)) {
- JS_ThrowTypeError(ctx, ".has is undefined");
- goto exception;
- }
- if (!JS_IsFunction(ctx, has)) {
- JS_ThrowTypeError(ctx, ".has is not a function");
- goto exception;
- }
- keys = JS_GetProperty(ctx, obj, JS_ATOM_keys);
- if (JS_IsException(keys))
- goto exception;
- if (JS_IsUndefined(keys)) {
- JS_ThrowTypeError(ctx, ".keys is undefined");
- goto exception;
- }
- if (!JS_IsFunction(ctx, keys)) {
- JS_ThrowTypeError(ctx, ".keys is not a function");
- goto exception;
- }
- *psize = size;
- *phas = has;
- *pkeys = keys;
- return 0;
- exception:
- JS_FreeValue(ctx, has);
- JS_FreeValue(ctx, keys);
- *psize = 0;
- *phas = JS_UNDEFINED;
- *pkeys = JS_UNDEFINED;
- return -1;
- }
- /* copy 'this_val' in a new set without side effects */
- static JSValue js_copy_set(JSContext *ctx, JSValueConst this_val)
- {
- JSValue newset;
- JSMapState *s, *t;
- struct list_head *el;
- JSMapRecord *mr;
-
- s = JS_GetOpaque2(ctx, this_val, JS_CLASS_SET);
- if (!s)
- return JS_EXCEPTION;
- newset = js_map_constructor(ctx, JS_UNDEFINED, 0, NULL, MAGIC_SET);
- if (JS_IsException(newset))
- return JS_EXCEPTION;
- t = JS_GetOpaque(newset, JS_CLASS_SET);
- // can't clone this_val using js_map_constructor(),
- // test262 mandates we don't call the .add method
- list_for_each(el, &s->records) {
- mr = list_entry(el, JSMapRecord, link);
- if (mr->empty)
- continue;
- if (!set_add_record(ctx, t, mr->key))
- goto exception;
- }
- return newset;
- exception:
- JS_FreeValue(ctx, newset);
- return JS_EXCEPTION;
- }
- static JSValue js_set_isDisjointFrom(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue item, iter, keys, has, next, rv, rval;
- int done;
- BOOL found;
- JSMapState *s;
- int64_t size;
- int ok;
- iter = JS_UNDEFINED;
- next = JS_UNDEFINED;
- rval = JS_EXCEPTION;
- s = JS_GetOpaque2(ctx, this_val, JS_CLASS_SET);
- if (!s)
- return JS_EXCEPTION;
- if (get_set_record(ctx, argv[0], &size, &has, &keys) < 0)
- goto exception;
- if (s->record_count <= size) {
- iter = js_create_map_iterator(ctx, this_val, 0, NULL, MAGIC_SET);
- if (JS_IsException(iter))
- goto exception;
- found = FALSE;
- do {
- item = js_map_iterator_next(ctx, iter, 0, NULL, &done, MAGIC_SET);
- if (JS_IsException(item))
- goto exception;
- if (done) // item is JS_UNDEFINED
- break;
- rv = JS_Call(ctx, has, argv[0], 1, (JSValueConst *)&item);
- JS_FreeValue(ctx, item);
- ok = JS_ToBoolFree(ctx, rv); // returns -1 if rv is JS_EXCEPTION
- if (ok < 0)
- goto exception;
- found = (ok > 0);
- } while (!found);
- } else {
- iter = JS_Call(ctx, keys, argv[0], 0, NULL);
- if (JS_IsException(iter))
- goto exception;
- next = JS_GetProperty(ctx, iter, JS_ATOM_next);
- if (JS_IsException(next))
- goto exception;
- found = FALSE;
- for(;;) {
- item = JS_IteratorNext(ctx, iter, next, 0, NULL, &done);
- if (JS_IsException(item))
- goto exception;
- if (done) // item is JS_UNDEFINED
- break;
- item = map_normalize_key(ctx, item);
- found = (NULL != map_find_record(ctx, s, item));
- JS_FreeValue(ctx, item);
- if (found) {
- JS_IteratorClose(ctx, iter, FALSE);
- break;
- }
- }
- }
- rval = !found ? JS_TRUE : JS_FALSE;
- exception:
- JS_FreeValue(ctx, has);
- JS_FreeValue(ctx, keys);
- JS_FreeValue(ctx, iter);
- JS_FreeValue(ctx, next);
- return rval;
- }
- static JSValue js_set_isSubsetOf(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue item, iter, keys, has, next, rv, rval;
- BOOL found;
- JSMapState *s;
- int64_t size;
- int done, ok;
- iter = JS_UNDEFINED;
- next = JS_UNDEFINED;
- rval = JS_EXCEPTION;
- s = JS_GetOpaque2(ctx, this_val, JS_CLASS_SET);
- if (!s)
- return JS_EXCEPTION;
- if (get_set_record(ctx, argv[0], &size, &has, &keys) < 0)
- goto exception;
- found = FALSE;
- if (s->record_count > size)
- goto fini;
- iter = js_create_map_iterator(ctx, this_val, 0, NULL, MAGIC_SET);
- if (JS_IsException(iter))
- goto exception;
- found = TRUE;
- do {
- item = js_map_iterator_next(ctx, iter, 0, NULL, &done, MAGIC_SET);
- if (JS_IsException(item))
- goto exception;
- if (done) // item is JS_UNDEFINED
- break;
- rv = JS_Call(ctx, has, argv[0], 1, (JSValueConst *)&item);
- JS_FreeValue(ctx, item);
- ok = JS_ToBoolFree(ctx, rv); // returns -1 if rv is JS_EXCEPTION
- if (ok < 0)
- goto exception;
- found = (ok > 0);
- } while (found);
- fini:
- rval = found ? JS_TRUE : JS_FALSE;
- exception:
- JS_FreeValue(ctx, has);
- JS_FreeValue(ctx, keys);
- JS_FreeValue(ctx, iter);
- JS_FreeValue(ctx, next);
- return rval;
- }
- static JSValue js_set_isSupersetOf(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue item, iter, keys, has, next, rval;
- int done;
- BOOL found;
- JSMapState *s;
- int64_t size;
- iter = JS_UNDEFINED;
- next = JS_UNDEFINED;
- rval = JS_EXCEPTION;
- s = JS_GetOpaque2(ctx, this_val, JS_CLASS_SET);
- if (!s)
- return JS_EXCEPTION;
- if (get_set_record(ctx, argv[0], &size, &has, &keys) < 0)
- goto exception;
- found = FALSE;
- if (s->record_count < size)
- goto fini;
- iter = JS_Call(ctx, keys, argv[0], 0, NULL);
- if (JS_IsException(iter))
- goto exception;
- next = JS_GetProperty(ctx, iter, JS_ATOM_next);
- if (JS_IsException(next))
- goto exception;
- found = TRUE;
- for(;;) {
- item = JS_IteratorNext(ctx, iter, next, 0, NULL, &done);
- if (JS_IsException(item))
- goto exception;
- if (done) // item is JS_UNDEFINED
- break;
- item = map_normalize_key(ctx, item);
- found = (NULL != map_find_record(ctx, s, item));
- JS_FreeValue(ctx, item);
- if (!found) {
- JS_IteratorClose(ctx, iter, FALSE);
- break;
- }
- }
- fini:
- rval = found ? JS_TRUE : JS_FALSE;
- exception:
- JS_FreeValue(ctx, has);
- JS_FreeValue(ctx, keys);
- JS_FreeValue(ctx, iter);
- JS_FreeValue(ctx, next);
- return rval;
- }
- static JSValue js_set_intersection(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue newset, item, iter, keys, has, next, rv;
- JSMapState *s, *t;
- JSMapRecord *mr;
- int64_t size;
- int done, ok;
- iter = JS_UNDEFINED;
- next = JS_UNDEFINED;
- newset = JS_UNDEFINED;
- s = JS_GetOpaque2(ctx, this_val, JS_CLASS_SET);
- if (!s)
- return JS_EXCEPTION;
- if (get_set_record(ctx, argv[0], &size, &has, &keys) < 0)
- goto exception;
- if (s->record_count > size) {
- iter = JS_Call(ctx, keys, argv[0], 0, NULL);
- if (JS_IsException(iter))
- goto exception;
- next = JS_GetProperty(ctx, iter, JS_ATOM_next);
- if (JS_IsException(next))
- goto exception;
- newset = js_map_constructor(ctx, JS_UNDEFINED, 0, NULL, MAGIC_SET);
- if (JS_IsException(newset))
- goto exception;
- t = JS_GetOpaque(newset, JS_CLASS_SET);
- for (;;) {
- item = JS_IteratorNext(ctx, iter, next, 0, NULL, &done);
- if (JS_IsException(item))
- goto exception;
- if (done) // item is JS_UNDEFINED
- break;
- item = map_normalize_key(ctx, item);
- if (!map_find_record(ctx, s, item)) {
- JS_FreeValue(ctx, item);
- } else if (map_find_record(ctx, t, item)) {
- JS_FreeValue(ctx, item); // no duplicates
- } else {
- mr = set_add_record(ctx, t, item);
- JS_FreeValue(ctx, item);
- if (!mr)
- goto exception;
- }
- }
- } else {
- iter = js_create_map_iterator(ctx, this_val, 0, NULL, MAGIC_SET);
- if (JS_IsException(iter))
- goto exception;
- newset = js_map_constructor(ctx, JS_UNDEFINED, 0, NULL, MAGIC_SET);
- if (JS_IsException(newset))
- goto exception;
- t = JS_GetOpaque(newset, JS_CLASS_SET);
- for (;;) {
- item = js_map_iterator_next(ctx, iter, 0, NULL, &done, MAGIC_SET);
- if (JS_IsException(item))
- goto exception;
- if (done) // item is JS_UNDEFINED
- break;
- rv = JS_Call(ctx, has, argv[0], 1, (JSValueConst *)&item);
- ok = JS_ToBoolFree(ctx, rv); // returns -1 if rv is JS_EXCEPTION
- if (ok > 0) {
- item = map_normalize_key(ctx, item);
- if (map_find_record(ctx, t, item)) {
- JS_FreeValue(ctx, item); // no duplicates
- } else {
- mr = set_add_record(ctx, t, item);
- JS_FreeValue(ctx, item);
- if (!mr)
- goto exception;
- }
- } else {
- JS_FreeValue(ctx, item);
- if (ok < 0)
- goto exception;
- }
- }
- }
- goto fini;
- exception:
- JS_FreeValue(ctx, newset);
- newset = JS_EXCEPTION;
- fini:
- JS_FreeValue(ctx, has);
- JS_FreeValue(ctx, keys);
- JS_FreeValue(ctx, iter);
- JS_FreeValue(ctx, next);
- return newset;
- }
- static JSValue js_set_difference(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue newset, item, iter, keys, has, next, rv;
- JSMapState *s, *t;
- int64_t size;
- int done;
- int ok;
- iter = JS_UNDEFINED;
- next = JS_UNDEFINED;
- newset = JS_UNDEFINED;
- s = JS_GetOpaque2(ctx, this_val, JS_CLASS_SET);
- if (!s)
- return JS_EXCEPTION;
- if (get_set_record(ctx, argv[0], &size, &has, &keys) < 0)
- goto exception;
- newset = js_copy_set(ctx, this_val);
- if (JS_IsException(newset))
- goto exception;
- t = JS_GetOpaque(newset, JS_CLASS_SET);
-
- if (s->record_count <= size) {
- iter = js_create_map_iterator(ctx, newset, 0, NULL, MAGIC_SET);
- if (JS_IsException(iter))
- goto exception;
- for (;;) {
- item = js_map_iterator_next(ctx, iter, 0, NULL, &done, MAGIC_SET);
- if (JS_IsException(item))
- goto exception;
- if (done) // item is JS_UNDEFINED
- break;
- rv = JS_Call(ctx, has, argv[0], 1, (JSValueConst *)&item);
- ok = JS_ToBoolFree(ctx, rv); // returns -1 if rv is JS_EXCEPTION
- if (ok < 0) {
- JS_FreeValue(ctx, item);
- goto exception;
- }
- if (ok) {
- map_delete_record(ctx, t, item);
- }
- JS_FreeValue(ctx, item);
- }
- } else {
- iter = JS_Call(ctx, keys, argv[0], 0, NULL);
- if (JS_IsException(iter))
- goto exception;
- next = JS_GetProperty(ctx, iter, JS_ATOM_next);
- if (JS_IsException(next))
- goto exception;
- for (;;) {
- item = JS_IteratorNext(ctx, iter, next, 0, NULL, &done);
- if (JS_IsException(item))
- goto exception;
- if (done) // item is JS_UNDEFINED
- break;
- map_delete_record(ctx, t, item);
- JS_FreeValue(ctx, item);
- }
- }
- goto fini;
- exception:
- JS_FreeValue(ctx, newset);
- newset = JS_EXCEPTION;
- fini:
- JS_FreeValue(ctx, has);
- JS_FreeValue(ctx, keys);
- JS_FreeValue(ctx, iter);
- JS_FreeValue(ctx, next);
- return newset;
- }
- static JSValue js_set_symmetricDifference(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue newset, item, iter, next, has, keys;
- JSMapState *s, *t;
- JSMapRecord *mr;
- int64_t size;
- int done;
- BOOL present;
- s = JS_GetOpaque2(ctx, this_val, JS_CLASS_SET);
- if (!s)
- return JS_EXCEPTION;
- if (get_set_record(ctx, argv[0], &size, &has, &keys) < 0)
- return JS_EXCEPTION;
- JS_FreeValue(ctx, has);
- next = JS_UNDEFINED;
- newset = JS_UNDEFINED;
- iter = JS_Call(ctx, keys, argv[0], 0, NULL);
- if (JS_IsException(iter))
- goto exception;
- next = JS_GetProperty(ctx, iter, JS_ATOM_next);
- if (JS_IsException(next))
- goto exception;
- newset = js_copy_set(ctx, this_val);
- if (JS_IsException(newset))
- goto exception;
- t = JS_GetOpaque(newset, JS_CLASS_SET);
- for (;;) {
- item = JS_IteratorNext(ctx, iter, next, 0, NULL, &done);
- if (JS_IsException(item))
- goto exception;
- if (done) // item is JS_UNDEFINED
- break;
- // note the subtlety here: due to mutating iterators, it's
- // possible for keys to disappear during iteration; test262
- // still expects us to maintain insertion order though, so
- // we first check |this|, then |new|; |new| is a copy of |this|
- // - if item exists in |this|, delete (if it exists) from |new|
- // - if item misses in |this| and |new|, add to |new|
- // - if item exists in |new| but misses in |this|, *don't* add it,
- // mutating iterator erased it
- item = map_normalize_key(ctx, item);
- present = (NULL != map_find_record(ctx, s, item));
- mr = map_find_record(ctx, t, item);
- if (present) {
- map_delete_record(ctx, t, item);
- JS_FreeValue(ctx, item);
- } else if (mr) {
- JS_FreeValue(ctx, item);
- } else {
- mr = set_add_record(ctx, t, item);
- JS_FreeValue(ctx, item);
- if (!mr)
- goto exception;
- }
- }
- goto fini;
- exception:
- JS_FreeValue(ctx, newset);
- newset = JS_EXCEPTION;
- fini:
- JS_FreeValue(ctx, next);
- JS_FreeValue(ctx, iter);
- JS_FreeValue(ctx, keys);
- return newset;
- }
- static JSValue js_set_union(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue newset, item, iter, next, has, keys, rv;
- JSMapState *s;
- int64_t size;
- int done;
- s = JS_GetOpaque2(ctx, this_val, JS_CLASS_SET);
- if (!s)
- return JS_EXCEPTION;
- if (get_set_record(ctx, argv[0], &size, &has, &keys) < 0)
- return JS_EXCEPTION;
- JS_FreeValue(ctx, has);
- next = JS_UNDEFINED;
- newset = JS_UNDEFINED;
- iter = JS_Call(ctx, keys, argv[0], 0, NULL);
- if (JS_IsException(iter))
- goto exception;
- next = JS_GetProperty(ctx, iter, JS_ATOM_next);
- if (JS_IsException(next))
- goto exception;
- newset = js_copy_set(ctx, this_val);
- if (JS_IsException(newset))
- goto exception;
- for (;;) {
- item = JS_IteratorNext(ctx, iter, next, 0, NULL, &done);
- if (JS_IsException(item))
- goto exception;
- if (done) // item is JS_UNDEFINED
- break;
- rv = js_map_set(ctx, newset, 1, (JSValueConst *)&item, MAGIC_SET);
- JS_FreeValue(ctx, item);
- if (JS_IsException(rv))
- goto exception;
- JS_FreeValue(ctx, rv);
- }
- goto fini;
- exception:
- JS_FreeValue(ctx, newset);
- newset = JS_EXCEPTION;
- fini:
- JS_FreeValue(ctx, next);
- JS_FreeValue(ctx, iter);
- JS_FreeValue(ctx, keys);
- return newset;
- }
- static const JSCFunctionListEntry js_map_funcs[] = {
- JS_CFUNC_MAGIC_DEF("groupBy", 2, js_object_groupBy, 1 ),
- JS_CGETSET_DEF("[Symbol.species]", js_get_this, NULL ),
- };
- static const JSCFunctionListEntry js_map_proto_funcs[] = {
- JS_CFUNC_MAGIC_DEF("set", 2, js_map_set, 0 ),
- JS_CFUNC_MAGIC_DEF("get", 1, js_map_get, 0 ),
- JS_CFUNC_MAGIC_DEF("getOrInsert", 2, js_map_getOrInsert,
- (JS_CLASS_MAP << 1) | /*computed*/FALSE ),
- JS_CFUNC_MAGIC_DEF("getOrInsertComputed", 2, js_map_getOrInsert,
- (JS_CLASS_MAP << 1) | /*computed*/TRUE ),
- JS_CFUNC_MAGIC_DEF("has", 1, js_map_has, 0 ),
- JS_CFUNC_MAGIC_DEF("delete", 1, js_map_delete, 0 ),
- JS_CFUNC_MAGIC_DEF("clear", 0, js_map_clear, 0 ),
- JS_CGETSET_MAGIC_DEF("size", js_map_get_size, NULL, 0),
- JS_CFUNC_MAGIC_DEF("forEach", 1, js_map_forEach, 0 ),
- JS_CFUNC_MAGIC_DEF("values", 0, js_create_map_iterator, (JS_ITERATOR_KIND_VALUE << 2) | 0 ),
- JS_CFUNC_MAGIC_DEF("keys", 0, js_create_map_iterator, (JS_ITERATOR_KIND_KEY << 2) | 0 ),
- JS_CFUNC_MAGIC_DEF("entries", 0, js_create_map_iterator, (JS_ITERATOR_KIND_KEY_AND_VALUE << 2) | 0 ),
- JS_ALIAS_DEF("[Symbol.iterator]", "entries" ),
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "Map", JS_PROP_CONFIGURABLE ),
- };
- static const JSCFunctionListEntry js_map_iterator_proto_funcs[] = {
- JS_ITERATOR_NEXT_DEF("next", 0, js_map_iterator_next, 0 ),
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "Map Iterator", JS_PROP_CONFIGURABLE ),
- };
- static const JSCFunctionListEntry js_set_proto_funcs[] = {
- JS_CFUNC_MAGIC_DEF("add", 1, js_map_set, MAGIC_SET ),
- JS_CFUNC_MAGIC_DEF("has", 1, js_map_has, MAGIC_SET ),
- JS_CFUNC_MAGIC_DEF("delete", 1, js_map_delete, MAGIC_SET ),
- JS_CFUNC_MAGIC_DEF("clear", 0, js_map_clear, MAGIC_SET ),
- JS_CGETSET_MAGIC_DEF("size", js_map_get_size, NULL, MAGIC_SET ),
- JS_CFUNC_MAGIC_DEF("forEach", 1, js_map_forEach, MAGIC_SET ),
- JS_CFUNC_DEF("isDisjointFrom", 1, js_set_isDisjointFrom ),
- JS_CFUNC_DEF("isSubsetOf", 1, js_set_isSubsetOf ),
- JS_CFUNC_DEF("isSupersetOf", 1, js_set_isSupersetOf ),
- JS_CFUNC_DEF("intersection", 1, js_set_intersection ),
- JS_CFUNC_DEF("difference", 1, js_set_difference ),
- JS_CFUNC_DEF("symmetricDifference", 1, js_set_symmetricDifference ),
- JS_CFUNC_DEF("union", 1, js_set_union ),
- JS_CFUNC_MAGIC_DEF("values", 0, js_create_map_iterator, (JS_ITERATOR_KIND_KEY << 2) | MAGIC_SET ),
- JS_ALIAS_DEF("keys", "values" ),
- JS_ALIAS_DEF("[Symbol.iterator]", "values" ),
- JS_CFUNC_MAGIC_DEF("entries", 0, js_create_map_iterator, (JS_ITERATOR_KIND_KEY_AND_VALUE << 2) | MAGIC_SET ),
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "Set", JS_PROP_CONFIGURABLE ),
- };
- static const JSCFunctionListEntry js_set_iterator_proto_funcs[] = {
- JS_ITERATOR_NEXT_DEF("next", 0, js_map_iterator_next, MAGIC_SET ),
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "Set Iterator", JS_PROP_CONFIGURABLE ),
- };
- static const JSCFunctionListEntry js_weak_map_proto_funcs[] = {
- JS_CFUNC_MAGIC_DEF("set", 2, js_map_set, MAGIC_WEAK ),
- JS_CFUNC_MAGIC_DEF("get", 1, js_map_get, MAGIC_WEAK ),
- JS_CFUNC_MAGIC_DEF("getOrInsert", 2, js_map_getOrInsert,
- (JS_CLASS_WEAKMAP << 1) | /*computed*/FALSE ),
- JS_CFUNC_MAGIC_DEF("getOrInsertComputed", 2, js_map_getOrInsert,
- (JS_CLASS_WEAKMAP << 1) | /*computed*/TRUE ),
- JS_CFUNC_MAGIC_DEF("has", 1, js_map_has, MAGIC_WEAK ),
- JS_CFUNC_MAGIC_DEF("delete", 1, js_map_delete, MAGIC_WEAK ),
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "WeakMap", JS_PROP_CONFIGURABLE ),
- };
- static const JSCFunctionListEntry js_weak_set_proto_funcs[] = {
- JS_CFUNC_MAGIC_DEF("add", 1, js_map_set, MAGIC_SET | MAGIC_WEAK ),
- JS_CFUNC_MAGIC_DEF("has", 1, js_map_has, MAGIC_SET | MAGIC_WEAK ),
- JS_CFUNC_MAGIC_DEF("delete", 1, js_map_delete, MAGIC_SET | MAGIC_WEAK ),
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "WeakSet", JS_PROP_CONFIGURABLE ),
- };
- static const JSCFunctionListEntry * const js_map_proto_funcs_ptr[6] = {
- js_map_proto_funcs,
- js_set_proto_funcs,
- js_weak_map_proto_funcs,
- js_weak_set_proto_funcs,
- js_map_iterator_proto_funcs,
- js_set_iterator_proto_funcs,
- };
- static const uint8_t js_map_proto_funcs_count[6] = {
- countof(js_map_proto_funcs),
- countof(js_set_proto_funcs),
- countof(js_weak_map_proto_funcs),
- countof(js_weak_set_proto_funcs),
- countof(js_map_iterator_proto_funcs),
- countof(js_set_iterator_proto_funcs),
- };
- int JS_AddIntrinsicMapSet(JSContext *ctx)
- {
- int i;
- JSValue obj1;
- char buf[ATOM_GET_STR_BUF_SIZE];
- for(i = 0; i < 4; i++) {
- JSCFunctionType ft;
- const char *name = JS_AtomGetStr(ctx, buf, sizeof(buf),
- JS_ATOM_Map + i);
- ft.constructor_magic = js_map_constructor;
- obj1 = JS_NewCConstructor(ctx, JS_CLASS_MAP + i, name,
- ft.generic, 0, JS_CFUNC_constructor_magic, i,
- JS_UNDEFINED,
- js_map_funcs, i < 2 ? countof(js_map_funcs) : 0,
- js_map_proto_funcs_ptr[i], js_map_proto_funcs_count[i],
- 0);
- if (JS_IsException(obj1))
- return -1;
- JS_FreeValue(ctx, obj1);
- }
- for(i = 0; i < 2; i++) {
- ctx->class_proto[JS_CLASS_MAP_ITERATOR + i] =
- JS_NewObjectProtoList(ctx, ctx->class_proto[JS_CLASS_ITERATOR],
- js_map_proto_funcs_ptr[i + 4],
- js_map_proto_funcs_count[i + 4]);
- if (JS_IsException(ctx->class_proto[JS_CLASS_MAP_ITERATOR + i]))
- return -1;
- }
- return 0;
- }
- /* Generator */
- static const JSCFunctionListEntry js_generator_function_proto_funcs[] = {
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "GeneratorFunction", JS_PROP_CONFIGURABLE),
- };
- static const JSCFunctionListEntry js_generator_proto_funcs[] = {
- JS_ITERATOR_NEXT_DEF("next", 1, js_generator_next, GEN_MAGIC_NEXT ),
- JS_ITERATOR_NEXT_DEF("return", 1, js_generator_next, GEN_MAGIC_RETURN ),
- JS_ITERATOR_NEXT_DEF("throw", 1, js_generator_next, GEN_MAGIC_THROW ),
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "Generator", JS_PROP_CONFIGURABLE),
- };
- /* Promise */
- typedef struct JSPromiseData {
- JSPromiseStateEnum promise_state;
- /* 0=fulfill, 1=reject, list of JSPromiseReactionData.link */
- struct list_head promise_reactions[2];
- BOOL is_handled; /* Note: only useful to debug */
- JSValue promise_result;
- } JSPromiseData;
- typedef struct JSPromiseFunctionDataResolved {
- int ref_count;
- BOOL already_resolved;
- } JSPromiseFunctionDataResolved;
- typedef struct JSPromiseFunctionData {
- JSValue promise;
- JSPromiseFunctionDataResolved *presolved;
- } JSPromiseFunctionData;
- typedef struct JSPromiseReactionData {
- struct list_head link; /* not used in promise_reaction_job */
- JSValue resolving_funcs[2];
- JSValue handler;
- } JSPromiseReactionData;
- JSPromiseStateEnum JS_PromiseState(JSContext *ctx, JSValue promise)
- {
- JSPromiseData *s = JS_GetOpaque(promise, JS_CLASS_PROMISE);
- if (!s)
- return -1;
- return s->promise_state;
- }
- JSValue JS_PromiseResult(JSContext *ctx, JSValue promise)
- {
- JSPromiseData *s = JS_GetOpaque(promise, JS_CLASS_PROMISE);
- if (!s)
- return JS_UNDEFINED;
- return JS_DupValue(ctx, s->promise_result);
- }
- static int js_create_resolving_functions(JSContext *ctx, JSValue *args,
- JSValueConst promise);
- static void promise_reaction_data_free(JSRuntime *rt,
- JSPromiseReactionData *rd)
- {
- JS_FreeValueRT(rt, rd->resolving_funcs[0]);
- JS_FreeValueRT(rt, rd->resolving_funcs[1]);
- JS_FreeValueRT(rt, rd->handler);
- js_free_rt(rt, rd);
- }
- static JSValue promise_reaction_job(JSContext *ctx, int argc,
- JSValueConst *argv)
- {
- JSValueConst handler, arg, func;
- JSValue res, res2;
- BOOL is_reject;
- assert(argc == 5);
- handler = argv[2];
- is_reject = JS_ToBool(ctx, argv[3]);
- arg = argv[4];
- #ifdef DUMP_PROMISE
- printf("promise_reaction_job: is_reject=%d\n", is_reject);
- #endif
- if (JS_IsUndefined(handler)) {
- if (is_reject) {
- res = JS_Throw(ctx, JS_DupValue(ctx, arg));
- } else {
- res = JS_DupValue(ctx, arg);
- }
- } else {
- res = JS_Call(ctx, handler, JS_UNDEFINED, 1, &arg);
- }
- is_reject = JS_IsException(res);
- if (is_reject)
- res = JS_GetException(ctx);
- func = argv[is_reject];
- /* as an extension, we support undefined as value to avoid
- creating a dummy promise in the 'await' implementation of async
- functions */
- if (!JS_IsUndefined(func)) {
- res2 = JS_Call(ctx, func, JS_UNDEFINED,
- 1, (JSValueConst *)&res);
- } else {
- res2 = JS_UNDEFINED;
- }
- JS_FreeValue(ctx, res);
- return res2;
- }
- void JS_SetHostPromiseRejectionTracker(JSRuntime *rt,
- JSHostPromiseRejectionTracker *cb,
- void *opaque)
- {
- rt->host_promise_rejection_tracker = cb;
- rt->host_promise_rejection_tracker_opaque = opaque;
- }
- static void fulfill_or_reject_promise(JSContext *ctx, JSValueConst promise,
- JSValueConst value, BOOL is_reject)
- {
- JSPromiseData *s = JS_GetOpaque(promise, JS_CLASS_PROMISE);
- struct list_head *el, *el1;
- JSPromiseReactionData *rd;
- JSValueConst args[5];
- if (!s || s->promise_state != JS_PROMISE_PENDING)
- return; /* should never happen */
- set_value(ctx, &s->promise_result, JS_DupValue(ctx, value));
- s->promise_state = JS_PROMISE_FULFILLED + is_reject;
- #ifdef DUMP_PROMISE
- printf("fulfill_or_reject_promise: is_reject=%d\n", is_reject);
- #endif
- if (s->promise_state == JS_PROMISE_REJECTED && !s->is_handled) {
- JSRuntime *rt = ctx->rt;
- if (rt->host_promise_rejection_tracker) {
- rt->host_promise_rejection_tracker(ctx, promise, value, FALSE,
- rt->host_promise_rejection_tracker_opaque);
- }
- }
- list_for_each_safe(el, el1, &s->promise_reactions[is_reject]) {
- rd = list_entry(el, JSPromiseReactionData, link);
- args[0] = rd->resolving_funcs[0];
- args[1] = rd->resolving_funcs[1];
- args[2] = rd->handler;
- args[3] = JS_NewBool(ctx, is_reject);
- args[4] = value;
- JS_EnqueueJob(ctx, promise_reaction_job, 5, args);
- list_del(&rd->link);
- promise_reaction_data_free(ctx->rt, rd);
- }
- list_for_each_safe(el, el1, &s->promise_reactions[1 - is_reject]) {
- rd = list_entry(el, JSPromiseReactionData, link);
- list_del(&rd->link);
- promise_reaction_data_free(ctx->rt, rd);
- }
- }
- static void reject_promise(JSContext *ctx, JSValueConst promise,
- JSValueConst value)
- {
- fulfill_or_reject_promise(ctx, promise, value, TRUE);
- }
- static JSValue js_promise_resolve_thenable_job(JSContext *ctx,
- int argc, JSValueConst *argv)
- {
- JSValueConst promise, thenable, then;
- JSValue args[2], res;
- #ifdef DUMP_PROMISE
- printf("js_promise_resolve_thenable_job\n");
- #endif
- assert(argc == 3);
- promise = argv[0];
- thenable = argv[1];
- then = argv[2];
- if (js_create_resolving_functions(ctx, args, promise) < 0)
- return JS_EXCEPTION;
- res = JS_Call(ctx, then, thenable, 2, (JSValueConst *)args);
- if (JS_IsException(res)) {
- JSValue error = JS_GetException(ctx);
- res = JS_Call(ctx, args[1], JS_UNDEFINED, 1, (JSValueConst *)&error);
- JS_FreeValue(ctx, error);
- }
- JS_FreeValue(ctx, args[0]);
- JS_FreeValue(ctx, args[1]);
- return res;
- }
- static void js_promise_resolve_function_free_resolved(JSRuntime *rt,
- JSPromiseFunctionDataResolved *sr)
- {
- if (--sr->ref_count == 0) {
- js_free_rt(rt, sr);
- }
- }
- static int js_create_resolving_functions(JSContext *ctx,
- JSValue *resolving_funcs,
- JSValueConst promise)
- {
- JSValue obj;
- JSPromiseFunctionData *s;
- JSPromiseFunctionDataResolved *sr;
- int i, ret;
- sr = js_malloc(ctx, sizeof(*sr));
- if (!sr)
- return -1;
- sr->ref_count = 1;
- sr->already_resolved = FALSE; /* must be shared between the two functions */
- ret = 0;
- for(i = 0; i < 2; i++) {
- obj = JS_NewObjectProtoClass(ctx, ctx->function_proto,
- JS_CLASS_PROMISE_RESOLVE_FUNCTION + i);
- if (JS_IsException(obj))
- goto fail;
- s = js_malloc(ctx, sizeof(*s));
- if (!s) {
- JS_FreeValue(ctx, obj);
- fail:
- if (i != 0)
- JS_FreeValue(ctx, resolving_funcs[0]);
- ret = -1;
- break;
- }
- sr->ref_count++;
- s->presolved = sr;
- s->promise = JS_DupValue(ctx, promise);
- JS_SetOpaque(obj, s);
- js_function_set_properties(ctx, obj, JS_ATOM_empty_string, 1);
- resolving_funcs[i] = obj;
- }
- js_promise_resolve_function_free_resolved(ctx->rt, sr);
- return ret;
- }
- static void js_promise_resolve_function_finalizer(JSRuntime *rt, JSValue val)
- {
- JSPromiseFunctionData *s = JS_VALUE_GET_OBJ(val)->u.promise_function_data;
- if (s) {
- js_promise_resolve_function_free_resolved(rt, s->presolved);
- JS_FreeValueRT(rt, s->promise);
- js_free_rt(rt, s);
- }
- }
- static void js_promise_resolve_function_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func)
- {
- JSPromiseFunctionData *s = JS_VALUE_GET_OBJ(val)->u.promise_function_data;
- if (s) {
- JS_MarkValue(rt, s->promise, mark_func);
- }
- }
- static JSValue js_promise_resolve_function_call(JSContext *ctx,
- JSValueConst func_obj,
- JSValueConst this_val,
- int argc, JSValueConst *argv,
- int flags)
- {
- JSObject *p = JS_VALUE_GET_OBJ(func_obj);
- JSPromiseFunctionData *s;
- JSValueConst resolution, args[3];
- JSValue then;
- BOOL is_reject;
- s = p->u.promise_function_data;
- if (!s || s->presolved->already_resolved)
- return JS_UNDEFINED;
- s->presolved->already_resolved = TRUE;
- is_reject = p->class_id - JS_CLASS_PROMISE_RESOLVE_FUNCTION;
- if (argc > 0)
- resolution = argv[0];
- else
- resolution = JS_UNDEFINED;
- #ifdef DUMP_PROMISE
- printf("js_promise_resolving_function_call: is_reject=%d ", is_reject);
- JS_DumpValue(ctx, "resolution", resolution);
- printf("\n");
- #endif
- if (is_reject || !JS_IsObject(resolution)) {
- goto done;
- } else if (js_same_value(ctx, resolution, s->promise)) {
- JS_ThrowTypeError(ctx, "promise self resolution");
- goto fail_reject;
- }
- then = JS_GetProperty(ctx, resolution, JS_ATOM_then);
- if (JS_IsException(then)) {
- JSValue error;
- fail_reject:
- error = JS_GetException(ctx);
- reject_promise(ctx, s->promise, error);
- JS_FreeValue(ctx, error);
- } else if (!JS_IsFunction(ctx, then)) {
- JS_FreeValue(ctx, then);
- done:
- fulfill_or_reject_promise(ctx, s->promise, resolution, is_reject);
- } else {
- args[0] = s->promise;
- args[1] = resolution;
- args[2] = then;
- JS_EnqueueJob(ctx, js_promise_resolve_thenable_job, 3, args);
- JS_FreeValue(ctx, then);
- }
- return JS_UNDEFINED;
- }
- static void js_promise_finalizer(JSRuntime *rt, JSValue val)
- {
- JSPromiseData *s = JS_GetOpaque(val, JS_CLASS_PROMISE);
- struct list_head *el, *el1;
- int i;
- if (!s)
- return;
- for(i = 0; i < 2; i++) {
- list_for_each_safe(el, el1, &s->promise_reactions[i]) {
- JSPromiseReactionData *rd =
- list_entry(el, JSPromiseReactionData, link);
- promise_reaction_data_free(rt, rd);
- }
- }
- JS_FreeValueRT(rt, s->promise_result);
- js_free_rt(rt, s);
- }
- static void js_promise_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func)
- {
- JSPromiseData *s = JS_GetOpaque(val, JS_CLASS_PROMISE);
- struct list_head *el;
- int i;
- if (!s)
- return;
- for(i = 0; i < 2; i++) {
- list_for_each(el, &s->promise_reactions[i]) {
- JSPromiseReactionData *rd =
- list_entry(el, JSPromiseReactionData, link);
- JS_MarkValue(rt, rd->resolving_funcs[0], mark_func);
- JS_MarkValue(rt, rd->resolving_funcs[1], mark_func);
- JS_MarkValue(rt, rd->handler, mark_func);
- }
- }
- JS_MarkValue(rt, s->promise_result, mark_func);
- }
- static JSValue js_promise_constructor(JSContext *ctx, JSValueConst new_target,
- int argc, JSValueConst *argv)
- {
- JSValueConst executor;
- JSValue obj;
- JSPromiseData *s;
- JSValue args[2], ret;
- int i;
- executor = argv[0];
- if (check_function(ctx, executor))
- return JS_EXCEPTION;
- obj = js_create_from_ctor(ctx, new_target, JS_CLASS_PROMISE);
- if (JS_IsException(obj))
- return JS_EXCEPTION;
- s = js_mallocz(ctx, sizeof(*s));
- if (!s)
- goto fail;
- s->promise_state = JS_PROMISE_PENDING;
- s->is_handled = FALSE;
- for(i = 0; i < 2; i++)
- init_list_head(&s->promise_reactions[i]);
- s->promise_result = JS_UNDEFINED;
- JS_SetOpaque(obj, s);
- if (js_create_resolving_functions(ctx, args, obj))
- goto fail;
- ret = JS_Call(ctx, executor, JS_UNDEFINED, 2, (JSValueConst *)args);
- if (JS_IsException(ret)) {
- JSValue ret2, error;
- error = JS_GetException(ctx);
- ret2 = JS_Call(ctx, args[1], JS_UNDEFINED, 1, (JSValueConst *)&error);
- JS_FreeValue(ctx, error);
- if (JS_IsException(ret2))
- goto fail1;
- JS_FreeValue(ctx, ret2);
- }
- JS_FreeValue(ctx, ret);
- JS_FreeValue(ctx, args[0]);
- JS_FreeValue(ctx, args[1]);
- return obj;
- fail1:
- JS_FreeValue(ctx, args[0]);
- JS_FreeValue(ctx, args[1]);
- fail:
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue js_promise_executor(JSContext *ctx,
- JSValueConst this_val,
- int argc, JSValueConst *argv,
- int magic, JSValue *func_data)
- {
- int i;
- for(i = 0; i < 2; i++) {
- if (!JS_IsUndefined(func_data[i]))
- return JS_ThrowTypeError(ctx, "resolving function already set");
- func_data[i] = JS_DupValue(ctx, argv[i]);
- }
- return JS_UNDEFINED;
- }
- static JSValue js_promise_executor_new(JSContext *ctx)
- {
- JSValueConst func_data[2];
- func_data[0] = JS_UNDEFINED;
- func_data[1] = JS_UNDEFINED;
- return JS_NewCFunctionData(ctx, js_promise_executor, 2,
- 0, 2, func_data);
- }
- static JSValue js_new_promise_capability(JSContext *ctx,
- JSValue *resolving_funcs,
- JSValueConst ctor)
- {
- JSValue executor, result_promise;
- JSCFunctionDataRecord *s;
- int i;
- executor = js_promise_executor_new(ctx);
- if (JS_IsException(executor))
- return executor;
- if (JS_IsUndefined(ctor)) {
- result_promise = js_promise_constructor(ctx, ctor, 1,
- (JSValueConst *)&executor);
- } else {
- result_promise = JS_CallConstructor(ctx, ctor, 1,
- (JSValueConst *)&executor);
- }
- if (JS_IsException(result_promise))
- goto fail;
- s = JS_GetOpaque(executor, JS_CLASS_C_FUNCTION_DATA);
- for(i = 0; i < 2; i++) {
- if (check_function(ctx, s->data[i]))
- goto fail;
- }
- for(i = 0; i < 2; i++)
- resolving_funcs[i] = JS_DupValue(ctx, s->data[i]);
- JS_FreeValue(ctx, executor);
- return result_promise;
- fail:
- JS_FreeValue(ctx, executor);
- JS_FreeValue(ctx, result_promise);
- return JS_EXCEPTION;
- }
- JSValue JS_NewPromiseCapability(JSContext *ctx, JSValue *resolving_funcs)
- {
- return js_new_promise_capability(ctx, resolving_funcs, JS_UNDEFINED);
- }
- static JSValue js_promise_resolve(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- JSValue result_promise, resolving_funcs[2], ret;
- BOOL is_reject = magic;
- if (!JS_IsObject(this_val))
- return JS_ThrowTypeErrorNotAnObject(ctx);
- if (!is_reject && JS_GetOpaque(argv[0], JS_CLASS_PROMISE)) {
- JSValue ctor;
- BOOL is_same;
- ctor = JS_GetProperty(ctx, argv[0], JS_ATOM_constructor);
- if (JS_IsException(ctor))
- return ctor;
- is_same = js_same_value(ctx, ctor, this_val);
- JS_FreeValue(ctx, ctor);
- if (is_same)
- return JS_DupValue(ctx, argv[0]);
- }
- result_promise = js_new_promise_capability(ctx, resolving_funcs, this_val);
- if (JS_IsException(result_promise))
- return result_promise;
- ret = JS_Call(ctx, resolving_funcs[is_reject], JS_UNDEFINED, 1, argv);
- JS_FreeValue(ctx, resolving_funcs[0]);
- JS_FreeValue(ctx, resolving_funcs[1]);
- if (JS_IsException(ret)) {
- JS_FreeValue(ctx, result_promise);
- return ret;
- }
- JS_FreeValue(ctx, ret);
- return result_promise;
- }
- static JSValue js_promise_withResolvers(JSContext *ctx,
- JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue result_promise, resolving_funcs[2], obj;
- if (!JS_IsObject(this_val))
- return JS_ThrowTypeErrorNotAnObject(ctx);
- result_promise = js_new_promise_capability(ctx, resolving_funcs, this_val);
- if (JS_IsException(result_promise))
- return result_promise;
- obj = JS_NewObject(ctx);
- if (JS_IsException(obj))
- goto exception;
- if (JS_DefinePropertyValue(ctx, obj, JS_ATOM_promise, result_promise,
- JS_PROP_C_W_E) < 0) {
- goto exception;
- }
- result_promise = JS_UNDEFINED;
- if (JS_DefinePropertyValue(ctx, obj, JS_ATOM_resolve, resolving_funcs[0],
- JS_PROP_C_W_E) < 0) {
- goto exception;
- }
- resolving_funcs[0] = JS_UNDEFINED;
- if (JS_DefinePropertyValue(ctx, obj, JS_ATOM_reject, resolving_funcs[1],
- JS_PROP_C_W_E) < 0) {
- goto exception;
- }
- return obj;
- exception:
- JS_FreeValue(ctx, resolving_funcs[0]);
- JS_FreeValue(ctx, resolving_funcs[1]);
- JS_FreeValue(ctx, result_promise);
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue js_promise_try(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue result_promise, resolving_funcs[2], ret, ret2;
- BOOL is_reject = 0;
- if (!JS_IsObject(this_val))
- return JS_ThrowTypeErrorNotAnObject(ctx);
- result_promise = js_new_promise_capability(ctx, resolving_funcs, this_val);
- if (JS_IsException(result_promise))
- return result_promise;
- ret = JS_Call(ctx, argv[0], JS_UNDEFINED, argc - 1, argv + 1);
- if (JS_IsException(ret)) {
- is_reject = 1;
- ret = JS_GetException(ctx);
- }
- ret2 = JS_Call(ctx, resolving_funcs[is_reject], JS_UNDEFINED, 1, (JSValueConst *)&ret);
- JS_FreeValue(ctx, resolving_funcs[0]);
- JS_FreeValue(ctx, resolving_funcs[1]);
- JS_FreeValue(ctx, ret);
- if (JS_IsException(ret2)) {
- JS_FreeValue(ctx, result_promise);
- return ret2;
- }
- JS_FreeValue(ctx, ret2);
- return result_promise;
- }
- static __exception int remainingElementsCount_add(JSContext *ctx,
- JSValueConst resolve_element_env,
- int addend)
- {
- JSValue val;
- int remainingElementsCount;
- val = JS_GetPropertyUint32(ctx, resolve_element_env, 0);
- if (JS_IsException(val))
- return -1;
- if (JS_ToInt32Free(ctx, &remainingElementsCount, val))
- return -1;
- remainingElementsCount += addend;
- if (JS_SetPropertyUint32(ctx, resolve_element_env, 0,
- JS_NewInt32(ctx, remainingElementsCount)) < 0)
- return -1;
- return (remainingElementsCount == 0);
- }
- #define PROMISE_MAGIC_all 0
- #define PROMISE_MAGIC_allSettled 1
- #define PROMISE_MAGIC_any 2
- static JSValue js_promise_all_resolve_element(JSContext *ctx,
- JSValueConst this_val,
- int argc, JSValueConst *argv,
- int magic,
- JSValue *func_data)
- {
- int resolve_type = magic & 3;
- int is_reject = magic & 4;
- BOOL alreadyCalled = JS_ToBool(ctx, func_data[0]);
- JSValueConst values = func_data[2];
- JSValueConst resolve = func_data[3];
- JSValueConst resolve_element_env = func_data[4];
- JSValue ret, obj;
- int is_zero, index;
- if (JS_ToInt32(ctx, &index, func_data[1]))
- return JS_EXCEPTION;
- if (alreadyCalled)
- return JS_UNDEFINED;
- func_data[0] = JS_NewBool(ctx, TRUE);
- if (resolve_type == PROMISE_MAGIC_allSettled) {
- JSValue str;
- obj = JS_NewObject(ctx);
- if (JS_IsException(obj))
- return JS_EXCEPTION;
- str = js_new_string8(ctx, is_reject ? "rejected" : "fulfilled");
- if (JS_IsException(str))
- goto fail1;
- if (JS_DefinePropertyValue(ctx, obj, JS_ATOM_status,
- str,
- JS_PROP_C_W_E) < 0)
- goto fail1;
- if (JS_DefinePropertyValue(ctx, obj,
- is_reject ? JS_ATOM_reason : JS_ATOM_value,
- JS_DupValue(ctx, argv[0]),
- JS_PROP_C_W_E) < 0) {
- fail1:
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- } else {
- obj = JS_DupValue(ctx, argv[0]);
- }
- if (JS_DefinePropertyValueUint32(ctx, values, index,
- obj, JS_PROP_C_W_E) < 0)
- return JS_EXCEPTION;
- is_zero = remainingElementsCount_add(ctx, resolve_element_env, -1);
- if (is_zero < 0)
- return JS_EXCEPTION;
- if (is_zero) {
- if (resolve_type == PROMISE_MAGIC_any) {
- JSValue error;
- error = js_aggregate_error_constructor(ctx, values);
- if (JS_IsException(error))
- return JS_EXCEPTION;
- ret = JS_Call(ctx, resolve, JS_UNDEFINED, 1, (JSValueConst *)&error);
- JS_FreeValue(ctx, error);
- } else {
- ret = JS_Call(ctx, resolve, JS_UNDEFINED, 1, (JSValueConst *)&values);
- }
- if (JS_IsException(ret))
- return ret;
- JS_FreeValue(ctx, ret);
- }
- return JS_UNDEFINED;
- }
- /* magic = 0: Promise.all 1: Promise.allSettled */
- static JSValue js_promise_all(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- JSValue result_promise, resolving_funcs[2], item, next_promise, ret;
- JSValue next_method = JS_UNDEFINED, values = JS_UNDEFINED;
- JSValue resolve_element_env = JS_UNDEFINED, resolve_element, reject_element;
- JSValue promise_resolve = JS_UNDEFINED, iter = JS_UNDEFINED;
- JSValueConst then_args[2], resolve_element_data[5];
- BOOL done;
- int index, is_zero, is_promise_any = (magic == PROMISE_MAGIC_any);
- if (!JS_IsObject(this_val))
- return JS_ThrowTypeErrorNotAnObject(ctx);
- result_promise = js_new_promise_capability(ctx, resolving_funcs, this_val);
- if (JS_IsException(result_promise))
- return result_promise;
- promise_resolve = JS_GetProperty(ctx, this_val, JS_ATOM_resolve);
- if (JS_IsException(promise_resolve) ||
- check_function(ctx, promise_resolve))
- goto fail_reject;
- iter = JS_GetIterator(ctx, argv[0], FALSE);
- if (JS_IsException(iter)) {
- JSValue error;
- fail_reject:
- error = JS_GetException(ctx);
- ret = JS_Call(ctx, resolving_funcs[1], JS_UNDEFINED, 1,
- (JSValueConst *)&error);
- JS_FreeValue(ctx, error);
- if (JS_IsException(ret))
- goto fail;
- JS_FreeValue(ctx, ret);
- } else {
- next_method = JS_GetProperty(ctx, iter, JS_ATOM_next);
- if (JS_IsException(next_method))
- goto fail_reject;
- values = JS_NewArray(ctx);
- if (JS_IsException(values))
- goto fail_reject;
- resolve_element_env = JS_NewArray(ctx);
- if (JS_IsException(resolve_element_env))
- goto fail_reject;
- /* remainingElementsCount field */
- if (JS_DefinePropertyValueUint32(ctx, resolve_element_env, 0,
- JS_NewInt32(ctx, 1),
- JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE | JS_PROP_WRITABLE) < 0)
- goto fail_reject;
- index = 0;
- for(;;) {
- /* XXX: conformance: should close the iterator if error on 'done'
- access, but not on 'value' access */
- item = JS_IteratorNext(ctx, iter, next_method, 0, NULL, &done);
- if (JS_IsException(item))
- goto fail_reject;
- if (done)
- break;
- next_promise = JS_Call(ctx, promise_resolve,
- this_val, 1, (JSValueConst *)&item);
- JS_FreeValue(ctx, item);
- if (JS_IsException(next_promise)) {
- fail_reject1:
- JS_IteratorClose(ctx, iter, TRUE);
- goto fail_reject;
- }
- resolve_element_data[0] = JS_NewBool(ctx, FALSE);
- resolve_element_data[1] = (JSValueConst)JS_NewInt32(ctx, index);
- resolve_element_data[2] = values;
- resolve_element_data[3] = resolving_funcs[is_promise_any];
- resolve_element_data[4] = resolve_element_env;
- resolve_element =
- JS_NewCFunctionData(ctx, js_promise_all_resolve_element, 1,
- magic, 5, resolve_element_data);
- if (JS_IsException(resolve_element)) {
- JS_FreeValue(ctx, next_promise);
- goto fail_reject1;
- }
- if (magic == PROMISE_MAGIC_allSettled) {
- reject_element =
- JS_NewCFunctionData(ctx, js_promise_all_resolve_element, 1,
- magic | 4, 5, resolve_element_data);
- if (JS_IsException(reject_element)) {
- JS_FreeValue(ctx, next_promise);
- goto fail_reject1;
- }
- } else if (magic == PROMISE_MAGIC_any) {
- if (JS_DefinePropertyValueUint32(ctx, values, index,
- JS_UNDEFINED, JS_PROP_C_W_E) < 0)
- goto fail_reject1;
- reject_element = resolve_element;
- resolve_element = JS_DupValue(ctx, resolving_funcs[0]);
- } else {
- reject_element = JS_DupValue(ctx, resolving_funcs[1]);
- }
- if (remainingElementsCount_add(ctx, resolve_element_env, 1) < 0) {
- JS_FreeValue(ctx, next_promise);
- JS_FreeValue(ctx, resolve_element);
- JS_FreeValue(ctx, reject_element);
- goto fail_reject1;
- }
- then_args[0] = resolve_element;
- then_args[1] = reject_element;
- ret = JS_InvokeFree(ctx, next_promise, JS_ATOM_then, 2, then_args);
- JS_FreeValue(ctx, resolve_element);
- JS_FreeValue(ctx, reject_element);
- if (check_exception_free(ctx, ret))
- goto fail_reject1;
- index++;
- }
- is_zero = remainingElementsCount_add(ctx, resolve_element_env, -1);
- if (is_zero < 0)
- goto fail_reject;
- if (is_zero) {
- if (magic == PROMISE_MAGIC_any) {
- JSValue error;
- error = js_aggregate_error_constructor(ctx, values);
- if (JS_IsException(error))
- goto fail_reject;
- JS_FreeValue(ctx, values);
- values = error;
- }
- ret = JS_Call(ctx, resolving_funcs[is_promise_any], JS_UNDEFINED,
- 1, (JSValueConst *)&values);
- if (check_exception_free(ctx, ret))
- goto fail_reject;
- }
- }
- done:
- JS_FreeValue(ctx, promise_resolve);
- JS_FreeValue(ctx, resolve_element_env);
- JS_FreeValue(ctx, values);
- JS_FreeValue(ctx, next_method);
- JS_FreeValue(ctx, iter);
- JS_FreeValue(ctx, resolving_funcs[0]);
- JS_FreeValue(ctx, resolving_funcs[1]);
- return result_promise;
- fail:
- JS_FreeValue(ctx, result_promise);
- result_promise = JS_EXCEPTION;
- goto done;
- }
- static JSValue js_promise_race(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue result_promise, resolving_funcs[2], item, next_promise, ret;
- JSValue next_method = JS_UNDEFINED, iter = JS_UNDEFINED;
- JSValue promise_resolve = JS_UNDEFINED;
- BOOL done;
- if (!JS_IsObject(this_val))
- return JS_ThrowTypeErrorNotAnObject(ctx);
- result_promise = js_new_promise_capability(ctx, resolving_funcs, this_val);
- if (JS_IsException(result_promise))
- return result_promise;
- promise_resolve = JS_GetProperty(ctx, this_val, JS_ATOM_resolve);
- if (JS_IsException(promise_resolve) ||
- check_function(ctx, promise_resolve))
- goto fail_reject;
- iter = JS_GetIterator(ctx, argv[0], FALSE);
- if (JS_IsException(iter)) {
- JSValue error;
- fail_reject:
- error = JS_GetException(ctx);
- ret = JS_Call(ctx, resolving_funcs[1], JS_UNDEFINED, 1,
- (JSValueConst *)&error);
- JS_FreeValue(ctx, error);
- if (JS_IsException(ret))
- goto fail;
- JS_FreeValue(ctx, ret);
- } else {
- next_method = JS_GetProperty(ctx, iter, JS_ATOM_next);
- if (JS_IsException(next_method))
- goto fail_reject;
- for(;;) {
- /* XXX: conformance: should close the iterator if error on 'done'
- access, but not on 'value' access */
- item = JS_IteratorNext(ctx, iter, next_method, 0, NULL, &done);
- if (JS_IsException(item))
- goto fail_reject;
- if (done)
- break;
- next_promise = JS_Call(ctx, promise_resolve,
- this_val, 1, (JSValueConst *)&item);
- JS_FreeValue(ctx, item);
- if (JS_IsException(next_promise)) {
- fail_reject1:
- JS_IteratorClose(ctx, iter, TRUE);
- goto fail_reject;
- }
- ret = JS_InvokeFree(ctx, next_promise, JS_ATOM_then, 2,
- (JSValueConst *)resolving_funcs);
- if (check_exception_free(ctx, ret))
- goto fail_reject1;
- }
- }
- done:
- JS_FreeValue(ctx, promise_resolve);
- JS_FreeValue(ctx, next_method);
- JS_FreeValue(ctx, iter);
- JS_FreeValue(ctx, resolving_funcs[0]);
- JS_FreeValue(ctx, resolving_funcs[1]);
- return result_promise;
- fail:
- //JS_FreeValue(ctx, next_method); // why not???
- JS_FreeValue(ctx, result_promise);
- result_promise = JS_EXCEPTION;
- goto done;
- }
- static __exception int perform_promise_then(JSContext *ctx,
- JSValueConst promise,
- JSValueConst *resolve_reject,
- JSValueConst *cap_resolving_funcs)
- {
- JSPromiseData *s = JS_GetOpaque(promise, JS_CLASS_PROMISE);
- JSPromiseReactionData *rd_array[2], *rd;
- int i, j;
- rd_array[0] = NULL;
- rd_array[1] = NULL;
- for(i = 0; i < 2; i++) {
- JSValueConst handler;
- rd = js_mallocz(ctx, sizeof(*rd));
- if (!rd) {
- if (i == 1)
- promise_reaction_data_free(ctx->rt, rd_array[0]);
- return -1;
- }
- for(j = 0; j < 2; j++)
- rd->resolving_funcs[j] = JS_DupValue(ctx, cap_resolving_funcs[j]);
- handler = resolve_reject[i];
- if (!JS_IsFunction(ctx, handler))
- handler = JS_UNDEFINED;
- rd->handler = JS_DupValue(ctx, handler);
- rd_array[i] = rd;
- }
- if (s->promise_state == JS_PROMISE_PENDING) {
- for(i = 0; i < 2; i++)
- list_add_tail(&rd_array[i]->link, &s->promise_reactions[i]);
- } else {
- JSValueConst args[5];
- if (s->promise_state == JS_PROMISE_REJECTED && !s->is_handled) {
- JSRuntime *rt = ctx->rt;
- if (rt->host_promise_rejection_tracker) {
- rt->host_promise_rejection_tracker(ctx, promise, s->promise_result,
- TRUE, rt->host_promise_rejection_tracker_opaque);
- }
- }
- i = s->promise_state - JS_PROMISE_FULFILLED;
- rd = rd_array[i];
- args[0] = rd->resolving_funcs[0];
- args[1] = rd->resolving_funcs[1];
- args[2] = rd->handler;
- args[3] = JS_NewBool(ctx, i);
- args[4] = s->promise_result;
- JS_EnqueueJob(ctx, promise_reaction_job, 5, args);
- for(i = 0; i < 2; i++)
- promise_reaction_data_free(ctx->rt, rd_array[i]);
- }
- s->is_handled = TRUE;
- return 0;
- }
- static JSValue js_promise_then(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue ctor, result_promise, resolving_funcs[2];
- JSPromiseData *s;
- int i, ret;
- s = JS_GetOpaque2(ctx, this_val, JS_CLASS_PROMISE);
- if (!s)
- return JS_EXCEPTION;
- ctor = JS_SpeciesConstructor(ctx, this_val, JS_UNDEFINED);
- if (JS_IsException(ctor))
- return ctor;
- result_promise = js_new_promise_capability(ctx, resolving_funcs, ctor);
- JS_FreeValue(ctx, ctor);
- if (JS_IsException(result_promise))
- return result_promise;
- ret = perform_promise_then(ctx, this_val, argv,
- (JSValueConst *)resolving_funcs);
- for(i = 0; i < 2; i++)
- JS_FreeValue(ctx, resolving_funcs[i]);
- if (ret) {
- JS_FreeValue(ctx, result_promise);
- return JS_EXCEPTION;
- }
- return result_promise;
- }
- static JSValue js_promise_catch(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValueConst args[2];
- args[0] = JS_UNDEFINED;
- args[1] = argv[0];
- return JS_Invoke(ctx, this_val, JS_ATOM_then, 2, args);
- }
- static JSValue js_promise_finally_value_thunk(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv,
- int magic, JSValue *func_data)
- {
- return JS_DupValue(ctx, func_data[0]);
- }
- static JSValue js_promise_finally_thrower(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv,
- int magic, JSValue *func_data)
- {
- return JS_Throw(ctx, JS_DupValue(ctx, func_data[0]));
- }
- static JSValue js_promise_then_finally_func(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv,
- int magic, JSValue *func_data)
- {
- JSValueConst ctor = func_data[0];
- JSValueConst onFinally = func_data[1];
- JSValue res, promise, ret, then_func;
- res = JS_Call(ctx, onFinally, JS_UNDEFINED, 0, NULL);
- if (JS_IsException(res))
- return res;
- promise = js_promise_resolve(ctx, ctor, 1, (JSValueConst *)&res, 0);
- JS_FreeValue(ctx, res);
- if (JS_IsException(promise))
- return promise;
- if (magic == 0) {
- then_func = JS_NewCFunctionData(ctx, js_promise_finally_value_thunk, 0,
- 0, 1, argv);
- } else {
- then_func = JS_NewCFunctionData(ctx, js_promise_finally_thrower, 0,
- 0, 1, argv);
- }
- if (JS_IsException(then_func)) {
- JS_FreeValue(ctx, promise);
- return then_func;
- }
- ret = JS_InvokeFree(ctx, promise, JS_ATOM_then, 1, (JSValueConst *)&then_func);
- JS_FreeValue(ctx, then_func);
- return ret;
- }
- static JSValue js_promise_finally(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValueConst onFinally = argv[0];
- JSValue ctor, ret;
- JSValue then_funcs[2];
- JSValueConst func_data[2];
- int i;
- ctor = JS_SpeciesConstructor(ctx, this_val, JS_UNDEFINED);
- if (JS_IsException(ctor))
- return ctor;
- if (!JS_IsFunction(ctx, onFinally)) {
- then_funcs[0] = JS_DupValue(ctx, onFinally);
- then_funcs[1] = JS_DupValue(ctx, onFinally);
- } else {
- func_data[0] = ctor;
- func_data[1] = onFinally;
- for(i = 0; i < 2; i++) {
- then_funcs[i] = JS_NewCFunctionData(ctx, js_promise_then_finally_func, 1, i, 2, func_data);
- if (JS_IsException(then_funcs[i])) {
- if (i == 1)
- JS_FreeValue(ctx, then_funcs[0]);
- JS_FreeValue(ctx, ctor);
- return JS_EXCEPTION;
- }
- }
- }
- JS_FreeValue(ctx, ctor);
- ret = JS_Invoke(ctx, this_val, JS_ATOM_then, 2, (JSValueConst *)then_funcs);
- JS_FreeValue(ctx, then_funcs[0]);
- JS_FreeValue(ctx, then_funcs[1]);
- return ret;
- }
- static const JSCFunctionListEntry js_promise_funcs[] = {
- JS_CFUNC_MAGIC_DEF("resolve", 1, js_promise_resolve, 0 ),
- JS_CFUNC_MAGIC_DEF("reject", 1, js_promise_resolve, 1 ),
- JS_CFUNC_MAGIC_DEF("all", 1, js_promise_all, PROMISE_MAGIC_all ),
- JS_CFUNC_MAGIC_DEF("allSettled", 1, js_promise_all, PROMISE_MAGIC_allSettled ),
- JS_CFUNC_MAGIC_DEF("any", 1, js_promise_all, PROMISE_MAGIC_any ),
- JS_CFUNC_DEF("try", 1, js_promise_try ),
- JS_CFUNC_DEF("race", 1, js_promise_race ),
- JS_CFUNC_DEF("withResolvers", 0, js_promise_withResolvers ),
- JS_CGETSET_DEF("[Symbol.species]", js_get_this, NULL),
- };
- static const JSCFunctionListEntry js_promise_proto_funcs[] = {
- JS_CFUNC_DEF("then", 2, js_promise_then ),
- JS_CFUNC_DEF("catch", 1, js_promise_catch ),
- JS_CFUNC_DEF("finally", 1, js_promise_finally ),
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "Promise", JS_PROP_CONFIGURABLE ),
- };
- /* AsyncFunction */
- static const JSCFunctionListEntry js_async_function_proto_funcs[] = {
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "AsyncFunction", JS_PROP_CONFIGURABLE ),
- };
- /* AsyncIteratorPrototype */
- static const JSCFunctionListEntry js_async_iterator_proto_funcs[] = {
- JS_CFUNC_DEF("[Symbol.asyncIterator]", 0, js_iterator_proto_iterator ),
- };
- /* AsyncFromSyncIteratorPrototype */
- typedef struct JSAsyncFromSyncIteratorData {
- JSValue sync_iter;
- JSValue next_method;
- } JSAsyncFromSyncIteratorData;
- static void js_async_from_sync_iterator_finalizer(JSRuntime *rt, JSValue val)
- {
- JSAsyncFromSyncIteratorData *s =
- JS_GetOpaque(val, JS_CLASS_ASYNC_FROM_SYNC_ITERATOR);
- if (s) {
- JS_FreeValueRT(rt, s->sync_iter);
- JS_FreeValueRT(rt, s->next_method);
- js_free_rt(rt, s);
- }
- }
- static void js_async_from_sync_iterator_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func)
- {
- JSAsyncFromSyncIteratorData *s =
- JS_GetOpaque(val, JS_CLASS_ASYNC_FROM_SYNC_ITERATOR);
- if (s) {
- JS_MarkValue(rt, s->sync_iter, mark_func);
- JS_MarkValue(rt, s->next_method, mark_func);
- }
- }
- static JSValue JS_CreateAsyncFromSyncIterator(JSContext *ctx,
- JSValueConst sync_iter)
- {
- JSValue async_iter, next_method;
- JSAsyncFromSyncIteratorData *s;
- next_method = JS_GetProperty(ctx, sync_iter, JS_ATOM_next);
- if (JS_IsException(next_method))
- return JS_EXCEPTION;
- async_iter = JS_NewObjectClass(ctx, JS_CLASS_ASYNC_FROM_SYNC_ITERATOR);
- if (JS_IsException(async_iter)) {
- JS_FreeValue(ctx, next_method);
- return async_iter;
- }
- s = js_mallocz(ctx, sizeof(*s));
- if (!s) {
- JS_FreeValue(ctx, async_iter);
- JS_FreeValue(ctx, next_method);
- return JS_EXCEPTION;
- }
- s->sync_iter = JS_DupValue(ctx, sync_iter);
- s->next_method = next_method;
- JS_SetOpaque(async_iter, s);
- return async_iter;
- }
- static JSValue js_async_from_sync_iterator_unwrap(JSContext *ctx,
- JSValueConst this_val,
- int argc, JSValueConst *argv,
- int magic, JSValue *func_data)
- {
- return js_create_iterator_result(ctx, JS_DupValue(ctx, argv[0]),
- JS_ToBool(ctx, func_data[0]));
- }
- static JSValue js_async_from_sync_iterator_unwrap_func_create(JSContext *ctx,
- BOOL done)
- {
- JSValueConst func_data[1];
- func_data[0] = (JSValueConst)JS_NewBool(ctx, done);
- return JS_NewCFunctionData(ctx, js_async_from_sync_iterator_unwrap,
- 1, 0, 1, func_data);
- }
- static JSValue js_async_from_sync_iterator_close_wrap(JSContext *ctx,
- JSValueConst this_val,
- int argc, JSValueConst *argv,
- int magic, JSValue *func_data)
- {
- JS_Throw(ctx, JS_DupValue(ctx, argv[0]));
- JS_IteratorClose(ctx, func_data[0], TRUE);
- return JS_EXCEPTION;
- }
- static JSValue js_async_from_sync_iterator_close_wrap_func_create(JSContext *ctx, JSValueConst sync_iter)
- {
- return JS_NewCFunctionData(ctx, js_async_from_sync_iterator_close_wrap,
- 1, 0, 1, &sync_iter);
- }
- static JSValue js_async_from_sync_iterator_next(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv,
- int magic)
- {
- JSValue promise, resolving_funcs[2], value, err, method;
- JSAsyncFromSyncIteratorData *s;
- int done;
- int is_reject;
- promise = JS_NewPromiseCapability(ctx, resolving_funcs);
- if (JS_IsException(promise))
- return JS_EXCEPTION;
- s = JS_GetOpaque(this_val, JS_CLASS_ASYNC_FROM_SYNC_ITERATOR);
- if (!s) {
- JS_ThrowTypeError(ctx, "not an Async-from-Sync Iterator");
- goto reject;
- }
- if (magic == GEN_MAGIC_NEXT) {
- method = JS_DupValue(ctx, s->next_method);
- } else {
- method = JS_GetProperty(ctx, s->sync_iter,
- magic == GEN_MAGIC_RETURN ? JS_ATOM_return :
- JS_ATOM_throw);
- if (JS_IsException(method))
- goto reject;
- if (JS_IsUndefined(method) || JS_IsNull(method)) {
- if (magic == GEN_MAGIC_RETURN) {
- err = js_create_iterator_result(ctx, JS_DupValue(ctx, argv[0]), TRUE);
- is_reject = 0;
- goto done_resolve;
- } else {
- if (JS_IteratorClose(ctx, s->sync_iter, FALSE))
- goto reject;
- JS_ThrowTypeError(ctx, "throw is not a method");
- goto reject;
- }
- }
- }
- value = JS_IteratorNext2(ctx, s->sync_iter, method,
- argc >= 1 ? 1 : 0, argv, &done);
- JS_FreeValue(ctx, method);
- if (JS_IsException(value))
- goto reject;
- if (done == 2) {
- JSValue obj = value;
- value = JS_IteratorGetCompleteValue(ctx, obj, &done);
- JS_FreeValue(ctx, obj);
- if (JS_IsException(value))
- goto reject;
- }
-
- if (JS_IsException(value))
- goto reject;
- {
- JSValue value_wrapper_promise, resolve_reject[2];
- int res;
- value_wrapper_promise = js_promise_resolve(ctx, ctx->promise_ctor,
- 1, (JSValueConst *)&value, 0);
- if (JS_IsException(value_wrapper_promise)) {
- JSValue res2;
- JS_FreeValue(ctx, value);
- if (magic != GEN_MAGIC_RETURN && !done) {
- JS_IteratorClose(ctx, s->sync_iter, TRUE);
- }
- reject:
- err = JS_GetException(ctx);
- is_reject = 1;
- done_resolve:
- res2 = JS_Call(ctx, resolving_funcs[is_reject], JS_UNDEFINED,
- 1, (JSValueConst *)&err);
- JS_FreeValue(ctx, err);
- JS_FreeValue(ctx, res2);
- JS_FreeValue(ctx, resolving_funcs[0]);
- JS_FreeValue(ctx, resolving_funcs[1]);
- return promise;
- }
- resolve_reject[0] =
- js_async_from_sync_iterator_unwrap_func_create(ctx, done);
- if (JS_IsException(resolve_reject[0])) {
- JS_FreeValue(ctx, value_wrapper_promise);
- goto fail;
- }
- if (done || magic == GEN_MAGIC_RETURN) {
- resolve_reject[1] = JS_UNDEFINED;
- } else {
- resolve_reject[1] =
- js_async_from_sync_iterator_close_wrap_func_create(ctx, s->sync_iter);
- if (JS_IsException(resolve_reject[1])) {
- JS_FreeValue(ctx, value_wrapper_promise);
- JS_FreeValue(ctx, resolve_reject[0]);
- goto fail;
- }
- }
- JS_FreeValue(ctx, value);
- res = perform_promise_then(ctx, value_wrapper_promise,
- (JSValueConst *)resolve_reject,
- (JSValueConst *)resolving_funcs);
- JS_FreeValue(ctx, resolve_reject[0]);
- JS_FreeValue(ctx, resolve_reject[1]);
- JS_FreeValue(ctx, value_wrapper_promise);
- JS_FreeValue(ctx, resolving_funcs[0]);
- JS_FreeValue(ctx, resolving_funcs[1]);
- if (res) {
- JS_FreeValue(ctx, promise);
- return JS_EXCEPTION;
- }
- }
- return promise;
- fail:
- JS_FreeValue(ctx, value);
- JS_FreeValue(ctx, resolving_funcs[0]);
- JS_FreeValue(ctx, resolving_funcs[1]);
- JS_FreeValue(ctx, promise);
- return JS_EXCEPTION;
- }
- static const JSCFunctionListEntry js_async_from_sync_iterator_proto_funcs[] = {
- JS_CFUNC_MAGIC_DEF("next", 1, js_async_from_sync_iterator_next, GEN_MAGIC_NEXT ),
- JS_CFUNC_MAGIC_DEF("return", 1, js_async_from_sync_iterator_next, GEN_MAGIC_RETURN ),
- JS_CFUNC_MAGIC_DEF("throw", 1, js_async_from_sync_iterator_next, GEN_MAGIC_THROW ),
- };
- /* AsyncGeneratorFunction */
- static const JSCFunctionListEntry js_async_generator_function_proto_funcs[] = {
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "AsyncGeneratorFunction", JS_PROP_CONFIGURABLE ),
- };
- /* AsyncGenerator prototype */
- static const JSCFunctionListEntry js_async_generator_proto_funcs[] = {
- JS_CFUNC_MAGIC_DEF("next", 1, js_async_generator_next, GEN_MAGIC_NEXT ),
- JS_CFUNC_MAGIC_DEF("return", 1, js_async_generator_next, GEN_MAGIC_RETURN ),
- JS_CFUNC_MAGIC_DEF("throw", 1, js_async_generator_next, GEN_MAGIC_THROW ),
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "AsyncGenerator", JS_PROP_CONFIGURABLE ),
- };
- static JSClassShortDef const js_async_class_def[] = {
- { JS_ATOM_Promise, js_promise_finalizer, js_promise_mark }, /* JS_CLASS_PROMISE */
- { JS_ATOM_PromiseResolveFunction, js_promise_resolve_function_finalizer, js_promise_resolve_function_mark }, /* JS_CLASS_PROMISE_RESOLVE_FUNCTION */
- { JS_ATOM_PromiseRejectFunction, js_promise_resolve_function_finalizer, js_promise_resolve_function_mark }, /* JS_CLASS_PROMISE_REJECT_FUNCTION */
- { JS_ATOM_AsyncFunction, js_bytecode_function_finalizer, js_bytecode_function_mark }, /* JS_CLASS_ASYNC_FUNCTION */
- { JS_ATOM_AsyncFunctionResolve, js_async_function_resolve_finalizer, js_async_function_resolve_mark }, /* JS_CLASS_ASYNC_FUNCTION_RESOLVE */
- { JS_ATOM_AsyncFunctionReject, js_async_function_resolve_finalizer, js_async_function_resolve_mark }, /* JS_CLASS_ASYNC_FUNCTION_REJECT */
- { JS_ATOM_empty_string, js_async_from_sync_iterator_finalizer, js_async_from_sync_iterator_mark }, /* JS_CLASS_ASYNC_FROM_SYNC_ITERATOR */
- { JS_ATOM_AsyncGeneratorFunction, js_bytecode_function_finalizer, js_bytecode_function_mark }, /* JS_CLASS_ASYNC_GENERATOR_FUNCTION */
- { JS_ATOM_AsyncGenerator, js_async_generator_finalizer, js_async_generator_mark }, /* JS_CLASS_ASYNC_GENERATOR */
- };
- int JS_AddIntrinsicPromise(JSContext *ctx)
- {
- JSRuntime *rt = ctx->rt;
- JSValue obj1;
- JSCFunctionType ft;
- if (!JS_IsRegisteredClass(rt, JS_CLASS_PROMISE)) {
- if (init_class_range(rt, js_async_class_def, JS_CLASS_PROMISE,
- countof(js_async_class_def)))
- return -1;
- rt->class_array[JS_CLASS_PROMISE_RESOLVE_FUNCTION].call = js_promise_resolve_function_call;
- rt->class_array[JS_CLASS_PROMISE_REJECT_FUNCTION].call = js_promise_resolve_function_call;
- rt->class_array[JS_CLASS_ASYNC_FUNCTION].call = js_async_function_call;
- rt->class_array[JS_CLASS_ASYNC_FUNCTION_RESOLVE].call = js_async_function_resolve_call;
- rt->class_array[JS_CLASS_ASYNC_FUNCTION_REJECT].call = js_async_function_resolve_call;
- rt->class_array[JS_CLASS_ASYNC_GENERATOR_FUNCTION].call = js_async_generator_function_call;
- }
- /* Promise */
- obj1 = JS_NewCConstructor(ctx, JS_CLASS_PROMISE, "Promise",
- js_promise_constructor, 1, JS_CFUNC_constructor, 0,
- JS_UNDEFINED,
- js_promise_funcs, countof(js_promise_funcs),
- js_promise_proto_funcs, countof(js_promise_proto_funcs),
- 0);
- if (JS_IsException(obj1))
- return -1;
- ctx->promise_ctor = obj1;
-
- /* AsyncFunction */
- ft.generic_magic = js_function_constructor;
- obj1 = JS_NewCConstructor(ctx, JS_CLASS_ASYNC_FUNCTION, "AsyncFunction",
- ft.generic, 1, JS_CFUNC_constructor_or_func_magic, JS_FUNC_ASYNC,
- ctx->function_ctor,
- NULL, 0,
- js_async_function_proto_funcs, countof(js_async_function_proto_funcs),
- JS_NEW_CTOR_NO_GLOBAL | JS_NEW_CTOR_READONLY);
- if (JS_IsException(obj1))
- return -1;
- JS_FreeValue(ctx, obj1);
-
- /* AsyncIteratorPrototype */
- ctx->async_iterator_proto =
- JS_NewObjectProtoList(ctx, ctx->class_proto[JS_CLASS_OBJECT],
- js_async_iterator_proto_funcs,
- countof(js_async_iterator_proto_funcs));
- if (JS_IsException(ctx->async_iterator_proto))
- return -1;
- /* AsyncFromSyncIteratorPrototype */
- ctx->class_proto[JS_CLASS_ASYNC_FROM_SYNC_ITERATOR] =
- JS_NewObjectProtoList(ctx, ctx->async_iterator_proto,
- js_async_from_sync_iterator_proto_funcs,
- countof(js_async_from_sync_iterator_proto_funcs));
- if (JS_IsException(ctx->class_proto[JS_CLASS_ASYNC_FROM_SYNC_ITERATOR]))
- return -1;
-
- /* AsyncGeneratorPrototype */
- ctx->class_proto[JS_CLASS_ASYNC_GENERATOR] =
- JS_NewObjectProtoList(ctx, ctx->async_iterator_proto,
- js_async_generator_proto_funcs,
- countof(js_async_generator_proto_funcs));
- if (JS_IsException(ctx->class_proto[JS_CLASS_ASYNC_GENERATOR]))
- return -1;
- /* AsyncGeneratorFunction */
- ft.generic_magic = js_function_constructor;
- obj1 = JS_NewCConstructor(ctx, JS_CLASS_ASYNC_GENERATOR_FUNCTION, "AsyncGeneratorFunction",
- ft.generic, 1, JS_CFUNC_constructor_or_func_magic, JS_FUNC_ASYNC_GENERATOR,
- ctx->function_ctor,
- NULL, 0,
- js_async_generator_function_proto_funcs, countof(js_async_generator_function_proto_funcs),
- JS_NEW_CTOR_NO_GLOBAL | JS_NEW_CTOR_READONLY);
- if (JS_IsException(obj1))
- return -1;
- JS_FreeValue(ctx, obj1);
- return JS_SetConstructor2(ctx, ctx->class_proto[JS_CLASS_ASYNC_GENERATOR_FUNCTION],
- ctx->class_proto[JS_CLASS_ASYNC_GENERATOR],
- JS_PROP_CONFIGURABLE, JS_PROP_CONFIGURABLE);
- }
- /* URI handling */
- static int string_get_hex(JSString *p, int k, int n) {
- int c = 0, h;
- while (n-- > 0) {
- if ((h = from_hex(string_get(p, k++))) < 0)
- return -1;
- c = (c << 4) | h;
- }
- return c;
- }
- static int isURIReserved(int c) {
- return c < 0x100 && memchr(";/?:@&=+$,#", c, sizeof(";/?:@&=+$,#") - 1) != NULL;
- }
- static int __attribute__((format(printf, 2, 3))) js_throw_URIError(JSContext *ctx, const char *fmt, ...)
- {
- va_list ap;
- va_start(ap, fmt);
- JS_ThrowError(ctx, JS_URI_ERROR, fmt, ap);
- va_end(ap);
- return -1;
- }
- static int hex_decode(JSContext *ctx, JSString *p, int k) {
- int c;
- if (k >= p->len || string_get(p, k) != '%')
- return js_throw_URIError(ctx, "expecting %%");
- if (k + 2 >= p->len || (c = string_get_hex(p, k + 1, 2)) < 0)
- return js_throw_URIError(ctx, "expecting hex digit");
- return c;
- }
- static JSValue js_global_decodeURI(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int isComponent)
- {
- JSValue str;
- StringBuffer b_s, *b = &b_s;
- JSString *p;
- int k, c, c1, n, c_min;
- str = JS_ToString(ctx, argv[0]);
- if (JS_IsException(str))
- return str;
- string_buffer_init(ctx, b, 0);
- p = JS_VALUE_GET_STRING(str);
- for (k = 0; k < p->len;) {
- c = string_get(p, k);
- if (c == '%') {
- c = hex_decode(ctx, p, k);
- if (c < 0)
- goto fail;
- k += 3;
- if (c < 0x80) {
- if (!isComponent && isURIReserved(c)) {
- c = '%';
- k -= 2;
- }
- } else {
- /* Decode URI-encoded UTF-8 sequence */
- if (c >= 0xc0 && c <= 0xdf) {
- n = 1;
- c_min = 0x80;
- c &= 0x1f;
- } else if (c >= 0xe0 && c <= 0xef) {
- n = 2;
- c_min = 0x800;
- c &= 0xf;
- } else if (c >= 0xf0 && c <= 0xf7) {
- n = 3;
- c_min = 0x10000;
- c &= 0x7;
- } else {
- n = 0;
- c_min = 1;
- c = 0;
- }
- while (n-- > 0) {
- c1 = hex_decode(ctx, p, k);
- if (c1 < 0)
- goto fail;
- k += 3;
- if ((c1 & 0xc0) != 0x80) {
- c = 0;
- break;
- }
- c = (c << 6) | (c1 & 0x3f);
- }
- if (c < c_min || c > 0x10FFFF || is_surrogate(c)) {
- js_throw_URIError(ctx, "malformed UTF-8");
- goto fail;
- }
- }
- } else {
- k++;
- }
- string_buffer_putc(b, c);
- }
- JS_FreeValue(ctx, str);
- return string_buffer_end(b);
- fail:
- JS_FreeValue(ctx, str);
- string_buffer_free(b);
- return JS_EXCEPTION;
- }
- static int isUnescaped(int c) {
- static char const unescaped_chars[] =
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- "abcdefghijklmnopqrstuvwxyz"
- "0123456789"
- "@*_+-./";
- return c < 0x100 &&
- memchr(unescaped_chars, c, sizeof(unescaped_chars) - 1);
- }
- static int isURIUnescaped(int c, int isComponent) {
- return c < 0x100 &&
- ((c >= 0x61 && c <= 0x7a) ||
- (c >= 0x41 && c <= 0x5a) ||
- (c >= 0x30 && c <= 0x39) ||
- memchr("-_.!~*'()", c, sizeof("-_.!~*'()") - 1) != NULL ||
- (!isComponent && isURIReserved(c)));
- }
- static int encodeURI_hex(StringBuffer *b, int c) {
- uint8_t buf[6];
- int n = 0;
- const char *hex = "0123456789ABCDEF";
- buf[n++] = '%';
- if (c >= 256) {
- buf[n++] = 'u';
- buf[n++] = hex[(c >> 12) & 15];
- buf[n++] = hex[(c >> 8) & 15];
- }
- buf[n++] = hex[(c >> 4) & 15];
- buf[n++] = hex[(c >> 0) & 15];
- return string_buffer_write8(b, buf, n);
- }
- static JSValue js_global_encodeURI(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv,
- int isComponent)
- {
- JSValue str;
- StringBuffer b_s, *b = &b_s;
- JSString *p;
- int k, c, c1;
- str = JS_ToString(ctx, argv[0]);
- if (JS_IsException(str))
- return str;
- p = JS_VALUE_GET_STRING(str);
- string_buffer_init(ctx, b, p->len);
- for (k = 0; k < p->len;) {
- c = string_get(p, k);
- k++;
- if (isURIUnescaped(c, isComponent)) {
- string_buffer_putc16(b, c);
- } else {
- if (is_lo_surrogate(c)) {
- js_throw_URIError(ctx, "invalid character");
- goto fail;
- } else if (is_hi_surrogate(c)) {
- if (k >= p->len) {
- js_throw_URIError(ctx, "expecting surrogate pair");
- goto fail;
- }
- c1 = string_get(p, k);
- k++;
- if (!is_lo_surrogate(c1)) {
- js_throw_URIError(ctx, "expecting surrogate pair");
- goto fail;
- }
- c = from_surrogate(c, c1);
- }
- if (c < 0x80) {
- encodeURI_hex(b, c);
- } else {
- /* XXX: use C UTF-8 conversion ? */
- if (c < 0x800) {
- encodeURI_hex(b, (c >> 6) | 0xc0);
- } else {
- if (c < 0x10000) {
- encodeURI_hex(b, (c >> 12) | 0xe0);
- } else {
- encodeURI_hex(b, (c >> 18) | 0xf0);
- encodeURI_hex(b, ((c >> 12) & 0x3f) | 0x80);
- }
- encodeURI_hex(b, ((c >> 6) & 0x3f) | 0x80);
- }
- encodeURI_hex(b, (c & 0x3f) | 0x80);
- }
- }
- }
- JS_FreeValue(ctx, str);
- return string_buffer_end(b);
- fail:
- JS_FreeValue(ctx, str);
- string_buffer_free(b);
- return JS_EXCEPTION;
- }
- static JSValue js_global_escape(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue str;
- StringBuffer b_s, *b = &b_s;
- JSString *p;
- int i, len, c;
- str = JS_ToString(ctx, argv[0]);
- if (JS_IsException(str))
- return str;
- p = JS_VALUE_GET_STRING(str);
- string_buffer_init(ctx, b, p->len);
- for (i = 0, len = p->len; i < len; i++) {
- c = string_get(p, i);
- if (isUnescaped(c)) {
- string_buffer_putc16(b, c);
- } else {
- encodeURI_hex(b, c);
- }
- }
- JS_FreeValue(ctx, str);
- return string_buffer_end(b);
- }
- static JSValue js_global_unescape(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue str;
- StringBuffer b_s, *b = &b_s;
- JSString *p;
- int i, len, c, n;
- str = JS_ToString(ctx, argv[0]);
- if (JS_IsException(str))
- return str;
- string_buffer_init(ctx, b, 0);
- p = JS_VALUE_GET_STRING(str);
- for (i = 0, len = p->len; i < len; i++) {
- c = string_get(p, i);
- if (c == '%') {
- if (i + 6 <= len
- && string_get(p, i + 1) == 'u'
- && (n = string_get_hex(p, i + 2, 4)) >= 0) {
- c = n;
- i += 6 - 1;
- } else
- if (i + 3 <= len
- && (n = string_get_hex(p, i + 1, 2)) >= 0) {
- c = n;
- i += 3 - 1;
- }
- }
- string_buffer_putc16(b, c);
- }
- JS_FreeValue(ctx, str);
- return string_buffer_end(b);
- }
- /* global object */
- static const JSCFunctionListEntry js_global_funcs[] = {
- JS_CFUNC_DEF("parseInt", 2, js_parseInt ),
- JS_CFUNC_DEF("parseFloat", 1, js_parseFloat ),
- JS_CFUNC_DEF("isNaN", 1, js_global_isNaN ),
- JS_CFUNC_DEF("isFinite", 1, js_global_isFinite ),
- JS_CFUNC_MAGIC_DEF("decodeURI", 1, js_global_decodeURI, 0 ),
- JS_CFUNC_MAGIC_DEF("decodeURIComponent", 1, js_global_decodeURI, 1 ),
- JS_CFUNC_MAGIC_DEF("encodeURI", 1, js_global_encodeURI, 0 ),
- JS_CFUNC_MAGIC_DEF("encodeURIComponent", 1, js_global_encodeURI, 1 ),
- JS_CFUNC_DEF("escape", 1, js_global_escape ),
- JS_CFUNC_DEF("unescape", 1, js_global_unescape ),
- JS_PROP_DOUBLE_DEF("Infinity", 1.0 / 0.0, 0 ),
- JS_PROP_DOUBLE_DEF("NaN", NAN, 0 ),
- JS_PROP_UNDEFINED_DEF("undefined", 0 ),
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "global", JS_PROP_CONFIGURABLE ),
- JS_CFUNC_DEF("eval", 1, js_global_eval ),
- };
- /* Date */
- static int64_t math_mod(int64_t a, int64_t b) {
- /* return positive modulo */
- int64_t m = a % b;
- return m + (m < 0) * b;
- }
- static int64_t floor_div(int64_t a, int64_t b) {
- /* integer division rounding toward -Infinity */
- int64_t m = a % b;
- return (a - (m + (m < 0) * b)) / b;
- }
- static JSValue js_Date_parse(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv);
- static __exception int JS_ThisTimeValue(JSContext *ctx, double *valp, JSValueConst this_val)
- {
- if (JS_VALUE_GET_TAG(this_val) == JS_TAG_OBJECT) {
- JSObject *p = JS_VALUE_GET_OBJ(this_val);
- if (p->class_id == JS_CLASS_DATE && JS_IsNumber(p->u.object_data))
- return JS_ToFloat64(ctx, valp, p->u.object_data);
- }
- JS_ThrowTypeError(ctx, "not a Date object");
- return -1;
- }
- static JSValue JS_SetThisTimeValue(JSContext *ctx, JSValueConst this_val, double v)
- {
- if (JS_VALUE_GET_TAG(this_val) == JS_TAG_OBJECT) {
- JSObject *p = JS_VALUE_GET_OBJ(this_val);
- if (p->class_id == JS_CLASS_DATE) {
- JS_FreeValue(ctx, p->u.object_data);
- p->u.object_data = JS_NewFloat64(ctx, v);
- return JS_DupValue(ctx, p->u.object_data);
- }
- }
- return JS_ThrowTypeError(ctx, "not a Date object");
- }
- static int64_t days_from_year(int64_t y) {
- return 365 * (y - 1970) + floor_div(y - 1969, 4) -
- floor_div(y - 1901, 100) + floor_div(y - 1601, 400);
- }
- static int64_t days_in_year(int64_t y) {
- return 365 + !(y % 4) - !(y % 100) + !(y % 400);
- }
- /* return the year, update days */
- static int64_t year_from_days(int64_t *days) {
- int64_t y, d1, nd, d = *days;
- y = floor_div(d * 10000, 3652425) + 1970;
- /* the initial approximation is very good, so only a few
- iterations are necessary */
- for(;;) {
- d1 = d - days_from_year(y);
- if (d1 < 0) {
- y--;
- d1 += days_in_year(y);
- } else {
- nd = days_in_year(y);
- if (d1 < nd)
- break;
- d1 -= nd;
- y++;
- }
- }
- *days = d1;
- return y;
- }
- static int const month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
- static char const month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
- static char const day_names[] = "SunMonTueWedThuFriSat";
- static __exception int get_date_fields(JSContext *ctx, JSValueConst obj,
- double fields[minimum_length(9)], int is_local, int force)
- {
- double dval;
- int64_t d, days, wd, y, i, md, h, m, s, ms, tz = 0;
- if (JS_ThisTimeValue(ctx, &dval, obj))
- return -1;
- if (isnan(dval)) {
- if (!force)
- return FALSE; /* NaN */
- d = 0; /* initialize all fields to 0 */
- } else {
- d = dval; /* assuming -8.64e15 <= dval <= -8.64e15 */
- if (is_local) {
- tz = -getTimezoneOffset(d);
- d += tz * 60000;
- }
- }
- /* result is >= 0, we can use % */
- h = math_mod(d, 86400000);
- days = (d - h) / 86400000;
- ms = h % 1000;
- h = (h - ms) / 1000;
- s = h % 60;
- h = (h - s) / 60;
- m = h % 60;
- h = (h - m) / 60;
- wd = math_mod(days + 4, 7); /* week day */
- y = year_from_days(&days);
- for(i = 0; i < 11; i++) {
- md = month_days[i];
- if (i == 1)
- md += days_in_year(y) - 365;
- if (days < md)
- break;
- days -= md;
- }
- fields[0] = y;
- fields[1] = i;
- fields[2] = days + 1;
- fields[3] = h;
- fields[4] = m;
- fields[5] = s;
- fields[6] = ms;
- fields[7] = wd;
- fields[8] = tz;
- return TRUE;
- }
- static double time_clip(double t) {
- if (t >= -8.64e15 && t <= 8.64e15)
- return trunc(t) + 0.0; /* convert -0 to +0 */
- else
- return NAN;
- }
- /* The spec mandates the use of 'double' and it specifies the order
- of the operations */
- static double set_date_fields(double fields[minimum_length(7)], int is_local) {
- double y, m, dt, ym, mn, day, h, s, milli, time, tv;
- int yi, mi, i;
- int64_t days;
- volatile double temp; /* enforce evaluation order */
- /* emulate 21.4.1.15 MakeDay ( year, month, date ) */
- y = fields[0];
- m = fields[1];
- dt = fields[2];
- ym = y + floor(m / 12);
- mn = fmod(m, 12);
- if (mn < 0)
- mn += 12;
- if (ym < -271821 || ym > 275760)
- return NAN;
- yi = ym;
- mi = mn;
- days = days_from_year(yi);
- for(i = 0; i < mi; i++) {
- days += month_days[i];
- if (i == 1)
- days += days_in_year(yi) - 365;
- }
- day = days + dt - 1;
- /* emulate 21.4.1.14 MakeTime ( hour, min, sec, ms ) */
- h = fields[3];
- m = fields[4];
- s = fields[5];
- milli = fields[6];
- /* Use a volatile intermediary variable to ensure order of evaluation
- * as specified in ECMA. This fixes a test262 error on
- * test262/test/built-ins/Date/UTC/fp-evaluation-order.js.
- * Without the volatile qualifier, the compile can generate code
- * that performs the computation in a different order or with instructions
- * that produce a different result such as FMA (float multiply and add).
- */
- time = h * 3600000;
- time += (temp = m * 60000);
- time += (temp = s * 1000);
- time += milli;
- /* emulate 21.4.1.16 MakeDate ( day, time ) */
- tv = (temp = day * 86400000) + time; /* prevent generation of FMA */
- if (!isfinite(tv))
- return NAN;
- /* adjust for local time and clip */
- if (is_local) {
- int64_t ti = tv < INT64_MIN ? INT64_MIN : tv >= 0x1p63 ? INT64_MAX : (int64_t)tv;
- tv += getTimezoneOffset(ti) * 60000;
- }
- return time_clip(tv);
- }
- static double set_date_fields_checked(double fields[minimum_length(7)], int is_local)
- {
- int i;
- double a;
- for(i = 0; i < 7; i++) {
- a = fields[i];
- if (!isfinite(a))
- return NAN;
- fields[i] = trunc(a);
- if (i == 0 && fields[0] >= 0 && fields[0] < 100)
- fields[0] += 1900;
- }
- return set_date_fields(fields, is_local);
- }
- static JSValue get_date_field(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- // get_date_field(obj, n, is_local)
- double fields[9];
- int res, n, is_local;
- is_local = magic & 0x0F;
- n = (magic >> 4) & 0x0F;
- res = get_date_fields(ctx, this_val, fields, is_local, 0);
- if (res < 0)
- return JS_EXCEPTION;
- if (!res)
- return JS_NAN;
- if (magic & 0x100) { // getYear
- fields[0] -= 1900;
- }
- return JS_NewFloat64(ctx, fields[n]);
- }
- static JSValue set_date_field(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- // _field(obj, first_field, end_field, args, is_local)
- double fields[9];
- int res, first_field, end_field, is_local, i, n, res1;
- double d, a;
- d = NAN;
- first_field = (magic >> 8) & 0x0F;
- end_field = (magic >> 4) & 0x0F;
- is_local = magic & 0x0F;
- res = get_date_fields(ctx, this_val, fields, is_local, first_field == 0);
- if (res < 0)
- return JS_EXCEPTION;
- res1 = res;
-
- // Argument coercion is observable and must be done unconditionally.
- n = min_int(argc, end_field - first_field);
- for(i = 0; i < n; i++) {
- if (JS_ToFloat64(ctx, &a, argv[i]))
- return JS_EXCEPTION;
- if (!isfinite(a))
- res = FALSE;
- fields[first_field + i] = trunc(a);
- }
- if (!res1)
- return JS_NAN; /* thisTimeValue is NaN */
- if (res && argc > 0)
- d = set_date_fields(fields, is_local);
- return JS_SetThisTimeValue(ctx, this_val, d);
- }
- /* fmt:
- 0: toUTCString: "Tue, 02 Jan 2018 23:04:46 GMT"
- 1: toString: "Wed Jan 03 2018 00:05:22 GMT+0100 (CET)"
- 2: toISOString: "2018-01-02T23:02:56.927Z"
- 3: toLocaleString: "1/2/2018, 11:40:40 PM"
- part: 1=date, 2=time 3=all
- XXX: should use a variant of strftime().
- */
- static JSValue get_date_string(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- // _string(obj, fmt, part)
- char buf[64];
- double fields[9];
- int res, fmt, part, pos;
- int y, mon, d, h, m, s, ms, wd, tz;
- fmt = (magic >> 4) & 0x0F;
- part = magic & 0x0F;
- res = get_date_fields(ctx, this_val, fields, fmt & 1, 0);
- if (res < 0)
- return JS_EXCEPTION;
- if (!res) {
- if (fmt == 2)
- return JS_ThrowRangeError(ctx, "Date value is NaN");
- else
- return js_new_string8(ctx, "Invalid Date");
- }
- y = fields[0];
- mon = fields[1];
- d = fields[2];
- h = fields[3];
- m = fields[4];
- s = fields[5];
- ms = fields[6];
- wd = fields[7];
- tz = fields[8];
- pos = 0;
- if (part & 1) { /* date part */
- switch(fmt) {
- case 0:
- pos += snprintf(buf + pos, sizeof(buf) - pos,
- "%.3s, %02d %.3s %0*d ",
- day_names + wd * 3, d,
- month_names + mon * 3, 4 + (y < 0), y);
- break;
- case 1:
- pos += snprintf(buf + pos, sizeof(buf) - pos,
- "%.3s %.3s %02d %0*d",
- day_names + wd * 3,
- month_names + mon * 3, d, 4 + (y < 0), y);
- if (part == 3) {
- buf[pos++] = ' ';
- }
- break;
- case 2:
- if (y >= 0 && y <= 9999) {
- pos += snprintf(buf + pos, sizeof(buf) - pos,
- "%04d", y);
- } else {
- pos += snprintf(buf + pos, sizeof(buf) - pos,
- "%+07d", y);
- }
- pos += snprintf(buf + pos, sizeof(buf) - pos,
- "-%02d-%02dT", mon + 1, d);
- break;
- case 3:
- pos += snprintf(buf + pos, sizeof(buf) - pos,
- "%02d/%02d/%0*d", mon + 1, d, 4 + (y < 0), y);
- if (part == 3) {
- buf[pos++] = ',';
- buf[pos++] = ' ';
- }
- break;
- }
- }
- if (part & 2) { /* time part */
- switch(fmt) {
- case 0:
- pos += snprintf(buf + pos, sizeof(buf) - pos,
- "%02d:%02d:%02d GMT", h, m, s);
- break;
- case 1:
- pos += snprintf(buf + pos, sizeof(buf) - pos,
- "%02d:%02d:%02d GMT", h, m, s);
- if (tz < 0) {
- buf[pos++] = '-';
- tz = -tz;
- } else {
- buf[pos++] = '+';
- }
- /* tz is >= 0, can use % */
- pos += snprintf(buf + pos, sizeof(buf) - pos,
- "%02d%02d", tz / 60, tz % 60);
- /* XXX: tack the time zone code? */
- break;
- case 2:
- pos += snprintf(buf + pos, sizeof(buf) - pos,
- "%02d:%02d:%02d.%03dZ", h, m, s, ms);
- break;
- case 3:
- pos += snprintf(buf + pos, sizeof(buf) - pos,
- "%02d:%02d:%02d %cM", (h + 11) % 12 + 1, m, s,
- (h < 12) ? 'A' : 'P');
- break;
- }
- }
- return JS_NewStringLen(ctx, buf, pos);
- }
- /* OS dependent: return the UTC time in ms since 1970. */
- static int64_t date_now(void) {
- struct timeval tv;
- gettimeofday(&tv, NULL);
- return (int64_t)tv.tv_sec * 1000 + (tv.tv_usec / 1000);
- }
- static JSValue js_date_constructor(JSContext *ctx, JSValueConst new_target,
- int argc, JSValueConst *argv)
- {
- // Date(y, mon, d, h, m, s, ms)
- JSValue rv;
- int i, n;
- double val;
- if (JS_IsUndefined(new_target)) {
- /* invoked as function */
- argc = 0;
- }
- n = argc;
- if (n == 0) {
- val = date_now();
- } else if (n == 1) {
- JSValue v, dv;
- if (JS_VALUE_GET_TAG(argv[0]) == JS_TAG_OBJECT) {
- JSObject *p = JS_VALUE_GET_OBJ(argv[0]);
- if (p->class_id == JS_CLASS_DATE && JS_IsNumber(p->u.object_data)) {
- if (JS_ToFloat64(ctx, &val, p->u.object_data))
- return JS_EXCEPTION;
- val = time_clip(val);
- goto has_val;
- }
- }
- v = JS_ToPrimitive(ctx, argv[0], HINT_NONE);
- if (JS_IsString(v)) {
- dv = js_Date_parse(ctx, JS_UNDEFINED, 1, (JSValueConst *)&v);
- JS_FreeValue(ctx, v);
- if (JS_IsException(dv))
- return JS_EXCEPTION;
- if (JS_ToFloat64Free(ctx, &val, dv))
- return JS_EXCEPTION;
- } else {
- if (JS_ToFloat64Free(ctx, &val, v))
- return JS_EXCEPTION;
- }
- val = time_clip(val);
- } else {
- double fields[] = { 0, 0, 1, 0, 0, 0, 0 };
- if (n > 7)
- n = 7;
- for(i = 0; i < n; i++) {
- if (JS_ToFloat64(ctx, &fields[i], argv[i]))
- return JS_EXCEPTION;
- }
- val = set_date_fields_checked(fields, 1);
- }
- has_val:
- #if 0
- JSValueConst args[3];
- args[0] = new_target;
- args[1] = ctx->class_proto[JS_CLASS_DATE];
- args[2] = JS_NewFloat64(ctx, val);
- rv = js___date_create(ctx, JS_UNDEFINED, 3, args);
- #else
- rv = js_create_from_ctor(ctx, new_target, JS_CLASS_DATE);
- if (!JS_IsException(rv))
- JS_SetObjectData(ctx, rv, JS_NewFloat64(ctx, val));
- #endif
- if (!JS_IsException(rv) && JS_IsUndefined(new_target)) {
- /* invoked as a function, return (new Date()).toString(); */
- JSValue s;
- s = get_date_string(ctx, rv, 0, NULL, 0x13);
- JS_FreeValue(ctx, rv);
- rv = s;
- }
- return rv;
- }
- static JSValue js_Date_UTC(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- // UTC(y, mon, d, h, m, s, ms)
- double fields[] = { 0, 0, 1, 0, 0, 0, 0 };
- int i, n;
- n = argc;
- if (n == 0)
- return JS_NAN;
- if (n > 7)
- n = 7;
- for(i = 0; i < n; i++) {
- if (JS_ToFloat64(ctx, &fields[i], argv[i]))
- return JS_EXCEPTION;
- }
- return JS_NewFloat64(ctx, set_date_fields_checked(fields, 0));
- }
- /* Date string parsing */
- static BOOL string_skip_char(const uint8_t *sp, int *pp, int c) {
- if (sp[*pp] == c) {
- *pp += 1;
- return TRUE;
- } else {
- return FALSE;
- }
- }
- /* skip spaces, update offset, return next char */
- static int string_skip_spaces(const uint8_t *sp, int *pp) {
- int c;
- while ((c = sp[*pp]) == ' ')
- *pp += 1;
- return c;
- }
- /* skip dashes dots and commas */
- static int string_skip_separators(const uint8_t *sp, int *pp) {
- int c;
- while ((c = sp[*pp]) == '-' || c == '/' || c == '.' || c == ',')
- *pp += 1;
- return c;
- }
- /* skip a word, stop on spaces, digits and separators, update offset */
- static int string_skip_until(const uint8_t *sp, int *pp, const char *stoplist) {
- int c;
- while (!strchr(stoplist, c = sp[*pp]))
- *pp += 1;
- return c;
- }
- /* parse a numeric field (max_digits = 0 -> no maximum) */
- static BOOL string_get_digits(const uint8_t *sp, int *pp, int *pval,
- int min_digits, int max_digits)
- {
- int v = 0;
- int c, p = *pp, p_start;
- p_start = p;
- while ((c = sp[p]) >= '0' && c <= '9') {
- /* arbitrary limit to 9 digits */
- if (v >= 100000000)
- return FALSE;
- v = v * 10 + c - '0';
- p++;
- if (p - p_start == max_digits)
- break;
- }
- if (p - p_start < min_digits)
- return FALSE;
- *pval = v;
- *pp = p;
- return TRUE;
- }
- static BOOL string_get_milliseconds(const uint8_t *sp, int *pp, int *pval) {
- /* parse optional fractional part as milliseconds and truncate. */
- /* spec does not indicate which rounding should be used */
- int mul = 100, ms = 0, c, p_start, p = *pp;
- c = sp[p];
- if (c == '.' || c == ',') {
- p++;
- p_start = p;
- while ((c = sp[p]) >= '0' && c <= '9') {
- ms += (c - '0') * mul;
- mul /= 10;
- p++;
- if (p - p_start == 9)
- break;
- }
- if (p > p_start) {
- /* only consume the separator if digits are present */
- *pval = ms;
- *pp = p;
- }
- }
- return TRUE;
- }
- static uint8_t upper_ascii(uint8_t c) {
- return c >= 'a' && c <= 'z' ? c - 'a' + 'A' : c;
- }
- static BOOL string_get_tzoffset(const uint8_t *sp, int *pp, int *tzp, BOOL strict) {
- int tz = 0, sgn, hh, mm, p = *pp;
- sgn = sp[p++];
- if (sgn == '+' || sgn == '-') {
- int n = p;
- if (!string_get_digits(sp, &p, &hh, 1, 0))
- return FALSE;
- n = p - n;
- if (strict && n != 2 && n != 4)
- return FALSE;
- while (n > 4) {
- n -= 2;
- hh /= 100;
- }
- if (n > 2) {
- mm = hh % 100;
- hh = hh / 100;
- } else {
- mm = 0;
- if (string_skip_char(sp, &p, ':')) {
- /* optional separator */
- if (!string_get_digits(sp, &p, &mm, 2, 2))
- return FALSE;
- } else {
- if (strict)
- return FALSE; /* [+-]HH is not accepted in strict mode */
- }
- }
- if (hh > 23 || mm > 59)
- return FALSE;
- tz = hh * 60 + mm;
- if (sgn != '+')
- tz = -tz;
- } else
- if (sgn != 'Z') {
- return FALSE;
- }
- *pp = p;
- *tzp = tz;
- return TRUE;
- }
- static BOOL string_match(const uint8_t *sp, int *pp, const char *s) {
- int p = *pp;
- while (*s != '\0') {
- if (upper_ascii(sp[p]) != upper_ascii(*s++))
- return FALSE;
- p++;
- }
- *pp = p;
- return TRUE;
- }
- static int find_abbrev(const uint8_t *sp, int p, const char *list, int count) {
- int n, i;
- for (n = 0; n < count; n++) {
- for (i = 0;; i++) {
- if (upper_ascii(sp[p + i]) != upper_ascii(list[n * 3 + i]))
- break;
- if (i == 2)
- return n;
- }
- }
- return -1;
- }
- static BOOL string_get_month(const uint8_t *sp, int *pp, int *pval) {
- int n;
- n = find_abbrev(sp, *pp, month_names, 12);
- if (n < 0)
- return FALSE;
- *pval = n + 1;
- *pp += 3;
- return TRUE;
- }
- /* parse toISOString format */
- static BOOL js_date_parse_isostring(const uint8_t *sp, int fields[9], BOOL *is_local) {
- int sgn, i, p = 0;
- /* initialize fields to the beginning of the Epoch */
- for (i = 0; i < 9; i++) {
- fields[i] = (i == 2);
- }
- *is_local = FALSE;
- /* year is either yyyy digits or [+-]yyyyyy */
- sgn = sp[p];
- if (sgn == '-' || sgn == '+') {
- p++;
- if (!string_get_digits(sp, &p, &fields[0], 6, 6))
- return FALSE;
- if (sgn == '-') {
- if (fields[0] == 0)
- return FALSE; // reject -000000
- fields[0] = -fields[0];
- }
- } else {
- if (!string_get_digits(sp, &p, &fields[0], 4, 4))
- return FALSE;
- }
- if (string_skip_char(sp, &p, '-')) {
- if (!string_get_digits(sp, &p, &fields[1], 2, 2)) /* month */
- return FALSE;
- if (fields[1] < 1)
- return FALSE;
- fields[1] -= 1;
- if (string_skip_char(sp, &p, '-')) {
- if (!string_get_digits(sp, &p, &fields[2], 2, 2)) /* day */
- return FALSE;
- if (fields[2] < 1)
- return FALSE;
- }
- }
- if (string_skip_char(sp, &p, 'T')) {
- *is_local = TRUE;
- if (!string_get_digits(sp, &p, &fields[3], 2, 2) /* hour */
- || !string_skip_char(sp, &p, ':')
- || !string_get_digits(sp, &p, &fields[4], 2, 2)) { /* minute */
- fields[3] = 100; // reject unconditionally
- return TRUE;
- }
- if (string_skip_char(sp, &p, ':')) {
- if (!string_get_digits(sp, &p, &fields[5], 2, 2)) /* second */
- return FALSE;
- string_get_milliseconds(sp, &p, &fields[6]);
- }
- }
- /* parse the time zone offset if present: [+-]HH:mm or [+-]HHmm */
- if (sp[p]) {
- *is_local = FALSE;
- if (!string_get_tzoffset(sp, &p, &fields[8], TRUE))
- return FALSE;
- }
- /* error if extraneous characters */
- return sp[p] == '\0';
- }
- static struct {
- char name[6];
- int16_t offset;
- } const js_tzabbr[] = {
- { "GMT", 0 }, // Greenwich Mean Time
- { "UTC", 0 }, // Coordinated Universal Time
- { "UT", 0 }, // Universal Time
- { "Z", 0 }, // Zulu Time
- { "EDT", -4 * 60 }, // Eastern Daylight Time
- { "EST", -5 * 60 }, // Eastern Standard Time
- { "CDT", -5 * 60 }, // Central Daylight Time
- { "CST", -6 * 60 }, // Central Standard Time
- { "MDT", -6 * 60 }, // Mountain Daylight Time
- { "MST", -7 * 60 }, // Mountain Standard Time
- { "PDT", -7 * 60 }, // Pacific Daylight Time
- { "PST", -8 * 60 }, // Pacific Standard Time
- { "WET", +0 * 60 }, // Western European Time
- { "WEST", +1 * 60 }, // Western European Summer Time
- { "CET", +1 * 60 }, // Central European Time
- { "CEST", +2 * 60 }, // Central European Summer Time
- { "EET", +2 * 60 }, // Eastern European Time
- { "EEST", +3 * 60 }, // Eastern European Summer Time
- };
- static BOOL string_get_tzabbr(const uint8_t *sp, int *pp, int *offset) {
- for (size_t i = 0; i < countof(js_tzabbr); i++) {
- if (string_match(sp, pp, js_tzabbr[i].name)) {
- *offset = js_tzabbr[i].offset;
- return TRUE;
- }
- }
- return FALSE;
- }
- /* parse toString, toUTCString and other formats */
- static BOOL js_date_parse_otherstring(const uint8_t *sp,
- int fields[minimum_length(9)],
- BOOL *is_local) {
- int c, i, val, p = 0, p_start;
- int num[3];
- BOOL has_year = FALSE;
- BOOL has_mon = FALSE;
- BOOL has_time = FALSE;
- int num_index = 0;
- /* initialize fields to the beginning of 2001-01-01 */
- fields[0] = 2001;
- fields[1] = 1;
- fields[2] = 1;
- for (i = 3; i < 9; i++) {
- fields[i] = 0;
- }
- *is_local = TRUE;
- while (string_skip_spaces(sp, &p)) {
- p_start = p;
- if ((c = sp[p]) == '+' || c == '-') {
- if (has_time && string_get_tzoffset(sp, &p, &fields[8], FALSE)) {
- *is_local = FALSE;
- } else {
- p++;
- if (string_get_digits(sp, &p, &val, 1, 0)) {
- if (c == '-') {
- if (val == 0)
- return FALSE;
- val = -val;
- }
- fields[0] = val;
- has_year = TRUE;
- }
- }
- } else
- if (string_get_digits(sp, &p, &val, 1, 0)) {
- if (string_skip_char(sp, &p, ':')) {
- /* time part */
- fields[3] = val;
- if (!string_get_digits(sp, &p, &fields[4], 1, 2))
- return FALSE;
- if (string_skip_char(sp, &p, ':')) {
- if (!string_get_digits(sp, &p, &fields[5], 1, 2))
- return FALSE;
- string_get_milliseconds(sp, &p, &fields[6]);
- }
- has_time = TRUE;
- if ((sp[p] == '+' || sp[p] == '-') &&
- string_get_tzoffset(sp, &p, &fields[8], FALSE)) {
- *is_local = FALSE;
- }
- } else {
- if (p - p_start > 2 && !has_year) {
- fields[0] = val;
- has_year = TRUE;
- } else
- if ((val < 1 || val > 31) && !has_year) {
- fields[0] = val + (val < 100) * 1900 + (val < 50) * 100;
- has_year = TRUE;
- } else {
- if (num_index == 3)
- return FALSE;
- num[num_index++] = val;
- }
- }
- } else
- if (string_get_month(sp, &p, &fields[1])) {
- has_mon = TRUE;
- string_skip_until(sp, &p, "0123456789 -/(");
- } else
- if (has_time && string_match(sp, &p, "PM")) {
- if (fields[3] < 12)
- fields[3] += 12;
- continue;
- } else
- if (has_time && string_match(sp, &p, "AM")) {
- if (fields[3] == 12)
- fields[3] -= 12;
- continue;
- } else
- if (string_get_tzabbr(sp, &p, &fields[8])) {
- *is_local = FALSE;
- continue;
- } else
- if (c == '(') { /* skip parenthesized phrase */
- int level = 0;
- while ((c = sp[p]) != '\0') {
- p++;
- level += (c == '(');
- level -= (c == ')');
- if (!level)
- break;
- }
- if (level > 0)
- return FALSE;
- } else
- if (c == ')') {
- return FALSE;
- } else {
- if (has_year + has_mon + has_time + num_index)
- return FALSE;
- /* skip a word */
- string_skip_until(sp, &p, " -/(");
- }
- string_skip_separators(sp, &p);
- }
- if (num_index + has_year + has_mon > 3)
- return FALSE;
- switch (num_index) {
- case 0:
- if (!has_year)
- return FALSE;
- break;
- case 1:
- if (has_mon)
- fields[2] = num[0];
- else
- fields[1] = num[0];
- break;
- case 2:
- if (has_year) {
- fields[1] = num[0];
- fields[2] = num[1];
- } else
- if (has_mon) {
- fields[0] = num[1] + (num[1] < 100) * 1900 + (num[1] < 50) * 100;
- fields[2] = num[0];
- } else {
- fields[1] = num[0];
- fields[2] = num[1];
- }
- break;
- case 3:
- fields[0] = num[2] + (num[2] < 100) * 1900 + (num[2] < 50) * 100;
- fields[1] = num[0];
- fields[2] = num[1];
- break;
- default:
- return FALSE;
- }
- if (fields[1] < 1 || fields[2] < 1)
- return FALSE;
- fields[1] -= 1;
- return TRUE;
- }
- static JSValue js_Date_parse(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue s, rv;
- int fields[9];
- double fields1[9];
- double d;
- int i, c;
- JSString *sp;
- uint8_t buf[128];
- BOOL is_local;
- rv = JS_NAN;
- s = JS_ToString(ctx, argv[0]);
- if (JS_IsException(s))
- return JS_EXCEPTION;
- sp = JS_VALUE_GET_STRING(s);
- /* convert the string as a byte array */
- for (i = 0; i < sp->len && i < (int)countof(buf) - 1; i++) {
- c = string_get(sp, i);
- if (c > 255)
- c = (c == 0x2212) ? '-' : 'x';
- buf[i] = c;
- }
- buf[i] = '\0';
- if (js_date_parse_isostring(buf, fields, &is_local)
- || js_date_parse_otherstring(buf, fields, &is_local)) {
- static int const field_max[6] = { 0, 11, 31, 24, 59, 59 };
- BOOL valid = TRUE;
- /* check field maximum values */
- for (i = 1; i < 6; i++) {
- if (fields[i] > field_max[i])
- valid = FALSE;
- }
- /* special case 24:00:00.000 */
- if (fields[3] == 24 && (fields[4] | fields[5] | fields[6]))
- valid = FALSE;
- if (valid) {
- for(i = 0; i < 7; i++)
- fields1[i] = fields[i];
- d = set_date_fields(fields1, is_local) - fields[8] * 60000;
- rv = JS_NewFloat64(ctx, d);
- }
- }
- JS_FreeValue(ctx, s);
- return rv;
- }
- static JSValue js_Date_now(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- // now()
- return JS_NewInt64(ctx, date_now());
- }
- static JSValue js_date_Symbol_toPrimitive(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- // Symbol_toPrimitive(hint)
- JSValueConst obj = this_val;
- JSAtom hint = JS_ATOM_NULL;
- int hint_num;
- if (!JS_IsObject(obj))
- return JS_ThrowTypeErrorNotAnObject(ctx);
- if (JS_IsString(argv[0])) {
- hint = JS_ValueToAtom(ctx, argv[0]);
- if (hint == JS_ATOM_NULL)
- return JS_EXCEPTION;
- JS_FreeAtom(ctx, hint);
- }
- switch (hint) {
- case JS_ATOM_number:
- case JS_ATOM_integer:
- hint_num = HINT_NUMBER;
- break;
- case JS_ATOM_string:
- case JS_ATOM_default:
- hint_num = HINT_STRING;
- break;
- default:
- return JS_ThrowTypeError(ctx, "invalid hint");
- }
- return JS_ToPrimitive(ctx, obj, hint_num | HINT_FORCE_ORDINARY);
- }
- static JSValue js_date_getTimezoneOffset(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- // getTimezoneOffset()
- double v;
- if (JS_ThisTimeValue(ctx, &v, this_val))
- return JS_EXCEPTION;
- if (isnan(v))
- return JS_NAN;
- else
- /* assuming -8.64e15 <= v <= -8.64e15 */
- return JS_NewInt64(ctx, getTimezoneOffset((int64_t)trunc(v)));
- }
- static JSValue js_date_getTime(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- // getTime()
- double v;
- if (JS_ThisTimeValue(ctx, &v, this_val))
- return JS_EXCEPTION;
- return JS_NewFloat64(ctx, v);
- }
- static JSValue js_date_setTime(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- // setTime(v)
- double v;
- if (JS_ThisTimeValue(ctx, &v, this_val) || JS_ToFloat64(ctx, &v, argv[0]))
- return JS_EXCEPTION;
- return JS_SetThisTimeValue(ctx, this_val, time_clip(v));
- }
- static JSValue js_date_setYear(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- // setYear(y)
- double y;
- JSValueConst args[1];
- if (JS_ThisTimeValue(ctx, &y, this_val) || JS_ToFloat64(ctx, &y, argv[0]))
- return JS_EXCEPTION;
- y = +y;
- if (isfinite(y)) {
- y = trunc(y);
- if (y >= 0 && y < 100)
- y += 1900;
- }
- args[0] = JS_NewFloat64(ctx, y);
- return set_date_field(ctx, this_val, 1, args, 0x011);
- }
- static JSValue js_date_toJSON(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- // toJSON(key)
- JSValue obj, tv, method, rv;
- double d;
- rv = JS_EXCEPTION;
- tv = JS_UNDEFINED;
- obj = JS_ToObject(ctx, this_val);
- tv = JS_ToPrimitive(ctx, obj, HINT_NUMBER);
- if (JS_IsException(tv))
- goto exception;
- if (JS_IsNumber(tv)) {
- if (JS_ToFloat64(ctx, &d, tv) < 0)
- goto exception;
- if (!isfinite(d)) {
- rv = JS_NULL;
- goto done;
- }
- }
- method = JS_GetProperty(ctx, obj, JS_ATOM_toISOString);
- if (JS_IsException(method))
- goto exception;
- if (!JS_IsFunction(ctx, method)) {
- JS_ThrowTypeError(ctx, "object needs toISOString method");
- JS_FreeValue(ctx, method);
- goto exception;
- }
- rv = JS_CallFree(ctx, method, obj, 0, NULL);
- exception:
- done:
- JS_FreeValue(ctx, obj);
- JS_FreeValue(ctx, tv);
- return rv;
- }
- static const JSCFunctionListEntry js_date_funcs[] = {
- JS_CFUNC_DEF("now", 0, js_Date_now ),
- JS_CFUNC_DEF("parse", 1, js_Date_parse ),
- JS_CFUNC_DEF("UTC", 7, js_Date_UTC ),
- };
- static const JSCFunctionListEntry js_date_proto_funcs[] = {
- JS_CFUNC_DEF("valueOf", 0, js_date_getTime ),
- JS_CFUNC_MAGIC_DEF("toString", 0, get_date_string, 0x13 ),
- JS_CFUNC_DEF("[Symbol.toPrimitive]", 1, js_date_Symbol_toPrimitive ),
- JS_CFUNC_MAGIC_DEF("toUTCString", 0, get_date_string, 0x03 ),
- JS_ALIAS_DEF("toGMTString", "toUTCString" ),
- JS_CFUNC_MAGIC_DEF("toISOString", 0, get_date_string, 0x23 ),
- JS_CFUNC_MAGIC_DEF("toDateString", 0, get_date_string, 0x11 ),
- JS_CFUNC_MAGIC_DEF("toTimeString", 0, get_date_string, 0x12 ),
- JS_CFUNC_MAGIC_DEF("toLocaleString", 0, get_date_string, 0x33 ),
- JS_CFUNC_MAGIC_DEF("toLocaleDateString", 0, get_date_string, 0x31 ),
- JS_CFUNC_MAGIC_DEF("toLocaleTimeString", 0, get_date_string, 0x32 ),
- JS_CFUNC_DEF("getTimezoneOffset", 0, js_date_getTimezoneOffset ),
- JS_CFUNC_DEF("getTime", 0, js_date_getTime ),
- JS_CFUNC_MAGIC_DEF("getYear", 0, get_date_field, 0x101 ),
- JS_CFUNC_MAGIC_DEF("getFullYear", 0, get_date_field, 0x01 ),
- JS_CFUNC_MAGIC_DEF("getUTCFullYear", 0, get_date_field, 0x00 ),
- JS_CFUNC_MAGIC_DEF("getMonth", 0, get_date_field, 0x11 ),
- JS_CFUNC_MAGIC_DEF("getUTCMonth", 0, get_date_field, 0x10 ),
- JS_CFUNC_MAGIC_DEF("getDate", 0, get_date_field, 0x21 ),
- JS_CFUNC_MAGIC_DEF("getUTCDate", 0, get_date_field, 0x20 ),
- JS_CFUNC_MAGIC_DEF("getHours", 0, get_date_field, 0x31 ),
- JS_CFUNC_MAGIC_DEF("getUTCHours", 0, get_date_field, 0x30 ),
- JS_CFUNC_MAGIC_DEF("getMinutes", 0, get_date_field, 0x41 ),
- JS_CFUNC_MAGIC_DEF("getUTCMinutes", 0, get_date_field, 0x40 ),
- JS_CFUNC_MAGIC_DEF("getSeconds", 0, get_date_field, 0x51 ),
- JS_CFUNC_MAGIC_DEF("getUTCSeconds", 0, get_date_field, 0x50 ),
- JS_CFUNC_MAGIC_DEF("getMilliseconds", 0, get_date_field, 0x61 ),
- JS_CFUNC_MAGIC_DEF("getUTCMilliseconds", 0, get_date_field, 0x60 ),
- JS_CFUNC_MAGIC_DEF("getDay", 0, get_date_field, 0x71 ),
- JS_CFUNC_MAGIC_DEF("getUTCDay", 0, get_date_field, 0x70 ),
- JS_CFUNC_DEF("setTime", 1, js_date_setTime ),
- JS_CFUNC_MAGIC_DEF("setMilliseconds", 1, set_date_field, 0x671 ),
- JS_CFUNC_MAGIC_DEF("setUTCMilliseconds", 1, set_date_field, 0x670 ),
- JS_CFUNC_MAGIC_DEF("setSeconds", 2, set_date_field, 0x571 ),
- JS_CFUNC_MAGIC_DEF("setUTCSeconds", 2, set_date_field, 0x570 ),
- JS_CFUNC_MAGIC_DEF("setMinutes", 3, set_date_field, 0x471 ),
- JS_CFUNC_MAGIC_DEF("setUTCMinutes", 3, set_date_field, 0x470 ),
- JS_CFUNC_MAGIC_DEF("setHours", 4, set_date_field, 0x371 ),
- JS_CFUNC_MAGIC_DEF("setUTCHours", 4, set_date_field, 0x370 ),
- JS_CFUNC_MAGIC_DEF("setDate", 1, set_date_field, 0x231 ),
- JS_CFUNC_MAGIC_DEF("setUTCDate", 1, set_date_field, 0x230 ),
- JS_CFUNC_MAGIC_DEF("setMonth", 2, set_date_field, 0x131 ),
- JS_CFUNC_MAGIC_DEF("setUTCMonth", 2, set_date_field, 0x130 ),
- JS_CFUNC_DEF("setYear", 1, js_date_setYear ),
- JS_CFUNC_MAGIC_DEF("setFullYear", 3, set_date_field, 0x031 ),
- JS_CFUNC_MAGIC_DEF("setUTCFullYear", 3, set_date_field, 0x030 ),
- JS_CFUNC_DEF("toJSON", 1, js_date_toJSON ),
- };
- JSValue JS_NewDate(JSContext *ctx, double epoch_ms)
- {
- JSValue obj = js_create_from_ctor(ctx, JS_UNDEFINED, JS_CLASS_DATE);
- if (JS_IsException(obj))
- return JS_EXCEPTION;
- JS_SetObjectData(ctx, obj, __JS_NewFloat64(ctx, time_clip(epoch_ms)));
- return obj;
- }
- int JS_AddIntrinsicDate(JSContext *ctx)
- {
- JSValue obj;
- /* Date */
- obj = JS_NewCConstructor(ctx, JS_CLASS_DATE, "Date",
- js_date_constructor, 7, JS_CFUNC_constructor_or_func, 0,
- JS_UNDEFINED,
- js_date_funcs, countof(js_date_funcs),
- js_date_proto_funcs, countof(js_date_proto_funcs),
- 0);
- if (JS_IsException(obj))
- return -1;
- JS_FreeValue(ctx, obj);
- return 0;
- }
- /* eval */
- int JS_AddIntrinsicEval(JSContext *ctx)
- {
- ctx->eval_internal = __JS_EvalInternal;
- return 0;
- }
- /* BigInt */
- static JSValue JS_ToBigIntCtorFree(JSContext *ctx, JSValue val)
- {
- uint32_t tag;
- redo:
- tag = JS_VALUE_GET_NORM_TAG(val);
- switch(tag) {
- case JS_TAG_INT:
- case JS_TAG_BOOL:
- val = JS_NewBigInt64(ctx, JS_VALUE_GET_INT(val));
- break;
- case JS_TAG_SHORT_BIG_INT:
- case JS_TAG_BIG_INT:
- break;
- case JS_TAG_FLOAT64:
- {
- double d = JS_VALUE_GET_FLOAT64(val);
- JSBigInt *r;
- int res;
- r = js_bigint_from_float64(ctx, &res, d);
- if (!r) {
- if (res == 0) {
- val = JS_EXCEPTION;
- } else if (res == 1) {
- val = JS_ThrowRangeError(ctx, "cannot convert to BigInt: not an integer");
- } else {
- val = JS_ThrowRangeError(ctx, "cannot convert NaN or Infinity to BigInt"); }
- } else {
- val = JS_CompactBigInt(ctx, r);
- }
- }
- break;
- case JS_TAG_STRING:
- case JS_TAG_STRING_ROPE:
- val = JS_StringToBigIntErr(ctx, val);
- break;
- case JS_TAG_OBJECT:
- val = JS_ToPrimitiveFree(ctx, val, HINT_NUMBER);
- if (JS_IsException(val))
- break;
- goto redo;
- case JS_TAG_NULL:
- case JS_TAG_UNDEFINED:
- default:
- JS_FreeValue(ctx, val);
- return JS_ThrowTypeError(ctx, "cannot convert to BigInt");
- }
- return val;
- }
- static JSValue js_bigint_constructor(JSContext *ctx,
- JSValueConst new_target,
- int argc, JSValueConst *argv)
- {
- if (!JS_IsUndefined(new_target))
- return JS_ThrowTypeErrorNotAConstructor(ctx, new_target);
- return JS_ToBigIntCtorFree(ctx, JS_DupValue(ctx, argv[0]));
- }
- static JSValue js_thisBigIntValue(JSContext *ctx, JSValueConst this_val)
- {
- if (JS_IsBigInt(ctx, this_val))
- return JS_DupValue(ctx, this_val);
- if (JS_VALUE_GET_TAG(this_val) == JS_TAG_OBJECT) {
- JSObject *p = JS_VALUE_GET_OBJ(this_val);
- if (p->class_id == JS_CLASS_BIG_INT) {
- if (JS_IsBigInt(ctx, p->u.object_data))
- return JS_DupValue(ctx, p->u.object_data);
- }
- }
- return JS_ThrowTypeError(ctx, "not a BigInt");
- }
- static JSValue js_bigint_toString(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue val;
- int base;
- JSValue ret;
- val = js_thisBigIntValue(ctx, this_val);
- if (JS_IsException(val))
- return val;
- if (argc == 0 || JS_IsUndefined(argv[0])) {
- base = 10;
- } else {
- base = js_get_radix(ctx, argv[0]);
- if (base < 0)
- goto fail;
- }
- ret = js_bigint_to_string1(ctx, val, base);
- JS_FreeValue(ctx, val);
- return ret;
- fail:
- JS_FreeValue(ctx, val);
- return JS_EXCEPTION;
- }
- static JSValue js_bigint_valueOf(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- return js_thisBigIntValue(ctx, this_val);
- }
- static JSValue js_bigint_asUintN(JSContext *ctx,
- JSValueConst this_val,
- int argc, JSValueConst *argv, int asIntN)
- {
- uint64_t bits;
- JSValue res, a;
-
- if (JS_ToIndex(ctx, &bits, argv[0]))
- return JS_EXCEPTION;
- a = JS_ToBigInt(ctx, argv[1]);
- if (JS_IsException(a))
- return JS_EXCEPTION;
- if (bits == 0) {
- JS_FreeValue(ctx, a);
- res = __JS_NewShortBigInt(ctx, 0);
- } else if (JS_VALUE_GET_TAG(a) == JS_TAG_SHORT_BIG_INT) {
- /* fast case */
- if (bits >= JS_SHORT_BIG_INT_BITS) {
- res = a;
- } else {
- uint64_t v;
- int shift;
- shift = 64 - bits;
- v = JS_VALUE_GET_SHORT_BIG_INT(a);
- v = v << shift;
- if (asIntN)
- v = (int64_t)v >> shift;
- else
- v = v >> shift;
- res = __JS_NewShortBigInt(ctx, v);
- }
- } else {
- JSBigInt *r, *p = JS_VALUE_GET_PTR(a);
- if (bits >= p->len * JS_LIMB_BITS) {
- res = a;
- } else {
- int len, shift, i;
- js_limb_t v;
- len = (bits + JS_LIMB_BITS - 1) / JS_LIMB_BITS;
- r = js_bigint_new(ctx, len);
- if (!r) {
- JS_FreeValue(ctx, a);
- return JS_EXCEPTION;
- }
- r->len = len;
- for(i = 0; i < len - 1; i++)
- r->tab[i] = p->tab[i];
- shift = (-bits) & (JS_LIMB_BITS - 1);
- /* 0 <= shift <= JS_LIMB_BITS - 1 */
- v = p->tab[len - 1] << shift;
- if (asIntN)
- v = (js_slimb_t)v >> shift;
- else
- v = v >> shift;
- r->tab[len - 1] = v;
- r = js_bigint_normalize(ctx, r);
- JS_FreeValue(ctx, a);
- res = JS_CompactBigInt(ctx, r);
- }
- }
- return res;
- }
- static const JSCFunctionListEntry js_bigint_funcs[] = {
- JS_CFUNC_MAGIC_DEF("asUintN", 2, js_bigint_asUintN, 0 ),
- JS_CFUNC_MAGIC_DEF("asIntN", 2, js_bigint_asUintN, 1 ),
- };
- static const JSCFunctionListEntry js_bigint_proto_funcs[] = {
- JS_CFUNC_DEF("toString", 0, js_bigint_toString ),
- JS_CFUNC_DEF("valueOf", 0, js_bigint_valueOf ),
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "BigInt", JS_PROP_CONFIGURABLE ),
- };
- static int JS_AddIntrinsicBigInt(JSContext *ctx)
- {
- JSValue obj1;
- obj1 = JS_NewCConstructor(ctx, JS_CLASS_BIG_INT, "BigInt",
- js_bigint_constructor, 1, JS_CFUNC_constructor_or_func, 0,
- JS_UNDEFINED,
- js_bigint_funcs, countof(js_bigint_funcs),
- js_bigint_proto_funcs, countof(js_bigint_proto_funcs),
- 0);
- if (JS_IsException(obj1))
- return -1;
- JS_FreeValue(ctx, obj1);
- return 0;
- }
- /* Minimum amount of objects to be able to compile code and display
- error messages. */
- static int JS_AddIntrinsicBasicObjects(JSContext *ctx)
- {
- JSValue obj;
- JSCFunctionType ft;
- int i;
- /* warning: ordering is tricky */
- ctx->class_proto[JS_CLASS_OBJECT] =
- JS_NewObjectProtoClassAlloc(ctx, JS_NULL, JS_CLASS_OBJECT,
- countof(js_object_proto_funcs) + 1);
- if (JS_IsException(ctx->class_proto[JS_CLASS_OBJECT]))
- return -1;
- JS_SetImmutablePrototype(ctx, ctx->class_proto[JS_CLASS_OBJECT]);
- /* 2 more properties: caller and arguments */
- ctx->function_proto = JS_NewCFunction3(ctx, js_function_proto, "", 0,
- JS_CFUNC_generic, 0,
- ctx->class_proto[JS_CLASS_OBJECT],
- countof(js_function_proto_funcs) + 3 + 2);
- if (JS_IsException(ctx->function_proto))
- return -1;
- ctx->class_proto[JS_CLASS_BYTECODE_FUNCTION] = JS_DupValue(ctx, ctx->function_proto);
- ctx->global_obj = JS_NewObjectProtoClassAlloc(ctx, ctx->class_proto[JS_CLASS_OBJECT],
- JS_CLASS_GLOBAL_OBJECT, 64);
- if (JS_IsException(ctx->global_obj))
- return -1;
- {
- JSObject *p;
- obj = JS_NewObjectProtoClassAlloc(ctx, JS_NULL, JS_CLASS_OBJECT, 4);
- p = JS_VALUE_GET_OBJ(ctx->global_obj);
- p->u.global_object.uninitialized_vars = obj;
- }
- ctx->global_var_obj = JS_NewObjectProtoClassAlloc(ctx, JS_NULL,
- JS_CLASS_OBJECT, 16);
- if (JS_IsException(ctx->global_var_obj))
- return -1;
- /* Error */
- ft.generic_magic = js_error_constructor;
- obj = JS_NewCConstructor(ctx, JS_CLASS_ERROR, "Error",
- ft.generic, 1, JS_CFUNC_constructor_or_func_magic, -1,
- JS_UNDEFINED,
- js_error_funcs, countof(js_error_funcs),
- js_error_proto_funcs, countof(js_error_proto_funcs),
- 0);
- if (JS_IsException(obj))
- return -1;
- for(i = 0; i < JS_NATIVE_ERROR_COUNT; i++) {
- JSValue func_obj;
- const JSCFunctionListEntry *funcs;
- int n_args;
- char buf[ATOM_GET_STR_BUF_SIZE];
- const char *name = JS_AtomGetStr(ctx, buf, sizeof(buf),
- JS_ATOM_EvalError + i);
- n_args = 1 + (i == JS_AGGREGATE_ERROR);
- funcs = js_native_error_proto_funcs + 2 * i;
- func_obj = JS_NewCConstructor(ctx, -1, name,
- ft.generic, n_args, JS_CFUNC_constructor_or_func_magic, i,
- obj,
- NULL, 0,
- funcs, 2,
- 0);
- if (JS_IsException(func_obj)) {
- JS_FreeValue(ctx, obj);
- return -1;
- }
- ctx->native_error_proto[i] = JS_GetProperty(ctx, func_obj, JS_ATOM_prototype);
- JS_FreeValue(ctx, func_obj);
- if (JS_IsException(ctx->native_error_proto[i])) {
- JS_FreeValue(ctx, obj);
- return -1;
- }
- }
- JS_FreeValue(ctx, obj);
- /* Array */
- obj = JS_NewCConstructor(ctx, JS_CLASS_ARRAY, "Array",
- js_array_constructor, 1, JS_CFUNC_constructor_or_func, 0,
- JS_UNDEFINED,
- js_array_funcs, countof(js_array_funcs),
- js_array_proto_funcs, countof(js_array_proto_funcs),
- JS_NEW_CTOR_PROTO_CLASS);
- if (JS_IsException(obj))
- return -1;
- ctx->array_ctor = obj;
- {
- JSObject *p = JS_VALUE_GET_OBJ(ctx->class_proto[JS_CLASS_ARRAY]);
- p->is_std_array_prototype = TRUE;
- }
-
- ctx->array_shape = js_new_shape2(ctx, get_proto_obj(ctx->class_proto[JS_CLASS_ARRAY]),
- JS_PROP_INITIAL_HASH_SIZE, 1);
- if (!ctx->array_shape)
- return -1;
- if (add_shape_property(ctx, &ctx->array_shape, NULL,
- JS_ATOM_length, JS_PROP_WRITABLE | JS_PROP_LENGTH))
- return -1;
- ctx->arguments_shape = js_new_shape2(ctx, get_proto_obj(ctx->class_proto[JS_CLASS_OBJECT]),
- JS_PROP_INITIAL_HASH_SIZE, 3);
- if (!ctx->arguments_shape)
- return -1;
- if (add_shape_property(ctx, &ctx->arguments_shape, NULL,
- JS_ATOM_length, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE))
- return -1;
- if (add_shape_property(ctx, &ctx->arguments_shape, NULL,
- JS_ATOM_Symbol_iterator, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE))
- return -1;
- if (add_shape_property(ctx, &ctx->arguments_shape, NULL,
- JS_ATOM_callee, JS_PROP_GETSET))
- return -1;
- ctx->mapped_arguments_shape = js_new_shape2(ctx, get_proto_obj(ctx->class_proto[JS_CLASS_OBJECT]),
- JS_PROP_INITIAL_HASH_SIZE, 3);
- if (!ctx->mapped_arguments_shape)
- return -1;
- if (add_shape_property(ctx, &ctx->mapped_arguments_shape, NULL,
- JS_ATOM_length, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE))
- return -1;
- if (add_shape_property(ctx, &ctx->mapped_arguments_shape, NULL,
- JS_ATOM_Symbol_iterator, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE))
- return -1;
- if (add_shape_property(ctx, &ctx->mapped_arguments_shape, NULL,
- JS_ATOM_callee, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE))
- return -1;
-
- return 0;
- }
- int JS_AddIntrinsicBaseObjects(JSContext *ctx)
- {
- JSValue obj1, obj2;
- JSCFunctionType ft;
- ctx->throw_type_error = JS_NewCFunction(ctx, js_throw_type_error, NULL, 0);
- if (JS_IsException(ctx->throw_type_error))
- return -1;
- /* add caller and arguments properties to throw a TypeError */
- if (JS_DefineProperty(ctx, ctx->function_proto, JS_ATOM_caller, JS_UNDEFINED,
- ctx->throw_type_error, ctx->throw_type_error,
- JS_PROP_HAS_GET | JS_PROP_HAS_SET |
- JS_PROP_HAS_CONFIGURABLE | JS_PROP_CONFIGURABLE) < 0)
- return -1;
- if (JS_DefineProperty(ctx, ctx->function_proto, JS_ATOM_arguments, JS_UNDEFINED,
- ctx->throw_type_error, ctx->throw_type_error,
- JS_PROP_HAS_GET | JS_PROP_HAS_SET |
- JS_PROP_HAS_CONFIGURABLE | JS_PROP_CONFIGURABLE) < 0)
- return -1;
- JS_FreeValue(ctx, js_object_seal(ctx, JS_UNDEFINED, 1, (JSValueConst *)&ctx->throw_type_error, 1));
- /* Object */
- obj1 = JS_NewCConstructor(ctx, JS_CLASS_OBJECT, "Object",
- js_object_constructor, 1, JS_CFUNC_constructor_or_func, 0,
- JS_UNDEFINED,
- js_object_funcs, countof(js_object_funcs),
- js_object_proto_funcs, countof(js_object_proto_funcs),
- JS_NEW_CTOR_PROTO_EXIST);
- if (JS_IsException(obj1))
- return -1;
- JS_FreeValue(ctx, obj1);
-
- /* Function */
- ft.generic_magic = js_function_constructor;
- obj1 = JS_NewCConstructor(ctx, JS_CLASS_BYTECODE_FUNCTION, "Function",
- ft.generic, 1, JS_CFUNC_constructor_or_func_magic, JS_FUNC_NORMAL,
- JS_UNDEFINED,
- NULL, 0,
- js_function_proto_funcs, countof(js_function_proto_funcs),
- JS_NEW_CTOR_PROTO_EXIST);
- if (JS_IsException(obj1))
- return -1;
- ctx->function_ctor = obj1;
- /* Iterator */
- obj2 = JS_NewCConstructor(ctx, JS_CLASS_ITERATOR, "Iterator",
- js_iterator_constructor, 0, JS_CFUNC_constructor_or_func, 0,
- JS_UNDEFINED,
- js_iterator_funcs, countof(js_iterator_funcs),
- js_iterator_proto_funcs, countof(js_iterator_proto_funcs),
- 0);
- if (JS_IsException(obj2))
- return -1;
- // quirk: Iterator.prototype.constructor is an accessor property
- // TODO(bnoordhuis) mildly inefficient because JS_NewGlobalCConstructor
- // first creates a .constructor value property that we then replace with
- // an accessor
- obj1 = JS_NewCFunctionData(ctx, js_iterator_constructor_getset,
- 0, 0, 1, (JSValueConst *)&obj2);
- if (JS_IsException(obj1)) {
- JS_FreeValue(ctx, obj2);
- return -1;
- }
- if (JS_DefineProperty(ctx, ctx->class_proto[JS_CLASS_ITERATOR],
- JS_ATOM_constructor, JS_UNDEFINED,
- obj1, obj1,
- JS_PROP_HAS_GET | JS_PROP_HAS_SET | JS_PROP_CONFIGURABLE) < 0) {
- JS_FreeValue(ctx, obj2);
- JS_FreeValue(ctx, obj1);
- return -1;
- }
- JS_FreeValue(ctx, obj1);
- ctx->iterator_ctor = obj2;
-
- ctx->class_proto[JS_CLASS_ITERATOR_CONCAT] =
- JS_NewObjectProtoList(ctx, ctx->class_proto[JS_CLASS_ITERATOR],
- js_iterator_concat_proto_funcs,
- countof(js_iterator_concat_proto_funcs));
- if (JS_IsException(ctx->class_proto[JS_CLASS_ITERATOR_CONCAT]))
- return -1;
- ctx->class_proto[JS_CLASS_ITERATOR_HELPER] =
- JS_NewObjectProtoList(ctx, ctx->class_proto[JS_CLASS_ITERATOR],
- js_iterator_helper_proto_funcs,
- countof(js_iterator_helper_proto_funcs));
- if (JS_IsException(ctx->class_proto[JS_CLASS_ITERATOR_HELPER]))
- return -1;
-
- ctx->class_proto[JS_CLASS_ITERATOR_WRAP] =
- JS_NewObjectProtoList(ctx, ctx->class_proto[JS_CLASS_ITERATOR],
- js_iterator_wrap_proto_funcs,
- countof(js_iterator_wrap_proto_funcs));
- if (JS_IsException(ctx->class_proto[JS_CLASS_ITERATOR_WRAP]))
- return -1;
- /* needed to initialize arguments[Symbol.iterator] */
- ctx->array_proto_values =
- JS_GetProperty(ctx, ctx->class_proto[JS_CLASS_ARRAY], JS_ATOM_values);
- if (JS_IsException(ctx->array_proto_values))
- return -1;
- ctx->class_proto[JS_CLASS_ARRAY_ITERATOR] =
- JS_NewObjectProtoList(ctx, ctx->class_proto[JS_CLASS_ITERATOR],
- js_array_iterator_proto_funcs,
- countof(js_array_iterator_proto_funcs));
- if (JS_IsException(ctx->class_proto[JS_CLASS_ARRAY_ITERATOR]))
- return -1;
- /* parseFloat and parseInteger must be defined before Number
- because of the Number.parseFloat and Number.parseInteger
- aliases */
- if (JS_SetPropertyFunctionList(ctx, ctx->global_obj, js_global_funcs,
- countof(js_global_funcs)))
- return -1;
- /* Number */
- obj1 = JS_NewCConstructor(ctx, JS_CLASS_NUMBER, "Number",
- js_number_constructor, 1, JS_CFUNC_constructor_or_func, 0,
- JS_UNDEFINED,
- js_number_funcs, countof(js_number_funcs),
- js_number_proto_funcs, countof(js_number_proto_funcs),
- JS_NEW_CTOR_PROTO_CLASS);
- if (JS_IsException(obj1))
- return -1;
- JS_FreeValue(ctx, obj1);
- if (JS_SetObjectData(ctx, ctx->class_proto[JS_CLASS_NUMBER], JS_NewInt32(ctx, 0)))
- return -1;
-
- /* Boolean */
- obj1 = JS_NewCConstructor(ctx, JS_CLASS_BOOLEAN, "Boolean",
- js_boolean_constructor, 1, JS_CFUNC_constructor_or_func, 0,
- JS_UNDEFINED,
- NULL, 0,
- js_boolean_proto_funcs, countof(js_boolean_proto_funcs),
- JS_NEW_CTOR_PROTO_CLASS);
- if (JS_IsException(obj1))
- return -1;
- JS_FreeValue(ctx, obj1);
- if (JS_SetObjectData(ctx, ctx->class_proto[JS_CLASS_BOOLEAN], JS_NewBool(ctx, FALSE)))
- return -1;
- /* String */
- obj1 = JS_NewCConstructor(ctx, JS_CLASS_STRING, "String",
- js_string_constructor, 1, JS_CFUNC_constructor_or_func, 0,
- JS_UNDEFINED,
- js_string_funcs, countof(js_string_funcs),
- js_string_proto_funcs, countof(js_string_proto_funcs),
- JS_NEW_CTOR_PROTO_CLASS);
- if (JS_IsException(obj1))
- return -1;
- JS_FreeValue(ctx, obj1);
- if (JS_SetObjectData(ctx, ctx->class_proto[JS_CLASS_STRING], JS_AtomToString(ctx, JS_ATOM_empty_string)))
- return -1;
- ctx->class_proto[JS_CLASS_STRING_ITERATOR] =
- JS_NewObjectProtoList(ctx, ctx->class_proto[JS_CLASS_ITERATOR],
- js_string_iterator_proto_funcs,
- countof(js_string_iterator_proto_funcs));
- if (JS_IsException(ctx->class_proto[JS_CLASS_STRING_ITERATOR]))
- return -1;
- /* Math: create as autoinit object */
- js_random_init(ctx);
- if (JS_SetPropertyFunctionList(ctx, ctx->global_obj, js_math_obj, countof(js_math_obj)))
- return -1;
- /* ES6 Reflect: create as autoinit object */
- if (JS_SetPropertyFunctionList(ctx, ctx->global_obj, js_reflect_obj, countof(js_reflect_obj)))
- return -1;
- /* ES6 Symbol */
- obj1 = JS_NewCConstructor(ctx, JS_CLASS_SYMBOL, "Symbol",
- js_symbol_constructor, 0, JS_CFUNC_constructor_or_func, 0,
- JS_UNDEFINED,
- js_symbol_funcs, countof(js_symbol_funcs),
- js_symbol_proto_funcs, countof(js_symbol_proto_funcs),
- 0);
- if (JS_IsException(obj1))
- return -1;
- JS_FreeValue(ctx, obj1);
-
- /* ES6 Generator */
- ctx->class_proto[JS_CLASS_GENERATOR] =
- JS_NewObjectProtoList(ctx, ctx->class_proto[JS_CLASS_ITERATOR],
- js_generator_proto_funcs,
- countof(js_generator_proto_funcs));
- if (JS_IsException(ctx->class_proto[JS_CLASS_GENERATOR]))
- return -1;
- ft.generic_magic = js_function_constructor;
- obj1 = JS_NewCConstructor(ctx, JS_CLASS_GENERATOR_FUNCTION, "GeneratorFunction",
- ft.generic, 1, JS_CFUNC_constructor_or_func_magic, JS_FUNC_GENERATOR,
- ctx->function_ctor,
- NULL, 0,
- js_generator_function_proto_funcs,
- countof(js_generator_function_proto_funcs),
- JS_NEW_CTOR_NO_GLOBAL | JS_NEW_CTOR_READONLY);
- if (JS_IsException(obj1))
- return -1;
- JS_FreeValue(ctx, obj1);
- if (JS_SetConstructor2(ctx, ctx->class_proto[JS_CLASS_GENERATOR_FUNCTION],
- ctx->class_proto[JS_CLASS_GENERATOR],
- JS_PROP_CONFIGURABLE, JS_PROP_CONFIGURABLE))
- return -1;
-
- /* global properties */
- ctx->eval_obj = JS_GetProperty(ctx, ctx->global_obj, JS_ATOM_eval);
- if (JS_IsException(ctx->eval_obj))
- return -1;
-
- if (JS_DefinePropertyValue(ctx, ctx->global_obj, JS_ATOM_globalThis,
- JS_DupValue(ctx, ctx->global_obj),
- JS_PROP_CONFIGURABLE | JS_PROP_WRITABLE) < 0)
- return -1;
- /* BigInt */
- if (JS_AddIntrinsicBigInt(ctx))
- return -1;
- return 0;
- }
- /* Typed Arrays */
- static uint8_t const typed_array_size_log2[JS_TYPED_ARRAY_COUNT] = {
- 0, 0, 0, 1, 1, 2, 2,
- 3, 3, // BigInt64Array, BigUint64Array
- 1, 2, 3 // Float16Array, Float32Array, Float64Array
- };
- static JSValue js_array_buffer_constructor3(JSContext *ctx,
- JSValueConst new_target,
- uint64_t len, uint64_t *max_len,
- JSClassID class_id,
- uint8_t *buf,
- JSFreeArrayBufferDataFunc *free_func,
- void *opaque, BOOL alloc_flag)
- {
- JSRuntime *rt = ctx->rt;
- JSValue obj;
- JSArrayBuffer *abuf = NULL;
- uint64_t sab_alloc_len;
- if (!alloc_flag && buf && max_len && free_func != js_array_buffer_free) {
- // not observable from JS land, only through C API misuse;
- // JS code cannot create externally managed buffers directly
- return JS_ThrowInternalError(ctx,
- "resizable ArrayBuffers not supported "
- "for externally managed buffers");
- }
- obj = js_create_from_ctor(ctx, new_target, class_id);
- if (JS_IsException(obj))
- return obj;
- /* XXX: we are currently limited to 2 GB */
- if (len > INT32_MAX) {
- JS_ThrowRangeError(ctx, "invalid array buffer length");
- goto fail;
- }
- if (max_len && *max_len > INT32_MAX) {
- JS_ThrowRangeError(ctx, "invalid max array buffer length");
- goto fail;
- }
- abuf = js_malloc(ctx, sizeof(*abuf));
- if (!abuf)
- goto fail;
- abuf->byte_length = len;
- abuf->max_byte_length = max_len ? *max_len : -1;
- if (alloc_flag) {
- if (class_id == JS_CLASS_SHARED_ARRAY_BUFFER &&
- rt->sab_funcs.sab_alloc) {
- // TOOD(bnoordhuis) resizing backing memory for SABs atomically
- // is hard so we cheat and allocate |maxByteLength| bytes upfront
- sab_alloc_len = max_len ? *max_len : len;
- abuf->data = rt->sab_funcs.sab_alloc(rt->sab_funcs.sab_opaque,
- max_int(sab_alloc_len, 1));
- if (!abuf->data)
- goto fail;
- memset(abuf->data, 0, sab_alloc_len);
- } else {
- /* the allocation must be done after the object creation */
- abuf->data = js_mallocz(ctx, max_int(len, 1));
- if (!abuf->data)
- goto fail;
- }
- } else {
- if (class_id == JS_CLASS_SHARED_ARRAY_BUFFER &&
- rt->sab_funcs.sab_dup) {
- rt->sab_funcs.sab_dup(rt->sab_funcs.sab_opaque, buf);
- }
- abuf->data = buf;
- }
- init_list_head(&abuf->array_list);
- abuf->detached = FALSE;
- abuf->shared = (class_id == JS_CLASS_SHARED_ARRAY_BUFFER);
- abuf->opaque = opaque;
- abuf->free_func = free_func;
- if (alloc_flag && buf)
- memcpy(abuf->data, buf, len);
- JS_SetOpaque(obj, abuf);
- return obj;
- fail:
- JS_FreeValue(ctx, obj);
- js_free(ctx, abuf);
- return JS_EXCEPTION;
- }
- static void js_array_buffer_free(JSRuntime *rt, void *opaque, void *ptr)
- {
- js_free_rt(rt, ptr);
- }
- static JSValue js_array_buffer_constructor2(JSContext *ctx,
- JSValueConst new_target,
- uint64_t len, uint64_t *max_len,
- JSClassID class_id)
- {
- return js_array_buffer_constructor3(ctx, new_target, len, max_len, class_id,
- NULL, js_array_buffer_free, NULL,
- TRUE);
- }
- static JSValue js_array_buffer_constructor1(JSContext *ctx,
- JSValueConst new_target,
- uint64_t len, uint64_t *max_len)
- {
- return js_array_buffer_constructor2(ctx, new_target, len, max_len,
- JS_CLASS_ARRAY_BUFFER);
- }
- JSValue JS_NewArrayBuffer(JSContext *ctx, uint8_t *buf, size_t len,
- JSFreeArrayBufferDataFunc *free_func, void *opaque,
- BOOL is_shared)
- {
- JSClassID class_id =
- is_shared ? JS_CLASS_SHARED_ARRAY_BUFFER : JS_CLASS_ARRAY_BUFFER;
- return js_array_buffer_constructor3(ctx, JS_UNDEFINED, len, NULL, class_id,
- buf, free_func, opaque, FALSE);
- }
- /* create a new ArrayBuffer of length 'len' and copy 'buf' to it */
- JSValue JS_NewArrayBufferCopy(JSContext *ctx, const uint8_t *buf, size_t len)
- {
- return js_array_buffer_constructor3(ctx, JS_UNDEFINED, len, NULL,
- JS_CLASS_ARRAY_BUFFER,
- (uint8_t *)buf,
- js_array_buffer_free, NULL,
- TRUE);
- }
- static JSValue js_array_buffer_constructor0(JSContext *ctx, JSValueConst new_target,
- int argc, JSValueConst *argv,
- JSClassID class_id)
- {
- uint64_t len, max_len, *pmax_len = NULL;
- JSValue obj, val;
- int64_t i;
- if (JS_ToIndex(ctx, &len, argv[0]))
- return JS_EXCEPTION;
- if (argc < 2)
- goto next;
- if (!JS_IsObject(argv[1]))
- goto next;
- obj = JS_ToObject(ctx, argv[1]);
- if (JS_IsException(obj))
- return JS_EXCEPTION;
- val = JS_GetProperty(ctx, obj, JS_ATOM_maxByteLength);
- JS_FreeValue(ctx, obj);
- if (JS_IsException(val))
- return JS_EXCEPTION;
- if (JS_IsUndefined(val))
- goto next;
- if (JS_ToInt64Free(ctx, &i, val))
- return JS_EXCEPTION;
- // don't have to check i < 0 because len >= 0
- if (len > i || i > MAX_SAFE_INTEGER)
- return JS_ThrowRangeError(ctx, "invalid array buffer max length");
- max_len = i;
- pmax_len = &max_len;
- next:
- return js_array_buffer_constructor2(ctx, new_target, len, pmax_len,
- class_id);
- }
- static JSValue js_array_buffer_constructor(JSContext *ctx,
- JSValueConst new_target,
- int argc, JSValueConst *argv)
- {
- return js_array_buffer_constructor0(ctx, new_target, argc, argv,
- JS_CLASS_ARRAY_BUFFER);
- }
- static JSValue js_shared_array_buffer_constructor(JSContext *ctx,
- JSValueConst new_target,
- int argc, JSValueConst *argv)
- {
- return js_array_buffer_constructor0(ctx, new_target, argc, argv,
- JS_CLASS_SHARED_ARRAY_BUFFER);
- }
- /* also used for SharedArrayBuffer */
- static void js_array_buffer_finalizer(JSRuntime *rt, JSValue val)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JSArrayBuffer *abuf = p->u.array_buffer;
- struct list_head *el, *el1;
- if (abuf) {
- /* The ArrayBuffer finalizer may be called before the typed
- array finalizers using it, so abuf->array_list is not
- necessarily empty. */
- list_for_each_safe(el, el1, &abuf->array_list) {
- JSTypedArray *ta;
- JSObject *p1;
- ta = list_entry(el, JSTypedArray, link);
- ta->link.prev = NULL;
- ta->link.next = NULL;
- p1 = ta->obj;
- /* Note: the typed array length and offset fields are not modified */
- if (p1->class_id != JS_CLASS_DATAVIEW) {
- p1->u.array.count = 0;
- p1->u.array.u.ptr = NULL;
- }
- }
- if (abuf->shared && rt->sab_funcs.sab_free) {
- rt->sab_funcs.sab_free(rt->sab_funcs.sab_opaque, abuf->data);
- } else {
- if (abuf->free_func)
- abuf->free_func(rt, abuf->opaque, abuf->data);
- }
- js_free_rt(rt, abuf);
- }
- }
- static JSValue js_array_buffer_isView(JSContext *ctx,
- JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSObject *p;
- BOOL res;
- res = FALSE;
- if (JS_VALUE_GET_TAG(argv[0]) == JS_TAG_OBJECT) {
- p = JS_VALUE_GET_OBJ(argv[0]);
- if (p->class_id >= JS_CLASS_UINT8C_ARRAY &&
- p->class_id <= JS_CLASS_DATAVIEW) {
- res = TRUE;
- }
- }
- return JS_NewBool(ctx, res);
- }
- static const JSCFunctionListEntry js_array_buffer_funcs[] = {
- JS_CFUNC_DEF("isView", 1, js_array_buffer_isView ),
- JS_CGETSET_DEF("[Symbol.species]", js_get_this, NULL ),
- };
- static JSValue JS_ThrowTypeErrorDetachedArrayBuffer(JSContext *ctx)
- {
- return JS_ThrowTypeError(ctx, "ArrayBuffer is detached");
- }
- static JSValue JS_ThrowTypeErrorArrayBufferOOB(JSContext *ctx)
- {
- return JS_ThrowTypeError(ctx, "ArrayBuffer is detached or resized");
- }
- // #sec-get-arraybuffer.prototype.detached
- static JSValue js_array_buffer_get_detached(JSContext *ctx,
- JSValueConst this_val)
- {
- JSArrayBuffer *abuf = JS_GetOpaque2(ctx, this_val, JS_CLASS_ARRAY_BUFFER);
- if (!abuf)
- return JS_EXCEPTION;
- if (abuf->shared)
- return JS_ThrowTypeError(ctx, "detached called on SharedArrayBuffer");
- return JS_NewBool(ctx, abuf->detached);
- }
- static JSValue js_array_buffer_get_byteLength(JSContext *ctx,
- JSValueConst this_val,
- int class_id)
- {
- JSArrayBuffer *abuf = JS_GetOpaque2(ctx, this_val, class_id);
- if (!abuf)
- return JS_EXCEPTION;
- /* return 0 if detached */
- return JS_NewUint32(ctx, abuf->byte_length);
- }
- static JSValue js_array_buffer_get_maxByteLength(JSContext *ctx,
- JSValueConst this_val,
- int class_id)
- {
- JSArrayBuffer *abuf = JS_GetOpaque2(ctx, this_val, class_id);
- if (!abuf)
- return JS_EXCEPTION;
- if (array_buffer_is_resizable(abuf))
- return JS_NewUint32(ctx, abuf->max_byte_length);
- return JS_NewUint32(ctx, abuf->byte_length);
- }
- static JSValue js_array_buffer_get_resizable(JSContext *ctx,
- JSValueConst this_val,
- int class_id)
- {
- JSArrayBuffer *abuf = JS_GetOpaque2(ctx, this_val, class_id);
- if (!abuf)
- return JS_EXCEPTION;
- return JS_NewBool(ctx, array_buffer_is_resizable(abuf));
- }
- static void js_array_buffer_update_typed_arrays(JSArrayBuffer *abuf)
- {
- uint32_t size_log2, size_elem;
- struct list_head *el;
- JSTypedArray *ta;
- JSObject *p;
- uint8_t *data;
- int64_t len;
- len = abuf->byte_length;
- data = abuf->data;
- // update lengths of all typed arrays backed by this array buffer
- list_for_each(el, &abuf->array_list) {
- ta = list_entry(el, JSTypedArray, link);
- p = ta->obj;
- if (p->class_id == JS_CLASS_DATAVIEW) {
- if (ta->track_rab) {
- if (ta->offset < len)
- ta->length = len - ta->offset;
- else
- ta->length = 0;
- }
- } else {
- p->u.array.count = 0;
- p->u.array.u.ptr = NULL;
- size_log2 = typed_array_size_log2(p->class_id);
- size_elem = 1 << size_log2;
- if (ta->track_rab) {
- if (len >= (int64_t)ta->offset + size_elem) {
- p->u.array.count = (len - ta->offset) >> size_log2;
- p->u.array.u.ptr = &data[ta->offset];
- }
- } else {
- if (len >= (int64_t)ta->offset + ta->length) {
- p->u.array.count = ta->length >> size_log2;
- p->u.array.u.ptr = &data[ta->offset];
- }
- }
- }
- }
-
- }
- void JS_DetachArrayBuffer(JSContext *ctx, JSValueConst obj)
- {
- JSArrayBuffer *abuf = JS_GetOpaque(obj, JS_CLASS_ARRAY_BUFFER);
- if (!abuf || abuf->detached)
- return;
- if (abuf->free_func)
- abuf->free_func(ctx->rt, abuf->opaque, abuf->data);
- abuf->data = NULL;
- abuf->byte_length = 0;
- abuf->detached = TRUE;
- js_array_buffer_update_typed_arrays(abuf);
- }
- /* get an ArrayBuffer or SharedArrayBuffer */
- static JSArrayBuffer *js_get_array_buffer(JSContext *ctx, JSValueConst obj)
- {
- JSObject *p;
- if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)
- goto fail;
- p = JS_VALUE_GET_OBJ(obj);
- if (p->class_id != JS_CLASS_ARRAY_BUFFER &&
- p->class_id != JS_CLASS_SHARED_ARRAY_BUFFER) {
- fail:
- JS_ThrowTypeErrorInvalidClass(ctx, JS_CLASS_ARRAY_BUFFER);
- return NULL;
- }
- return p->u.array_buffer;
- }
- /* return NULL if exception. WARNING: any JS call can detach the
- buffer and render the returned pointer invalid */
- uint8_t *JS_GetArrayBuffer(JSContext *ctx, size_t *psize, JSValueConst obj)
- {
- JSArrayBuffer *abuf = js_get_array_buffer(ctx, obj);
- if (!abuf)
- goto fail;
- if (abuf->detached) {
- JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
- goto fail;
- }
- *psize = abuf->byte_length;
- return abuf->data;
- fail:
- *psize = 0;
- return NULL;
- }
- static BOOL array_buffer_is_resizable(const JSArrayBuffer *abuf)
- {
- return abuf->max_byte_length >= 0;
- }
- // ES #sec-arraybuffer.prototype.transfer
- static JSValue js_array_buffer_transfer(JSContext *ctx,
- JSValueConst this_val,
- int argc, JSValueConst *argv,
- int transfer_to_fixed_length)
- {
- JSArrayBuffer *abuf;
- uint64_t new_len, *pmax_len, max_len;
- JSValue res;
- abuf = JS_GetOpaque2(ctx, this_val, JS_CLASS_ARRAY_BUFFER);
- if (!abuf)
- return JS_EXCEPTION;
- if (abuf->shared)
- return JS_ThrowTypeError(ctx, "cannot transfer a SharedArrayBuffer");
- if (argc < 1 || JS_IsUndefined(argv[0]))
- new_len = abuf->byte_length;
- else if (JS_ToIndex(ctx, &new_len, argv[0]))
- return JS_EXCEPTION;
- if (abuf->detached)
- return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
- pmax_len = NULL;
- if (!transfer_to_fixed_length) {
- if (array_buffer_is_resizable(abuf)) { // carry over maxByteLength
- max_len = abuf->max_byte_length;
- if (new_len > max_len)
- return JS_ThrowTypeError(ctx, "invalid array buffer length");
- // TODO(bnoordhuis) support externally managed RABs
- if (abuf->free_func == js_array_buffer_free)
- pmax_len = &max_len;
- }
- }
- /* create an empty AB */
- if (new_len == 0) {
- res = js_array_buffer_constructor2(ctx, JS_UNDEFINED, 0, pmax_len, JS_CLASS_ARRAY_BUFFER);
- if (JS_IsException(res))
- return res;
- JS_DetachArrayBuffer(ctx, this_val);
- } else {
- uint64_t old_len;
-
- old_len = abuf->byte_length;
- /* if length mismatch, realloc. Otherwise, use the same backing buffer. */
- if (new_len != old_len) {
- /* XXX: we are currently limited to 2 GB */
- if (new_len > INT32_MAX)
- return JS_ThrowRangeError(ctx, "invalid array buffer length");
- if (abuf->free_func != js_array_buffer_free) {
- JSArrayBuffer *new_abuf;
- /* cannot use js_realloc() because the buffer was
- allocated with a custom allocator */
- res = js_array_buffer_constructor2(ctx, JS_UNDEFINED, new_len, pmax_len, JS_CLASS_ARRAY_BUFFER);
- if (JS_IsException(res))
- return res;
- new_abuf = JS_GetOpaque2(ctx, res, JS_CLASS_ARRAY_BUFFER);
- memcpy(new_abuf->data, abuf->data, min_int(old_len, new_len));
- abuf->free_func(ctx->rt, abuf->opaque, abuf->data);
- } else {
- JSArrayBuffer *new_abuf;
- uint8_t *new_bs;
- /* reallocate the buffer after the new array buffer is
- created in case the new array buffer creation
- fails. */
- res = js_array_buffer_constructor2(ctx, JS_UNDEFINED, 0, pmax_len, JS_CLASS_ARRAY_BUFFER);
- if (JS_IsException(res))
- return res;
- new_bs = js_realloc(ctx, abuf->data, new_len);
- if (!new_bs) {
- JS_FreeValue(ctx, res);
- return JS_EXCEPTION;
- }
- if (new_len > old_len)
- memset(new_bs + old_len, 0, new_len - old_len);
- new_abuf = JS_GetOpaque2(ctx, res, JS_CLASS_ARRAY_BUFFER);
- js_free(ctx, new_abuf->data);
- new_abuf->data = new_bs;
- new_abuf->byte_length = new_len;
- }
- } else {
- /* can keep the custom free function */
- res = js_array_buffer_constructor3(ctx, JS_UNDEFINED, new_len, pmax_len,
- JS_CLASS_ARRAY_BUFFER,
- abuf->data, abuf->free_func,
- abuf->opaque, FALSE);
- if (JS_IsException(res))
- return res;
- }
- /* neuter the backing buffer */
- abuf->data = NULL;
- abuf->byte_length = 0;
- abuf->detached = TRUE;
- js_array_buffer_update_typed_arrays(abuf);
- }
- return res;
- }
- static JSValue js_array_buffer_resize(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int class_id)
- {
- JSArrayBuffer *abuf;
- uint8_t *data;
- int64_t len;
- abuf = JS_GetOpaque2(ctx, this_val, class_id);
- if (!abuf)
- return JS_EXCEPTION;
- if (JS_ToInt64(ctx, &len, argv[0]))
- return JS_EXCEPTION;
- if (abuf->detached)
- return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
- if (!array_buffer_is_resizable(abuf))
- return JS_ThrowTypeError(ctx, "array buffer is not resizable");
- // TODO(bnoordhuis) support externally managed RABs
- if (abuf->free_func != js_array_buffer_free)
- return JS_ThrowTypeError(ctx, "external array buffer is not resizable");
- if (len < 0 || len > abuf->max_byte_length) {
- bad_length:
- return JS_ThrowRangeError(ctx, "invalid array buffer length");
- }
- // SABs can only grow and we don't need to realloc because
- // js_array_buffer_constructor3 commits all memory upfront;
- // regular RABs are resizable both ways and realloc
- if (abuf->shared) {
- if (len < abuf->byte_length)
- goto bad_length;
- // Note this is off-spec; there's supposed to be a single atomic
- // |byteLength| property that's shared across SABs but we store
- // it per SAB instead. That means when thread A calls sab.grow(2)
- // at time t0, and thread B calls sab.grow(1) at time t1, we don't
- // throw a TypeError in thread B as the spec says we should,
- // instead both threads get their own view of the backing memory,
- // 2 bytes big in A, and 1 byte big in B
- abuf->byte_length = len;
- } else {
- data = js_realloc(ctx, abuf->data, max_int(len, 1));
- if (!data)
- return JS_EXCEPTION;
- if (len > abuf->byte_length)
- memset(&data[abuf->byte_length], 0, len - abuf->byte_length);
- abuf->byte_length = len;
- abuf->data = data;
- }
- js_array_buffer_update_typed_arrays(abuf);
- return JS_UNDEFINED;
- }
- static JSValue js_array_buffer_slice(JSContext *ctx,
- JSValueConst this_val,
- int argc, JSValueConst *argv, int class_id)
- {
- JSArrayBuffer *abuf, *new_abuf;
- int64_t len, start, end, new_len;
- JSValue ctor, new_obj;
- abuf = JS_GetOpaque2(ctx, this_val, class_id);
- if (!abuf)
- return JS_EXCEPTION;
- if (abuf->detached)
- return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
- len = abuf->byte_length;
- if (JS_ToInt64Clamp(ctx, &start, argv[0], 0, len, len))
- return JS_EXCEPTION;
- end = len;
- if (!JS_IsUndefined(argv[1])) {
- if (JS_ToInt64Clamp(ctx, &end, argv[1], 0, len, len))
- return JS_EXCEPTION;
- }
- new_len = max_int64(end - start, 0);
- ctor = JS_SpeciesConstructor(ctx, this_val, JS_UNDEFINED);
- if (JS_IsException(ctor))
- return ctor;
- if (JS_IsUndefined(ctor)) {
- new_obj = js_array_buffer_constructor2(ctx, JS_UNDEFINED, new_len,
- NULL, class_id);
- } else {
- JSValue args[1];
- args[0] = JS_NewInt64(ctx, new_len);
- new_obj = JS_CallConstructor(ctx, ctor, 1, (JSValueConst *)args);
- JS_FreeValue(ctx, ctor);
- JS_FreeValue(ctx, args[0]);
- }
- if (JS_IsException(new_obj))
- return new_obj;
- new_abuf = JS_GetOpaque2(ctx, new_obj, class_id);
- if (!new_abuf)
- goto fail;
- if (js_same_value(ctx, new_obj, this_val)) {
- JS_ThrowTypeError(ctx, "cannot use identical ArrayBuffer");
- goto fail;
- }
- if (new_abuf->detached) {
- JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
- goto fail;
- }
- if (new_abuf->byte_length < new_len) {
- JS_ThrowTypeError(ctx, "new ArrayBuffer is too small");
- goto fail;
- }
- /* must test again because of side effects */
- if (abuf->detached || abuf->byte_length < start + new_len) {
- JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
- goto fail;
- }
- memcpy(new_abuf->data, abuf->data + start, new_len);
- return new_obj;
- fail:
- JS_FreeValue(ctx, new_obj);
- return JS_EXCEPTION;
- }
- static const JSCFunctionListEntry js_array_buffer_proto_funcs[] = {
- JS_CGETSET_MAGIC_DEF("byteLength", js_array_buffer_get_byteLength, NULL, JS_CLASS_ARRAY_BUFFER ),
- JS_CGETSET_MAGIC_DEF("maxByteLength", js_array_buffer_get_maxByteLength, NULL, JS_CLASS_ARRAY_BUFFER ),
- JS_CGETSET_MAGIC_DEF("resizable", js_array_buffer_get_resizable, NULL, JS_CLASS_ARRAY_BUFFER ),
- JS_CGETSET_DEF("detached", js_array_buffer_get_detached, NULL ),
- JS_CFUNC_MAGIC_DEF("resize", 1, js_array_buffer_resize, JS_CLASS_ARRAY_BUFFER ),
- JS_CFUNC_MAGIC_DEF("slice", 2, js_array_buffer_slice, JS_CLASS_ARRAY_BUFFER ),
- JS_CFUNC_MAGIC_DEF("transfer", 0, js_array_buffer_transfer, 0 ),
- JS_CFUNC_MAGIC_DEF("transferToFixedLength", 0, js_array_buffer_transfer, 1 ),
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "ArrayBuffer", JS_PROP_CONFIGURABLE ),
- };
- /* SharedArrayBuffer */
- static const JSCFunctionListEntry js_shared_array_buffer_funcs[] = {
- JS_CGETSET_DEF("[Symbol.species]", js_get_this, NULL ),
- };
- static const JSCFunctionListEntry js_shared_array_buffer_proto_funcs[] = {
- JS_CGETSET_MAGIC_DEF("byteLength", js_array_buffer_get_byteLength, NULL, JS_CLASS_SHARED_ARRAY_BUFFER ),
- JS_CGETSET_MAGIC_DEF("maxByteLength", js_array_buffer_get_maxByteLength, NULL, JS_CLASS_SHARED_ARRAY_BUFFER ),
- JS_CGETSET_MAGIC_DEF("growable", js_array_buffer_get_resizable, NULL, JS_CLASS_SHARED_ARRAY_BUFFER ),
- JS_CFUNC_MAGIC_DEF("grow", 1, js_array_buffer_resize, JS_CLASS_SHARED_ARRAY_BUFFER ),
- JS_CFUNC_MAGIC_DEF("slice", 2, js_array_buffer_slice, JS_CLASS_SHARED_ARRAY_BUFFER ),
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "SharedArrayBuffer", JS_PROP_CONFIGURABLE ),
- };
- static JSObject *get_typed_array(JSContext *ctx, JSValueConst this_val)
- {
- JSObject *p;
- if (JS_VALUE_GET_TAG(this_val) != JS_TAG_OBJECT)
- goto fail;
- p = JS_VALUE_GET_OBJ(this_val);
- if (!(p->class_id >= JS_CLASS_UINT8C_ARRAY &&
- p->class_id <= JS_CLASS_FLOAT64_ARRAY)) {
- fail:
- JS_ThrowTypeError(ctx, "not a TypedArray");
- return NULL;
- }
- return p;
- }
- // is the typed array detached or out of bounds relative to its RAB?
- // |p| must be a typed array, *not* a DataView
- static BOOL typed_array_is_oob(JSObject *p)
- {
- JSArrayBuffer *abuf;
- JSTypedArray *ta;
- int len, size_elem;
- int64_t end;
- assert(p->class_id >= JS_CLASS_UINT8C_ARRAY);
- assert(p->class_id <= JS_CLASS_FLOAT64_ARRAY);
- ta = p->u.typed_array;
- abuf = ta->buffer->u.array_buffer;
- if (abuf->detached)
- return TRUE;
- len = abuf->byte_length;
- if (ta->offset > len)
- return TRUE;
- if (ta->track_rab)
- return FALSE;
- if (len < (int64_t)ta->offset + ta->length)
- return TRUE;
- size_elem = 1 << typed_array_size_log2(p->class_id);
- end = (int64_t)ta->offset + (int64_t)p->u.array.count * size_elem;
- return end > len;
- }
- // Be *very* careful if you touch the typed array's memory directly:
- // the length is only valid until the next call into JS land because
- // JS code can detach or resize the backing array buffer. Functions
- // like JS_GetProperty and JS_ToIndex call JS code.
- //
- // Exclusively reading or writing elements with JS_GetProperty,
- // JS_GetPropertyInt64, JS_SetProperty, etc. is safe because they
- // perform bounds checks, as does js_get_fast_array_element.
- static int js_typed_array_get_length_unsafe(JSContext *ctx, JSValueConst obj)
- {
- JSObject *p;
- p = get_typed_array(ctx, obj);
- if (!p)
- return -1;
- if (typed_array_is_oob(p)) {
- JS_ThrowTypeErrorArrayBufferOOB(ctx);
- return -1;
- }
- return p->u.array.count;
- }
- static int validate_typed_array(JSContext *ctx, JSValueConst this_val)
- {
- JSObject *p;
- p = get_typed_array(ctx, this_val);
- if (!p)
- return -1;
- if (typed_array_is_oob(p)) {
- JS_ThrowTypeErrorArrayBufferOOB(ctx);
- return -1;
- }
- return 0;
- }
- static JSValue js_typed_array_get_length(JSContext *ctx,
- JSValueConst this_val)
- {
- JSObject *p;
- p = get_typed_array(ctx, this_val);
- if (!p)
- return JS_EXCEPTION;
- return JS_NewInt32(ctx, p->u.array.count);
- }
- static JSValue js_typed_array_get_buffer(JSContext *ctx,
- JSValueConst this_val)
- {
- JSObject *p;
- JSTypedArray *ta;
- p = get_typed_array(ctx, this_val);
- if (!p)
- return JS_EXCEPTION;
- ta = p->u.typed_array;
- return JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, ta->buffer));
- }
- static JSValue js_typed_array_get_byteLength(JSContext *ctx,
- JSValueConst this_val)
- {
- JSObject *p;
- JSTypedArray *ta;
- int size_log2;
- p = get_typed_array(ctx, this_val);
- if (!p)
- return JS_EXCEPTION;
- if (typed_array_is_oob(p))
- return JS_NewInt32(ctx, 0);
- ta = p->u.typed_array;
- if (!ta->track_rab)
- return JS_NewUint32(ctx, ta->length);
- size_log2 = typed_array_size_log2(p->class_id);
- return JS_NewInt64(ctx, (int64_t)p->u.array.count << size_log2);
- }
- static JSValue js_typed_array_get_byteOffset(JSContext *ctx,
- JSValueConst this_val)
- {
- JSObject *p;
- JSTypedArray *ta;
- p = get_typed_array(ctx, this_val);
- if (!p)
- return JS_EXCEPTION;
- if (typed_array_is_oob(p))
- return JS_NewInt32(ctx, 0);
- ta = p->u.typed_array;
- return JS_NewUint32(ctx, ta->offset);
- }
- JSValue JS_NewTypedArray(JSContext *ctx, int argc, JSValueConst *argv,
- JSTypedArrayEnum type)
- {
- if (type < JS_TYPED_ARRAY_UINT8C || type > JS_TYPED_ARRAY_FLOAT64)
- return JS_ThrowRangeError(ctx, "invalid typed array type");
- return js_typed_array_constructor(ctx, JS_UNDEFINED, argc, argv,
- JS_CLASS_UINT8C_ARRAY + type);
- }
- /* Return the buffer associated to the typed array or an exception if
- it is not a typed array or if the buffer is detached. pbyte_offset,
- pbyte_length or pbytes_per_element can be NULL. */
- JSValue JS_GetTypedArrayBuffer(JSContext *ctx, JSValueConst obj,
- size_t *pbyte_offset,
- size_t *pbyte_length,
- size_t *pbytes_per_element)
- {
- JSObject *p;
- JSTypedArray *ta;
- p = get_typed_array(ctx, obj);
- if (!p)
- return JS_EXCEPTION;
- if (typed_array_is_oob(p))
- return JS_ThrowTypeErrorArrayBufferOOB(ctx);
- ta = p->u.typed_array;
- if (pbyte_offset)
- *pbyte_offset = ta->offset;
- if (pbyte_length)
- *pbyte_length = ta->length;
- if (pbytes_per_element) {
- *pbytes_per_element = 1 << typed_array_size_log2(p->class_id);
- }
- return JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, ta->buffer));
- }
- static JSValue js_typed_array_get_toStringTag(JSContext *ctx,
- JSValueConst this_val)
- {
- JSObject *p;
- if (JS_VALUE_GET_TAG(this_val) != JS_TAG_OBJECT)
- return JS_UNDEFINED;
- p = JS_VALUE_GET_OBJ(this_val);
- if (!(p->class_id >= JS_CLASS_UINT8C_ARRAY &&
- p->class_id <= JS_CLASS_FLOAT64_ARRAY))
- return JS_UNDEFINED;
- return JS_AtomToString(ctx, ctx->rt->class_array[p->class_id].class_name);
- }
- static JSValue js_typed_array_set_internal(JSContext *ctx,
- JSValueConst dst,
- JSValueConst src,
- JSValueConst off)
- {
- JSObject *p;
- JSObject *src_p;
- uint32_t i;
- int64_t dst_len, src_len, offset;
- JSValue val, src_obj = JS_UNDEFINED;
- p = get_typed_array(ctx, dst);
- if (!p)
- goto fail;
- if (JS_ToInt64Sat(ctx, &offset, off))
- goto fail;
- if (offset < 0)
- goto range_error;
- if (typed_array_is_oob(p)) {
- detached:
- JS_ThrowTypeErrorArrayBufferOOB(ctx);
- goto fail;
- }
- dst_len = p->u.array.count;
- src_obj = JS_ToObject(ctx, src);
- if (JS_IsException(src_obj))
- goto fail;
- src_p = JS_VALUE_GET_OBJ(src_obj);
- if (src_p->class_id >= JS_CLASS_UINT8C_ARRAY &&
- src_p->class_id <= JS_CLASS_FLOAT64_ARRAY) {
- JSTypedArray *dest_ta = p->u.typed_array;
- JSArrayBuffer *dest_abuf = dest_ta->buffer->u.array_buffer;
- JSTypedArray *src_ta = src_p->u.typed_array;
- JSArrayBuffer *src_abuf = src_ta->buffer->u.array_buffer;
- int shift = typed_array_size_log2(p->class_id);
- if (typed_array_is_oob(src_p))
- goto detached;
- src_len = src_p->u.array.count;
- if (offset > dst_len - src_len)
- goto range_error;
- /* copying between typed objects */
- if (src_p->class_id == p->class_id) {
- /* same type, use memmove */
- memmove(dest_abuf->data + dest_ta->offset + (offset << shift),
- src_abuf->data + src_ta->offset, src_len << shift);
- goto done;
- }
- if (dest_abuf->data == src_abuf->data) {
- /* copying between the same buffer using different types of mappings
- would require a temporary buffer */
- }
- /* otherwise, default behavior is slow but correct */
- } else {
- // can change |dst| as a side effect; per spec,
- // perform the range check against its old length
- if (js_get_length64(ctx, &src_len, src_obj))
- goto fail;
- if (offset > dst_len - src_len) {
- range_error:
- JS_ThrowRangeError(ctx, "invalid array length");
- goto fail;
- }
- }
- for(i = 0; i < src_len; i++) {
- val = JS_GetPropertyUint32(ctx, src_obj, i);
- if (JS_IsException(val))
- goto fail;
- if (JS_SetPropertyUint32(ctx, dst, offset + i, val) < 0)
- goto fail;
- }
- done:
- JS_FreeValue(ctx, src_obj);
- return JS_UNDEFINED;
- fail:
- JS_FreeValue(ctx, src_obj);
- return JS_EXCEPTION;
- }
- static JSValue js_typed_array_at(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSObject *p;
- int64_t idx, len;
- p = get_typed_array(ctx, this_val);
- if (!p)
- return JS_EXCEPTION;
- if (typed_array_is_oob(p))
- return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
- len = p->u.array.count;
- // note: can change p->u.array.count
- if (JS_ToInt64Sat(ctx, &idx, argv[0]))
- return JS_EXCEPTION;
- if (idx < 0)
- idx = len + idx;
- len = p->u.array.count;
- if (idx < 0 || idx >= len)
- return JS_UNDEFINED;
- return JS_GetPropertyInt64(ctx, this_val, idx);
- }
- static JSValue js_typed_array_with(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue arr, val;
- JSObject *p;
- int64_t idx, len;
- p = get_typed_array(ctx, this_val);
- if (!p)
- return JS_EXCEPTION;
- if (typed_array_is_oob(p))
- return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
- len = p->u.array.count;
- if (JS_ToInt64Sat(ctx, &idx, argv[0]))
- return JS_EXCEPTION;
- if (idx < 0)
- idx = len + idx;
- val = JS_ToPrimitive(ctx, argv[1], HINT_NUMBER);
- if (JS_IsException(val))
- return JS_EXCEPTION;
- if (typed_array_is_oob(p) || idx < 0 || idx >= p->u.array.count)
- return JS_ThrowRangeError(ctx, "invalid array index");
- /* warning: 'this_val' may have been resized, so 'len' may be
- larger than its length */
- arr = js_typed_array_constructor_ta(ctx, JS_UNDEFINED, this_val,
- p->class_id, len);
- if (JS_IsException(arr)) {
- JS_FreeValue(ctx, val);
- return JS_EXCEPTION;
- }
- if (JS_SetPropertyInt64(ctx, arr, idx, val) < 0) {
- JS_FreeValue(ctx, arr);
- return JS_EXCEPTION;
- }
- return arr;
- }
- static JSValue js_typed_array_set(JSContext *ctx,
- JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValueConst offset = JS_UNDEFINED;
- if (argc > 1) {
- offset = argv[1];
- }
- return js_typed_array_set_internal(ctx, this_val, argv[0], offset);
- }
- static JSValue js_create_typed_array_iterator(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int magic)
- {
- if (validate_typed_array(ctx, this_val))
- return JS_EXCEPTION;
- return js_create_array_iterator(ctx, this_val, argc, argv, magic);
- }
- static JSValue js_typed_array_create(JSContext *ctx, JSValueConst ctor,
- int argc, JSValueConst *argv)
- {
- JSValue ret;
- int new_len;
- int64_t len;
- ret = JS_CallConstructor(ctx, ctor, argc, argv);
- if (JS_IsException(ret))
- return ret;
- /* validate the typed array */
- new_len = js_typed_array_get_length_unsafe(ctx, ret);
- if (new_len < 0)
- goto fail;
- if (argc == 1) {
- /* ensure that it is large enough */
- if (JS_ToLengthFree(ctx, &len, JS_DupValue(ctx, argv[0])))
- goto fail;
- if (new_len < len) {
- JS_ThrowTypeError(ctx, "TypedArray length is too small");
- fail:
- JS_FreeValue(ctx, ret);
- return JS_EXCEPTION;
- }
- }
- return ret;
- }
- #if 0
- static JSValue js_typed_array___create(JSContext *ctx,
- JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- return js_typed_array_create(ctx, argv[0], max_int(argc - 1, 0), argv + 1);
- }
- #endif
- static JSValue js_typed_array___speciesCreate(JSContext *ctx,
- JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValueConst obj;
- JSObject *p;
- JSValue ctor, ret;
- int argc1;
- obj = argv[0];
- p = get_typed_array(ctx, obj);
- if (!p)
- return JS_EXCEPTION;
- ctor = JS_SpeciesConstructor(ctx, obj, JS_UNDEFINED);
- if (JS_IsException(ctor))
- return ctor;
- argc1 = max_int(argc - 1, 0);
- if (JS_IsUndefined(ctor)) {
- ret = js_typed_array_constructor(ctx, JS_UNDEFINED, argc1, argv + 1,
- p->class_id);
- } else {
- ret = js_typed_array_create(ctx, ctor, argc1, argv + 1);
- JS_FreeValue(ctx, ctor);
- }
- return ret;
- }
- static JSValue js_typed_array_from(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- // from(items, mapfn = void 0, this_arg = void 0)
- JSValueConst items = argv[0], mapfn, this_arg;
- JSValueConst args[2];
- JSValue iter, arr, r, v, v2;
- int64_t k, len;
- int mapping;
- mapping = FALSE;
- mapfn = JS_UNDEFINED;
- this_arg = JS_UNDEFINED;
- r = JS_UNDEFINED;
- arr = JS_UNDEFINED;
- iter = JS_UNDEFINED;
- if (argc > 1) {
- mapfn = argv[1];
- if (!JS_IsUndefined(mapfn)) {
- if (check_function(ctx, mapfn))
- goto exception;
- mapping = 1;
- if (argc > 2)
- this_arg = argv[2];
- }
- }
- iter = JS_GetProperty(ctx, items, JS_ATOM_Symbol_iterator);
- if (JS_IsException(iter))
- goto exception;
- if (!JS_IsUndefined(iter) && !JS_IsNull(iter)) {
- uint32_t len1;
- if (!JS_IsFunction(ctx, iter)) {
- JS_ThrowTypeError(ctx, "value is not iterable");
- goto exception;
- }
- arr = js_array_from_iterator(ctx, &len1, items, iter);
- if (JS_IsException(arr))
- goto exception;
- len = len1;
- } else {
- arr = JS_ToObject(ctx, items);
- if (JS_IsException(arr))
- goto exception;
- if (js_get_length64(ctx, &len, arr) < 0)
- goto exception;
- }
- v = JS_NewInt64(ctx, len);
- args[0] = v;
- r = js_typed_array_create(ctx, this_val, 1, args);
- JS_FreeValue(ctx, v);
- if (JS_IsException(r))
- goto exception;
- for(k = 0; k < len; k++) {
- v = JS_GetPropertyInt64(ctx, arr, k);
- if (JS_IsException(v))
- goto exception;
- if (mapping) {
- args[0] = v;
- args[1] = JS_NewInt32(ctx, k);
- v2 = JS_Call(ctx, mapfn, this_arg, 2, args);
- JS_FreeValue(ctx, v);
- v = v2;
- if (JS_IsException(v))
- goto exception;
- }
- if (JS_SetPropertyInt64(ctx, r, k, v) < 0)
- goto exception;
- }
- goto done;
- exception:
- JS_FreeValue(ctx, r);
- r = JS_EXCEPTION;
- done:
- JS_FreeValue(ctx, arr);
- JS_FreeValue(ctx, iter);
- return r;
- }
- static JSValue js_typed_array_of(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue obj;
- JSValueConst args[1];
- int i;
- args[0] = JS_NewInt32(ctx, argc);
- obj = js_typed_array_create(ctx, this_val, 1, args);
- if (JS_IsException(obj))
- return obj;
- for(i = 0; i < argc; i++) {
- if (JS_SetPropertyUint32(ctx, obj, i, JS_DupValue(ctx, argv[i])) < 0) {
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- }
- return obj;
- }
- static JSValue js_typed_array_copyWithin(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSObject *p;
- int len, to, from, final, count, shift, space;
- p = get_typed_array(ctx, this_val);
- if (!p)
- return JS_EXCEPTION;
- if (typed_array_is_oob(p))
- return JS_ThrowTypeErrorArrayBufferOOB(ctx);
- len = p->u.array.count;
- if (JS_ToInt32Clamp(ctx, &to, argv[0], 0, len, len))
- return JS_EXCEPTION;
- if (JS_ToInt32Clamp(ctx, &from, argv[1], 0, len, len))
- return JS_EXCEPTION;
- final = len;
- if (argc > 2 && !JS_IsUndefined(argv[2])) {
- if (JS_ToInt32Clamp(ctx, &final, argv[2], 0, len, len))
- return JS_EXCEPTION;
- }
- if (typed_array_is_oob(p))
- return JS_ThrowTypeErrorArrayBufferOOB(ctx);
- // RAB may have been resized by evil .valueOf method
- space = p->u.array.count - max_int(to, from);
- count = min_int(final - from, len - to);
- count = min_int(count, space);
- if (count > 0) {
- shift = typed_array_size_log2(p->class_id);
- memmove(p->u.array.u.uint8_ptr + (to << shift),
- p->u.array.u.uint8_ptr + (from << shift),
- count << shift);
- }
- return JS_DupValue(ctx, this_val);
- }
- static JSValue js_typed_array_fill(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSObject *p;
- int len, k, final, shift;
- uint64_t v64;
- p = get_typed_array(ctx, this_val);
- if (!p)
- return JS_EXCEPTION;
- if (typed_array_is_oob(p))
- return JS_ThrowTypeErrorArrayBufferOOB(ctx);
- len = p->u.array.count;
- if (p->class_id == JS_CLASS_UINT8C_ARRAY) {
- int32_t v;
- if (JS_ToUint8ClampFree(ctx, &v, JS_DupValue(ctx, argv[0])))
- return JS_EXCEPTION;
- v64 = v;
- } else if (p->class_id <= JS_CLASS_UINT32_ARRAY) {
- uint32_t v;
- if (JS_ToUint32(ctx, &v, argv[0]))
- return JS_EXCEPTION;
- v64 = v;
- } else if (p->class_id <= JS_CLASS_BIG_UINT64_ARRAY) {
- if (JS_ToBigInt64(ctx, (int64_t *)&v64, argv[0]))
- return JS_EXCEPTION;
- } else {
- double d;
- if (JS_ToFloat64(ctx, &d, argv[0]))
- return JS_EXCEPTION;
- if (p->class_id == JS_CLASS_FLOAT16_ARRAY) {
- v64 = tofp16(d);
- } else if (p->class_id == JS_CLASS_FLOAT32_ARRAY) {
- union {
- float f;
- uint32_t u32;
- } u;
- u.f = d;
- v64 = u.u32;
- } else {
- JSFloat64Union u;
- u.d = d;
- v64 = u.u64;
- }
- }
- k = 0;
- if (argc > 1) {
- if (JS_ToInt32Clamp(ctx, &k, argv[1], 0, len, len))
- return JS_EXCEPTION;
- }
- final = len;
- if (argc > 2 && !JS_IsUndefined(argv[2])) {
- if (JS_ToInt32Clamp(ctx, &final, argv[2], 0, len, len))
- return JS_EXCEPTION;
- }
- if (typed_array_is_oob(p))
- return JS_ThrowTypeErrorArrayBufferOOB(ctx);
- // RAB may have been resized by evil .valueOf method
- final = min_int(final, p->u.array.count);
- shift = typed_array_size_log2(p->class_id);
- switch(shift) {
- case 0:
- if (k < final) {
- memset(p->u.array.u.uint8_ptr + k, v64, final - k);
- }
- break;
- case 1:
- for(; k < final; k++) {
- p->u.array.u.uint16_ptr[k] = v64;
- }
- break;
- case 2:
- for(; k < final; k++) {
- p->u.array.u.uint32_ptr[k] = v64;
- }
- break;
- case 3:
- for(; k < final; k++) {
- p->u.array.u.uint64_ptr[k] = v64;
- }
- break;
- default:
- abort();
- }
- return JS_DupValue(ctx, this_val);
- }
- static JSValue js_typed_array_find(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int mode)
- {
- JSValueConst func, this_arg;
- JSValueConst args[3];
- JSValue val, index_val, res;
- int len, k, end;
- int dir;
- val = JS_UNDEFINED;
- len = js_typed_array_get_length_unsafe(ctx, this_val);
- if (len < 0)
- goto exception;
- func = argv[0];
- if (check_function(ctx, func))
- goto exception;
- this_arg = JS_UNDEFINED;
- if (argc > 1)
- this_arg = argv[1];
- k = 0;
- dir = 1;
- end = len;
- if (mode == ArrayFindLast || mode == ArrayFindLastIndex) {
- k = len - 1;
- dir = -1;
- end = -1;
- }
- for(; k != end; k += dir) {
- index_val = JS_NewInt32(ctx, k);
- val = JS_GetPropertyValue(ctx, this_val, index_val);
- if (JS_IsException(val))
- goto exception;
- args[0] = val;
- args[1] = index_val;
- args[2] = this_val;
- res = JS_Call(ctx, func, this_arg, 3, args);
- if (JS_IsException(res))
- goto exception;
- if (JS_ToBoolFree(ctx, res)) {
- if (mode == ArrayFindIndex || mode == ArrayFindLastIndex) {
- JS_FreeValue(ctx, val);
- return index_val;
- } else {
- return val;
- }
- }
- JS_FreeValue(ctx, val);
- }
- if (mode == ArrayFindIndex || mode == ArrayFindLastIndex)
- return JS_NewInt32(ctx, -1);
- else
- return JS_UNDEFINED;
- exception:
- JS_FreeValue(ctx, val);
- return JS_EXCEPTION;
- }
- #define special_indexOf 0
- #define special_lastIndexOf 1
- #define special_includes -1
- static JSValue js_typed_array_indexOf(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int special)
- {
- JSObject *p;
- int len, tag, is_int, is_bigint, k, stop, inc, res = -1;
- int64_t v64;
- double d;
- float f;
- uint16_t hf;
- p = get_typed_array(ctx, this_val);
- if (!p)
- return JS_EXCEPTION;
- if (typed_array_is_oob(p))
- return JS_ThrowTypeErrorArrayBufferOOB(ctx);
- len = p->u.array.count;
- if (len == 0)
- goto done;
- if (special == special_lastIndexOf) {
- k = len - 1;
- if (argc > 1) {
- int64_t k1;
- if (JS_ToInt64Clamp(ctx, &k1, argv[1], -1, len - 1, len))
- goto exception;
- k = k1;
- if (k < 0)
- goto done;
- }
- stop = -1;
- inc = -1;
- } else {
- k = 0;
- if (argc > 1) {
- if (JS_ToInt32Clamp(ctx, &k, argv[1], 0, len, len))
- goto exception;
- }
- stop = len;
- inc = 1;
- }
- /* includes function: 'undefined' can be found if searching out of bounds */
- if (len > p->u.array.count && special == special_includes &&
- JS_IsUndefined(argv[0]) && k < len) {
- res = 0;
- goto done;
- }
- // RAB may have been resized by evil .valueOf method
- len = min_int(len, p->u.array.count);
- if (len == 0)
- goto done;
- if (special == special_lastIndexOf)
- k = min_int(k, len - 1);
- else
- k = min_int(k, len);
- stop = min_int(stop, len);
- is_bigint = 0;
- is_int = 0; /* avoid warning */
- v64 = 0; /* avoid warning */
- tag = JS_VALUE_GET_NORM_TAG(argv[0]);
- if (tag == JS_TAG_INT) {
- is_int = 1;
- v64 = JS_VALUE_GET_INT(argv[0]);
- d = v64;
- } else
- if (tag == JS_TAG_FLOAT64) {
- d = JS_VALUE_GET_FLOAT64(argv[0]);
- if (d >= INT64_MIN && d < 0x1p63) {
- v64 = d;
- is_int = (v64 == d);
- }
- } else if (tag == JS_TAG_BIG_INT || tag == JS_TAG_SHORT_BIG_INT) {
- JSBigIntBuf buf1;
- JSBigInt *p1;
- int sz = (64 / JS_LIMB_BITS);
- if (tag == JS_TAG_SHORT_BIG_INT)
- p1 = js_bigint_set_short(&buf1, argv[0]);
- else
- p1 = JS_VALUE_GET_PTR(argv[0]);
-
- if (p->class_id == JS_CLASS_BIG_INT64_ARRAY) {
- if (p1->len > sz)
- goto done; /* does not fit an int64 : cannot be found */
- } else if (p->class_id == JS_CLASS_BIG_UINT64_ARRAY) {
- if (js_bigint_sign(p1))
- goto done; /* v < 0 */
- if (p1->len <= sz) {
- /* OK */
- } else if (p1->len == sz + 1 && p1->tab[sz] == 0) {
- /* 2^63 <= v <= 2^64-1 */
- } else {
- goto done;
- }
- } else {
- goto done;
- }
- if (JS_ToBigInt64(ctx, &v64, argv[0]))
- goto exception;
- d = 0;
- is_bigint = 1;
- } else {
- goto done;
- }
- switch (p->class_id) {
- case JS_CLASS_INT8_ARRAY:
- if (is_int && (int8_t)v64 == v64)
- goto scan8;
- break;
- case JS_CLASS_UINT8C_ARRAY:
- case JS_CLASS_UINT8_ARRAY:
- if (is_int && (uint8_t)v64 == v64) {
- const uint8_t *pv, *pp;
- uint16_t v;
- scan8:
- pv = p->u.array.u.uint8_ptr;
- v = v64;
- if (inc > 0) {
- pp = NULL;
- if (pv)
- pp = memchr(pv + k, v, len - k);
- if (pp)
- res = pp - pv;
- } else {
- for (; k != stop; k += inc) {
- if (pv[k] == v) {
- res = k;
- break;
- }
- }
- }
- }
- break;
- case JS_CLASS_INT16_ARRAY:
- if (is_int && (int16_t)v64 == v64)
- goto scan16;
- break;
- case JS_CLASS_UINT16_ARRAY:
- if (is_int && (uint16_t)v64 == v64) {
- const uint16_t *pv;
- uint16_t v;
- scan16:
- pv = p->u.array.u.uint16_ptr;
- v = v64;
- for (; k != stop; k += inc) {
- if (pv[k] == v) {
- res = k;
- break;
- }
- }
- }
- break;
- case JS_CLASS_INT32_ARRAY:
- if (is_int && (int32_t)v64 == v64)
- goto scan32;
- break;
- case JS_CLASS_UINT32_ARRAY:
- if (is_int && (uint32_t)v64 == v64) {
- const uint32_t *pv;
- uint32_t v;
- scan32:
- pv = p->u.array.u.uint32_ptr;
- v = v64;
- for (; k != stop; k += inc) {
- if (pv[k] == v) {
- res = k;
- break;
- }
- }
- }
- break;
- case JS_CLASS_FLOAT16_ARRAY:
- if (is_bigint)
- break;
- if (isnan(d)) {
- const uint16_t *pv = p->u.array.u.fp16_ptr;
- /* special case: indexOf returns -1, includes finds NaN */
- if (special != special_includes)
- goto done;
- for (; k != stop; k += inc) {
- if (isfp16nan(pv[k])) {
- res = k;
- break;
- }
- }
- } else if (d == 0) {
- // special case: includes also finds negative zero
- const uint16_t *pv = p->u.array.u.fp16_ptr;
- for (; k != stop; k += inc) {
- if (isfp16zero(pv[k])) {
- res = k;
- break;
- }
- }
- } else if (hf = tofp16(d), d == fromfp16(hf)) {
- const uint16_t *pv = p->u.array.u.fp16_ptr;
- for (; k != stop; k += inc) {
- if (pv[k] == hf) {
- res = k;
- break;
- }
- }
- }
- break;
- case JS_CLASS_FLOAT32_ARRAY:
- if (is_bigint)
- break;
- if (isnan(d)) {
- const float *pv = p->u.array.u.float_ptr;
- /* special case: indexOf returns -1, includes finds NaN */
- if (special != special_includes)
- goto done;
- for (; k != stop; k += inc) {
- if (isnan(pv[k])) {
- res = k;
- break;
- }
- }
- } else if ((f = (float)d) == d) {
- const float *pv = p->u.array.u.float_ptr;
- for (; k != stop; k += inc) {
- if (pv[k] == f) {
- res = k;
- break;
- }
- }
- }
- break;
- case JS_CLASS_FLOAT64_ARRAY:
- if (is_bigint)
- break;
- if (isnan(d)) {
- const double *pv = p->u.array.u.double_ptr;
- /* special case: indexOf returns -1, includes finds NaN */
- if (special != special_includes)
- goto done;
- for (; k != stop; k += inc) {
- if (isnan(pv[k])) {
- res = k;
- break;
- }
- }
- } else {
- const double *pv = p->u.array.u.double_ptr;
- for (; k != stop; k += inc) {
- if (pv[k] == d) {
- res = k;
- break;
- }
- }
- }
- break;
- case JS_CLASS_BIG_INT64_ARRAY:
- if (is_bigint) {
- goto scan64;
- }
- break;
- case JS_CLASS_BIG_UINT64_ARRAY:
- if (is_bigint) {
- const uint64_t *pv;
- uint64_t v;
- scan64:
- pv = p->u.array.u.uint64_ptr;
- v = v64;
- for (; k != stop; k += inc) {
- if (pv[k] == v) {
- res = k;
- break;
- }
- }
- }
- break;
- }
- done:
- if (special == special_includes)
- return JS_NewBool(ctx, res >= 0);
- else
- return JS_NewInt32(ctx, res);
- exception:
- return JS_EXCEPTION;
- }
- static JSValue js_typed_array_join(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv, int toLocaleString)
- {
- JSValue sep = JS_UNDEFINED, el;
- StringBuffer b_s, *b = &b_s;
- JSString *s = NULL;
- JSObject *p;
- int i, len, oldlen, newlen;
- int c;
- p = get_typed_array(ctx, this_val);
- if (!p)
- return JS_EXCEPTION;
- if (typed_array_is_oob(p))
- return JS_ThrowTypeErrorArrayBufferOOB(ctx);
- len = oldlen = newlen = p->u.array.count;
- c = ','; /* default separator */
- if (!toLocaleString && argc > 0 && !JS_IsUndefined(argv[0])) {
- sep = JS_ToString(ctx, argv[0]);
- if (JS_IsException(sep))
- goto exception;
- s = JS_VALUE_GET_STRING(sep);
- if (s->len == 1 && !s->is_wide_char)
- c = s->u.str8[0];
- else
- c = -1;
- // ToString(sep) can detach or resize the arraybuffer as a side effect
- newlen = p->u.array.count;
- len = min_int(len, newlen);
- }
- string_buffer_init(ctx, b, 0);
- /* XXX: optimize with direct access */
- for(i = 0; i < len; i++) {
- if (i > 0) {
- if (c >= 0) {
- if (string_buffer_putc8(b, c))
- goto fail;
- } else {
- if (string_buffer_concat(b, s, 0, s->len))
- goto fail;
- }
- }
- el = JS_GetPropertyUint32(ctx, this_val, i);
- /* Can return undefined for example if the typed array is detached */
- if (!JS_IsNull(el) && !JS_IsUndefined(el)) {
- if (JS_IsException(el))
- goto fail;
- if (toLocaleString) {
- el = JS_ToLocaleStringFree(ctx, el);
- }
- if (string_buffer_concat_value_free(b, el))
- goto fail;
- }
- }
- // add extra separators in case RAB was resized by evil .valueOf method
- i = max_int(1, newlen);
- for(/*empty*/; i < oldlen; i++) {
- if (c >= 0) {
- if (string_buffer_putc8(b, c))
- goto fail;
- } else {
- if (string_buffer_concat(b, s, 0, s->len))
- goto fail;
- }
- }
- JS_FreeValue(ctx, sep);
- return string_buffer_end(b);
- fail:
- string_buffer_free(b);
- JS_FreeValue(ctx, sep);
- exception:
- return JS_EXCEPTION;
- }
- static JSValue js_typed_array_reverse(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSObject *p;
- int len;
- len = js_typed_array_get_length_unsafe(ctx, this_val);
- if (len < 0)
- return JS_EXCEPTION;
- if (len > 0) {
- p = JS_VALUE_GET_OBJ(this_val);
- switch (typed_array_size_log2(p->class_id)) {
- case 0:
- {
- uint8_t *p1 = p->u.array.u.uint8_ptr;
- uint8_t *p2 = p1 + len - 1;
- while (p1 < p2) {
- uint8_t v = *p1;
- *p1++ = *p2;
- *p2-- = v;
- }
- }
- break;
- case 1:
- {
- uint16_t *p1 = p->u.array.u.uint16_ptr;
- uint16_t *p2 = p1 + len - 1;
- while (p1 < p2) {
- uint16_t v = *p1;
- *p1++ = *p2;
- *p2-- = v;
- }
- }
- break;
- case 2:
- {
- uint32_t *p1 = p->u.array.u.uint32_ptr;
- uint32_t *p2 = p1 + len - 1;
- while (p1 < p2) {
- uint32_t v = *p1;
- *p1++ = *p2;
- *p2-- = v;
- }
- }
- break;
- case 3:
- {
- uint64_t *p1 = p->u.array.u.uint64_ptr;
- uint64_t *p2 = p1 + len - 1;
- while (p1 < p2) {
- uint64_t v = *p1;
- *p1++ = *p2;
- *p2-- = v;
- }
- }
- break;
- default:
- abort();
- }
- }
- return JS_DupValue(ctx, this_val);
- }
- static JSValue js_typed_array_toReversed(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue arr, ret;
- JSObject *p;
- p = get_typed_array(ctx, this_val);
- if (!p)
- return JS_EXCEPTION;
- arr = js_typed_array_constructor_ta(ctx, JS_UNDEFINED, this_val,
- p->class_id, p->u.array.count);
- if (JS_IsException(arr))
- return JS_EXCEPTION;
- ret = js_typed_array_reverse(ctx, arr, argc, argv);
- JS_FreeValue(ctx, arr);
- return ret;
- }
- static void slice_memcpy(uint8_t *dst, const uint8_t *src, size_t len)
- {
- if (dst + len <= src || dst >= src + len) {
- /* no overlap: can use memcpy */
- memcpy(dst, src, len);
- } else {
- /* otherwise the spec mandates byte copy */
- while (len-- != 0)
- *dst++ = *src++;
- }
- }
- static JSValue js_typed_array_slice(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValueConst args[2];
- JSValue arr, val;
- JSObject *p, *p1;
- int n, len, start, final, count, shift, space;
- arr = JS_UNDEFINED;
- p = get_typed_array(ctx, this_val);
- if (!p)
- goto exception;
- if (typed_array_is_oob(p))
- return JS_ThrowTypeErrorArrayBufferOOB(ctx);
- len = p->u.array.count;
- if (JS_ToInt32Clamp(ctx, &start, argv[0], 0, len, len))
- goto exception;
- final = len;
- if (!JS_IsUndefined(argv[1])) {
- if (JS_ToInt32Clamp(ctx, &final, argv[1], 0, len, len))
- goto exception;
- }
- count = max_int(final - start, 0);
- shift = typed_array_size_log2(p->class_id);
- args[0] = this_val;
- args[1] = JS_NewInt32(ctx, count);
- arr = js_typed_array___speciesCreate(ctx, JS_UNDEFINED, 2, args);
- if (JS_IsException(arr))
- goto exception;
- if (count > 0) {
- if (validate_typed_array(ctx, this_val)
- || validate_typed_array(ctx, arr))
- goto exception;
- p1 = get_typed_array(ctx, arr);
- space = max_int(0, p->u.array.count - start);
- count = min_int(count, space);
- if (p1 != NULL && p->class_id == p1->class_id) {
- slice_memcpy(p1->u.array.u.uint8_ptr,
- p->u.array.u.uint8_ptr + (start << shift),
- count << shift);
- } else {
- for (n = 0; n < count; n++) {
- val = JS_GetPropertyValue(ctx, this_val, JS_NewInt32(ctx, start + n));
- if (JS_IsException(val))
- goto exception;
- if (JS_SetPropertyValue(ctx, arr, JS_NewInt32(ctx, n), val,
- JS_PROP_THROW) < 0)
- goto exception;
- }
- }
- }
- return arr;
- exception:
- JS_FreeValue(ctx, arr);
- return JS_EXCEPTION;
- }
- static JSValue js_typed_array_subarray(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValueConst args[4];
- JSValue arr, ta_buffer;
- JSTypedArray *ta;
- JSObject *p;
- int len, start, final, count, shift, offset;
- BOOL is_auto;
-
- p = get_typed_array(ctx, this_val);
- if (!p)
- goto exception;
- len = p->u.array.count;
- if (JS_ToInt32Clamp(ctx, &start, argv[0], 0, len, len))
- goto exception;
- shift = typed_array_size_log2(p->class_id);
- ta = p->u.typed_array;
- /* Read byteOffset (ta->offset) even if detached */
- offset = ta->offset + (start << shift);
- final = len;
- if (JS_IsUndefined(argv[1])) {
- is_auto = ta->track_rab;
- } else {
- is_auto = FALSE;
- if (JS_ToInt32Clamp(ctx, &final, argv[1], 0, len, len))
- goto exception;
- }
- count = max_int(final - start, 0);
- ta_buffer = js_typed_array_get_buffer(ctx, this_val);
- if (JS_IsException(ta_buffer))
- goto exception;
- args[0] = this_val;
- args[1] = ta_buffer;
- args[2] = JS_NewInt32(ctx, offset);
- args[3] = JS_NewInt32(ctx, count);
- arr = js_typed_array___speciesCreate(ctx, JS_UNDEFINED, is_auto ? 3 : 4, args);
- JS_FreeValue(ctx, ta_buffer);
- return arr;
- exception:
- return JS_EXCEPTION;
- }
- /* TypedArray.prototype.sort */
- static int js_cmp_doubles(double x, double y)
- {
- if (isnan(x)) return isnan(y) ? 0 : +1;
- if (isnan(y)) return -1;
- if (x < y) return -1;
- if (x > y) return 1;
- if (x != 0) return 0;
- if (signbit(x)) return signbit(y) ? 0 : -1;
- else return signbit(y) ? 1 : 0;
- }
- static int js_TA_cmp_int8(const void *a, const void *b, void *opaque) {
- return *(const int8_t *)a - *(const int8_t *)b;
- }
- static int js_TA_cmp_uint8(const void *a, const void *b, void *opaque) {
- return *(const uint8_t *)a - *(const uint8_t *)b;
- }
- static int js_TA_cmp_int16(const void *a, const void *b, void *opaque) {
- return *(const int16_t *)a - *(const int16_t *)b;
- }
- static int js_TA_cmp_uint16(const void *a, const void *b, void *opaque) {
- return *(const uint16_t *)a - *(const uint16_t *)b;
- }
- static int js_TA_cmp_int32(const void *a, const void *b, void *opaque) {
- int32_t x = *(const int32_t *)a;
- int32_t y = *(const int32_t *)b;
- return (y < x) - (y > x);
- }
- static int js_TA_cmp_uint32(const void *a, const void *b, void *opaque) {
- uint32_t x = *(const uint32_t *)a;
- uint32_t y = *(const uint32_t *)b;
- return (y < x) - (y > x);
- }
- static int js_TA_cmp_int64(const void *a, const void *b, void *opaque) {
- int64_t x = *(const int64_t *)a;
- int64_t y = *(const int64_t *)b;
- return (y < x) - (y > x);
- }
- static int js_TA_cmp_uint64(const void *a, const void *b, void *opaque) {
- uint64_t x = *(const uint64_t *)a;
- uint64_t y = *(const uint64_t *)b;
- return (y < x) - (y > x);
- }
- static int js_TA_cmp_float16(const void *a, const void *b, void *opaque) {
- return js_cmp_doubles(fromfp16(*(const uint16_t *)a),
- fromfp16(*(const uint16_t *)b));
- }
- static int js_TA_cmp_float32(const void *a, const void *b, void *opaque) {
- return js_cmp_doubles(*(const float *)a, *(const float *)b);
- }
- static int js_TA_cmp_float64(const void *a, const void *b, void *opaque) {
- return js_cmp_doubles(*(const double *)a, *(const double *)b);
- }
- static JSValue js_TA_get_int8(JSContext *ctx, const void *a) {
- return JS_NewInt32(ctx, *(const int8_t *)a);
- }
- static JSValue js_TA_get_uint8(JSContext *ctx, const void *a) {
- return JS_NewInt32(ctx, *(const uint8_t *)a);
- }
- static JSValue js_TA_get_int16(JSContext *ctx, const void *a) {
- return JS_NewInt32(ctx, *(const int16_t *)a);
- }
- static JSValue js_TA_get_uint16(JSContext *ctx, const void *a) {
- return JS_NewInt32(ctx, *(const uint16_t *)a);
- }
- static JSValue js_TA_get_int32(JSContext *ctx, const void *a) {
- return JS_NewInt32(ctx, *(const int32_t *)a);
- }
- static JSValue js_TA_get_uint32(JSContext *ctx, const void *a) {
- return JS_NewUint32(ctx, *(const uint32_t *)a);
- }
- static JSValue js_TA_get_int64(JSContext *ctx, const void *a) {
- return JS_NewBigInt64(ctx, *(int64_t *)a);
- }
- static JSValue js_TA_get_uint64(JSContext *ctx, const void *a) {
- return JS_NewBigUint64(ctx, *(uint64_t *)a);
- }
- static JSValue js_TA_get_float16(JSContext *ctx, const void *a) {
- return __JS_NewFloat64(ctx, fromfp16(*(const uint16_t *)a));
- }
- static JSValue js_TA_get_float32(JSContext *ctx, const void *a) {
- return __JS_NewFloat64(ctx, *(const float *)a);
- }
- static JSValue js_TA_get_float64(JSContext *ctx, const void *a) {
- return __JS_NewFloat64(ctx, *(const double *)a);
- }
- struct TA_sort_context {
- JSContext *ctx;
- int exception; /* 1 = exception, 2 = detached typed array */
- uint8_t *array;
- JSValueConst cmp;
- JSValue (*getfun)(JSContext *ctx, const void *a);
- int elt_size;
- };
- static int js_TA_cmp_generic(const void *a, const void *b, void *opaque) {
- struct TA_sort_context *psc = opaque;
- JSContext *ctx = psc->ctx;
- uint32_t a_idx, b_idx;
- JSValueConst argv[2];
- JSValue res;
- int cmp;
-
- cmp = 0;
- if (!psc->exception) {
- /* Note: the typed array can be detached without causing an
- error */
- a_idx = *(uint32_t *)a;
- b_idx = *(uint32_t *)b;
- argv[0] = psc->getfun(ctx, psc->array +
- a_idx * (size_t)psc->elt_size);
- argv[1] = psc->getfun(ctx, psc->array +
- b_idx * (size_t)(psc->elt_size));
- res = JS_Call(ctx, psc->cmp, JS_UNDEFINED, 2, argv);
- if (JS_IsException(res)) {
- psc->exception = 1;
- goto done;
- }
- if (JS_VALUE_GET_TAG(res) == JS_TAG_INT) {
- int val = JS_VALUE_GET_INT(res);
- cmp = (val > 0) - (val < 0);
- } else {
- double val;
- if (JS_ToFloat64Free(ctx, &val, res) < 0) {
- psc->exception = 1;
- goto done;
- } else {
- cmp = (val > 0) - (val < 0);
- }
- }
- if (cmp == 0) {
- /* make sort stable: compare array offsets */
- cmp = (a_idx > b_idx) - (a_idx < b_idx);
- }
- done:
- JS_FreeValue(ctx, (JSValue)argv[0]);
- JS_FreeValue(ctx, (JSValue)argv[1]);
- }
- return cmp;
- }
- static JSValue js_typed_array_sort(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSObject *p;
- int len;
- size_t elt_size;
- struct TA_sort_context tsc;
- int (*cmpfun)(const void *a, const void *b, void *opaque);
- tsc.ctx = ctx;
- tsc.exception = 0;
- tsc.cmp = argv[0];
- if (!JS_IsUndefined(tsc.cmp) && check_function(ctx, tsc.cmp))
- return JS_EXCEPTION;
- len = js_typed_array_get_length_unsafe(ctx, this_val);
- if (len < 0)
- return JS_EXCEPTION;
- if (len > 1) {
- p = JS_VALUE_GET_OBJ(this_val);
- switch (p->class_id) {
- case JS_CLASS_INT8_ARRAY:
- tsc.getfun = js_TA_get_int8;
- cmpfun = js_TA_cmp_int8;
- break;
- case JS_CLASS_UINT8C_ARRAY:
- case JS_CLASS_UINT8_ARRAY:
- tsc.getfun = js_TA_get_uint8;
- cmpfun = js_TA_cmp_uint8;
- break;
- case JS_CLASS_INT16_ARRAY:
- tsc.getfun = js_TA_get_int16;
- cmpfun = js_TA_cmp_int16;
- break;
- case JS_CLASS_UINT16_ARRAY:
- tsc.getfun = js_TA_get_uint16;
- cmpfun = js_TA_cmp_uint16;
- break;
- case JS_CLASS_INT32_ARRAY:
- tsc.getfun = js_TA_get_int32;
- cmpfun = js_TA_cmp_int32;
- break;
- case JS_CLASS_UINT32_ARRAY:
- tsc.getfun = js_TA_get_uint32;
- cmpfun = js_TA_cmp_uint32;
- break;
- case JS_CLASS_BIG_INT64_ARRAY:
- tsc.getfun = js_TA_get_int64;
- cmpfun = js_TA_cmp_int64;
- break;
- case JS_CLASS_BIG_UINT64_ARRAY:
- tsc.getfun = js_TA_get_uint64;
- cmpfun = js_TA_cmp_uint64;
- break;
- case JS_CLASS_FLOAT16_ARRAY:
- tsc.getfun = js_TA_get_float16;
- cmpfun = js_TA_cmp_float16;
- break;
- case JS_CLASS_FLOAT32_ARRAY:
- tsc.getfun = js_TA_get_float32;
- cmpfun = js_TA_cmp_float32;
- break;
- case JS_CLASS_FLOAT64_ARRAY:
- tsc.getfun = js_TA_get_float64;
- cmpfun = js_TA_cmp_float64;
- break;
- default:
- abort();
- }
- elt_size = 1 << typed_array_size_log2(p->class_id);
- if (!JS_IsUndefined(tsc.cmp)) {
- uint32_t *array_idx;
- void *array;
- size_t i, j;
- /* the array must be copied because the comparison
- function may modify it */
- array = js_malloc(ctx, len * elt_size);
- if (!array)
- return JS_EXCEPTION;
- memcpy(array, p->u.array.u.ptr, len * elt_size);
-
- /* array_idx is needed to have a stable sort */
- array_idx = js_malloc(ctx, len * sizeof(array_idx[0]));
- if (!array_idx) {
- js_free(ctx, array);
- return JS_EXCEPTION;
- }
- for(i = 0; i < len; i++)
- array_idx[i] = i;
- tsc.elt_size = elt_size;
- tsc.array = array;
- rqsort(array_idx, len, sizeof(array_idx[0]),
- js_TA_cmp_generic, &tsc);
- if (tsc.exception) {
- if (tsc.exception == 1) {
- js_free(ctx, array_idx);
- js_free(ctx, array);
- return JS_EXCEPTION;
- }
- /* detached typed array during the sort: no error */
- } else {
- void *array_ptr = p->u.array.u.ptr;
- len = min_int(len, p->u.array.count);
- switch(elt_size) {
- case 1:
- for(i = 0; i < len; i++) {
- j = array_idx[i];
- ((uint8_t *)array_ptr)[i] = ((uint8_t *)array)[j];
- }
- break;
- case 2:
- for(i = 0; i < len; i++) {
- j = array_idx[i];
- ((uint16_t *)array_ptr)[i] = ((uint16_t *)array)[j];
- }
- break;
- case 4:
- for(i = 0; i < len; i++) {
- j = array_idx[i];
- ((uint32_t *)array_ptr)[i] = ((uint32_t *)array)[j];
- }
- break;
- case 8:
- for(i = 0; i < len; i++) {
- j = array_idx[i];
- ((uint64_t *)array_ptr)[i] = ((uint64_t *)array)[j];
- }
- break;
- default:
- abort();
- }
- }
- js_free(ctx, array_idx);
- js_free(ctx, array);
- } else {
- rqsort(p->u.array.u.ptr, len, elt_size, cmpfun, &tsc);
- if (tsc.exception)
- return JS_EXCEPTION;
- }
- }
- return JS_DupValue(ctx, this_val);
- }
- static JSValue js_typed_array_toSorted(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValue arr, ret;
- JSObject *p;
- p = get_typed_array(ctx, this_val);
- if (!p)
- return JS_EXCEPTION;
- arr = js_typed_array_constructor_ta(ctx, JS_UNDEFINED, this_val,
- p->class_id, p->u.array.count);
- if (JS_IsException(arr))
- return JS_EXCEPTION;
- ret = js_typed_array_sort(ctx, arr, argc, argv);
- JS_FreeValue(ctx, arr);
- return ret;
- }
- /* Uint8Array base64/hex (tc39 proposal-arraybuffer-base64) */
- enum {
- B64_ALPHABET_BASE64 = 0,
- B64_ALPHABET_BASE64URL = 1,
- };
- enum {
- B64_LAST_LOOSE = 0,
- B64_LAST_STRICT = 1,
- B64_LAST_STOP_BEFORE_PARTIAL = 2,
- };
- static const unsigned char b64_enc[64] = {
- 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
- 'Q','R','S','T','U','V','W','X','Y','Z',
- 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p',
- 'q','r','s','t','u','v','w','x','y','z',
- '0','1','2','3','4','5','6','7','8','9',
- '+','/'
- };
- static const unsigned char b64url_enc[64] = {
- 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
- 'Q','R','S','T','U','V','W','X','Y','Z',
- 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p',
- 'q','r','s','t','u','v','w','x','y','z',
- '0','1','2','3','4','5','6','7','8','9',
- '-','_'
- };
- #define K_WS 64
- #define K_ER 65
- static const uint8_t b64_dec[256] = {
- [ 0]=K_ER, [ 1]=K_ER, [ 2]=K_ER, [ 3]=K_ER, [ 4]=K_ER, [ 5]=K_ER, [ 6]=K_ER, [ 7]=K_ER,
- [ 8]=K_ER, [ 9]=K_WS, [ 10]=K_WS, [ 11]=K_ER, [ 12]=K_WS, [ 13]=K_WS, [ 14]=K_ER, [ 15]=K_ER,
- [ 16]=K_ER, [ 17]=K_ER, [ 18]=K_ER, [ 19]=K_ER, [ 20]=K_ER, [ 21]=K_ER, [ 22]=K_ER, [ 23]=K_ER,
- [ 24]=K_ER, [ 25]=K_ER, [ 26]=K_ER, [ 27]=K_ER, [ 28]=K_ER, [ 29]=K_ER, [ 30]=K_ER, [ 31]=K_ER,
- [' ']=K_WS, ['!']=K_ER, ['"']=K_ER, ['#']=K_ER, ['$']=K_ER, ['%']=K_ER, ['&']=K_ER, [ 39]=K_ER,
- ['(']=K_ER, [')']=K_ER, ['*']=K_ER, ['+']= 62, [',']=K_ER, ['-']=K_ER, ['.']=K_ER, ['/']= 63,
- ['0']= 52, ['1']= 53, ['2']= 54, ['3']= 55, ['4']= 56, ['5']= 57, ['6']= 58, ['7']= 59,
- ['8']= 60, ['9']= 61, [':']=K_ER, [';']=K_ER, ['<']=K_ER, ['=']=K_ER, ['>']=K_ER, ['?']=K_ER,
- ['@']=K_ER, ['A']= 0, ['B']= 1, ['C']= 2, ['D']= 3, ['E']= 4, ['F']= 5, ['G']= 6,
- ['H']= 7, ['I']= 8, ['J']= 9, ['K']= 10, ['L']= 11, ['M']= 12, ['N']= 13, ['O']= 14,
- ['P']= 15, ['Q']= 16, ['R']= 17, ['S']= 18, ['T']= 19, ['U']= 20, ['V']= 21, ['W']= 22,
- ['X']= 23, ['Y']= 24, ['Z']= 25, ['[']=K_ER, [ 92]=K_ER, [']']=K_ER, ['^']=K_ER, ['_']=K_ER,
- ['`']=K_ER, ['a']= 26, ['b']= 27, ['c']= 28, ['d']= 29, ['e']= 30, ['f']= 31, ['g']= 32,
- ['h']= 33, ['i']= 34, ['j']= 35, ['k']= 36, ['l']= 37, ['m']= 38, ['n']= 39, ['o']= 40,
- ['p']= 41, ['q']= 42, ['r']= 43, ['s']= 44, ['t']= 45, ['u']= 46, ['v']= 47, ['w']= 48,
- ['x']= 49, ['y']= 50, ['z']= 51, ['{']=K_ER, ['|']=K_ER, ['}']=K_ER, ['~']=K_ER, [127]=K_ER,
- [128]=K_ER, [129]=K_ER, [130]=K_ER, [131]=K_ER, [132]=K_ER, [133]=K_ER, [134]=K_ER, [135]=K_ER,
- [136]=K_ER, [137]=K_ER, [138]=K_ER, [139]=K_ER, [140]=K_ER, [141]=K_ER, [142]=K_ER, [143]=K_ER,
- [144]=K_ER, [145]=K_ER, [146]=K_ER, [147]=K_ER, [148]=K_ER, [149]=K_ER, [150]=K_ER, [151]=K_ER,
- [152]=K_ER, [153]=K_ER, [154]=K_ER, [155]=K_ER, [156]=K_ER, [157]=K_ER, [158]=K_ER, [159]=K_ER,
- [160]=K_ER, [161]=K_ER, [162]=K_ER, [163]=K_ER, [164]=K_ER, [165]=K_ER, [166]=K_ER, [167]=K_ER,
- [168]=K_ER, [169]=K_ER, [170]=K_ER, [171]=K_ER, [172]=K_ER, [173]=K_ER, [174]=K_ER, [175]=K_ER,
- [176]=K_ER, [177]=K_ER, [178]=K_ER, [179]=K_ER, [180]=K_ER, [181]=K_ER, [182]=K_ER, [183]=K_ER,
- [184]=K_ER, [185]=K_ER, [186]=K_ER, [187]=K_ER, [188]=K_ER, [189]=K_ER, [190]=K_ER, [191]=K_ER,
- [192]=K_ER, [193]=K_ER, [194]=K_ER, [195]=K_ER, [196]=K_ER, [197]=K_ER, [198]=K_ER, [199]=K_ER,
- [200]=K_ER, [201]=K_ER, [202]=K_ER, [203]=K_ER, [204]=K_ER, [205]=K_ER, [206]=K_ER, [207]=K_ER,
- [208]=K_ER, [209]=K_ER, [210]=K_ER, [211]=K_ER, [212]=K_ER, [213]=K_ER, [214]=K_ER, [215]=K_ER,
- [216]=K_ER, [217]=K_ER, [218]=K_ER, [219]=K_ER, [220]=K_ER, [221]=K_ER, [222]=K_ER, [223]=K_ER,
- [224]=K_ER, [225]=K_ER, [226]=K_ER, [227]=K_ER, [228]=K_ER, [229]=K_ER, [230]=K_ER, [231]=K_ER,
- [232]=K_ER, [233]=K_ER, [234]=K_ER, [235]=K_ER, [236]=K_ER, [237]=K_ER, [238]=K_ER, [239]=K_ER,
- [240]=K_ER, [241]=K_ER, [242]=K_ER, [243]=K_ER, [244]=K_ER, [245]=K_ER, [246]=K_ER, [247]=K_ER,
- [248]=K_ER, [249]=K_ER, [250]=K_ER, [251]=K_ER, [252]=K_ER, [253]=K_ER, [254]=K_ER, [255]=K_ER,
- };
- static const uint8_t b64url_dec[256] = {
- [ 0]=K_ER, [ 1]=K_ER, [ 2]=K_ER, [ 3]=K_ER, [ 4]=K_ER, [ 5]=K_ER, [ 6]=K_ER, [ 7]=K_ER,
- [ 8]=K_ER, [ 9]=K_WS, [ 10]=K_WS, [ 11]=K_ER, [ 12]=K_WS, [ 13]=K_WS, [ 14]=K_ER, [ 15]=K_ER,
- [ 16]=K_ER, [ 17]=K_ER, [ 18]=K_ER, [ 19]=K_ER, [ 20]=K_ER, [ 21]=K_ER, [ 22]=K_ER, [ 23]=K_ER,
- [ 24]=K_ER, [ 25]=K_ER, [ 26]=K_ER, [ 27]=K_ER, [ 28]=K_ER, [ 29]=K_ER, [ 30]=K_ER, [ 31]=K_ER,
- [' ']=K_WS, ['!']=K_ER, ['"']=K_ER, ['#']=K_ER, ['$']=K_ER, ['%']=K_ER, ['&']=K_ER, [ 39]=K_ER,
- ['(']=K_ER, [')']=K_ER, ['*']=K_ER, ['+']=K_ER, [',']=K_ER, ['-']= 62, ['.']=K_ER, ['/']=K_ER,
- ['0']= 52, ['1']= 53, ['2']= 54, ['3']= 55, ['4']= 56, ['5']= 57, ['6']= 58, ['7']= 59,
- ['8']= 60, ['9']= 61, [':']=K_ER, [';']=K_ER, ['<']=K_ER, ['=']=K_ER, ['>']=K_ER, ['?']=K_ER,
- ['@']=K_ER, ['A']= 0, ['B']= 1, ['C']= 2, ['D']= 3, ['E']= 4, ['F']= 5, ['G']= 6,
- ['H']= 7, ['I']= 8, ['J']= 9, ['K']= 10, ['L']= 11, ['M']= 12, ['N']= 13, ['O']= 14,
- ['P']= 15, ['Q']= 16, ['R']= 17, ['S']= 18, ['T']= 19, ['U']= 20, ['V']= 21, ['W']= 22,
- ['X']= 23, ['Y']= 24, ['Z']= 25, ['[']=K_ER, [ 92]=K_ER, [']']=K_ER, ['^']=K_ER, ['_']= 63,
- ['`']=K_ER, ['a']= 26, ['b']= 27, ['c']= 28, ['d']= 29, ['e']= 30, ['f']= 31, ['g']= 32,
- ['h']= 33, ['i']= 34, ['j']= 35, ['k']= 36, ['l']= 37, ['m']= 38, ['n']= 39, ['o']= 40,
- ['p']= 41, ['q']= 42, ['r']= 43, ['s']= 44, ['t']= 45, ['u']= 46, ['v']= 47, ['w']= 48,
- ['x']= 49, ['y']= 50, ['z']= 51, ['{']=K_ER, ['|']=K_ER, ['}']=K_ER, ['~']=K_ER, [127]=K_ER,
- [128]=K_ER, [129]=K_ER, [130]=K_ER, [131]=K_ER, [132]=K_ER, [133]=K_ER, [134]=K_ER, [135]=K_ER,
- [136]=K_ER, [137]=K_ER, [138]=K_ER, [139]=K_ER, [140]=K_ER, [141]=K_ER, [142]=K_ER, [143]=K_ER,
- [144]=K_ER, [145]=K_ER, [146]=K_ER, [147]=K_ER, [148]=K_ER, [149]=K_ER, [150]=K_ER, [151]=K_ER,
- [152]=K_ER, [153]=K_ER, [154]=K_ER, [155]=K_ER, [156]=K_ER, [157]=K_ER, [158]=K_ER, [159]=K_ER,
- [160]=K_ER, [161]=K_ER, [162]=K_ER, [163]=K_ER, [164]=K_ER, [165]=K_ER, [166]=K_ER, [167]=K_ER,
- [168]=K_ER, [169]=K_ER, [170]=K_ER, [171]=K_ER, [172]=K_ER, [173]=K_ER, [174]=K_ER, [175]=K_ER,
- [176]=K_ER, [177]=K_ER, [178]=K_ER, [179]=K_ER, [180]=K_ER, [181]=K_ER, [182]=K_ER, [183]=K_ER,
- [184]=K_ER, [185]=K_ER, [186]=K_ER, [187]=K_ER, [188]=K_ER, [189]=K_ER, [190]=K_ER, [191]=K_ER,
- [192]=K_ER, [193]=K_ER, [194]=K_ER, [195]=K_ER, [196]=K_ER, [197]=K_ER, [198]=K_ER, [199]=K_ER,
- [200]=K_ER, [201]=K_ER, [202]=K_ER, [203]=K_ER, [204]=K_ER, [205]=K_ER, [206]=K_ER, [207]=K_ER,
- [208]=K_ER, [209]=K_ER, [210]=K_ER, [211]=K_ER, [212]=K_ER, [213]=K_ER, [214]=K_ER, [215]=K_ER,
- [216]=K_ER, [217]=K_ER, [218]=K_ER, [219]=K_ER, [220]=K_ER, [221]=K_ER, [222]=K_ER, [223]=K_ER,
- [224]=K_ER, [225]=K_ER, [226]=K_ER, [227]=K_ER, [228]=K_ER, [229]=K_ER, [230]=K_ER, [231]=K_ER,
- [232]=K_ER, [233]=K_ER, [234]=K_ER, [235]=K_ER, [236]=K_ER, [237]=K_ER, [238]=K_ER, [239]=K_ER,
- [240]=K_ER, [241]=K_ER, [242]=K_ER, [243]=K_ER, [244]=K_ER, [245]=K_ER, [246]=K_ER, [247]=K_ER,
- [248]=K_ER, [249]=K_ER, [250]=K_ER, [251]=K_ER, [252]=K_ER, [253]=K_ER, [254]=K_ER, [255]=K_ER,
- };
-
- static size_t b64_encode(const uint8_t *src, size_t len, char *dst,
- const unsigned char *alpha)
- {
- size_t i, j;
- for (i = 0, j = 0; i + 3 <= len; i += 3, j += 4) {
- uint32_t v = 65536*src[i] + 256*src[i + 1] + src[i + 2];
- dst[j + 0] = alpha[(v >> 18) & 63];
- dst[j + 1] = alpha[(v >> 12) & 63];
- dst[j + 2] = alpha[(v >> 6) & 63];
- dst[j + 3] = alpha[v & 63];
- }
- size_t rem = len - i;
- if (rem == 1) {
- uint32_t v = 65536*src[i];
- dst[j++] = alpha[(v >> 18) & 63];
- dst[j++] = alpha[(v >> 12) & 63];
- dst[j++] = '=';
- dst[j++] = '=';
- } else if (rem == 2) {
- uint32_t v = 65536*src[i] + 256*src[i + 1];
- dst[j++] = alpha[(v >> 18) & 63];
- dst[j++] = alpha[(v >> 12) & 63];
- dst[j++] = alpha[(v >> 6) & 63];
- dst[j++] = '=';
- }
- return j;
- }
- static size_t b64_skip_ws(const char *src, size_t len, size_t index,
- const uint8_t *dec_table)
- {
- while (index < len && dec_table[(unsigned char)src[index]] == K_WS)
- index++;
- return index;
- }
- /* Implements the FromBase64 abstract operation.
- src/src_len: the input string (must be ASCII/latin1)
- dst/max_len: output buffer
- flags: b64_flags or b64_flags_url (selects valid characters)
- last_chunk: B64_LAST_LOOSE, B64_LAST_STRICT, or B64_LAST_STOP_BEFORE_PARTIAL
- *p_read: set to number of input characters consumed
- *p_err: set to 1 on error, 0 on success
- Returns: number of bytes written to dst */
- static size_t from_base64(const char *src, size_t src_len,
- uint8_t *dst, size_t max_len,
- const uint8_t *dec_table, int last_chunk,
- size_t *p_read, int *p_err)
- {
- size_t read = 0, written = 0;
- uint32_t v, acc = 0;
- int seen = 0;
- size_t index = 0;
- uint8_t ch;
-
- *p_err = 0;
- if (max_len == 0) {
- *p_read = 0;
- return 0;
- }
- for (;;) {
- if (seen == 0) {
- /* Fast path: decode complete groups of 4 valid characters.
- Breaks out on whitespace, padding, invalid chars, or capacity. */
- while (index + 4 <= src_len && written + 3 <= max_len) {
- uint32_t v0, v1, v2, v3;
- v0 = dec_table[(unsigned char)src[index]];
- v1 = dec_table[(unsigned char)src[index + 1]];
- v2 = dec_table[(unsigned char)src[index + 2]];
- v3 = dec_table[(unsigned char)src[index + 3]];
- if ((v0 | v1 | v2 | v3) >= 64)
- break;
- v = (v0 << 18) | (v1 << 12) | (v2 << 6) | v3;
- dst[written] = (uint8_t)(v >> 16);
- dst[written + 1] = (uint8_t)(v >> 8);
- dst[written + 2] = (uint8_t)(v);
- written += 3;
- index += 4;
- }
- read = index;
-
- if (written >= max_len) {
- *p_read = read;
- return written;
- }
- }
-
- /* Slow path: handle whitespace, padding, partial groups, capacity. */
- index = b64_skip_ws(src, src_len, index, dec_table);
- if (index == src_len) {
- if (seen > 0) {
- if (last_chunk == B64_LAST_STOP_BEFORE_PARTIAL) {
- *p_read = read;
- return written;
- }
- if (last_chunk == B64_LAST_STRICT) {
- *p_err = 1;
- return 0;
- }
- /* loose */
- if (seen == 1) {
- *p_err = 1;
- return 0;
- }
- break;
- }
- *p_read = src_len;
- return written;
- }
- ch = src[index++];
- if (ch == '=') {
- if (seen < 2) {
- *p_err = 1;
- return 0;
- }
- index = b64_skip_ws(src, src_len, index, dec_table);
- if (seen == 2) {
- if (index == src_len) {
- if (last_chunk == B64_LAST_STOP_BEFORE_PARTIAL) {
- *p_read = read;
- return written;
- }
- *p_err = 1;
- return 0;
- }
- if (src[index] == '=') {
- index++;
- index = b64_skip_ws(src, src_len, index, dec_table);
- } else {
- *p_err = 1;
- return 0;
- }
- }
- /* After padding, only whitespace is allowed */
- if (index != src_len) {
- *p_err = 1;
- return 0;
- }
- if (last_chunk == B64_LAST_STRICT) {
- uint32_t mask = (seen == 2) ? 0xF : 0x3;
- if (acc & mask) {
- *p_err = 1;
- return 0;
- }
- }
- break;
- }
- v = dec_table[ch];
- if (v >= 64) {
- *p_err = 1;
- return 0;
- }
- /* Check remaining capacity before committing to this group */
- {
- size_t remaining = max_len - written;
- if ((remaining == 1 && seen == 2) ||
- (remaining == 2 && seen == 3)) {
- *p_read = read;
- return written;
- }
- }
- acc = (acc << 6) | v;
- seen++;
- if (seen == 4) {
- dst[written] = (uint8_t)(acc >> 16);
- dst[written + 1] = (uint8_t)(acc >> 8);
- dst[written + 2] = (uint8_t)(acc);
- written += 3;
- acc = 0;
- seen = 0;
- read = index;
- if (written >= max_len) {
- *p_read = read;
- return written;
- }
- }
- }
- if (seen == 2) {
- dst[written++] = (uint8_t)(acc >> 4);
- } else if (seen == 3) {
- dst[written] = (uint8_t)(acc >> 10);
- dst[written + 1] = (uint8_t)(acc >> 2);
- written += 2;
- }
- *p_read = src_len;
- return written;
- }
- /* Hex helpers */
- static const char u8a_hex_digits[] = "0123456789abcdef";
- static size_t u8a_hex_encode(const uint8_t *src, size_t len, char *dst)
- {
- for (size_t i = 0; i < len; i++) {
- dst[i * 2] = u8a_hex_digits[src[i] >> 4];
- dst[i * 2 + 1] = u8a_hex_digits[src[i] & 0xF];
- }
- return len * 2;
- }
- /* Decode hex string to bytes.
- Returns bytes written. Sets *p_read to chars consumed, *p_err on error. */
- static size_t u8a_hex_decode(const char *src, size_t src_len,
- uint8_t *dst, size_t max_len,
- size_t *p_read, int *p_err)
- {
- size_t written = 0, i = 0;
- *p_err = 0;
- if (src_len & 1) {
- *p_err = 1;
- return 0;
- }
- while (i < src_len && written < max_len) {
- int hi = from_hex(src[i]);
- int lo = from_hex(src[i + 1]);
- if (hi < 0 || lo < 0) {
- *p_err = 1;
- return 0;
- }
- dst[written++] = (uint8_t)((hi << 4) | lo);
- i += 2;
- }
- *p_read = i;
- return written;
- }
- static JSValue JS_NewUint8ArrayCopy(JSContext *ctx, const uint8_t *buf, size_t len)
- {
- JSValue buffer, obj;
- JSArrayBuffer *abuf;
- buffer = js_array_buffer_constructor3(ctx, JS_UNDEFINED, len, NULL,
- JS_CLASS_ARRAY_BUFFER,
- (uint8_t *)buf,
- js_array_buffer_free, NULL,
- TRUE);
- if (JS_IsException(buffer))
- return JS_EXCEPTION;
- obj = js_create_from_ctor(ctx, JS_UNDEFINED, JS_CLASS_UINT8_ARRAY);
- if (JS_IsException(obj)) {
- JS_FreeValue(ctx, buffer);
- return JS_EXCEPTION;
- }
- abuf = js_get_array_buffer(ctx, buffer);
- assert(abuf != NULL);
- if (typed_array_init(ctx, obj, buffer, 0, abuf->byte_length, /*track_rab*/FALSE)) {
- // 'buffer' is freed on error above.
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- return obj;
- }
- /* Validate that this_val is a Uint8Array (type check only, no detach check).
- Returns the JSObject pointer or NULL on error (throws). */
- static JSObject *check_uint8array(JSContext *ctx, JSValueConst this_val)
- {
- JSObject *p;
- if (JS_VALUE_GET_TAG(this_val) != JS_TAG_OBJECT)
- goto fail;
- p = JS_VALUE_GET_OBJ(this_val);
- if (p->class_id != JS_CLASS_UINT8_ARRAY)
- goto fail;
- return p;
- fail:
- JS_ThrowTypeError(ctx, "not a Uint8Array");
- return NULL;
- }
- /* Get the data pointer and length of a Uint8Array, checking for detached
- buffers. Must be called after options are read (per spec ordering).
- Returns 0 on success, -1 on error (throws). */
- static int get_uint8array_bytes(JSContext *ctx, JSObject *p,
- uint8_t **pdata, size_t *plen)
- {
- if (typed_array_is_oob(p)) {
- JS_ThrowTypeErrorArrayBufferOOB(ctx);
- *pdata = NULL; /* fail safe */
- *plen = 0;
- return -1;
- }
- *pdata = p->u.array.u.uint8_ptr;
- *plen = p->u.array.count;
- return 0;
- }
- /* Validate options is undefined or an object (GetOptionsObject).
- Returns 0 on success, -1 on error (throws). */
- static int check_options_object(JSContext *ctx, JSValueConst options)
- {
- if (JS_IsUndefined(options))
- return 0;
- if (!JS_IsObject(options)) {
- JS_ThrowTypeError(ctx, "options must be an object");
- return -1;
- }
- return 0;
- }
- /* Parse the 'alphabet' option from an options object.
- Returns B64_ALPHABET_BASE64 or B64_ALPHABET_BASE64URL, or -1 on error. */
- static int parse_alphabet_option(JSContext *ctx, JSValueConst options)
- {
- JSValue val;
- const char *str;
- int ret;
- if (JS_IsUndefined(options))
- return B64_ALPHABET_BASE64;
- val = JS_GetProperty(ctx, options, JS_ATOM_alphabet);
- if (JS_IsException(val))
- return -1;
- if (JS_IsUndefined(val))
- return B64_ALPHABET_BASE64;
- if (!JS_IsString(val)) {
- JS_FreeValue(ctx, val);
- JS_ThrowTypeError(ctx, "expected string for alphabet");
- return -1;
- }
- str = JS_ToCString(ctx, val);
- JS_FreeValue(ctx, val);
- if (!str)
- return -1;
- if (!strcmp(str, "base64"))
- ret = B64_ALPHABET_BASE64;
- else if (!strcmp(str, "base64url"))
- ret = B64_ALPHABET_BASE64URL;
- else {
- JS_ThrowTypeError(ctx, "invalid alphabet");
- ret = -1;
- }
- JS_FreeCString(ctx, str);
- return ret;
- }
- /* Parse the 'lastChunkHandling' option. Returns mode or -1 on error. */
- static int parse_last_chunk_option(JSContext *ctx, JSValueConst options)
- {
- JSValue val;
- const char *str;
- int ret;
- if (JS_IsUndefined(options))
- return B64_LAST_LOOSE;
- val = JS_GetProperty(ctx, options, JS_ATOM_lastChunkHandling);
- if (JS_IsException(val))
- return -1;
- if (JS_IsUndefined(val))
- return B64_LAST_LOOSE;
- if (!JS_IsString(val)) {
- JS_FreeValue(ctx, val);
- JS_ThrowTypeError(ctx, "expected string for lastChunkHandling");
- return -1;
- }
- str = JS_ToCString(ctx, val);
- JS_FreeValue(ctx, val);
- if (!str)
- return -1;
- if (!strcmp(str, "loose"))
- ret = B64_LAST_LOOSE;
- else if (!strcmp(str, "strict"))
- ret = B64_LAST_STRICT;
- else if (!strcmp(str, "stop-before-partial"))
- ret = B64_LAST_STOP_BEFORE_PARTIAL;
- else {
- JS_ThrowTypeError(ctx, "invalid lastChunkHandling option");
- ret = -1;
- }
- JS_FreeCString(ctx, str);
- return ret;
- }
- /* Uint8Array.prototype.toBase64([options]) */
- static JSValue js_uint8array_to_base64(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- uint8_t *data;
- size_t len;
- JSValueConst options;
- JSObject *p;
- int alphabet, omit_padding;
- size_t out_len, written;
- JSString *ostr;
- char *dst;
- p = check_uint8array(ctx, this_val);
- if (!p)
- return JS_EXCEPTION;
- options = argc > 0 ? argv[0] : JS_UNDEFINED;
- if (check_options_object(ctx, options))
- return JS_EXCEPTION;
- alphabet = parse_alphabet_option(ctx, options);
- if (alphabet < 0)
- return JS_EXCEPTION;
- omit_padding = 0;
- if (!JS_IsUndefined(options)) {
- JSValue op_val = JS_GetProperty(ctx, options, JS_ATOM_omitPadding);
- if (JS_IsException(op_val))
- return JS_EXCEPTION;
- omit_padding = JS_ToBool(ctx, op_val);
- JS_FreeValue(ctx, op_val);
- }
- if (get_uint8array_bytes(ctx, p, &data, &len))
- return JS_EXCEPTION;
- out_len = 4 * ((len + 2) / 3);
- if (unlikely(out_len > JS_STRING_LEN_MAX))
- return JS_ThrowRangeError(ctx, "output too large");
- ostr = js_alloc_string(ctx, out_len, 0);
- if (!ostr)
- return JS_EXCEPTION;
- dst = (char *)ostr->u.str8;
- written = b64_encode(data, len, dst,
- alphabet == B64_ALPHABET_BASE64URL ? b64url_enc : b64_enc);
- if (omit_padding) {
- while (written > 0 && dst[written - 1] == '=')
- written--;
- }
- dst[written] = '\0';
- ostr->len = written;
- return JS_MKPTR(JS_TAG_STRING, ostr);
- }
- /* Uint8Array.prototype.toHex() */
- static JSValue js_uint8array_to_hex(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- uint8_t *data;
- size_t len, out_len;
- JSObject *p;
- JSString *ostr;
- p = check_uint8array(ctx, this_val);
- if (!p)
- return JS_EXCEPTION;
- if (get_uint8array_bytes(ctx, p, &data, &len))
- return JS_EXCEPTION;
- out_len = len * 2;
- if (unlikely(out_len > JS_STRING_LEN_MAX))
- return JS_ThrowRangeError(ctx, "output too large");
- ostr = js_alloc_string(ctx, out_len, 0);
- if (!ostr)
- return JS_EXCEPTION;
- u8a_hex_encode(data, len, (char *)ostr->u.str8);
- ostr->u.str8[out_len] = '\0';
- return JS_MKPTR(JS_TAG_STRING, ostr);
- }
- /* Uint8Array.fromBase64(string[, options]) */
- static JSValue js_uint8array_from_base64(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- const char *str;
- size_t str_len, read_pos, decoded_len, out_cap;
- int alphabet, last_chunk, err;
- uint8_t *buf;
- JSValue result;
- JSValueConst options;
-
- if (!JS_IsString(argv[0]))
- return JS_ThrowTypeError(ctx, "expected string");
- str = JS_ToCStringLen(ctx, &str_len, argv[0]);
- if (!str)
- return JS_EXCEPTION;
- options = argc > 1 ? argv[1] : JS_UNDEFINED;
- if (check_options_object(ctx, options)) {
- JS_FreeCString(ctx, str);
- return JS_EXCEPTION;
- }
- alphabet = parse_alphabet_option(ctx, options);
- if (alphabet < 0) {
- JS_FreeCString(ctx, str);
- return JS_EXCEPTION;
- }
- last_chunk = parse_last_chunk_option(ctx, options);
- if (last_chunk < 0) {
- JS_FreeCString(ctx, str);
- return JS_EXCEPTION;
- }
- out_cap = (str_len / 4) * 3 + 3;
- buf = js_malloc(ctx, out_cap);
- if (!buf) {
- JS_FreeCString(ctx, str);
- return JS_EXCEPTION;
- }
- decoded_len = from_base64(str, str_len, buf, out_cap,
- alphabet == B64_ALPHABET_BASE64URL
- ? b64url_dec : b64_dec,
- last_chunk, &read_pos, &err);
- JS_FreeCString(ctx, str);
- if (err) {
- js_free(ctx, buf);
- return JS_ThrowSyntaxError(ctx, "invalid base64 string");
- }
- result = JS_NewUint8ArrayCopy(ctx, buf, decoded_len);
- js_free(ctx, buf);
- return result;
- }
- /* Uint8Array.fromHex(string) */
- static JSValue js_uint8array_from_hex(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- const char *str;
- size_t str_len, read_pos, decoded_len, out_cap;
- int err;
- uint8_t *buf;
- JSValue result;
- if (!JS_IsString(argv[0]))
- return JS_ThrowTypeError(ctx, "expected string");
- str = JS_ToCStringLen(ctx, &str_len, argv[0]);
- if (!str)
- return JS_EXCEPTION;
- out_cap = str_len / 2 + 1;
- buf = js_malloc(ctx, out_cap);
- if (!buf) {
- JS_FreeCString(ctx, str);
- return JS_EXCEPTION;
- }
- decoded_len = u8a_hex_decode(str, str_len, buf, out_cap, &read_pos, &err);
- JS_FreeCString(ctx, str);
- if (err) {
- js_free(ctx, buf);
- return JS_ThrowSyntaxError(ctx, "invalid hex string");
- }
- /* XXX: could avoid the copy */
- result = JS_NewUint8ArrayCopy(ctx, buf, decoded_len);
- js_free(ctx, buf);
- return result;
- }
- /* Return a { read, written } result object */
- static JSValue js_make_read_written(JSContext *ctx, size_t read, size_t written)
- {
- JSValue obj = JS_NewObject(ctx);
- if (JS_IsException(obj))
- return JS_EXCEPTION;
- if (JS_DefinePropertyValueStr(ctx, obj, "read",
- JS_NewUint32(ctx, read), JS_PROP_C_W_E) < 0)
- goto fail;
- if (JS_DefinePropertyValueStr(ctx, obj, "written",
- JS_NewUint32(ctx, written), JS_PROP_C_W_E) < 0)
- goto fail;
- return obj;
- fail:
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- /* Uint8Array.prototype.setFromBase64(string[, options]) */
- static JSValue js_uint8array_set_from_base64(JSContext *ctx,
- JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- uint8_t *data;
- size_t len;
- const char *str;
- size_t str_len, read_pos, decoded_len;
- JSObject *p;
- int alphabet, last_chunk, err;
- JSValueConst options;
- p = check_uint8array(ctx, this_val);
- if (!p)
- return JS_EXCEPTION;
- if (!JS_IsString(argv[0]))
- return JS_ThrowTypeError(ctx, "expected string");
- str = JS_ToCStringLen(ctx, &str_len, argv[0]);
- if (!str)
- return JS_EXCEPTION;
- options = argc > 1 ? argv[1] : JS_UNDEFINED;
- if (check_options_object(ctx, options)) {
- JS_FreeCString(ctx, str);
- return JS_EXCEPTION;
- }
- alphabet = parse_alphabet_option(ctx, options);
- if (alphabet < 0) {
- JS_FreeCString(ctx, str);
- return JS_EXCEPTION;
- }
- last_chunk = parse_last_chunk_option(ctx, options);
- if (last_chunk < 0) {
- JS_FreeCString(ctx, str);
- return JS_EXCEPTION;
- }
- if (get_uint8array_bytes(ctx, p, &data, &len)) {
- JS_FreeCString(ctx, str);
- return JS_EXCEPTION;
- }
- decoded_len = from_base64(str, str_len, data, len,
- alphabet == B64_ALPHABET_BASE64URL
- ? b64url_dec : b64_dec,
- last_chunk, &read_pos, &err);
- JS_FreeCString(ctx, str);
- if (err)
- return JS_ThrowSyntaxError(ctx, "invalid base64 string");
- return js_make_read_written(ctx, read_pos, decoded_len);
- }
- /* Uint8Array.prototype.setFromHex(string) */
- static JSValue js_uint8array_set_from_hex(JSContext *ctx,
- JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- uint8_t *data;
- size_t len;
- const char *str;
- size_t str_len, read_pos, decoded_len;
- JSObject *p;
- int err;
- p = check_uint8array(ctx, this_val);
- if (!p)
- return JS_EXCEPTION;
- if (!JS_IsString(argv[0]))
- return JS_ThrowTypeError(ctx, "expected string");
- str = JS_ToCStringLen(ctx, &str_len, argv[0]);
- if (!str)
- return JS_EXCEPTION;
- if (get_uint8array_bytes(ctx, p, &data, &len)) {
- JS_FreeCString(ctx, str);
- return JS_EXCEPTION;
- }
- decoded_len = u8a_hex_decode(str, str_len, data, len, &read_pos, &err);
- JS_FreeCString(ctx, str);
- if (err)
- return JS_ThrowSyntaxError(ctx, "invalid hex string");
- return js_make_read_written(ctx, read_pos, decoded_len);
- }
- static const JSCFunctionListEntry js_typed_array_base_funcs[] = {
- JS_CFUNC_DEF("from", 1, js_typed_array_from ),
- JS_CFUNC_DEF("of", 0, js_typed_array_of ),
- JS_CGETSET_DEF("[Symbol.species]", js_get_this, NULL ),
- };
- static const JSCFunctionListEntry js_typed_array_base_proto_funcs[] = {
- JS_CGETSET_DEF("length", js_typed_array_get_length, NULL ),
- JS_CFUNC_DEF("at", 1, js_typed_array_at ),
- JS_CFUNC_DEF("with", 2, js_typed_array_with ),
- JS_CGETSET_DEF("buffer", js_typed_array_get_buffer, NULL ),
- JS_CGETSET_DEF("byteLength", js_typed_array_get_byteLength, NULL ),
- JS_CGETSET_DEF("byteOffset", js_typed_array_get_byteOffset, NULL ),
- JS_CFUNC_DEF("set", 1, js_typed_array_set ),
- JS_CFUNC_MAGIC_DEF("values", 0, js_create_typed_array_iterator, JS_ITERATOR_KIND_VALUE ),
- JS_ALIAS_DEF("[Symbol.iterator]", "values" ),
- JS_CFUNC_MAGIC_DEF("keys", 0, js_create_typed_array_iterator, JS_ITERATOR_KIND_KEY ),
- JS_CFUNC_MAGIC_DEF("entries", 0, js_create_typed_array_iterator, JS_ITERATOR_KIND_KEY_AND_VALUE ),
- JS_CGETSET_DEF("[Symbol.toStringTag]", js_typed_array_get_toStringTag, NULL ),
- JS_CFUNC_DEF("copyWithin", 2, js_typed_array_copyWithin ),
- JS_CFUNC_MAGIC_DEF("every", 1, js_array_every, special_every | special_TA ),
- JS_CFUNC_MAGIC_DEF("some", 1, js_array_every, special_some | special_TA ),
- JS_CFUNC_MAGIC_DEF("forEach", 1, js_array_every, special_forEach | special_TA ),
- JS_CFUNC_MAGIC_DEF("map", 1, js_array_every, special_map | special_TA ),
- JS_CFUNC_MAGIC_DEF("filter", 1, js_array_every, special_filter | special_TA ),
- JS_CFUNC_MAGIC_DEF("reduce", 1, js_array_reduce, special_reduce | special_TA ),
- JS_CFUNC_MAGIC_DEF("reduceRight", 1, js_array_reduce, special_reduceRight | special_TA ),
- JS_CFUNC_DEF("fill", 1, js_typed_array_fill ),
- JS_CFUNC_MAGIC_DEF("find", 1, js_typed_array_find, ArrayFind ),
- JS_CFUNC_MAGIC_DEF("findIndex", 1, js_typed_array_find, ArrayFindIndex ),
- JS_CFUNC_MAGIC_DEF("findLast", 1, js_typed_array_find, ArrayFindLast ),
- JS_CFUNC_MAGIC_DEF("findLastIndex", 1, js_typed_array_find, ArrayFindLastIndex ),
- JS_CFUNC_DEF("reverse", 0, js_typed_array_reverse ),
- JS_CFUNC_DEF("toReversed", 0, js_typed_array_toReversed ),
- JS_CFUNC_DEF("slice", 2, js_typed_array_slice ),
- JS_CFUNC_DEF("subarray", 2, js_typed_array_subarray ),
- JS_CFUNC_DEF("sort", 1, js_typed_array_sort ),
- JS_CFUNC_DEF("toSorted", 1, js_typed_array_toSorted ),
- JS_CFUNC_MAGIC_DEF("join", 1, js_typed_array_join, 0 ),
- JS_CFUNC_MAGIC_DEF("toLocaleString", 0, js_typed_array_join, 1 ),
- JS_CFUNC_MAGIC_DEF("indexOf", 1, js_typed_array_indexOf, special_indexOf ),
- JS_CFUNC_MAGIC_DEF("lastIndexOf", 1, js_typed_array_indexOf, special_lastIndexOf ),
- JS_CFUNC_MAGIC_DEF("includes", 1, js_typed_array_indexOf, special_includes ),
- //JS_ALIAS_BASE_DEF("toString", "toString", 2 /* Array.prototype. */), @@@
- };
- static const JSCFunctionListEntry js_typed_array_funcs[] = {
- JS_PROP_INT32_DEF("BYTES_PER_ELEMENT", 1, 0),
- JS_PROP_INT32_DEF("BYTES_PER_ELEMENT", 2, 0),
- JS_PROP_INT32_DEF("BYTES_PER_ELEMENT", 4, 0),
- JS_PROP_INT32_DEF("BYTES_PER_ELEMENT", 8, 0),
- };
- static const JSCFunctionListEntry js_uint8array_proto_funcs[] = {
- JS_PROP_INT32_DEF("BYTES_PER_ELEMENT", 1, 0),
- JS_CFUNC_DEF("toBase64", 0, js_uint8array_to_base64),
- JS_CFUNC_DEF("toHex", 0, js_uint8array_to_hex),
- JS_CFUNC_DEF("setFromBase64", 1, js_uint8array_set_from_base64),
- JS_CFUNC_DEF("setFromHex", 1, js_uint8array_set_from_hex),
- };
- static const JSCFunctionListEntry js_uint8array_funcs[] = {
- JS_PROP_INT32_DEF("BYTES_PER_ELEMENT", 1, 0),
- JS_CFUNC_DEF("fromBase64", 1, js_uint8array_from_base64),
- JS_CFUNC_DEF("fromHex", 1, js_uint8array_from_hex),
- };
- static JSValue js_typed_array_base_constructor(JSContext *ctx,
- JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- return JS_ThrowTypeError(ctx, "cannot be called");
- }
- /* 'obj' must be an allocated typed array object */
- static int typed_array_init(JSContext *ctx, JSValueConst obj,
- JSValue buffer, uint64_t offset, uint64_t len,
- BOOL track_rab)
- {
- JSTypedArray *ta;
- JSObject *p, *pbuffer;
- JSArrayBuffer *abuf;
- int size_log2;
- p = JS_VALUE_GET_OBJ(obj);
- size_log2 = typed_array_size_log2(p->class_id);
- ta = js_malloc(ctx, sizeof(*ta));
- if (!ta) {
- JS_FreeValue(ctx, buffer);
- return -1;
- }
- pbuffer = JS_VALUE_GET_OBJ(buffer);
- abuf = pbuffer->u.array_buffer;
- ta->obj = p;
- ta->buffer = pbuffer;
- ta->offset = offset;
- ta->length = len << size_log2;
- ta->track_rab = track_rab;
- list_add_tail(&ta->link, &abuf->array_list);
- p->u.typed_array = ta;
- p->u.array.count = len;
- p->u.array.u.ptr = abuf->data + offset;
- return 0;
- }
- static JSValue js_array_from_iterator(JSContext *ctx, uint32_t *plen,
- JSValueConst obj, JSValueConst method)
- {
- JSValue arr, iter, next_method = JS_UNDEFINED, val;
- BOOL done;
- uint32_t k;
- *plen = 0;
- arr = JS_NewArray(ctx);
- if (JS_IsException(arr))
- return arr;
- iter = JS_GetIterator2(ctx, obj, method);
- if (JS_IsException(iter))
- goto fail;
- next_method = JS_GetProperty(ctx, iter, JS_ATOM_next);
- if (JS_IsException(next_method))
- goto fail;
- k = 0;
- for(;;) {
- val = JS_IteratorNext(ctx, iter, next_method, 0, NULL, &done);
- if (JS_IsException(val))
- goto fail;
- if (done)
- break;
- if (JS_CreateDataPropertyUint32(ctx, arr, k, val, JS_PROP_THROW) < 0)
- goto fail;
- k++;
- }
- JS_FreeValue(ctx, next_method);
- JS_FreeValue(ctx, iter);
- *plen = k;
- return arr;
- fail:
- JS_FreeValue(ctx, next_method);
- JS_FreeValue(ctx, iter);
- JS_FreeValue(ctx, arr);
- return JS_EXCEPTION;
- }
- static JSValue js_typed_array_constructor_obj(JSContext *ctx,
- JSValueConst new_target,
- JSValueConst obj,
- int classid)
- {
- JSValue iter, ret, arr = JS_UNDEFINED, val, buffer;
- uint32_t i;
- int size_log2;
- int64_t len;
- size_log2 = typed_array_size_log2(classid);
- ret = js_create_from_ctor(ctx, new_target, classid);
- if (JS_IsException(ret))
- return JS_EXCEPTION;
- iter = JS_GetProperty(ctx, obj, JS_ATOM_Symbol_iterator);
- if (JS_IsException(iter))
- goto fail;
- if (!JS_IsUndefined(iter) && !JS_IsNull(iter)) {
- uint32_t len1;
- arr = js_array_from_iterator(ctx, &len1, obj, iter);
- JS_FreeValue(ctx, iter);
- if (JS_IsException(arr))
- goto fail;
- len = len1;
- } else {
- if (js_get_length64(ctx, &len, obj))
- goto fail;
- arr = JS_DupValue(ctx, obj);
- }
- buffer = js_array_buffer_constructor1(ctx, JS_UNDEFINED,
- len << size_log2,
- NULL);
- if (JS_IsException(buffer))
- goto fail;
- if (typed_array_init(ctx, ret, buffer, 0, len, /*track_rab*/FALSE))
- goto fail;
- for(i = 0; i < len; i++) {
- val = JS_GetPropertyUint32(ctx, arr, i);
- if (JS_IsException(val))
- goto fail;
- if (JS_SetPropertyUint32(ctx, ret, i, val) < 0)
- goto fail;
- }
- JS_FreeValue(ctx, arr);
- return ret;
- fail:
- JS_FreeValue(ctx, arr);
- JS_FreeValue(ctx, ret);
- return JS_EXCEPTION;
- }
- static JSValue js_typed_array_constructor_ta(JSContext *ctx,
- JSValueConst new_target,
- JSValueConst src_obj,
- int classid, uint32_t len)
- {
- JSObject *p, *src_buffer;
- JSTypedArray *ta;
- JSValue obj, buffer;
- uint32_t i;
- int size_log2;
- JSArrayBuffer *src_abuf, *abuf;
- obj = js_create_from_ctor(ctx, new_target, classid);
- if (JS_IsException(obj))
- return obj;
- p = JS_VALUE_GET_OBJ(src_obj);
- if (typed_array_is_oob(p)) {
- JS_ThrowTypeErrorArrayBufferOOB(ctx);
- goto fail;
- }
- size_log2 = typed_array_size_log2(classid);
- buffer = js_array_buffer_constructor1(ctx, JS_UNDEFINED,
- (uint64_t)len << size_log2,
- NULL);
- if (JS_IsException(buffer))
- goto fail;
- /* necessary because it could have been detached */
- if (typed_array_is_oob(p)) {
- JS_FreeValue(ctx, buffer);
- JS_ThrowTypeErrorArrayBufferOOB(ctx);
- goto fail;
- }
- abuf = JS_GetOpaque(buffer, JS_CLASS_ARRAY_BUFFER);
- if (typed_array_init(ctx, obj, buffer, 0, len, /*track_rab*/FALSE))
- goto fail;
- ta = p->u.typed_array;
- src_buffer = ta->buffer;
- src_abuf = src_buffer->u.array_buffer;
- if (p->class_id == classid &&
- (int64_t)ta->offset + (int64_t)abuf->byte_length <= src_abuf->byte_length) {
- /* same type and no overflow: copy the content */
- memcpy(abuf->data, src_abuf->data + ta->offset, abuf->byte_length);
- } else {
- for(i = 0; i < len; i++) {
- JSValue val;
- val = JS_GetPropertyUint32(ctx, src_obj, i);
- if (JS_IsException(val))
- goto fail;
- if (JS_SetPropertyUint32(ctx, obj, i, val) < 0)
- goto fail;
- }
- }
- return obj;
- fail:
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static JSValue js_typed_array_constructor(JSContext *ctx,
- JSValueConst new_target,
- int argc, JSValueConst *argv,
- int classid)
- {
- BOOL track_rab = FALSE;
- JSValue buffer, obj;
- JSArrayBuffer *abuf;
- int size_log2;
- uint64_t len, offset;
- size_log2 = typed_array_size_log2(classid);
- if (JS_VALUE_GET_TAG(argv[0]) != JS_TAG_OBJECT) {
- if (JS_ToIndex(ctx, &len, argv[0]))
- return JS_EXCEPTION;
- obj = js_create_from_ctor(ctx, new_target, classid);
- if (JS_IsException(obj))
- return JS_EXCEPTION;
- buffer = js_array_buffer_constructor1(ctx, JS_UNDEFINED,
- len << size_log2,
- NULL);
- if (JS_IsException(buffer))
- goto fail;
- offset = 0;
- } else {
- JSObject *p = JS_VALUE_GET_OBJ(argv[0]);
- if (p->class_id == JS_CLASS_ARRAY_BUFFER ||
- p->class_id == JS_CLASS_SHARED_ARRAY_BUFFER) {
- obj = js_create_from_ctor(ctx, new_target, classid);
- if (JS_IsException(obj))
- return JS_EXCEPTION;
- if (JS_ToIndex(ctx, &offset, argv[1]))
- goto fail;
- if ((offset & ((1 << size_log2) - 1)) != 0)
- goto invalid_offset;
- abuf = p->u.array_buffer;
- if (JS_IsUndefined(argv[2])) {
- if (abuf->detached) {
- JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
- goto fail;
- }
- if (offset > abuf->byte_length) {
- invalid_offset:
- JS_ThrowRangeError(ctx, "invalid offset");
- goto fail;
- }
- track_rab = array_buffer_is_resizable(abuf);
- if (!track_rab) {
- if ((abuf->byte_length & ((1 << size_log2) - 1)) != 0)
- goto invalid_length;
- }
- len = (abuf->byte_length - offset) >> size_log2;
- } else {
- if (JS_ToIndex(ctx, &len, argv[2]))
- goto fail;
- if (abuf->detached) {
- JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
- goto fail;
- }
- if ((offset + (len << size_log2)) > abuf->byte_length) {
- invalid_length:
- JS_ThrowRangeError(ctx, "invalid length");
- goto fail;
- }
- }
- buffer = JS_DupValue(ctx, argv[0]);
- } else {
- if (p->class_id >= JS_CLASS_UINT8C_ARRAY &&
- p->class_id <= JS_CLASS_FLOAT64_ARRAY) {
- return js_typed_array_constructor_ta(ctx, new_target, argv[0],
- classid, p->u.array.count);
- } else {
- return js_typed_array_constructor_obj(ctx, new_target, argv[0], classid);
- }
- }
- }
- if (typed_array_init(ctx, obj, buffer, offset, len, track_rab))
- goto fail;
- return obj;
- fail:
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- static void js_typed_array_finalizer(JSRuntime *rt, JSValue val)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JSTypedArray *ta = p->u.typed_array;
- if (ta) {
- /* during the GC the finalizers are called in an arbitrary
- order so the ArrayBuffer finalizer may have been called */
- if (ta->link.next) {
- list_del(&ta->link);
- }
- JS_FreeValueRT(rt, JS_MKPTR(JS_TAG_OBJECT, ta->buffer));
- js_free_rt(rt, ta);
- }
- }
- static void js_typed_array_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func)
- {
- JSObject *p = JS_VALUE_GET_OBJ(val);
- JSTypedArray *ta = p->u.typed_array;
- if (ta) {
- JS_MarkValue(rt, JS_MKPTR(JS_TAG_OBJECT, ta->buffer), mark_func);
- }
- }
- static JSValue js_dataview_constructor(JSContext *ctx,
- JSValueConst new_target,
- int argc, JSValueConst *argv)
- {
- BOOL recompute_len = FALSE;
- BOOL track_rab = FALSE;
- JSArrayBuffer *abuf;
- uint64_t offset;
- uint32_t len;
- JSValueConst buffer;
- JSValue obj;
- JSTypedArray *ta;
- JSObject *p;
- buffer = argv[0];
- abuf = js_get_array_buffer(ctx, buffer);
- if (!abuf)
- return JS_EXCEPTION;
- offset = 0;
- if (argc > 1) {
- if (JS_ToIndex(ctx, &offset, argv[1]))
- return JS_EXCEPTION;
- }
- if (abuf->detached)
- return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
- if (offset > abuf->byte_length)
- return JS_ThrowRangeError(ctx, "invalid byteOffset");
- len = abuf->byte_length - offset;
- if (argc > 2 && !JS_IsUndefined(argv[2])) {
- uint64_t l;
- if (JS_ToIndex(ctx, &l, argv[2]))
- return JS_EXCEPTION;
- if (l > len)
- return JS_ThrowRangeError(ctx, "invalid byteLength");
- len = l;
- } else {
- recompute_len = TRUE;
- track_rab = array_buffer_is_resizable(abuf);
- }
- obj = js_create_from_ctor(ctx, new_target, JS_CLASS_DATAVIEW);
- if (JS_IsException(obj))
- return JS_EXCEPTION;
- if (abuf->detached) {
- /* could have been detached in js_create_from_ctor() */
- JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
- goto fail;
- }
- // RAB could have been resized in js_create_from_ctor()
- if (offset > abuf->byte_length) {
- goto out_of_bound;
- } else if (recompute_len) {
- len = abuf->byte_length - offset;
- } else if (offset + len > abuf->byte_length) {
- out_of_bound:
- JS_ThrowRangeError(ctx, "invalid byteOffset or byteLength");
- goto fail;
- }
- ta = js_malloc(ctx, sizeof(*ta));
- if (!ta) {
- fail:
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- p = JS_VALUE_GET_OBJ(obj);
- ta->obj = p;
- ta->buffer = JS_VALUE_GET_OBJ(JS_DupValue(ctx, buffer));
- ta->offset = offset;
- ta->length = len;
- ta->track_rab = track_rab;
- list_add_tail(&ta->link, &abuf->array_list);
- p->u.typed_array = ta;
- return obj;
- }
- // is the DataView out of bounds relative to its parent arraybuffer?
- static BOOL dataview_is_oob(JSObject *p)
- {
- JSArrayBuffer *abuf;
- JSTypedArray *ta;
- assert(p->class_id == JS_CLASS_DATAVIEW);
- ta = p->u.typed_array;
- abuf = ta->buffer->u.array_buffer;
- if (abuf->detached)
- return TRUE;
- if (ta->offset > abuf->byte_length)
- return TRUE;
- if (ta->track_rab)
- return FALSE;
- return (int64_t)ta->offset + ta->length > abuf->byte_length;
- }
- static JSObject *get_dataview(JSContext *ctx, JSValueConst this_val)
- {
- JSObject *p;
- if (JS_VALUE_GET_TAG(this_val) != JS_TAG_OBJECT)
- goto fail;
- p = JS_VALUE_GET_OBJ(this_val);
- if (p->class_id != JS_CLASS_DATAVIEW) {
- fail:
- JS_ThrowTypeError(ctx, "not a DataView");
- return NULL;
- }
- return p;
- }
- static JSValue js_dataview_get_buffer(JSContext *ctx, JSValueConst this_val)
- {
- JSObject *p;
- JSTypedArray *ta;
- p = get_dataview(ctx, this_val);
- if (!p)
- return JS_EXCEPTION;
- ta = p->u.typed_array;
- return JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, ta->buffer));
- }
- static JSValue js_dataview_get_byteLength(JSContext *ctx, JSValueConst this_val)
- {
- JSArrayBuffer *abuf;
- JSTypedArray *ta;
- JSObject *p;
- p = get_dataview(ctx, this_val);
- if (!p)
- return JS_EXCEPTION;
- if (dataview_is_oob(p))
- return JS_ThrowTypeErrorArrayBufferOOB(ctx);
- ta = p->u.typed_array;
- if (ta->track_rab) {
- abuf = ta->buffer->u.array_buffer;
- return JS_NewUint32(ctx, abuf->byte_length - ta->offset);
- }
- return JS_NewUint32(ctx, ta->length);
- }
- static JSValue js_dataview_get_byteOffset(JSContext *ctx, JSValueConst this_val)
- {
- JSTypedArray *ta;
- JSObject *p;
- p = get_dataview(ctx, this_val);
- if (!p)
- return JS_EXCEPTION;
- if (dataview_is_oob(p))
- return JS_ThrowTypeErrorArrayBufferOOB(ctx);
- ta = p->u.typed_array;
- return JS_NewUint32(ctx, ta->offset);
- }
- static JSValue js_dataview_getValue(JSContext *ctx,
- JSValueConst this_obj,
- int argc, JSValueConst *argv, int class_id)
- {
- JSTypedArray *ta;
- JSArrayBuffer *abuf;
- BOOL littleEndian, is_swap;
- int size;
- uint8_t *ptr;
- uint32_t v;
- uint64_t pos;
- ta = JS_GetOpaque2(ctx, this_obj, JS_CLASS_DATAVIEW);
- if (!ta)
- return JS_EXCEPTION;
- size = 1 << typed_array_size_log2(class_id);
- if (JS_ToIndex(ctx, &pos, argv[0]))
- return JS_EXCEPTION;
- littleEndian = argc > 1 && JS_ToBool(ctx, argv[1]);
- is_swap = littleEndian ^ !is_be();
- abuf = ta->buffer->u.array_buffer;
- if (abuf->detached)
- return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
- // order matters: this check should come before the next one
- if ((pos + size) > ta->length)
- return JS_ThrowRangeError(ctx, "out of bound");
- // test262 expects a TypeError for this and V8, in its infinite wisdom,
- // throws a "detached array buffer" exception, but IMO that doesn't make
- // sense because the buffer is not in fact detached, it's still there
- if ((int64_t)ta->offset + ta->length > abuf->byte_length)
- return JS_ThrowTypeError(ctx, "out of bound");
- ptr = abuf->data + ta->offset + pos;
- switch(class_id) {
- case JS_CLASS_INT8_ARRAY:
- return JS_NewInt32(ctx, *(int8_t *)ptr);
- case JS_CLASS_UINT8_ARRAY:
- return JS_NewInt32(ctx, *(uint8_t *)ptr);
- case JS_CLASS_INT16_ARRAY:
- v = get_u16(ptr);
- if (is_swap)
- v = bswap16(v);
- return JS_NewInt32(ctx, (int16_t)v);
- case JS_CLASS_UINT16_ARRAY:
- v = get_u16(ptr);
- if (is_swap)
- v = bswap16(v);
- return JS_NewInt32(ctx, v);
- case JS_CLASS_INT32_ARRAY:
- v = get_u32(ptr);
- if (is_swap)
- v = bswap32(v);
- return JS_NewInt32(ctx, v);
- case JS_CLASS_UINT32_ARRAY:
- v = get_u32(ptr);
- if (is_swap)
- v = bswap32(v);
- return JS_NewUint32(ctx, v);
- case JS_CLASS_BIG_INT64_ARRAY:
- {
- uint64_t v;
- v = get_u64(ptr);
- if (is_swap)
- v = bswap64(v);
- return JS_NewBigInt64(ctx, v);
- }
- break;
- case JS_CLASS_BIG_UINT64_ARRAY:
- {
- uint64_t v;
- v = get_u64(ptr);
- if (is_swap)
- v = bswap64(v);
- return JS_NewBigUint64(ctx, v);
- }
- break;
- case JS_CLASS_FLOAT16_ARRAY:
- {
- uint16_t v;
- v = get_u16(ptr);
- if (is_swap)
- v = bswap16(v);
- return __JS_NewFloat64(ctx, fromfp16(v));
- }
- case JS_CLASS_FLOAT32_ARRAY:
- {
- union {
- float f;
- uint32_t i;
- } u;
- v = get_u32(ptr);
- if (is_swap)
- v = bswap32(v);
- u.i = v;
- return __JS_NewFloat64(ctx, u.f);
- }
- case JS_CLASS_FLOAT64_ARRAY:
- {
- union {
- double f;
- uint64_t i;
- } u;
- u.i = get_u64(ptr);
- if (is_swap)
- u.i = bswap64(u.i);
- return __JS_NewFloat64(ctx, u.f);
- }
- default:
- abort();
- }
- }
- static JSValue js_dataview_setValue(JSContext *ctx,
- JSValueConst this_obj,
- int argc, JSValueConst *argv, int class_id)
- {
- JSTypedArray *ta;
- JSArrayBuffer *abuf;
- BOOL littleEndian, is_swap;
- int size;
- uint8_t *ptr;
- uint64_t v64;
- uint32_t v;
- uint64_t pos;
- JSValueConst val;
- ta = JS_GetOpaque2(ctx, this_obj, JS_CLASS_DATAVIEW);
- if (!ta)
- return JS_EXCEPTION;
- size = 1 << typed_array_size_log2(class_id);
- if (JS_ToIndex(ctx, &pos, argv[0]))
- return JS_EXCEPTION;
- val = argv[1];
- v = 0; /* avoid warning */
- v64 = 0; /* avoid warning */
- if (class_id <= JS_CLASS_UINT32_ARRAY) {
- if (JS_ToUint32(ctx, &v, val))
- return JS_EXCEPTION;
- } else if (class_id <= JS_CLASS_BIG_UINT64_ARRAY) {
- if (JS_ToBigInt64(ctx, (int64_t *)&v64, val))
- return JS_EXCEPTION;
- } else {
- double d;
- if (JS_ToFloat64(ctx, &d, val))
- return JS_EXCEPTION;
- if (class_id == JS_CLASS_FLOAT16_ARRAY) {
- v = tofp16(d);
- } else if (class_id == JS_CLASS_FLOAT32_ARRAY) {
- union {
- float f;
- uint32_t i;
- } u;
- u.f = d;
- v = u.i;
- } else {
- JSFloat64Union u;
- u.d = d;
- v64 = u.u64;
- }
- }
- littleEndian = argc > 2 && JS_ToBool(ctx, argv[2]);
- is_swap = littleEndian ^ !is_be();
- abuf = ta->buffer->u.array_buffer;
- if (abuf->detached)
- return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
- // order matters: this check should come before the next one
- if ((pos + size) > ta->length)
- return JS_ThrowRangeError(ctx, "out of bound");
- // test262 expects a TypeError for this and V8, in its infinite wisdom,
- // throws a "detached array buffer" exception, but IMO that doesn't make
- // sense because the buffer is not in fact detached, it's still there
- if ((int64_t)ta->offset + ta->length > abuf->byte_length)
- return JS_ThrowTypeError(ctx, "out of bound");
- ptr = abuf->data + ta->offset + pos;
- switch(class_id) {
- case JS_CLASS_INT8_ARRAY:
- case JS_CLASS_UINT8_ARRAY:
- *ptr = v;
- break;
- case JS_CLASS_INT16_ARRAY:
- case JS_CLASS_UINT16_ARRAY:
- case JS_CLASS_FLOAT16_ARRAY:
- if (is_swap)
- v = bswap16(v);
- put_u16(ptr, v);
- break;
- case JS_CLASS_INT32_ARRAY:
- case JS_CLASS_UINT32_ARRAY:
- case JS_CLASS_FLOAT32_ARRAY:
- if (is_swap)
- v = bswap32(v);
- put_u32(ptr, v);
- break;
- case JS_CLASS_BIG_INT64_ARRAY:
- case JS_CLASS_BIG_UINT64_ARRAY:
- case JS_CLASS_FLOAT64_ARRAY:
- if (is_swap)
- v64 = bswap64(v64);
- put_u64(ptr, v64);
- break;
- default:
- abort();
- }
- return JS_UNDEFINED;
- }
- static const JSCFunctionListEntry js_dataview_proto_funcs[] = {
- JS_CGETSET_DEF("buffer", js_dataview_get_buffer, NULL ),
- JS_CGETSET_DEF("byteLength", js_dataview_get_byteLength, NULL ),
- JS_CGETSET_DEF("byteOffset", js_dataview_get_byteOffset, NULL ),
- JS_CFUNC_MAGIC_DEF("getInt8", 1, js_dataview_getValue, JS_CLASS_INT8_ARRAY ),
- JS_CFUNC_MAGIC_DEF("getUint8", 1, js_dataview_getValue, JS_CLASS_UINT8_ARRAY ),
- JS_CFUNC_MAGIC_DEF("getInt16", 1, js_dataview_getValue, JS_CLASS_INT16_ARRAY ),
- JS_CFUNC_MAGIC_DEF("getUint16", 1, js_dataview_getValue, JS_CLASS_UINT16_ARRAY ),
- JS_CFUNC_MAGIC_DEF("getInt32", 1, js_dataview_getValue, JS_CLASS_INT32_ARRAY ),
- JS_CFUNC_MAGIC_DEF("getUint32", 1, js_dataview_getValue, JS_CLASS_UINT32_ARRAY ),
- JS_CFUNC_MAGIC_DEF("getBigInt64", 1, js_dataview_getValue, JS_CLASS_BIG_INT64_ARRAY ),
- JS_CFUNC_MAGIC_DEF("getBigUint64", 1, js_dataview_getValue, JS_CLASS_BIG_UINT64_ARRAY ),
- JS_CFUNC_MAGIC_DEF("getFloat16", 1, js_dataview_getValue, JS_CLASS_FLOAT16_ARRAY ),
- JS_CFUNC_MAGIC_DEF("getFloat32", 1, js_dataview_getValue, JS_CLASS_FLOAT32_ARRAY ),
- JS_CFUNC_MAGIC_DEF("getFloat64", 1, js_dataview_getValue, JS_CLASS_FLOAT64_ARRAY ),
- JS_CFUNC_MAGIC_DEF("setInt8", 2, js_dataview_setValue, JS_CLASS_INT8_ARRAY ),
- JS_CFUNC_MAGIC_DEF("setUint8", 2, js_dataview_setValue, JS_CLASS_UINT8_ARRAY ),
- JS_CFUNC_MAGIC_DEF("setInt16", 2, js_dataview_setValue, JS_CLASS_INT16_ARRAY ),
- JS_CFUNC_MAGIC_DEF("setUint16", 2, js_dataview_setValue, JS_CLASS_UINT16_ARRAY ),
- JS_CFUNC_MAGIC_DEF("setInt32", 2, js_dataview_setValue, JS_CLASS_INT32_ARRAY ),
- JS_CFUNC_MAGIC_DEF("setUint32", 2, js_dataview_setValue, JS_CLASS_UINT32_ARRAY ),
- JS_CFUNC_MAGIC_DEF("setBigInt64", 2, js_dataview_setValue, JS_CLASS_BIG_INT64_ARRAY ),
- JS_CFUNC_MAGIC_DEF("setBigUint64", 2, js_dataview_setValue, JS_CLASS_BIG_UINT64_ARRAY ),
- JS_CFUNC_MAGIC_DEF("setFloat16", 2, js_dataview_setValue, JS_CLASS_FLOAT16_ARRAY ),
- JS_CFUNC_MAGIC_DEF("setFloat32", 2, js_dataview_setValue, JS_CLASS_FLOAT32_ARRAY ),
- JS_CFUNC_MAGIC_DEF("setFloat64", 2, js_dataview_setValue, JS_CLASS_FLOAT64_ARRAY ),
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "DataView", JS_PROP_CONFIGURABLE ),
- };
- /* Atomics */
- #ifdef CONFIG_ATOMICS
- typedef enum AtomicsOpEnum {
- ATOMICS_OP_ADD,
- ATOMICS_OP_AND,
- ATOMICS_OP_OR,
- ATOMICS_OP_SUB,
- ATOMICS_OP_XOR,
- ATOMICS_OP_EXCHANGE,
- ATOMICS_OP_COMPARE_EXCHANGE,
- ATOMICS_OP_LOAD,
- } AtomicsOpEnum;
- static JSObject *js_atomics_get_buf(JSContext *ctx,
- JSValueConst obj, JSValueConst idx_val,
- uint64_t *pidx, int is_waitable)
- {
- JSObject *p;
- JSTypedArray *ta;
- JSArrayBuffer *abuf;
- uint64_t idx;
- BOOL err;
- int old_len;
- if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)
- goto fail;
- p = JS_VALUE_GET_OBJ(obj);
- if (is_waitable)
- err = (p->class_id != JS_CLASS_INT32_ARRAY &&
- p->class_id != JS_CLASS_BIG_INT64_ARRAY);
- else
- err = !(p->class_id >= JS_CLASS_INT8_ARRAY &&
- p->class_id <= JS_CLASS_BIG_UINT64_ARRAY);
- if (err) {
- fail:
- JS_ThrowTypeError(ctx, "integer TypedArray expected");
- return NULL;
- }
- ta = p->u.typed_array;
- abuf = ta->buffer->u.array_buffer;
- if (!abuf->shared) {
- if (is_waitable == 2) {
- JS_ThrowTypeError(ctx, "not a SharedArrayBuffer TypedArray");
- return NULL;
- }
- if (abuf->detached) {
- JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
- return NULL;
- }
- }
- old_len = p->u.array.count;
-
- if (JS_ToIndex(ctx, &idx, idx_val)) {
- return NULL;
- }
- if (idx >= old_len)
- goto oob;
- if (is_waitable != 1) {
- /* RevalidateAtomicAccess() */
- if (typed_array_is_oob(p)) {
- JS_ThrowTypeErrorArrayBufferOOB(ctx);
- return NULL;
- }
- if (idx >= p->u.array.count) {
- oob:
- JS_ThrowRangeError(ctx, "out-of-bound access");
- return NULL;
- }
- }
- *pidx = idx;
- return p;
- }
- static JSValue js_atomics_op(JSContext *ctx,
- JSValueConst this_obj,
- int argc, JSValueConst *argv, int op)
- {
- int size_log2;
- uint64_t v, a, rep_val, idx;
- void *ptr;
- JSValue ret;
- JSObject *p;
-
- p = js_atomics_get_buf(ctx, argv[0], argv[1], &idx, 0);
- if (!p)
- return JS_EXCEPTION;
- size_log2 = typed_array_size_log2(p->class_id);
- rep_val = 0;
- if (op == ATOMICS_OP_LOAD) {
- v = 0;
- } else {
- if (size_log2 == 3) {
- int64_t v64;
- if (JS_ToBigInt64(ctx, &v64, argv[2]))
- return JS_EXCEPTION;
- v = v64;
- if (op == ATOMICS_OP_COMPARE_EXCHANGE) {
- if (JS_ToBigInt64(ctx, &v64, argv[3]))
- return JS_EXCEPTION;
- rep_val = v64;
- }
- } else {
- uint32_t v32;
- if (JS_ToUint32(ctx, &v32, argv[2]))
- return JS_EXCEPTION;
- v = v32;
- if (op == ATOMICS_OP_COMPARE_EXCHANGE) {
- if (JS_ToUint32(ctx, &v32, argv[3]))
- return JS_EXCEPTION;
- rep_val = v32;
- }
- }
- if (typed_array_is_oob(p))
- return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
- if (idx >= p->u.array.count)
- return JS_ThrowRangeError(ctx, "out-of-bound access");
- }
- ptr = p->u.array.u.uint8_ptr + ((uintptr_t)idx << size_log2);
-
- switch(op | (size_log2 << 3)) {
- #define OP(op_name, func_name) \
- case ATOMICS_OP_ ## op_name | (0 << 3): \
- a = func_name((_Atomic(uint8_t) *)ptr, v); \
- break; \
- case ATOMICS_OP_ ## op_name | (1 << 3): \
- a = func_name((_Atomic(uint16_t) *)ptr, v); \
- break; \
- case ATOMICS_OP_ ## op_name | (2 << 3): \
- a = func_name((_Atomic(uint32_t) *)ptr, v); \
- break; \
- case ATOMICS_OP_ ## op_name | (3 << 3): \
- a = func_name((_Atomic(uint64_t) *)ptr, v); \
- break;
- OP(ADD, atomic_fetch_add)
- OP(AND, atomic_fetch_and)
- OP(OR, atomic_fetch_or)
- OP(SUB, atomic_fetch_sub)
- OP(XOR, atomic_fetch_xor)
- OP(EXCHANGE, atomic_exchange)
- #undef OP
- case ATOMICS_OP_LOAD | (0 << 3):
- a = atomic_load((_Atomic(uint8_t) *)ptr);
- break;
- case ATOMICS_OP_LOAD | (1 << 3):
- a = atomic_load((_Atomic(uint16_t) *)ptr);
- break;
- case ATOMICS_OP_LOAD | (2 << 3):
- a = atomic_load((_Atomic(uint32_t) *)ptr);
- break;
- case ATOMICS_OP_LOAD | (3 << 3):
- a = atomic_load((_Atomic(uint64_t) *)ptr);
- break;
- case ATOMICS_OP_COMPARE_EXCHANGE | (0 << 3):
- {
- uint8_t v1 = v;
- atomic_compare_exchange_strong((_Atomic(uint8_t) *)ptr, &v1, rep_val);
- a = v1;
- }
- break;
- case ATOMICS_OP_COMPARE_EXCHANGE | (1 << 3):
- {
- uint16_t v1 = v;
- atomic_compare_exchange_strong((_Atomic(uint16_t) *)ptr, &v1, rep_val);
- a = v1;
- }
- break;
- case ATOMICS_OP_COMPARE_EXCHANGE | (2 << 3):
- {
- uint32_t v1 = v;
- atomic_compare_exchange_strong((_Atomic(uint32_t) *)ptr, &v1, rep_val);
- a = v1;
- }
- break;
- case ATOMICS_OP_COMPARE_EXCHANGE | (3 << 3):
- {
- uint64_t v1 = v;
- atomic_compare_exchange_strong((_Atomic(uint64_t) *)ptr, &v1, rep_val);
- a = v1;
- }
- break;
- default:
- abort();
- }
- switch(p->class_id) {
- case JS_CLASS_INT8_ARRAY:
- a = (int8_t)a;
- goto done;
- case JS_CLASS_UINT8_ARRAY:
- a = (uint8_t)a;
- goto done;
- case JS_CLASS_INT16_ARRAY:
- a = (int16_t)a;
- goto done;
- case JS_CLASS_UINT16_ARRAY:
- a = (uint16_t)a;
- goto done;
- case JS_CLASS_INT32_ARRAY:
- done:
- ret = JS_NewInt32(ctx, a);
- break;
- case JS_CLASS_UINT32_ARRAY:
- ret = JS_NewUint32(ctx, a);
- break;
- case JS_CLASS_BIG_INT64_ARRAY:
- ret = JS_NewBigInt64(ctx, a);
- break;
- case JS_CLASS_BIG_UINT64_ARRAY:
- ret = JS_NewBigUint64(ctx, a);
- break;
- default:
- abort();
- }
- return ret;
- }
- static JSValue js_atomics_store(JSContext *ctx,
- JSValueConst this_obj,
- int argc, JSValueConst *argv)
- {
- int size_log2;
- void *ptr;
- JSValue ret;
- JSObject *p;
- uint64_t idx;
- int64_t v;
- p = js_atomics_get_buf(ctx, argv[0], argv[1], &idx, 0);
- if (!p)
- return JS_EXCEPTION;
- size_log2 = typed_array_size_log2(p->class_id);
- if (size_log2 == 3) {
- ret = JS_ToBigIntFree(ctx, JS_DupValue(ctx, argv[2]));
- if (JS_IsException(ret))
- return ret;
- if (JS_ToBigInt64(ctx, &v, ret)) {
- JS_FreeValue(ctx, ret);
- return JS_EXCEPTION;
- }
- } else {
- uint32_t v32;
- /* XXX: spec, would be simpler to return the written value */
- ret = JS_ToIntegerFree(ctx, JS_DupValue(ctx, argv[2]));
- if (JS_IsException(ret))
- return ret;
- if (JS_ToUint32(ctx, &v32, ret)) {
- JS_FreeValue(ctx, ret);
- return JS_EXCEPTION;
- }
- v = v32;
- }
- if (typed_array_is_oob(p))
- return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
- if (idx >= p->u.array.count)
- return JS_ThrowRangeError(ctx, "out-of-bound access");
- ptr = p->u.array.u.uint8_ptr + ((uintptr_t)idx << size_log2);
-
- switch(size_log2) {
- case 0:
- atomic_store((_Atomic(uint8_t) *)ptr, v);
- break;
- case 1:
- atomic_store((_Atomic(uint16_t) *)ptr, v);
- break;
- case 2:
- atomic_store((_Atomic(uint32_t) *)ptr, v);
- break;
- case 3:
- atomic_store((_Atomic(uint64_t) *)ptr, v);
- break;
- default:
- abort();
- }
- return ret;
- }
- static JSValue js_atomics_isLockFree(JSContext *ctx,
- JSValueConst this_obj,
- int argc, JSValueConst *argv)
- {
- int v, ret;
- if (JS_ToInt32Sat(ctx, &v, argv[0]))
- return JS_EXCEPTION;
- ret = (v == 1 || v == 2 || v == 4 || v == 8);
- return JS_NewBool(ctx, ret);
- }
- typedef struct JSAtomicsWaiter {
- struct list_head link;
- BOOL linked;
- pthread_cond_t cond;
- int32_t *ptr;
- } JSAtomicsWaiter;
- static pthread_mutex_t js_atomics_mutex = PTHREAD_MUTEX_INITIALIZER;
- static struct list_head js_atomics_waiter_list =
- LIST_HEAD_INIT(js_atomics_waiter_list);
- #if defined(__aarch64__)
- static inline void cpu_pause(void)
- {
- asm volatile("yield" ::: "memory");
- }
- #elif defined(__x86_64) || defined(__i386__)
- static inline void cpu_pause(void)
- {
- asm volatile("pause" ::: "memory");
- }
- #else
- static inline void cpu_pause(void)
- {
- }
- #endif
- // no-op: Atomics.pause() is not allowed to block or yield to another
- // thread, only to hint the CPU that it should back off for a bit;
- // the amount of work we do here is a good enough substitute
- static JSValue js_atomics_pause(JSContext *ctx, JSValueConst this_obj,
- int argc, JSValueConst *argv)
- {
- double d;
- if (argc > 0) {
- switch (JS_VALUE_GET_NORM_TAG(argv[0])) {
- case JS_TAG_FLOAT64: // accepted if and only if fraction == 0.0
- d = JS_VALUE_GET_FLOAT64(argv[0]);
- if (isfinite(d))
- if (0 == modf(d, &d))
- break;
- // fallthru
- default:
- return JS_ThrowTypeError(ctx, "not an integral number");
- case JS_TAG_UNDEFINED:
- case JS_TAG_INT:
- break;
- }
- }
- cpu_pause();
- return JS_UNDEFINED;
- }
- static JSValue js_atomics_wait(JSContext *ctx,
- JSValueConst this_obj,
- int argc, JSValueConst *argv)
- {
- JSObject *p;
- int64_t v;
- int32_t v32;
- uint64_t idx;
- void *ptr;
- int64_t timeout;
- struct timespec ts;
- JSAtomicsWaiter waiter_s, *waiter;
- int ret, size_log2, res;
- double d;
- p = js_atomics_get_buf(ctx, argv[0], argv[1], &idx, 2);
- if (!p)
- return JS_EXCEPTION;
- size_log2 = typed_array_size_log2(p->class_id);
- ptr = p->u.array.u.uint8_ptr + ((uintptr_t)idx << size_log2);
-
- /* 'argv[0]' is a SharedArrayBuffer so it cannot be detached nor reduced */
- if (size_log2 == 3) {
- if (JS_ToBigInt64(ctx, &v, argv[2]))
- return JS_EXCEPTION;
- } else {
- if (JS_ToInt32(ctx, &v32, argv[2]))
- return JS_EXCEPTION;
- v = v32;
- }
- if (JS_ToFloat64(ctx, &d, argv[3]))
- return JS_EXCEPTION;
- /* must use INT64_MAX + 1 because INT64_MAX cannot be exactly represented as a double */
- if (isnan(d) || d >= 0x1p63)
- timeout = INT64_MAX;
- else if (d < 0)
- timeout = 0;
- else
- timeout = (int64_t)d;
- if (!ctx->rt->can_block)
- return JS_ThrowTypeError(ctx, "cannot block in this thread");
- /* XXX: inefficient if large number of waiters, should hash on
- 'ptr' value */
- /* XXX: use Linux futexes when available ? */
- pthread_mutex_lock(&js_atomics_mutex);
- if (size_log2 == 3) {
- res = *(int64_t *)ptr != v;
- } else {
- res = *(int32_t *)ptr != v;
- }
- if (res) {
- pthread_mutex_unlock(&js_atomics_mutex);
- return JS_AtomToString(ctx, JS_ATOM_not_equal);
- }
- waiter = &waiter_s;
- waiter->ptr = ptr;
- pthread_cond_init(&waiter->cond, NULL);
- waiter->linked = TRUE;
- list_add_tail(&waiter->link, &js_atomics_waiter_list);
- if (timeout == INT64_MAX) {
- pthread_cond_wait(&waiter->cond, &js_atomics_mutex);
- ret = 0;
- } else {
- /* XXX: use clock monotonic */
- clock_gettime(CLOCK_REALTIME, &ts);
- ts.tv_sec += timeout / 1000;
- ts.tv_nsec += (timeout % 1000) * 1000000;
- if (ts.tv_nsec >= 1000000000) {
- ts.tv_nsec -= 1000000000;
- ts.tv_sec++;
- }
- ret = pthread_cond_timedwait(&waiter->cond, &js_atomics_mutex,
- &ts);
- }
- if (waiter->linked)
- list_del(&waiter->link);
- pthread_mutex_unlock(&js_atomics_mutex);
- pthread_cond_destroy(&waiter->cond);
- if (ret == ETIMEDOUT) {
- return JS_AtomToString(ctx, JS_ATOM_timed_out);
- } else {
- return JS_AtomToString(ctx, JS_ATOM_ok);
- }
- }
- static JSValue js_atomics_notify(JSContext *ctx,
- JSValueConst this_obj,
- int argc, JSValueConst *argv)
- {
- struct list_head *el, *el1, waiter_list;
- int32_t count, n;
- uint64_t idx;
- int size_log2;
- void *ptr;
- JSAtomicsWaiter *waiter;
- JSArrayBuffer *abuf;
- JSObject *p;
-
- p = js_atomics_get_buf(ctx, argv[0], argv[1], &idx, 1);
- if (!p)
- return JS_EXCEPTION;
- size_log2 = typed_array_size_log2(p->class_id);
-
- if (JS_IsUndefined(argv[2])) {
- count = INT32_MAX;
- } else {
- if (JS_ToInt32Clamp(ctx, &count, argv[2], 0, INT32_MAX, 0))
- return JS_EXCEPTION;
- }
- n = 0;
- abuf = p->u.typed_array->buffer->u.array_buffer;
- if (abuf->shared && count > 0) {
- /* 'argv[0]' is a SharedArrayBuffer so it cannot be detached nor reduced */
- ptr = p->u.array.u.uint8_ptr + ((uintptr_t)idx << size_log2);
- pthread_mutex_lock(&js_atomics_mutex);
- init_list_head(&waiter_list);
- list_for_each_safe(el, el1, &js_atomics_waiter_list) {
- waiter = list_entry(el, JSAtomicsWaiter, link);
- if (waiter->ptr == ptr) {
- list_del(&waiter->link);
- waiter->linked = FALSE;
- list_add_tail(&waiter->link, &waiter_list);
- n++;
- if (n >= count)
- break;
- }
- }
- list_for_each(el, &waiter_list) {
- waiter = list_entry(el, JSAtomicsWaiter, link);
- pthread_cond_signal(&waiter->cond);
- }
- pthread_mutex_unlock(&js_atomics_mutex);
- }
- return JS_NewInt32(ctx, n);
- }
- static const JSCFunctionListEntry js_atomics_funcs[] = {
- JS_CFUNC_MAGIC_DEF("add", 3, js_atomics_op, ATOMICS_OP_ADD ),
- JS_CFUNC_MAGIC_DEF("and", 3, js_atomics_op, ATOMICS_OP_AND ),
- JS_CFUNC_MAGIC_DEF("or", 3, js_atomics_op, ATOMICS_OP_OR ),
- JS_CFUNC_MAGIC_DEF("sub", 3, js_atomics_op, ATOMICS_OP_SUB ),
- JS_CFUNC_MAGIC_DEF("xor", 3, js_atomics_op, ATOMICS_OP_XOR ),
- JS_CFUNC_MAGIC_DEF("exchange", 3, js_atomics_op, ATOMICS_OP_EXCHANGE ),
- JS_CFUNC_MAGIC_DEF("compareExchange", 4, js_atomics_op, ATOMICS_OP_COMPARE_EXCHANGE ),
- JS_CFUNC_MAGIC_DEF("load", 2, js_atomics_op, ATOMICS_OP_LOAD ),
- JS_CFUNC_DEF("store", 3, js_atomics_store ),
- JS_CFUNC_DEF("isLockFree", 1, js_atomics_isLockFree ),
- JS_CFUNC_DEF("pause", 0, js_atomics_pause ),
- JS_CFUNC_DEF("wait", 4, js_atomics_wait ),
- JS_CFUNC_DEF("notify", 3, js_atomics_notify ),
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "Atomics", JS_PROP_CONFIGURABLE ),
- };
- static const JSCFunctionListEntry js_atomics_obj[] = {
- JS_OBJECT_DEF("Atomics", js_atomics_funcs, countof(js_atomics_funcs), JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE ),
- };
- static int JS_AddIntrinsicAtomics(JSContext *ctx)
- {
- /* add Atomics as autoinit object */
- return JS_SetPropertyFunctionList(ctx, ctx->global_obj, js_atomics_obj, countof(js_atomics_obj));
- }
- #endif /* CONFIG_ATOMICS */
- int JS_AddIntrinsicTypedArrays(JSContext *ctx)
- {
- JSValue typed_array_base_func, typed_array_base_proto, obj;
- int i, ret;
- obj = JS_NewCConstructor(ctx, JS_CLASS_ARRAY_BUFFER, "ArrayBuffer",
- js_array_buffer_constructor, 1, JS_CFUNC_constructor, 0,
- JS_UNDEFINED,
- js_array_buffer_funcs, countof(js_array_buffer_funcs),
- js_array_buffer_proto_funcs, countof(js_array_buffer_proto_funcs),
- 0);
- if (JS_IsException(obj))
- return -1;
- JS_FreeValue(ctx, obj);
- obj = JS_NewCConstructor(ctx, JS_CLASS_SHARED_ARRAY_BUFFER, "SharedArrayBuffer",
- js_shared_array_buffer_constructor, 1, JS_CFUNC_constructor, 0,
- JS_UNDEFINED,
- js_shared_array_buffer_funcs, countof(js_shared_array_buffer_funcs),
- js_shared_array_buffer_proto_funcs, countof(js_shared_array_buffer_proto_funcs),
- 0);
- if (JS_IsException(obj))
- return -1;
- JS_FreeValue(ctx, obj);
- typed_array_base_func =
- JS_NewCConstructor(ctx, -1, "TypedArray",
- js_typed_array_base_constructor, 0, JS_CFUNC_constructor_or_func, 0,
- JS_UNDEFINED,
- js_typed_array_base_funcs, countof(js_typed_array_base_funcs),
- js_typed_array_base_proto_funcs, countof(js_typed_array_base_proto_funcs),
- JS_NEW_CTOR_NO_GLOBAL);
- if (JS_IsException(typed_array_base_func))
- return -1;
- /* TypedArray.prototype.toString must be the same object as Array.prototype.toString */
- obj = JS_GetProperty(ctx, ctx->class_proto[JS_CLASS_ARRAY], JS_ATOM_toString);
- if (JS_IsException(obj))
- goto fail;
- /* XXX: should use alias method in JSCFunctionListEntry */ //@@@
- typed_array_base_proto = JS_GetProperty(ctx, typed_array_base_func, JS_ATOM_prototype);
- if (JS_IsException(typed_array_base_proto))
- goto fail;
- ret = JS_DefinePropertyValue(ctx, typed_array_base_proto, JS_ATOM_toString, obj,
- JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
- JS_FreeValue(ctx, typed_array_base_proto);
- if (ret < 0)
- goto fail;
-
- /* Used to squelch a -Wcast-function-type warning. */
- JSCFunctionType ft = { .generic_magic = js_typed_array_constructor };
- for(i = JS_CLASS_UINT8C_ARRAY; i < JS_CLASS_UINT8C_ARRAY + JS_TYPED_ARRAY_COUNT; i++) {
- char buf[ATOM_GET_STR_BUF_SIZE];
- const char *name;
-
- name = JS_AtomGetStr(ctx, buf, sizeof(buf),
- JS_ATOM_Uint8ClampedArray + i - JS_CLASS_UINT8C_ARRAY);
- if (i == JS_CLASS_UINT8_ARRAY) {
- obj = JS_NewCConstructor(ctx, i, name,
- ft.generic, 3, JS_CFUNC_constructor_magic, i,
- typed_array_base_func,
- js_uint8array_funcs, countof(js_uint8array_funcs),
- js_uint8array_proto_funcs, countof(js_uint8array_proto_funcs),
- 0);
- } else {
- const JSCFunctionListEntry *bpe = js_typed_array_funcs + typed_array_size_log2(i);
- obj = JS_NewCConstructor(ctx, i, name,
- ft.generic, 3, JS_CFUNC_constructor_magic, i,
- typed_array_base_func,
- bpe, 1,
- bpe, 1,
- 0);
- }
- if (JS_IsException(obj)) {
- fail:
- JS_FreeValue(ctx, typed_array_base_func);
- return -1;
- }
- JS_FreeValue(ctx, obj);
- }
- JS_FreeValue(ctx, typed_array_base_func);
- /* DataView */
- obj = JS_NewCConstructor(ctx, JS_CLASS_DATAVIEW, "DataView",
- js_dataview_constructor, 1, JS_CFUNC_constructor, 0,
- JS_UNDEFINED,
- NULL, 0,
- js_dataview_proto_funcs, countof(js_dataview_proto_funcs),
- 0);
- if (JS_IsException(obj))
- return -1;
- JS_FreeValue(ctx, obj);
- /* Atomics */
- #ifdef CONFIG_ATOMICS
- if (JS_AddIntrinsicAtomics(ctx))
- return -1;
- #endif
- return 0;
- }
- /* WeakRef */
- typedef struct JSWeakRefData {
- JSWeakRefHeader weakref_header;
- JSValue target;
- } JSWeakRefData;
- static void js_weakref_finalizer(JSRuntime *rt, JSValue val)
- {
- JSWeakRefData *wrd = JS_GetOpaque(val, JS_CLASS_WEAK_REF);
- if (!wrd)
- return;
- js_weakref_free(rt, wrd->target);
- list_del(&wrd->weakref_header.link);
- js_free_rt(rt, wrd);
- }
- static void weakref_delete_weakref(JSRuntime *rt, JSWeakRefHeader *wh)
- {
- JSWeakRefData *wrd = container_of(wh, JSWeakRefData, weakref_header);
- if (!js_weakref_is_live(wrd->target)) {
- js_weakref_free(rt, wrd->target);
- wrd->target = JS_UNDEFINED;
- }
- }
- static JSValue js_weakref_constructor(JSContext *ctx, JSValueConst new_target,
- int argc, JSValueConst *argv)
- {
- JSValueConst arg;
- JSValue obj;
- if (JS_IsUndefined(new_target))
- return JS_ThrowTypeError(ctx, "constructor requires 'new'");
- arg = argv[0];
- if (!js_weakref_is_target(arg))
- return JS_ThrowTypeError(ctx, "invalid target");
- obj = js_create_from_ctor(ctx, new_target, JS_CLASS_WEAK_REF);
- if (JS_IsException(obj))
- return JS_EXCEPTION;
- JSWeakRefData *wrd = js_mallocz(ctx, sizeof(*wrd));
- if (!wrd) {
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- wrd->target = js_weakref_new(ctx, arg);
- wrd->weakref_header.weakref_type = JS_WEAKREF_TYPE_WEAKREF;
- list_add_tail(&wrd->weakref_header.link, &ctx->rt->weakref_list);
- JS_SetOpaque(obj, wrd);
- return obj;
- }
- static JSValue js_weakref_deref(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
- {
- JSWeakRefData *wrd = JS_GetOpaque2(ctx, this_val, JS_CLASS_WEAK_REF);
- if (!wrd)
- return JS_EXCEPTION;
- if (js_weakref_is_live(wrd->target))
- return JS_DupValue(ctx, wrd->target);
- else
- return JS_UNDEFINED;
- }
- static const JSCFunctionListEntry js_weakref_proto_funcs[] = {
- JS_CFUNC_DEF("deref", 0, js_weakref_deref ),
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "WeakRef", JS_PROP_CONFIGURABLE ),
- };
- static const JSClassShortDef js_weakref_class_def[] = {
- { JS_ATOM_WeakRef, js_weakref_finalizer, NULL }, /* JS_CLASS_WEAK_REF */
- };
- typedef struct JSFinRecEntry {
- struct list_head link;
- JSValue target;
- JSValue held_val;
- JSValue token;
- } JSFinRecEntry;
- typedef struct JSFinalizationRegistryData {
- JSWeakRefHeader weakref_header;
- struct list_head entries; /* list of JSFinRecEntry.link */
- JSContext *realm;
- JSValue cb;
- } JSFinalizationRegistryData;
- static void js_finrec_finalizer(JSRuntime *rt, JSValue val)
- {
- JSFinalizationRegistryData *frd = JS_GetOpaque(val, JS_CLASS_FINALIZATION_REGISTRY);
- if (frd) {
- struct list_head *el, *el1;
- list_for_each_safe(el, el1, &frd->entries) {
- JSFinRecEntry *fre = list_entry(el, JSFinRecEntry, link);
- js_weakref_free(rt, fre->target);
- js_weakref_free(rt, fre->token);
- JS_FreeValueRT(rt, fre->held_val);
- js_free_rt(rt, fre);
- }
- JS_FreeValueRT(rt, frd->cb);
- JS_FreeContext(frd->realm);
- list_del(&frd->weakref_header.link);
- js_free_rt(rt, frd);
- }
- }
- static void js_finrec_mark(JSRuntime *rt, JSValueConst val,
- JS_MarkFunc *mark_func)
- {
- JSFinalizationRegistryData *frd = JS_GetOpaque(val, JS_CLASS_FINALIZATION_REGISTRY);
- struct list_head *el;
- if (frd) {
- list_for_each(el, &frd->entries) {
- JSFinRecEntry *fre = list_entry(el, JSFinRecEntry, link);
- JS_MarkValue(rt, fre->held_val, mark_func);
- }
- JS_MarkValue(rt, frd->cb, mark_func);
- mark_func(rt, &frd->realm->header);
- }
- }
- static JSValue js_finrec_job(JSContext *ctx, int argc, JSValueConst *argv)
- {
- return JS_Call(ctx, argv[0], JS_UNDEFINED, 1, &argv[1]);
- }
- static void finrec_delete_weakref(JSRuntime *rt, JSWeakRefHeader *wh)
- {
- JSFinalizationRegistryData *frd = container_of(wh, JSFinalizationRegistryData, weakref_header);
- struct list_head *el, *el1;
- list_for_each_safe(el, el1, &frd->entries) {
- JSFinRecEntry *fre = list_entry(el, JSFinRecEntry, link);
- if (!js_weakref_is_live(fre->token)) {
- js_weakref_free(rt, fre->token);
- fre->token = JS_UNDEFINED;
- }
- if (!js_weakref_is_live(fre->target)) {
- JSValueConst args[2];
- args[0] = frd->cb;
- args[1] = fre->held_val;
- /* no exception is raised to avoid recursing into the GC */
- JS_EnqueueJob2(frd->realm, js_finrec_job, 2, args, TRUE);
-
- js_weakref_free(rt, fre->target);
- js_weakref_free(rt, fre->token);
- JS_FreeValueRT(rt, fre->held_val);
- list_del(&fre->link);
- js_free_rt(rt, fre);
- }
- }
- }
- static JSValue js_finrec_constructor(JSContext *ctx, JSValueConst new_target,
- int argc, JSValueConst *argv)
- {
- JSValueConst cb;
- JSValue obj;
- JSFinalizationRegistryData *frd;
-
- if (JS_IsUndefined(new_target))
- return JS_ThrowTypeError(ctx, "constructor requires 'new'");
- cb = argv[0];
- if (!JS_IsFunction(ctx, cb))
- return JS_ThrowTypeError(ctx, "argument must be a function");
- obj = js_create_from_ctor(ctx, new_target, JS_CLASS_FINALIZATION_REGISTRY);
- if (JS_IsException(obj))
- return JS_EXCEPTION;
- frd = js_mallocz(ctx, sizeof(*frd));
- if (!frd) {
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
- frd->weakref_header.weakref_type = JS_WEAKREF_TYPE_FINREC;
- list_add_tail(&frd->weakref_header.link, &ctx->rt->weakref_list);
- init_list_head(&frd->entries);
- frd->realm = JS_DupContext(ctx);
- frd->cb = JS_DupValue(ctx, cb);
- JS_SetOpaque(obj, frd);
- return obj;
- }
- static JSValue js_finrec_register(JSContext *ctx, JSValueConst this_val,
- int argc, JSValueConst *argv)
- {
- JSValueConst target, held_val, token;
- JSFinalizationRegistryData *frd;
- JSFinRecEntry *fre;
- frd = JS_GetOpaque2(ctx, this_val, JS_CLASS_FINALIZATION_REGISTRY);
- if (!frd)
- return JS_EXCEPTION;
- target = argv[0];
- held_val = argv[1];
- token = argc > 2 ? argv[2] : JS_UNDEFINED;
- if (!js_weakref_is_target(target))
- return JS_ThrowTypeError(ctx, "invalid target");
- if (js_same_value(ctx, target, held_val))
- return JS_ThrowTypeError(ctx, "held value cannot be the target");
- if (!JS_IsUndefined(token) && !js_weakref_is_target(token))
- return JS_ThrowTypeError(ctx, "invalid unregister token");
- fre = js_malloc(ctx, sizeof(*fre));
- if (!fre)
- return JS_EXCEPTION;
- fre->target = js_weakref_new(ctx, target);
- fre->held_val = JS_DupValue(ctx, held_val);
- fre->token = js_weakref_new(ctx, token);
- list_add_tail(&fre->link, &frd->entries);
- return JS_UNDEFINED;
- }
- static JSValue js_finrec_unregister(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
- {
- JSFinalizationRegistryData *frd = JS_GetOpaque2(ctx, this_val, JS_CLASS_FINALIZATION_REGISTRY);
- JSValueConst token;
- BOOL removed;
- struct list_head *el, *el1;
- if (!frd)
- return JS_EXCEPTION;
- token = argv[0];
- if (!js_weakref_is_target(token))
- return JS_ThrowTypeError(ctx, "invalid unregister token");
- removed = FALSE;
- list_for_each_safe(el, el1, &frd->entries) {
- JSFinRecEntry *fre = list_entry(el, JSFinRecEntry, link);
- if (js_weakref_is_live(fre->token) && js_same_value(ctx, fre->token, token)) {
- js_weakref_free(ctx->rt, fre->target);
- js_weakref_free(ctx->rt, fre->token);
- JS_FreeValue(ctx, fre->held_val);
- list_del(&fre->link);
- js_free(ctx, fre);
- removed = TRUE;
- }
- }
- return JS_NewBool(ctx, removed);
- }
- static const JSCFunctionListEntry js_finrec_proto_funcs[] = {
- JS_CFUNC_DEF("register", 2, js_finrec_register ),
- JS_CFUNC_DEF("unregister", 1, js_finrec_unregister ),
- JS_PROP_STRING_DEF("[Symbol.toStringTag]", "FinalizationRegistry", JS_PROP_CONFIGURABLE ),
- };
- static const JSClassShortDef js_finrec_class_def[] = {
- { JS_ATOM_FinalizationRegistry, js_finrec_finalizer, js_finrec_mark }, /* JS_CLASS_FINALIZATION_REGISTRY */
- };
- int JS_AddIntrinsicWeakRef(JSContext *ctx)
- {
- JSRuntime *rt = ctx->rt;
- JSValue obj;
-
- /* WeakRef */
- if (!JS_IsRegisteredClass(rt, JS_CLASS_WEAK_REF)) {
- if (init_class_range(rt, js_weakref_class_def, JS_CLASS_WEAK_REF,
- countof(js_weakref_class_def)))
- return -1;
- }
- obj = JS_NewCConstructor(ctx, JS_CLASS_WEAK_REF, "WeakRef",
- js_weakref_constructor, 1, JS_CFUNC_constructor_or_func, 0,
- JS_UNDEFINED,
- NULL, 0,
- js_weakref_proto_funcs, countof(js_weakref_proto_funcs),
- 0);
- if (JS_IsException(obj))
- return -1;
- JS_FreeValue(ctx, obj);
- /* FinalizationRegistry */
- if (!JS_IsRegisteredClass(rt, JS_CLASS_FINALIZATION_REGISTRY)) {
- if (init_class_range(rt, js_finrec_class_def, JS_CLASS_FINALIZATION_REGISTRY,
- countof(js_finrec_class_def)))
- return -1;
- }
- obj = JS_NewCConstructor(ctx, JS_CLASS_FINALIZATION_REGISTRY, "FinalizationRegistry",
- js_finrec_constructor, 1, JS_CFUNC_constructor_or_func, 0,
- JS_UNDEFINED,
- NULL, 0,
- js_finrec_proto_funcs, countof(js_finrec_proto_funcs),
- 0);
- if (JS_IsException(obj))
- return -1;
- JS_FreeValue(ctx, obj);
- return 0;
- }
|