compiler_test.go 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874
  1. package goja
  2. import (
  3. "os"
  4. "sync"
  5. "testing"
  6. )
  7. const TESTLIB = `
  8. function $ERROR(message) {
  9. throw new Error(message);
  10. }
  11. function Test262Error() {
  12. }
  13. function assert(mustBeTrue, message) {
  14. if (mustBeTrue === true) {
  15. return;
  16. }
  17. if (message === undefined) {
  18. message = 'Expected true but got ' + String(mustBeTrue);
  19. }
  20. $ERROR(message);
  21. }
  22. assert._isSameValue = function (a, b) {
  23. if (a === b) {
  24. // Handle +/-0 vs. -/+0
  25. return a !== 0 || 1 / a === 1 / b;
  26. }
  27. // Handle NaN vs. NaN
  28. return a !== a && b !== b;
  29. };
  30. assert.sameValue = function (actual, expected, message) {
  31. if (assert._isSameValue(actual, expected)) {
  32. return;
  33. }
  34. if (message === undefined) {
  35. message = '';
  36. } else {
  37. message += ' ';
  38. }
  39. message += 'Expected SameValue(«' + String(actual) + '», «' + String(expected) + '») to be true';
  40. $ERROR(message);
  41. };
  42. assert.throws = function (expectedErrorConstructor, func, message) {
  43. if (typeof func !== "function") {
  44. $ERROR('assert.throws requires two arguments: the error constructor ' +
  45. 'and a function to run');
  46. return;
  47. }
  48. if (message === undefined) {
  49. message = '';
  50. } else {
  51. message += ' ';
  52. }
  53. try {
  54. func();
  55. } catch (thrown) {
  56. if (typeof thrown !== 'object' || thrown === null) {
  57. message += 'Thrown value was not an object!';
  58. $ERROR(message);
  59. } else if (thrown.constructor !== expectedErrorConstructor) {
  60. message += 'Expected a ' + expectedErrorConstructor.name + ' but got a ' + thrown.constructor.name;
  61. $ERROR(message);
  62. }
  63. return;
  64. }
  65. message += 'Expected a ' + expectedErrorConstructor.name + ' to be thrown but no exception was thrown at all';
  66. $ERROR(message);
  67. };
  68. function compareArray(a, b) {
  69. if (b.length !== a.length) {
  70. return false;
  71. }
  72. for (var i = 0; i < a.length; i++) {
  73. if (b[i] !== a[i]) {
  74. return false;
  75. }
  76. }
  77. return true;
  78. }
  79. `
  80. const TESTLIBX = `
  81. function looksNative(fn) {
  82. return /native code/.test(Function.prototype.toString.call(fn));
  83. }
  84. function deepEqual(a, b) {
  85. if (typeof a === "object") {
  86. if (typeof b === "object") {
  87. if (a === b) {
  88. return true;
  89. }
  90. if (Reflect.getPrototypeOf(a) !== Reflect.getPrototypeOf(b)) {
  91. return false;
  92. }
  93. var keysA = Object.keys(a);
  94. var keysB = Object.keys(b);
  95. if (keysA.length !== keysB.length) {
  96. return false;
  97. }
  98. if (!compareArray(keysA.sort(), keysB.sort())) {
  99. return false;
  100. }
  101. for (var i = 0; i < keysA.length; i++) {
  102. var key = keysA[i];
  103. if (!deepEqual(a[key], b[key])) {
  104. return false;
  105. }
  106. }
  107. return true;
  108. } else {
  109. return false;
  110. }
  111. }
  112. return assert._isSameValue(a, b);
  113. }
  114. function assertStack(e, expected) {
  115. const lines = e.stack.split('\n');
  116. assert.sameValue(lines.length, expected.length + 2, "Stack lengths mismatch");
  117. let lnum = 1;
  118. for (const [file, func, line, col] of expected) {
  119. const expLine = func === "" ?
  120. "\tat " + file + ":" + line + ":" + col + "(" :
  121. "\tat " + func + " (" + file + ":" + line + ":" + col + "(";
  122. assert.sameValue(lines[lnum].substring(0, expLine.length), expLine, "line " + lnum);
  123. lnum++;
  124. }
  125. }
  126. `
  127. var (
  128. // The reason it's implemented this way rather than just as _testLib = MustCompile(...)
  129. // is because when you try to debug the compiler and set a breakpoint it gets triggered during the
  130. // initialisation which is annoying.
  131. _testLib, _testLibX *Program
  132. testLibOnce, testLibXOnce sync.Once
  133. )
  134. func testLib() *Program {
  135. testLibOnce.Do(func() {
  136. _testLib = MustCompile("testlib.js", TESTLIB, false)
  137. })
  138. return _testLib
  139. }
  140. func testLibX() *Program {
  141. testLibXOnce.Do(func() {
  142. _testLibX = MustCompile("testlibx.js", TESTLIBX, false)
  143. })
  144. return _testLibX
  145. }
  146. func (r *Runtime) testPrg(p *Program, expectedResult Value, t *testing.T) {
  147. p.dumpCode(t.Logf)
  148. v, err := r.RunProgram(p)
  149. if err != nil {
  150. if ex, ok := err.(*Exception); ok {
  151. t.Fatalf("Exception: %v", ex.String())
  152. }
  153. }
  154. vm := r.vm
  155. t.Logf("stack size: %d", len(vm.stack))
  156. t.Logf("stashAllocs: %d", vm.stashAllocs)
  157. if v == nil && expectedResult != nil || !v.SameAs(expectedResult) {
  158. t.Fatalf("Result: %+v, expected: %+v", v, expectedResult)
  159. }
  160. if vm.sp != 0 {
  161. t.Fatalf("sp: %d", vm.sp)
  162. }
  163. if l := len(vm.iterStack); l > 0 {
  164. t.Fatalf("iter stack is not empty: %d", l)
  165. }
  166. }
  167. func (r *Runtime) testScriptWithTestLib(script string, expectedResult Value, t *testing.T) {
  168. _, err := r.RunProgram(testLib())
  169. if err != nil {
  170. t.Fatal(err)
  171. }
  172. r.testScript(script, expectedResult, t)
  173. }
  174. func (r *Runtime) testScriptWithTestLibX(script string, expectedResult Value, t *testing.T) {
  175. _, err := r.RunProgram(testLib())
  176. if err != nil {
  177. t.Fatal(err)
  178. }
  179. _, err = r.RunProgram(testLibX())
  180. if err != nil {
  181. t.Fatal(err)
  182. }
  183. r.testScript(script, expectedResult, t)
  184. }
  185. func (r *Runtime) testScript(script string, expectedResult Value, t *testing.T) {
  186. r.testPrg(MustCompile("test.js", script, false), expectedResult, t)
  187. }
  188. func testScript(script string, expectedResult Value, t *testing.T) {
  189. New().testScript(script, expectedResult, t)
  190. }
  191. func testScriptWithTestLib(script string, expectedResult Value, t *testing.T) {
  192. New().testScriptWithTestLib(script, expectedResult, t)
  193. }
  194. func testScriptWithTestLibX(script string, expectedResult Value, t *testing.T) {
  195. New().testScriptWithTestLibX(script, expectedResult, t)
  196. }
  197. func (r *Runtime) testAsyncFunc(src string, expectedResult Value, t *testing.T) {
  198. v, err := r.RunScript("test.js", "(async function test() {"+src+"\n})()")
  199. if err != nil {
  200. t.Fatal(err)
  201. }
  202. promise := v.Export().(*Promise)
  203. switch s := promise.State(); s {
  204. case PromiseStateFulfilled:
  205. if res := promise.Result(); res == nil && expectedResult != nil || !res.SameAs(expectedResult) {
  206. t.Fatalf("Result: %+v, expected: %+v", res, expectedResult)
  207. }
  208. case PromiseStateRejected:
  209. res := promise.Result()
  210. if resObj, ok := res.(*Object); ok {
  211. if stack := resObj.Get("stack"); stack != nil {
  212. t.Fatal(stack.String())
  213. }
  214. }
  215. t.Fatal(res.String())
  216. default:
  217. t.Fatalf("Unexpected promise state: %v", s)
  218. }
  219. }
  220. func (r *Runtime) testAsyncFuncWithTestLib(src string, expectedResult Value, t *testing.T) {
  221. _, err := r.RunProgram(testLib())
  222. if err != nil {
  223. t.Fatal(err)
  224. }
  225. r.testAsyncFunc(src, expectedResult, t)
  226. }
  227. func (r *Runtime) testAsyncFuncWithTestLibX(src string, expectedResult Value, t *testing.T) {
  228. _, err := r.RunProgram(testLib())
  229. if err != nil {
  230. t.Fatal(err)
  231. }
  232. _, err = r.RunProgram(testLibX())
  233. if err != nil {
  234. t.Fatal(err)
  235. }
  236. r.testAsyncFunc(src, expectedResult, t)
  237. }
  238. func testAsyncFunc(src string, expectedResult Value, t *testing.T) {
  239. New().testAsyncFunc(src, expectedResult, t)
  240. }
  241. func testAsyncFuncWithTestLib(src string, expectedResult Value, t *testing.T) {
  242. New().testAsyncFuncWithTestLib(src, expectedResult, t)
  243. }
  244. func testAsyncFuncWithTestLibX(src string, expectedResult Value, t *testing.T) {
  245. New().testAsyncFuncWithTestLibX(src, expectedResult, t)
  246. }
  247. func TestEmptyProgram(t *testing.T) {
  248. const SCRIPT = `
  249. `
  250. testScript(SCRIPT, _undefined, t)
  251. }
  252. func TestResultEmptyBlock(t *testing.T) {
  253. const SCRIPT = `
  254. undefined;
  255. {}
  256. `
  257. testScript(SCRIPT, _undefined, t)
  258. }
  259. func TestResultVarDecl(t *testing.T) {
  260. const SCRIPT = `
  261. 7; var x = 1;
  262. `
  263. testScript(SCRIPT, valueInt(7), t)
  264. }
  265. func TestResultLexDecl(t *testing.T) {
  266. const SCRIPT = `
  267. 7; {let x = 1};
  268. `
  269. testScript(SCRIPT, valueInt(7), t)
  270. }
  271. func TestResultLexDeclBreak(t *testing.T) {
  272. const SCRIPT = `
  273. L:{ 7; {let x = 1; break L;}};
  274. `
  275. testScript(SCRIPT, valueInt(7), t)
  276. }
  277. func TestResultLexDeclNested(t *testing.T) {
  278. const SCRIPT = `
  279. 7; {let x = (function() { return eval("8; {let y = 9}")})()};
  280. `
  281. testScript(SCRIPT, valueInt(7), t)
  282. }
  283. func TestErrorProto(t *testing.T) {
  284. const SCRIPT = `
  285. var e = new TypeError();
  286. e.name;
  287. `
  288. testScript(SCRIPT, asciiString("TypeError"), t)
  289. }
  290. func TestThis1(t *testing.T) {
  291. const SCRIPT = `
  292. function independent() {
  293. return this.prop;
  294. }
  295. var o = {};
  296. o.b = {g: independent, prop: 42};
  297. o.b.g();
  298. `
  299. testScript(SCRIPT, intToValue(42), t)
  300. }
  301. func TestThis2(t *testing.T) {
  302. const SCRIPT = `
  303. var o = {
  304. prop: 37,
  305. f: function() {
  306. return this.prop;
  307. }
  308. };
  309. o.f();
  310. `
  311. testScript(SCRIPT, intToValue(37), t)
  312. }
  313. func TestThisStrict(t *testing.T) {
  314. const SCRIPT = `
  315. "use strict";
  316. Object.defineProperty(Object.prototype, "x", { get: function () { return this; } });
  317. (5).x === 5;
  318. `
  319. testScript(SCRIPT, valueTrue, t)
  320. }
  321. func TestThisNoStrict(t *testing.T) {
  322. const SCRIPT = `
  323. Object.defineProperty(Object.prototype, "x", { get: function () { return this; } });
  324. (5).x == 5;
  325. `
  326. testScript(SCRIPT, valueTrue, t)
  327. }
  328. func TestNestedFuncVarResolution(t *testing.T) {
  329. const SCRIPT = `
  330. (function outer() {
  331. var v = 42;
  332. function inner() {
  333. return v;
  334. }
  335. return inner();
  336. })();
  337. `
  338. testScript(SCRIPT, valueInt(42), t)
  339. }
  340. func TestNestedFuncVarResolution1(t *testing.T) {
  341. const SCRIPT = `
  342. function outer(argOuter) {
  343. var called = 0;
  344. var inner = function(argInner) {
  345. if (arguments.length !== 1) {
  346. throw new Error();
  347. }
  348. called++;
  349. if (argOuter !== 1) {
  350. throw new Error("argOuter");
  351. }
  352. if (argInner !== 2) {
  353. throw new Error("argInner");
  354. }
  355. };
  356. inner(2);
  357. }
  358. outer(1);
  359. `
  360. testScript(SCRIPT, _undefined, t)
  361. }
  362. func TestCallFewerArgs(t *testing.T) {
  363. const SCRIPT = `
  364. function A(a, b, c) {
  365. return String(a) + " " + String(b) + " " + String(c);
  366. }
  367. A(1, 2);
  368. `
  369. testScript(SCRIPT, asciiString("1 2 undefined"), t)
  370. }
  371. func TestCallFewerArgsClosureNoArgs(t *testing.T) {
  372. const SCRIPT = `
  373. var x;
  374. function A(a, b, c) {
  375. var y = a;
  376. x = function() { return " " + y };
  377. return String(a) + " " + String(b) + " " + String(c);
  378. }
  379. A(1, 2) + x();
  380. `
  381. testScript(SCRIPT, asciiString("1 2 undefined 1"), t)
  382. }
  383. func TestCallFewerArgsClosureArgs(t *testing.T) {
  384. const SCRIPT = `
  385. var x;
  386. function A(a, b, c) {
  387. var y = b;
  388. x = function() { return " " + a + " " + y };
  389. return String(a) + " " + String(b) + " " + String(c);
  390. }
  391. A(1, 2) + x();
  392. `
  393. testScript(SCRIPT, asciiString("1 2 undefined 1 2"), t)
  394. }
  395. func TestCallMoreArgs(t *testing.T) {
  396. const SCRIPT = `
  397. function A(a, b) {
  398. var c = 4;
  399. return a - b + c;
  400. }
  401. A(1, 2, 3);
  402. `
  403. testScript(SCRIPT, intToValue(3), t)
  404. }
  405. func TestCallMoreArgsDynamic(t *testing.T) {
  406. const SCRIPT = `
  407. function A(a, b) {
  408. var c = 4;
  409. if (false) {
  410. eval("");
  411. }
  412. return a - b + c;
  413. }
  414. A(1, 2, 3);
  415. `
  416. testScript(SCRIPT, intToValue(3), t)
  417. }
  418. func TestCallLessArgsDynamic(t *testing.T) {
  419. const SCRIPT = `
  420. function A(a, b, c) {
  421. // Make it stashful
  422. function B() {
  423. return a;
  424. }
  425. return String(a) + " " + String(b) + " " + String(c);
  426. }
  427. A(1, 2);
  428. `
  429. testScript(SCRIPT, asciiString("1 2 undefined"), t)
  430. }
  431. func TestCallLessArgsDynamicLocalVar(t *testing.T) {
  432. const SCRIPT = `
  433. function f(param) {
  434. var a = 42;
  435. if (false) {
  436. eval("");
  437. }
  438. return a;
  439. }
  440. f();
  441. `
  442. testScript(SCRIPT, intToValue(42), t)
  443. }
  444. /*
  445. func TestFib(t *testing.T) {
  446. testScript(TEST_FIB, valueInt(9227465), t)
  447. }
  448. */
  449. func TestNativeCall(t *testing.T) {
  450. const SCRIPT = `
  451. var o = Object(1);
  452. Object.defineProperty(o, "test", {value: 42});
  453. o.test;
  454. `
  455. testScript(SCRIPT, intToValue(42), t)
  456. }
  457. func TestJSCall(t *testing.T) {
  458. const SCRIPT = `
  459. function getter() {
  460. return this.x;
  461. }
  462. var o = Object(1);
  463. o.x = 42;
  464. Object.defineProperty(o, "test", {get: getter});
  465. o.test;
  466. `
  467. testScript(SCRIPT, intToValue(42), t)
  468. }
  469. func TestLoop1(t *testing.T) {
  470. const SCRIPT = `
  471. function A() {
  472. var x = 1;
  473. for (var i = 0; i < 1; i++) {
  474. var x = 2;
  475. }
  476. return x;
  477. }
  478. A();
  479. `
  480. testScript(SCRIPT, intToValue(2), t)
  481. }
  482. func TestLoopBreak(t *testing.T) {
  483. const SCRIPT = `
  484. function A() {
  485. var x = 1;
  486. for (var i = 0; i < 1; i++) {
  487. break;
  488. var x = 2;
  489. }
  490. return x;
  491. }
  492. A();
  493. `
  494. testScript(SCRIPT, intToValue(1), t)
  495. }
  496. func TestForLoopOptionalExpr(t *testing.T) {
  497. const SCRIPT = `
  498. function A() {
  499. var x = 1;
  500. for (;;) {
  501. break;
  502. var x = 2;
  503. }
  504. return x;
  505. }
  506. A();
  507. `
  508. testScript(SCRIPT, intToValue(1), t)
  509. }
  510. func TestBlockBreak(t *testing.T) {
  511. const SCRIPT = `
  512. var rv = 0;
  513. B1: {
  514. rv = 1;
  515. B2: {
  516. rv = 2;
  517. break B1;
  518. }
  519. rv = 3;
  520. }
  521. rv;
  522. `
  523. testScript(SCRIPT, intToValue(2), t)
  524. }
  525. func TestTry(t *testing.T) {
  526. const SCRIPT = `
  527. function A() {
  528. var x = 1;
  529. try {
  530. x = 2;
  531. } catch(e) {
  532. x = 3;
  533. } finally {
  534. x = 4;
  535. }
  536. return x;
  537. }
  538. A();
  539. `
  540. testScript(SCRIPT, intToValue(4), t)
  541. }
  542. func TestTryOptionalCatchBinding(t *testing.T) {
  543. const SCRIPT = `
  544. try {
  545. throw null;
  546. } catch {
  547. }
  548. `
  549. testScript(SCRIPT, _undefined, t)
  550. }
  551. func TestTryCatch(t *testing.T) {
  552. const SCRIPT = `
  553. function A() {
  554. var x;
  555. try {
  556. throw 4;
  557. } catch(e) {
  558. x = e;
  559. }
  560. return x;
  561. }
  562. A();
  563. `
  564. testScript(SCRIPT, intToValue(4), t)
  565. }
  566. func TestTryCatchDirectEval(t *testing.T) {
  567. const SCRIPT = `
  568. function A() {
  569. var x;
  570. try {
  571. throw 4;
  572. } catch(e) {
  573. eval("x = e");
  574. }
  575. return x;
  576. }
  577. A();
  578. `
  579. testScript(SCRIPT, intToValue(4), t)
  580. }
  581. func TestTryExceptionInCatch(t *testing.T) {
  582. const SCRIPT = `
  583. function A() {
  584. var x;
  585. try {
  586. throw 4;
  587. } catch(e) {
  588. throw 5;
  589. }
  590. return x;
  591. }
  592. var rv;
  593. try {
  594. A();
  595. } catch (e) {
  596. rv = e;
  597. }
  598. rv;
  599. `
  600. testScript(SCRIPT, intToValue(5), t)
  601. }
  602. func TestTryContinueInCatch(t *testing.T) {
  603. const SCRIPT = `
  604. var c3 = 0, fin3 = 0;
  605. while (c3 < 2) {
  606. try {
  607. throw "ex1";
  608. } catch(er1) {
  609. c3 += 1;
  610. continue;
  611. } finally {
  612. fin3 = 1;
  613. }
  614. fin3 = 0;
  615. }
  616. fin3;
  617. `
  618. testScript(SCRIPT, intToValue(1), t)
  619. }
  620. func TestContinueInWith(t *testing.T) {
  621. const SCRIPT = `
  622. var x;
  623. var o = {x: 0};
  624. for (var i = 0; i < 2; i++) {
  625. with(o) {
  626. x = i;
  627. if (i === 0) {
  628. continue;
  629. }
  630. }
  631. break;
  632. }
  633. x;
  634. `
  635. testScript(SCRIPT, _undefined, t)
  636. }
  637. func TestTryContinueInFinally(t *testing.T) {
  638. const SCRIPT = `
  639. var c3 = 0, fin3 = 0;
  640. while (c3 < 2) {
  641. try {
  642. throw "ex1";
  643. } catch(er1) {
  644. c3 += 1;
  645. } finally {
  646. fin3 = 1;
  647. continue;
  648. }
  649. fin3 = 0;
  650. }
  651. fin3;
  652. `
  653. testScript(SCRIPT, intToValue(1), t)
  654. }
  655. func TestTryBreakFinallyContinue(t *testing.T) {
  656. const SCRIPT = `
  657. for (var i = 0; i < 3; i++) {
  658. try {
  659. break;
  660. } finally {
  661. continue;
  662. }
  663. }
  664. `
  665. testScript(SCRIPT, _undefined, t)
  666. }
  667. func TestTryBreakFinallyContinueWithResult(t *testing.T) {
  668. const SCRIPT = `
  669. for (var i = 0; i < 3; i++) {
  670. try {
  671. true;
  672. break;
  673. } finally {
  674. continue;
  675. }
  676. }
  677. `
  678. testScript(SCRIPT, _undefined, t)
  679. }
  680. func TestTryBreakFinallyContinueWithResult1(t *testing.T) {
  681. const SCRIPT = `
  682. for (var i = 0; i < 3; i++) {
  683. try {
  684. true;
  685. break;
  686. } finally {
  687. var x = 1;
  688. continue;
  689. }
  690. }
  691. `
  692. testScript(SCRIPT, _undefined, t)
  693. }
  694. func TestTryBreakFinallyContinueWithResultNested(t *testing.T) {
  695. const SCRIPT = `
  696. LOOP:
  697. for (var i = 0; i < 3; i++) {
  698. try {
  699. if (true) {
  700. false; break;
  701. }
  702. } finally {
  703. if (true) {
  704. true; continue;
  705. }
  706. }
  707. }
  708. `
  709. testScript(SCRIPT, valueTrue, t)
  710. }
  711. func TestTryBreakOuterFinallyContinue(t *testing.T) {
  712. const SCRIPT = `
  713. let iCount = 0, jCount = 0;
  714. OUTER: for (let i = 0; i < 1; i++) {
  715. iCount++;
  716. for (let j = 0; j < 2; j++) {
  717. jCount++;
  718. try {
  719. break OUTER;
  720. } finally {
  721. continue;
  722. }
  723. }
  724. }
  725. ""+iCount+jCount;
  726. `
  727. testScript(SCRIPT, asciiString("12"), t)
  728. }
  729. func TestTryIllegalContinueWithFinallyOverride(t *testing.T) {
  730. const SCRIPT = `
  731. L: {
  732. while (Math.random() > 0.5) {
  733. try {
  734. continue L;
  735. } finally {
  736. break;
  737. }
  738. }
  739. }
  740. `
  741. _, err := Compile("", SCRIPT, false)
  742. if err == nil {
  743. t.Fatal("expected error")
  744. }
  745. }
  746. func TestTryIllegalContinueWithFinallyOverrideNoLabel(t *testing.T) {
  747. const SCRIPT = `
  748. L: {
  749. try {
  750. continue;
  751. } finally {
  752. break L;
  753. }
  754. }
  755. `
  756. _, err := Compile("", SCRIPT, false)
  757. if err == nil {
  758. t.Fatal("expected error")
  759. }
  760. }
  761. func TestTryIllegalContinueWithFinallyOverrideDummy(t *testing.T) {
  762. const SCRIPT = `
  763. L: {
  764. while (false) {
  765. try {
  766. continue L;
  767. } finally {
  768. break;
  769. }
  770. }
  771. }
  772. `
  773. _, err := Compile("", SCRIPT, false)
  774. if err == nil {
  775. t.Fatal("expected error")
  776. }
  777. }
  778. func TestTryNoResult(t *testing.T) {
  779. const SCRIPT = `
  780. true;
  781. L:
  782. try {
  783. break L;
  784. } finally {
  785. }
  786. `
  787. testScript(SCRIPT, _undefined, t)
  788. }
  789. func TestCatchLexicalEnv(t *testing.T) {
  790. const SCRIPT = `
  791. function F() {
  792. try {
  793. throw 1;
  794. } catch (e) {
  795. var x = e;
  796. }
  797. return x;
  798. }
  799. F();
  800. `
  801. testScript(SCRIPT, intToValue(1), t)
  802. }
  803. func TestThrowType(t *testing.T) {
  804. const SCRIPT = `
  805. function Exception(message) {
  806. this.message = message;
  807. }
  808. function A() {
  809. try {
  810. throw new Exception("boo!");
  811. } catch(e) {
  812. return e;
  813. }
  814. }
  815. var thrown = A();
  816. thrown !== null && typeof thrown === "object" && thrown.constructor === Exception;
  817. `
  818. testScript(SCRIPT, valueTrue, t)
  819. }
  820. func TestThrowConstructorName(t *testing.T) {
  821. const SCRIPT = `
  822. function Exception(message) {
  823. this.message = message;
  824. }
  825. function A() {
  826. try {
  827. throw new Exception("boo!");
  828. } catch(e) {
  829. return e;
  830. }
  831. }
  832. A().constructor.name;
  833. `
  834. testScript(SCRIPT, asciiString("Exception"), t)
  835. }
  836. func TestThrowNativeConstructorName(t *testing.T) {
  837. const SCRIPT = `
  838. function A() {
  839. try {
  840. throw new TypeError();
  841. } catch(e) {
  842. return e;
  843. }
  844. }
  845. A().constructor.name;
  846. `
  847. testScript(SCRIPT, asciiString("TypeError"), t)
  848. }
  849. func TestEmptyTryNoCatch(t *testing.T) {
  850. const SCRIPT = `
  851. var called = false;
  852. try {
  853. } finally {
  854. called = true;
  855. }
  856. called;
  857. `
  858. testScript(SCRIPT, valueTrue, t)
  859. }
  860. func TestTryReturnFromCatch(t *testing.T) {
  861. const SCRIPT = `
  862. function f(o) {
  863. var x = 42;
  864. function innerf(o) {
  865. try {
  866. throw o;
  867. } catch (e) {
  868. return x;
  869. }
  870. }
  871. return innerf(o);
  872. }
  873. f({});
  874. `
  875. testScript(SCRIPT, valueInt(42), t)
  876. }
  877. func TestTryCompletionResult(t *testing.T) {
  878. const SCRIPT = `
  879. 99; do { -99; try { 39 } catch (e) { -1 } finally { break; -2 }; } while (false);
  880. `
  881. testScript(SCRIPT, _undefined, t)
  882. }
  883. func TestIfElse(t *testing.T) {
  884. const SCRIPT = `
  885. var rv;
  886. if (rv === undefined) {
  887. rv = "passed";
  888. } else {
  889. rv = "failed";
  890. }
  891. rv;
  892. `
  893. testScript(SCRIPT, asciiString("passed"), t)
  894. }
  895. func TestIfElseRetVal(t *testing.T) {
  896. const SCRIPT = `
  897. var x;
  898. if (x === undefined) {
  899. "passed";
  900. } else {
  901. "failed";
  902. }
  903. `
  904. testScript(SCRIPT, asciiString("passed"), t)
  905. }
  906. func TestWhileReturnValue(t *testing.T) {
  907. const SCRIPT = `
  908. var x = 0;
  909. while(true) {
  910. x = 1;
  911. break;
  912. }
  913. `
  914. testScript(SCRIPT, intToValue(1), t)
  915. }
  916. func TestIfElseLabel(t *testing.T) {
  917. const SCRIPT = `
  918. var x = 0;
  919. abc: if (true) {
  920. x = 1;
  921. break abc;
  922. }
  923. `
  924. testScript(SCRIPT, intToValue(1), t)
  925. }
  926. func TestIfMultipleLabels(t *testing.T) {
  927. const SCRIPT = `
  928. var x = 0;
  929. xyz:abc: if (true) {
  930. break xyz;
  931. }
  932. `
  933. testScript(SCRIPT, _undefined, t)
  934. }
  935. func TestBreakOutOfTry(t *testing.T) {
  936. const SCRIPT = `
  937. function A() {
  938. var x = 1;
  939. B: {
  940. try {
  941. x = 2;
  942. } catch(e) {
  943. x = 3;
  944. } finally {
  945. break B;
  946. x = 4;
  947. }
  948. }
  949. return x;
  950. }
  951. A();
  952. `
  953. testScript(SCRIPT, intToValue(2), t)
  954. }
  955. func TestReturnOutOfTryNested(t *testing.T) {
  956. const SCRIPT = `
  957. function A() {
  958. function nested() {
  959. try {
  960. return 1;
  961. } catch(e) {
  962. return 2;
  963. }
  964. }
  965. return nested();
  966. }
  967. A();
  968. `
  969. testScript(SCRIPT, intToValue(1), t)
  970. }
  971. func TestReturnOutOfTryWithFinally(t *testing.T) {
  972. const SCRIPT = `
  973. function test() {
  974. try {
  975. return 'Hello, world!';
  976. } finally {
  977. const dummy = 'unexpected';
  978. }
  979. }
  980. test();
  981. `
  982. testScript(SCRIPT, asciiString("Hello, world!"), t)
  983. }
  984. func TestContinueLoop(t *testing.T) {
  985. const SCRIPT = `
  986. function A() {
  987. var r = 0;
  988. for (var i = 0; i < 5; i++) {
  989. if (i > 1) {
  990. continue;
  991. }
  992. r++;
  993. }
  994. return r;
  995. }
  996. A();
  997. `
  998. testScript(SCRIPT, intToValue(2), t)
  999. }
  1000. func TestContinueOutOfTry(t *testing.T) {
  1001. const SCRIPT = `
  1002. function A() {
  1003. var r = 0;
  1004. for (var i = 0; i < 5; i++) {
  1005. try {
  1006. if (i > 1) {
  1007. continue;
  1008. }
  1009. } catch(e) {
  1010. return 99;
  1011. }
  1012. r++;
  1013. }
  1014. return r;
  1015. }
  1016. A();
  1017. `
  1018. testScript(SCRIPT, intToValue(2), t)
  1019. }
  1020. func TestThisInCatch(t *testing.T) {
  1021. const SCRIPT = `
  1022. function O() {
  1023. try {
  1024. f();
  1025. } catch (e) {
  1026. this.value = e.toString();
  1027. }
  1028. }
  1029. function f() {
  1030. throw "ex";
  1031. }
  1032. var o = new O();
  1033. o.value;
  1034. `
  1035. testScript(SCRIPT, asciiString("ex"), t)
  1036. }
  1037. func TestNestedTry(t *testing.T) {
  1038. const SCRIPT = `
  1039. var ex;
  1040. try {
  1041. throw "ex1";
  1042. } catch (er1) {
  1043. try {
  1044. throw "ex2";
  1045. } catch (er1) {
  1046. ex = er1;
  1047. }
  1048. }
  1049. ex;
  1050. `
  1051. testScript(SCRIPT, asciiString("ex2"), t)
  1052. }
  1053. func TestNestedTryInStashlessFunc(t *testing.T) {
  1054. const SCRIPT = `
  1055. function f() {
  1056. var ex1, ex2;
  1057. try {
  1058. throw "ex1";
  1059. } catch (er1) {
  1060. try {
  1061. throw "ex2";
  1062. } catch (er1) {
  1063. ex2 = er1;
  1064. }
  1065. ex1 = er1;
  1066. }
  1067. return ex1 == "ex1" && ex2 == "ex2";
  1068. }
  1069. f();
  1070. `
  1071. testScript(SCRIPT, valueTrue, t)
  1072. }
  1073. func TestEvalLexicalDecl(t *testing.T) {
  1074. const SCRIPT = `
  1075. eval("let x = true; x;");
  1076. `
  1077. testScript(SCRIPT, valueTrue, t)
  1078. }
  1079. func TestEvalInCatchInStashlessFunc(t *testing.T) {
  1080. const SCRIPT = `
  1081. function f() {
  1082. var ex;
  1083. try {
  1084. throw "ex1";
  1085. } catch (er1) {
  1086. eval("ex = er1");
  1087. }
  1088. return ex;
  1089. }
  1090. f();
  1091. `
  1092. testScript(SCRIPT, asciiString("ex1"), t)
  1093. }
  1094. func TestCatchClosureInStashlessFunc(t *testing.T) {
  1095. const SCRIPT = `
  1096. function f() {
  1097. var ex;
  1098. try {
  1099. throw "ex1";
  1100. } catch (er1) {
  1101. return function() {
  1102. return er1;
  1103. }
  1104. }
  1105. }
  1106. f()();
  1107. `
  1108. testScript(SCRIPT, asciiString("ex1"), t)
  1109. }
  1110. func TestCatchVarNotUsedInStashlessFunc(t *testing.T) {
  1111. const SCRIPT = `
  1112. function f() {
  1113. var ex;
  1114. try {
  1115. throw "ex1";
  1116. } catch (er1) {
  1117. ex = "ok";
  1118. }
  1119. return ex;
  1120. }
  1121. f();
  1122. `
  1123. testScript(SCRIPT, asciiString("ok"), t)
  1124. }
  1125. func TestNew(t *testing.T) {
  1126. const SCRIPT = `
  1127. function O() {
  1128. this.x = 42;
  1129. }
  1130. new O().x;
  1131. `
  1132. testScript(SCRIPT, intToValue(42), t)
  1133. }
  1134. func TestStringConstructor(t *testing.T) {
  1135. const SCRIPT = `
  1136. function F() {
  1137. return String(33) + " " + String("cows");
  1138. }
  1139. F();
  1140. `
  1141. testScript(SCRIPT, asciiString("33 cows"), t)
  1142. }
  1143. func TestError(t *testing.T) {
  1144. const SCRIPT = `
  1145. function F() {
  1146. return new Error("test");
  1147. }
  1148. var e = F();
  1149. e.message == "test" && e.name == "Error";
  1150. `
  1151. testScript(SCRIPT, valueTrue, t)
  1152. }
  1153. func TestTypeError(t *testing.T) {
  1154. const SCRIPT = `
  1155. function F() {
  1156. return new TypeError("test");
  1157. }
  1158. var e = F();
  1159. e.message == "test" && e.name == "TypeError";
  1160. `
  1161. testScript(SCRIPT, valueTrue, t)
  1162. }
  1163. func TestToString(t *testing.T) {
  1164. const SCRIPT = `
  1165. var o = {x: 42};
  1166. o.toString = function() {
  1167. return String(this.x);
  1168. }
  1169. var o1 = {};
  1170. o.toString() + " ### " + o1.toString();
  1171. `
  1172. testScript(SCRIPT, asciiString("42 ### [object Object]"), t)
  1173. }
  1174. func TestEvalOrder(t *testing.T) {
  1175. const SCRIPT = `
  1176. var o = {f: function() {return 42}, x: 0};
  1177. var trace = "";
  1178. function F1() {
  1179. trace += "First!";
  1180. return o;
  1181. }
  1182. function F2() {
  1183. trace += "Second!";
  1184. return "f";
  1185. }
  1186. function F3() {
  1187. trace += "Third!";
  1188. }
  1189. var rv = F1()[F2()](F3());
  1190. rv += trace;
  1191. rv;
  1192. `
  1193. testScript(SCRIPT, asciiString("42First!Second!Third!"), t)
  1194. }
  1195. func TestPostfixIncBracket(t *testing.T) {
  1196. const SCRIPT = `
  1197. var o = {x: 42};
  1198. var trace = "";
  1199. function F1() {
  1200. trace += "First!";
  1201. return o;
  1202. }
  1203. function F2() {
  1204. trace += "Second!";
  1205. return "x";
  1206. }
  1207. var rv = F1()[F2()]++;
  1208. rv + trace + o.x;
  1209. `
  1210. testScript(SCRIPT, asciiString("42First!Second!43"), t)
  1211. }
  1212. func TestPostfixIncDot(t *testing.T) {
  1213. const SCRIPT = `
  1214. var o = {x: 42};
  1215. var trace = "";
  1216. function F1() {
  1217. trace += "First!";
  1218. return o;
  1219. }
  1220. var rv = F1().x++;
  1221. rv + trace + o.x;
  1222. `
  1223. testScript(SCRIPT, asciiString("42First!43"), t)
  1224. }
  1225. func TestPrefixIncBracket(t *testing.T) {
  1226. const SCRIPT = `
  1227. var o = {x: 42};
  1228. var trace = "";
  1229. function F1() {
  1230. trace += "First!";
  1231. return o;
  1232. }
  1233. function F2() {
  1234. trace += "Second!";
  1235. return "x";
  1236. }
  1237. var rv = ++F1()[F2()];
  1238. rv + trace + o.x;
  1239. `
  1240. testScript(SCRIPT, asciiString("43First!Second!43"), t)
  1241. }
  1242. func TestPrefixIncDot(t *testing.T) {
  1243. const SCRIPT = `
  1244. var o = {x: 42};
  1245. var trace = "";
  1246. function F1() {
  1247. trace += "First!";
  1248. return o;
  1249. }
  1250. var rv = ++F1().x;
  1251. rv + trace + o.x;
  1252. `
  1253. testScript(SCRIPT, asciiString("43First!43"), t)
  1254. }
  1255. func TestPostDecObj(t *testing.T) {
  1256. const SCRIPT = `
  1257. var object = {valueOf: function() {return 1}};
  1258. var y = object--;
  1259. var ok = false;
  1260. if (y === 1) {
  1261. ok = true;
  1262. }
  1263. ok;
  1264. `
  1265. testScript(SCRIPT, valueTrue, t)
  1266. }
  1267. func TestPropAcc1(t *testing.T) {
  1268. const SCRIPT = `
  1269. 1..toString()
  1270. `
  1271. testScript(SCRIPT, asciiString("1"), t)
  1272. }
  1273. func TestEvalDirect(t *testing.T) {
  1274. const SCRIPT = `
  1275. var rv = false;
  1276. function foo(){ rv = true; }
  1277. var o = { };
  1278. function f() {
  1279. try {
  1280. eval("o.bar( foo() );");
  1281. } catch (e) {
  1282. }
  1283. }
  1284. f();
  1285. rv;
  1286. `
  1287. testScript(SCRIPT, valueTrue, t)
  1288. }
  1289. func TestEvalRet(t *testing.T) {
  1290. const SCRIPT = `
  1291. eval("for (var i = 0; i < 3; i++) {i}")
  1292. `
  1293. testScript(SCRIPT, valueInt(2), t)
  1294. }
  1295. func TestEvalFunctionDecl(t *testing.T) {
  1296. const SCRIPT = `
  1297. eval("function F() {}")
  1298. `
  1299. testScript(SCRIPT, _undefined, t)
  1300. }
  1301. func TestEvalFunctionExpr(t *testing.T) {
  1302. const SCRIPT = `
  1303. eval("(function F() {return 42;})")()
  1304. `
  1305. testScript(SCRIPT, intToValue(42), t)
  1306. }
  1307. func TestEvalDirectScope(t *testing.T) {
  1308. const SCRIPT = `
  1309. var __10_4_2_1_3 = "str";
  1310. function testcase() {
  1311. var __10_4_2_1_3 = "str1";
  1312. try {
  1313. throw "error";
  1314. } catch (e) {
  1315. var __10_4_2_1_3 = "str2";
  1316. return eval("__10_4_2_1_3");
  1317. }
  1318. }
  1319. testcase();
  1320. `
  1321. testScript(SCRIPT, asciiString("str2"), t)
  1322. }
  1323. func TestEvalDirectScope1(t *testing.T) {
  1324. const SCRIPT = `
  1325. 'use strict';
  1326. var __10_4_2_1_5 = "str";
  1327. function testcase() {
  1328. var __10_4_2_1_5 = "str1";
  1329. var r = eval("\
  1330. var __10_4_2_1_5 = \'str2\'; \
  1331. eval(\"\'str2\' === __10_4_2_1_5\")\
  1332. ");
  1333. return r;
  1334. }
  1335. testcase();
  1336. `
  1337. testScript(SCRIPT, valueTrue, t)
  1338. }
  1339. func TestEvalDirectCreateBinding(t *testing.T) {
  1340. const SCRIPT = `
  1341. function f() {
  1342. eval("var x = true");
  1343. return x;
  1344. }
  1345. var res = f();
  1346. var thrown = false;
  1347. try {
  1348. x;
  1349. } catch(e) {
  1350. if (e instanceof ReferenceError) {
  1351. thrown = true;
  1352. } else {
  1353. throw e;
  1354. }
  1355. }
  1356. res && thrown;
  1357. `
  1358. testScript(SCRIPT, valueTrue, t)
  1359. }
  1360. func TestEvalDirectCreateBinding1(t *testing.T) {
  1361. const SCRIPT = `
  1362. function f() {
  1363. eval("let x = 1; var y = 2; function f1() {return x};");
  1364. assert.throws(ReferenceError, function() { x });
  1365. return ""+y+f1();
  1366. }
  1367. f();
  1368. `
  1369. testScriptWithTestLib(SCRIPT, asciiString("21"), t)
  1370. }
  1371. func TestEvalDirectCreateBinding3(t *testing.T) {
  1372. const SCRIPT = `
  1373. function f() {
  1374. let x;
  1375. try {
  1376. eval("var y=1, x=2");
  1377. } catch(e) {}
  1378. return y;
  1379. }
  1380. assert.throws(ReferenceError, f);
  1381. `
  1382. testScriptWithTestLib(SCRIPT, _undefined, t)
  1383. }
  1384. func TestEvalGlobalStrict(t *testing.T) {
  1385. const SCRIPT = `
  1386. 'use strict';
  1387. var evalStr =
  1388. 'for (var x in this) {\n'+
  1389. ' if ( x === \'Math\' ) {\n'+
  1390. ' }\n'+
  1391. '}\n';
  1392. eval(evalStr);
  1393. `
  1394. testScript(SCRIPT, _undefined, t)
  1395. }
  1396. func TestEvalEmptyStrict(t *testing.T) {
  1397. const SCRIPT = `
  1398. 'use strict';
  1399. eval("");
  1400. `
  1401. testScript(SCRIPT, _undefined, t)
  1402. }
  1403. func TestEvalFuncDecl(t *testing.T) {
  1404. const SCRIPT = `
  1405. 'use strict';
  1406. var funcA = eval("function __funcA(__arg){return __arg;}; __funcA");
  1407. typeof funcA;
  1408. `
  1409. testScript(SCRIPT, asciiString("function"), t)
  1410. }
  1411. func TestGetAfterSet(t *testing.T) {
  1412. const SCRIPT = `
  1413. function f() {
  1414. var x = 1;
  1415. return x;
  1416. }
  1417. `
  1418. testScript(SCRIPT, _undefined, t)
  1419. }
  1420. func TestForLoopRet(t *testing.T) {
  1421. const SCRIPT = `
  1422. for (var i = 0; i < 20; i++) { if (i > 2) {break;} else { i }}
  1423. `
  1424. testScript(SCRIPT, _undefined, t)
  1425. }
  1426. func TestForLoopRet1(t *testing.T) {
  1427. const SCRIPT = `
  1428. for (var i = 0; i < 20; i++) { if (i > 2) {42;; {L:{break;}}} else { i }}
  1429. `
  1430. testScript(SCRIPT, intToValue(42), t)
  1431. }
  1432. func TestForInLoopRet(t *testing.T) {
  1433. const SCRIPT = `
  1434. var o = [1, 2, 3, 4];
  1435. for (var i in o) { if (i > 2) {break;} else { i }}
  1436. `
  1437. testScript(SCRIPT, _undefined, t)
  1438. }
  1439. func TestForInLoopRet1(t *testing.T) {
  1440. const SCRIPT = `
  1441. var o = {};
  1442. o.x = 1;
  1443. o.y = 2;
  1444. for (var i in o) {
  1445. true;
  1446. }
  1447. `
  1448. testScript(SCRIPT, valueTrue, t)
  1449. }
  1450. func TestDoWhileLoopRet(t *testing.T) {
  1451. const SCRIPT = `
  1452. var i = 0;
  1453. do {
  1454. if (i > 2) {
  1455. break;
  1456. } else {
  1457. i;
  1458. }
  1459. } while (i++ < 20);
  1460. `
  1461. testScript(SCRIPT, _undefined, t)
  1462. }
  1463. func TestDoWhileContinueRet(t *testing.T) {
  1464. const SCRIPT = `
  1465. var i = 0;
  1466. do {
  1467. if (i > 2) {
  1468. true;
  1469. continue;
  1470. } else {
  1471. i;
  1472. }
  1473. } while (i++ < 20);
  1474. `
  1475. testScript(SCRIPT, valueTrue, t)
  1476. }
  1477. func TestWhileLoopRet(t *testing.T) {
  1478. const SCRIPT = `
  1479. var i; while (i < 20) { if (i > 2) {break;} else { i++ }}
  1480. `
  1481. testScript(SCRIPT, _undefined, t)
  1482. }
  1483. func TestLoopRet1(t *testing.T) {
  1484. const SCRIPT = `
  1485. for (var i = 0; i < 20; i++) { }
  1486. `
  1487. testScript(SCRIPT, _undefined, t)
  1488. }
  1489. func TestInstanceof(t *testing.T) {
  1490. const SCRIPT = `
  1491. var rv;
  1492. try {
  1493. true();
  1494. } catch (e) {
  1495. rv = e instanceof TypeError;
  1496. }
  1497. rv;
  1498. `
  1499. testScript(SCRIPT, valueTrue, t)
  1500. }
  1501. func TestStrictAssign(t *testing.T) {
  1502. const SCRIPT = `
  1503. 'use strict';
  1504. var rv;
  1505. var called = false;
  1506. function F() {
  1507. called = true;
  1508. return 1;
  1509. }
  1510. try {
  1511. x = F();
  1512. } catch (e) {
  1513. rv = e instanceof ReferenceError;
  1514. }
  1515. rv + " " + called;
  1516. `
  1517. testScript(SCRIPT, asciiString("true true"), t)
  1518. }
  1519. func TestStrictScope(t *testing.T) {
  1520. const SCRIPT = `
  1521. var rv;
  1522. var called = false;
  1523. function F() {
  1524. 'use strict';
  1525. x = 1;
  1526. }
  1527. try {
  1528. F();
  1529. } catch (e) {
  1530. rv = e instanceof ReferenceError;
  1531. }
  1532. x = 1;
  1533. rv + " " + x;
  1534. `
  1535. testScript(SCRIPT, asciiString("true 1"), t)
  1536. }
  1537. func TestStringObj(t *testing.T) {
  1538. const SCRIPT = `
  1539. var s = new String("test");
  1540. s[0] + s[2] + s[1];
  1541. `
  1542. testScript(SCRIPT, asciiString("tse"), t)
  1543. }
  1544. func TestStringPrimitive(t *testing.T) {
  1545. const SCRIPT = `
  1546. var s = "test";
  1547. s[0] + s[2] + s[1];
  1548. `
  1549. testScript(SCRIPT, asciiString("tse"), t)
  1550. }
  1551. func TestCallGlobalObject(t *testing.T) {
  1552. const SCRIPT = `
  1553. var rv;
  1554. try {
  1555. this();
  1556. } catch (e) {
  1557. rv = e instanceof TypeError
  1558. }
  1559. rv;
  1560. `
  1561. testScript(SCRIPT, valueTrue, t)
  1562. }
  1563. func TestFuncLength(t *testing.T) {
  1564. const SCRIPT = `
  1565. function F(x, y) {
  1566. }
  1567. F.length
  1568. `
  1569. testScript(SCRIPT, intToValue(2), t)
  1570. }
  1571. func TestNativeFuncLength(t *testing.T) {
  1572. const SCRIPT = `
  1573. eval.length + Object.defineProperty.length + String.length
  1574. `
  1575. testScript(SCRIPT, intToValue(5), t)
  1576. }
  1577. func TestArguments(t *testing.T) {
  1578. const SCRIPT = `
  1579. function F() {
  1580. return arguments.length + " " + arguments[1];
  1581. }
  1582. F(1,2,3)
  1583. `
  1584. testScript(SCRIPT, asciiString("3 2"), t)
  1585. }
  1586. func TestArgumentsPut(t *testing.T) {
  1587. const SCRIPT = `
  1588. function F(x, y) {
  1589. arguments[0] -= arguments[1];
  1590. return x;
  1591. }
  1592. F(5, 2)
  1593. `
  1594. testScript(SCRIPT, intToValue(3), t)
  1595. }
  1596. func TestArgumentsPutStrict(t *testing.T) {
  1597. const SCRIPT = `
  1598. function F(x, y) {
  1599. 'use strict';
  1600. arguments[0] -= arguments[1];
  1601. return x;
  1602. }
  1603. F(5, 2)
  1604. `
  1605. testScript(SCRIPT, intToValue(5), t)
  1606. }
  1607. func TestArgumentsExtra(t *testing.T) {
  1608. const SCRIPT = `
  1609. function F(x, y) {
  1610. return arguments[2];
  1611. }
  1612. F(1, 2, 42)
  1613. `
  1614. testScript(SCRIPT, intToValue(42), t)
  1615. }
  1616. func TestArgumentsExist(t *testing.T) {
  1617. const SCRIPT = `
  1618. function F(x, arguments) {
  1619. return arguments;
  1620. }
  1621. F(1, 42)
  1622. `
  1623. testScript(SCRIPT, intToValue(42), t)
  1624. }
  1625. func TestArgumentsDelete(t *testing.T) {
  1626. const SCRIPT = `
  1627. function f(x) {
  1628. delete arguments[0];
  1629. arguments[0] = 42;
  1630. return x;
  1631. }
  1632. f(1)
  1633. `
  1634. testScript(SCRIPT, intToValue(1), t)
  1635. }
  1636. func TestArgumentsInEval(t *testing.T) {
  1637. const SCRIPT = `
  1638. function f() {
  1639. return eval("arguments");
  1640. }
  1641. f(1)[0];
  1642. `
  1643. testScript(SCRIPT, intToValue(1), t)
  1644. }
  1645. func TestArgumentsRedeclareInEval(t *testing.T) {
  1646. const SCRIPT = `
  1647. assert.sameValue("arguments" in this, false, "No global 'arguments' binding");
  1648. function f(p = eval("var arguments = 'param'"), arguments) {}
  1649. assert.throws(SyntaxError, f);
  1650. assert.sameValue("arguments" in this, false, "No global 'arguments' binding");
  1651. `
  1652. testScriptWithTestLib(SCRIPT, _undefined, t)
  1653. }
  1654. func TestArgumentsRedeclareArrow(t *testing.T) {
  1655. const SCRIPT = `
  1656. const oldArguments = globalThis.arguments;
  1657. let count = 0;
  1658. const f = (p = eval("var arguments = 'param'"), q = () => arguments) => {
  1659. var arguments = "local";
  1660. assert.sameValue(arguments, "local", "arguments");
  1661. assert.sameValue(q(), "param", "q");
  1662. count++;
  1663. }
  1664. f();
  1665. assert.sameValue(count, 1);
  1666. assert.sameValue(globalThis.arguments, oldArguments, "globalThis.arguments unchanged");
  1667. `
  1668. testScriptWithTestLib(SCRIPT, _undefined, t)
  1669. }
  1670. func TestEvalParamWithDef(t *testing.T) {
  1671. const SCRIPT = `
  1672. function f(param = 0) {
  1673. eval("var param = 1");
  1674. return param;
  1675. }
  1676. f();
  1677. `
  1678. testScript(SCRIPT, valueInt(1), t)
  1679. }
  1680. func TestArgumentsRedefinedAsLetDyn(t *testing.T) {
  1681. const SCRIPT = `
  1682. function f() {
  1683. let arguments;
  1684. eval(""); // force dynamic scope
  1685. return arguments;
  1686. }
  1687. f(1,2);
  1688. `
  1689. testScript(SCRIPT, _undefined, t)
  1690. }
  1691. func TestWith(t *testing.T) {
  1692. const SCRIPT = `
  1693. var b = 1;
  1694. var o = {a: 41};
  1695. with(o) {
  1696. a += b;
  1697. }
  1698. o.a;
  1699. `
  1700. testScript(SCRIPT, intToValue(42), t)
  1701. }
  1702. func TestWithInFunc(t *testing.T) {
  1703. const SCRIPT = `
  1704. function F() {
  1705. var b = 1;
  1706. var c = 0;
  1707. var o = {a: 40, c: 1};
  1708. with(o) {
  1709. a += b + c;
  1710. }
  1711. return o.a;
  1712. }
  1713. F();
  1714. `
  1715. testScript(SCRIPT, intToValue(42), t)
  1716. }
  1717. func TestAssignNonExtendable(t *testing.T) {
  1718. const SCRIPT = `
  1719. 'use strict';
  1720. function F() {
  1721. this.x = 1;
  1722. }
  1723. var o = new F();
  1724. Object.preventExtensions(o);
  1725. o.x = 42;
  1726. o.x;
  1727. `
  1728. testScript(SCRIPT, intToValue(42), t)
  1729. }
  1730. func TestAssignNonExtendable1(t *testing.T) {
  1731. const SCRIPT = `
  1732. 'use strict';
  1733. function F() {
  1734. }
  1735. var o = new F();
  1736. var rv;
  1737. Object.preventExtensions(o);
  1738. try {
  1739. o.x = 42;
  1740. } catch (e) {
  1741. rv = e.constructor === TypeError;
  1742. }
  1743. rv += " " + o.x;
  1744. rv;
  1745. `
  1746. testScript(SCRIPT, asciiString("true undefined"), t)
  1747. }
  1748. func TestAssignStrict(t *testing.T) {
  1749. const SCRIPT = `
  1750. 'use strict';
  1751. try {
  1752. eval("eval = 42");
  1753. } catch(e) {
  1754. var rv = e instanceof SyntaxError
  1755. }
  1756. rv;
  1757. `
  1758. testScript(SCRIPT, valueTrue, t)
  1759. }
  1760. func TestIllegalArgmentName(t *testing.T) {
  1761. const SCRIPT = `
  1762. 'use strict';
  1763. try {
  1764. eval("function F(eval) {}");
  1765. } catch (e) {
  1766. var rv = e instanceof SyntaxError
  1767. }
  1768. rv;
  1769. `
  1770. testScript(SCRIPT, valueTrue, t)
  1771. }
  1772. func TestFunction(t *testing.T) {
  1773. const SCRIPT = `
  1774. var f0 = Function("");
  1775. var f1 = Function("return ' one'");
  1776. var f2 = Function("arg", "return ' ' + arg");
  1777. f0() + f1() + f2("two");
  1778. `
  1779. testScript(SCRIPT, asciiString("undefined one two"), t)
  1780. }
  1781. func TestFunction1(t *testing.T) {
  1782. const SCRIPT = `
  1783. var f = function f1(count) {
  1784. if (count == 0) {
  1785. return true;
  1786. }
  1787. return f1(count-1);
  1788. }
  1789. f(1);
  1790. `
  1791. testScript(SCRIPT, valueTrue, t)
  1792. }
  1793. func TestFunction2(t *testing.T) {
  1794. const SCRIPT = `
  1795. var trace = "";
  1796. function f(count) {
  1797. trace += "f("+count+")";
  1798. if (count == 0) {
  1799. return;
  1800. }
  1801. return f(count-1);
  1802. }
  1803. function f1() {
  1804. trace += "f1";
  1805. }
  1806. var f2 = f;
  1807. f = f1;
  1808. f2(1);
  1809. trace;
  1810. `
  1811. testScript(SCRIPT, asciiString("f(1)f1"), t)
  1812. }
  1813. func TestFunctionToString(t *testing.T) {
  1814. const SCRIPT = `
  1815. Function("arg1", "arg2", "return 42").toString();
  1816. `
  1817. testScript(SCRIPT, asciiString("function anonymous(arg1,arg2\n) {\nreturn 42\n}"), t)
  1818. }
  1819. func TestObjectLiteral(t *testing.T) {
  1820. const SCRIPT = `
  1821. var getterCalled = false;
  1822. var setterCalled = false;
  1823. var o = {get x() {getterCalled = true}, set x(_) {setterCalled = true}};
  1824. o.x;
  1825. o.x = 42;
  1826. getterCalled && setterCalled;
  1827. `
  1828. testScript(SCRIPT, valueTrue, t)
  1829. }
  1830. func TestConst(t *testing.T) {
  1831. const SCRIPT = `
  1832. var v1 = true && true;
  1833. var v2 = 1/(-1 * 0);
  1834. var v3 = 1 == 2 || v1;
  1835. var v4 = true && false
  1836. v1 === true && v2 === -Infinity && v3 === v1 && v4 === false;
  1837. `
  1838. testScript(SCRIPT, valueTrue, t)
  1839. }
  1840. func TestConstWhile(t *testing.T) {
  1841. const SCRIPT = `
  1842. var c = 0;
  1843. while (2 + 2 === 4) {
  1844. if (++c > 9) {
  1845. break;
  1846. }
  1847. }
  1848. c === 10;
  1849. `
  1850. testScript(SCRIPT, valueTrue, t)
  1851. }
  1852. func TestConstWhileThrow(t *testing.T) {
  1853. const SCRIPT = `
  1854. var thrown = false;
  1855. try {
  1856. while ('s' in true) {
  1857. break;
  1858. }
  1859. } catch (e) {
  1860. thrown = e instanceof TypeError
  1861. }
  1862. thrown;
  1863. `
  1864. testScript(SCRIPT, valueTrue, t)
  1865. }
  1866. func TestDupParams(t *testing.T) {
  1867. const SCRIPT = `
  1868. function F(x, y, x) {
  1869. return x;
  1870. }
  1871. F(1, 2);
  1872. `
  1873. testScript(SCRIPT, _undefined, t)
  1874. }
  1875. func TestUseUnsuppliedParam(t *testing.T) {
  1876. const SCRIPT = `
  1877. function getMessage(message) {
  1878. if (message === undefined) {
  1879. message = '';
  1880. }
  1881. message += " 123 456";
  1882. return message;
  1883. }
  1884. getMessage();
  1885. `
  1886. testScript(SCRIPT, asciiString(" 123 456"), t)
  1887. }
  1888. func TestForInLetWithInitializer(t *testing.T) {
  1889. const SCRIPT = `for (let x = 3 in {}) { }`
  1890. _, err := Compile("", SCRIPT, false)
  1891. if err == nil {
  1892. t.Fatal("Expected error")
  1893. }
  1894. }
  1895. func TestForInLoop(t *testing.T) {
  1896. const SCRIPT = `
  1897. function Proto() {}
  1898. Proto.prototype.x = 42;
  1899. var o = new Proto();
  1900. o.y = 44;
  1901. o.x = 45;
  1902. var hasX = false;
  1903. var hasY = false;
  1904. for (var i in o) {
  1905. switch(i) {
  1906. case "x":
  1907. if (hasX) {
  1908. throw new Error("Already has X");
  1909. }
  1910. hasX = true;
  1911. break;
  1912. case "y":
  1913. if (hasY) {
  1914. throw new Error("Already has Y");
  1915. }
  1916. hasY = true;
  1917. break;
  1918. }
  1919. }
  1920. hasX && hasY;
  1921. `
  1922. testScript(SCRIPT, valueTrue, t)
  1923. }
  1924. func TestWhileLoopResult(t *testing.T) {
  1925. const SCRIPT = `
  1926. while(false);
  1927. `
  1928. testScript(SCRIPT, _undefined, t)
  1929. }
  1930. func TestEmptySwitch(t *testing.T) {
  1931. const SCRIPT = `
  1932. switch(1){}
  1933. `
  1934. testScript(SCRIPT, _undefined, t)
  1935. }
  1936. func TestEmptyDoWhile(t *testing.T) {
  1937. const SCRIPT = `
  1938. do {} while(false)
  1939. `
  1940. testScript(SCRIPT, _undefined, t)
  1941. }
  1942. func TestSwitch(t *testing.T) {
  1943. const SCRIPT = `
  1944. function F(x) {
  1945. var i = 0;
  1946. switch (x) {
  1947. case 0:
  1948. i++;
  1949. case 1:
  1950. i++;
  1951. default:
  1952. i++;
  1953. case 2:
  1954. i++;
  1955. break;
  1956. case 3:
  1957. i++;
  1958. }
  1959. return i;
  1960. }
  1961. F(0) + F(1) + F(2) + F(4);
  1962. `
  1963. testScript(SCRIPT, intToValue(10), t)
  1964. }
  1965. func TestSwitchDefFirst(t *testing.T) {
  1966. const SCRIPT = `
  1967. function F(x) {
  1968. var i = 0;
  1969. switch (x) {
  1970. default:
  1971. i++;
  1972. case 0:
  1973. i++;
  1974. case 1:
  1975. i++;
  1976. case 2:
  1977. i++;
  1978. break;
  1979. case 3:
  1980. i++;
  1981. }
  1982. return i;
  1983. }
  1984. F(0) + F(1) + F(2) + F(4);
  1985. `
  1986. testScript(SCRIPT, intToValue(10), t)
  1987. }
  1988. func TestSwitchResult(t *testing.T) {
  1989. const SCRIPT = `
  1990. var x = 2;
  1991. switch (x) {
  1992. case 0:
  1993. "zero";
  1994. case 1:
  1995. "one";
  1996. case 2:
  1997. "two";
  1998. break;
  1999. case 3:
  2000. "three";
  2001. default:
  2002. "default";
  2003. }
  2004. `
  2005. testScript(SCRIPT, asciiString("two"), t)
  2006. }
  2007. func TestSwitchResult1(t *testing.T) {
  2008. const SCRIPT = `
  2009. var x = 0;
  2010. switch (x) { case 0: "two"; case 1: break}
  2011. `
  2012. testScript(SCRIPT, asciiString("two"), t)
  2013. }
  2014. func TestSwitchResult2(t *testing.T) {
  2015. const SCRIPT = `
  2016. 6; switch ("a") { case "a": 7; case "b": }
  2017. `
  2018. testScript(SCRIPT, valueInt(7), t)
  2019. }
  2020. func TestSwitchResultJumpIntoEmptyEval(t *testing.T) {
  2021. const SCRIPT = `
  2022. function t(x) {
  2023. return eval("switch(x) { case 1: 2; break; case 2: let x = 1; case 3: x+2; break; case 4: default: 9}");
  2024. }
  2025. ""+t(2)+t();
  2026. `
  2027. testScript(SCRIPT, asciiString("39"), t)
  2028. }
  2029. func TestSwitchResultJumpIntoEmpty(t *testing.T) {
  2030. const SCRIPT = `
  2031. switch(2) { case 1: 2; break; case 2: let x = 1; case 3: x+2; case 4: {let y = 2}; break; default: 9};
  2032. `
  2033. testScript(SCRIPT, valueInt(3), t)
  2034. }
  2035. func TestSwitchLexical(t *testing.T) {
  2036. const SCRIPT = `
  2037. switch (true) { case true: let x = 1; }
  2038. `
  2039. testScript(SCRIPT, _undefined, t)
  2040. }
  2041. func TestSwitchBreakOuter(t *testing.T) {
  2042. const SCRIPT = `
  2043. LOOP:
  2044. for (let i = 0; i < 10; i++) {
  2045. switch (i) {
  2046. case 0:
  2047. continue;
  2048. case 1:
  2049. let x = 1;
  2050. continue;
  2051. case 2:
  2052. try {
  2053. x++;
  2054. } catch (e) {
  2055. if (e instanceof ReferenceError) {
  2056. break LOOP;
  2057. }
  2058. }
  2059. throw new Error("Exception was not thrown");
  2060. }
  2061. }
  2062. `
  2063. testScript(SCRIPT, _undefined, t)
  2064. }
  2065. func TestIfBreakResult(t *testing.T) {
  2066. const SCRIPT = `
  2067. L: {if (true) {42;} break L;}
  2068. `
  2069. testScript(SCRIPT, intToValue(42), t)
  2070. }
  2071. func TestSwitchNoMatch(t *testing.T) {
  2072. const SCRIPT = `
  2073. var result;
  2074. var x;
  2075. switch (x) {
  2076. case 0:
  2077. result = "2";
  2078. break;
  2079. }
  2080. result;
  2081. `
  2082. testScript(SCRIPT, _undefined, t)
  2083. }
  2084. func TestSwitchNoMatchNoDefault(t *testing.T) {
  2085. const SCRIPT = `
  2086. switch (1) {
  2087. case 0:
  2088. }
  2089. `
  2090. testScript(SCRIPT, _undefined, t)
  2091. }
  2092. func TestSwitchNoMatchNoDefaultNoResult(t *testing.T) {
  2093. const SCRIPT = `
  2094. switch (1) {
  2095. case 0:
  2096. }
  2097. 42;
  2098. `
  2099. testScript(SCRIPT, intToValue(42), t)
  2100. }
  2101. func TestSwitchNoMatchNoDefaultNoResultMatch(t *testing.T) {
  2102. const SCRIPT = `
  2103. switch (1) {
  2104. case 1:
  2105. }
  2106. 42;
  2107. `
  2108. testScript(SCRIPT, intToValue(42), t)
  2109. }
  2110. func TestEmptySwitchNoResult(t *testing.T) {
  2111. const SCRIPT = `
  2112. switch (1) {}
  2113. 42;
  2114. `
  2115. testScript(SCRIPT, intToValue(42), t)
  2116. }
  2117. func TestGetOwnPropertyNames(t *testing.T) {
  2118. const SCRIPT = `
  2119. var o = {
  2120. prop1: 42,
  2121. prop2: "test"
  2122. }
  2123. var hasProp1 = false;
  2124. var hasProp2 = false;
  2125. var names = Object.getOwnPropertyNames(o);
  2126. for (var i in names) {
  2127. var p = names[i];
  2128. switch(p) {
  2129. case "prop1":
  2130. hasProp1 = true;
  2131. break;
  2132. case "prop2":
  2133. hasProp2 = true;
  2134. break;
  2135. }
  2136. }
  2137. hasProp1 && hasProp2;
  2138. `
  2139. testScript(SCRIPT, valueTrue, t)
  2140. }
  2141. func TestArrayLiteral(t *testing.T) {
  2142. const SCRIPT = `
  2143. var f1Called = false;
  2144. var f2Called = false;
  2145. var f3Called = false;
  2146. var errorThrown = false;
  2147. function F1() {
  2148. f1Called = true;
  2149. }
  2150. function F2() {
  2151. f2Called = true;
  2152. }
  2153. function F3() {
  2154. f3Called = true;
  2155. }
  2156. try {
  2157. var a = [F1(), x(F3()), F2()];
  2158. } catch(e) {
  2159. if (e instanceof ReferenceError) {
  2160. errorThrown = true;
  2161. } else {
  2162. throw e;
  2163. }
  2164. }
  2165. f1Called && !f2Called && f3Called && errorThrown && a === undefined;
  2166. `
  2167. testScript(SCRIPT, valueTrue, t)
  2168. }
  2169. func TestJumpOutOfReturn(t *testing.T) {
  2170. const SCRIPT = `
  2171. function f() {
  2172. var a;
  2173. if (a == 0) {
  2174. return true;
  2175. }
  2176. }
  2177. f();
  2178. `
  2179. testScript(SCRIPT, _undefined, t)
  2180. }
  2181. func TestSwitchJumpOutOfReturn(t *testing.T) {
  2182. const SCRIPT = `
  2183. function f(x) {
  2184. switch(x) {
  2185. case 0:
  2186. break;
  2187. default:
  2188. return x;
  2189. }
  2190. }
  2191. f(0);
  2192. `
  2193. testScript(SCRIPT, _undefined, t)
  2194. }
  2195. func TestSetToReadOnlyPropertyStrictBracket(t *testing.T) {
  2196. const SCRIPT = `
  2197. 'use strict';
  2198. var o = {};
  2199. var thrown = false;
  2200. Object.defineProperty(o, "test", {value: 42, configurable: true});
  2201. try {
  2202. o["test"] = 43;
  2203. } catch (e) {
  2204. thrown = e instanceof TypeError;
  2205. }
  2206. thrown;
  2207. `
  2208. testScript(SCRIPT, valueTrue, t)
  2209. }
  2210. func TestSetToReadOnlyPropertyStrictDot(t *testing.T) {
  2211. const SCRIPT = `
  2212. 'use strict';
  2213. var o = {};
  2214. var thrown = false;
  2215. Object.defineProperty(o, "test", {value: 42, configurable: true});
  2216. try {
  2217. o.test = 43;
  2218. } catch (e) {
  2219. thrown = e instanceof TypeError;
  2220. }
  2221. thrown;
  2222. `
  2223. testScript(SCRIPT, valueTrue, t)
  2224. }
  2225. func TestDeleteNonConfigurablePropertyStrictBracket(t *testing.T) {
  2226. const SCRIPT = `
  2227. 'use strict';
  2228. var o = {};
  2229. var thrown = false;
  2230. Object.defineProperty(o, "test", {value: 42});
  2231. try {
  2232. delete o["test"];
  2233. } catch (e) {
  2234. thrown = e instanceof TypeError;
  2235. }
  2236. thrown;
  2237. `
  2238. testScript(SCRIPT, valueTrue, t)
  2239. }
  2240. func TestDeleteNonConfigurablePropertyStrictDot(t *testing.T) {
  2241. const SCRIPT = `
  2242. 'use strict';
  2243. var o = {};
  2244. var thrown = false;
  2245. Object.defineProperty(o, "test", {value: 42});
  2246. try {
  2247. delete o.test;
  2248. } catch (e) {
  2249. thrown = e instanceof TypeError;
  2250. }
  2251. thrown;
  2252. `
  2253. testScript(SCRIPT, valueTrue, t)
  2254. }
  2255. func TestCompound1(t *testing.T) {
  2256. const SCRIPT = `
  2257. var x = 0;
  2258. var scope = {x: 1};
  2259. var f;
  2260. with (scope) {
  2261. f = function() {
  2262. x *= (delete scope.x, 2);
  2263. }
  2264. }
  2265. f();
  2266. scope.x === 2 && x === 0;
  2267. `
  2268. testScript(SCRIPT, valueTrue, t)
  2269. }
  2270. func TestCompound2(t *testing.T) {
  2271. const SCRIPT = `
  2272. var x;
  2273. x = "x";
  2274. x ^= "1";
  2275. `
  2276. testScript(SCRIPT, intToValue(1), t)
  2277. }
  2278. func TestDeleteArguments(t *testing.T) {
  2279. defer func() {
  2280. if _, ok := recover().(*CompilerSyntaxError); !ok {
  2281. t.Fatal("Expected syntax error")
  2282. }
  2283. }()
  2284. const SCRIPT = `
  2285. 'use strict';
  2286. function f() {
  2287. delete arguments;
  2288. }
  2289. `
  2290. testScript(SCRIPT, _undefined, t)
  2291. }
  2292. func TestReturnUndefined(t *testing.T) {
  2293. const SCRIPT = `
  2294. function f() {
  2295. return x;
  2296. }
  2297. var thrown = false;
  2298. try {
  2299. f();
  2300. } catch (e) {
  2301. thrown = e instanceof ReferenceError;
  2302. }
  2303. thrown;
  2304. `
  2305. testScript(SCRIPT, valueTrue, t)
  2306. }
  2307. func TestForBreak(t *testing.T) {
  2308. const SCRIPT = `
  2309. var supreme, count;
  2310. supreme = 5;
  2311. var __evaluated = eval("for(count=0;;) {if (count===supreme)break;else count++; }");
  2312. if (__evaluated !== void 0) {
  2313. throw new Error('#1: __evaluated === 4. Actual: __evaluated ==='+ __evaluated );
  2314. }
  2315. `
  2316. testScript(SCRIPT, _undefined, t)
  2317. }
  2318. func TestLargeNumberLiteral(t *testing.T) {
  2319. const SCRIPT = `
  2320. var x = 0x800000000000000000000;
  2321. x.toString();
  2322. `
  2323. testScript(SCRIPT, asciiString("9.671406556917033e+24"), t)
  2324. }
  2325. func TestIncDelete(t *testing.T) {
  2326. const SCRIPT = `
  2327. var o = {x: 1};
  2328. o.x += (delete o.x, 1);
  2329. o.x;
  2330. `
  2331. testScript(SCRIPT, intToValue(2), t)
  2332. }
  2333. func TestCompoundAssignRefError(t *testing.T) {
  2334. const SCRIPT = `
  2335. var thrown = false;
  2336. try {
  2337. a *= 1;
  2338. } catch (e) {
  2339. if (e instanceof ReferenceError) {
  2340. thrown = true;
  2341. } else {
  2342. throw e;
  2343. }
  2344. }
  2345. thrown;
  2346. `
  2347. testScript(SCRIPT, valueTrue, t)
  2348. }
  2349. func TestObjectLiteral__Proto__(t *testing.T) {
  2350. const SCRIPT = `
  2351. var o = {
  2352. __proto__: null,
  2353. test: 42
  2354. }
  2355. Object.getPrototypeOf(o);
  2356. `
  2357. testScript(SCRIPT, _null, t)
  2358. }
  2359. func TestEmptyCodeError(t *testing.T) {
  2360. if _, err := New().RunString(`i`); err == nil {
  2361. t.Fatal("Expected an error")
  2362. } else {
  2363. if e := err.Error(); e != "ReferenceError: i is not defined at <eval>:1:1(0)" {
  2364. t.Fatalf("Unexpected error: '%s'", e)
  2365. }
  2366. }
  2367. }
  2368. func TestForOfArray(t *testing.T) {
  2369. const SCRIPT = `
  2370. var array = [0, 'a', true, false, null, /* hole */, undefined, NaN];
  2371. var i = 0;
  2372. for (var value of array) {
  2373. assert.sameValue(value, array[i], 'element at index ' + i);
  2374. i++;
  2375. }
  2376. assert.sameValue(i, 8, 'Visits all elements');
  2377. `
  2378. testScriptWithTestLib(SCRIPT, _undefined, t)
  2379. }
  2380. func TestForOfReturn(t *testing.T) {
  2381. const SCRIPT = `
  2382. var callCount = 0;
  2383. var iterationCount = 0;
  2384. var iterable = {};
  2385. var x = {
  2386. set attr(_) {
  2387. throw new Test262Error();
  2388. }
  2389. };
  2390. iterable[Symbol.iterator] = function() {
  2391. return {
  2392. next: function() {
  2393. return { done: false, value: 0 };
  2394. },
  2395. return: function() {
  2396. callCount += 1;
  2397. }
  2398. }
  2399. };
  2400. assert.throws(Test262Error, function() {
  2401. for (x.attr of iterable) {
  2402. iterationCount += 1;
  2403. }
  2404. });
  2405. assert.sameValue(iterationCount, 0, 'The loop body is not evaluated');
  2406. assert.sameValue(callCount, 1, 'Iterator is closed');
  2407. `
  2408. testScriptWithTestLib(SCRIPT, _undefined, t)
  2409. }
  2410. func TestForOfReturn1(t *testing.T) {
  2411. const SCRIPT = `
  2412. var iterable = {};
  2413. var iterationCount = 0;
  2414. iterable[Symbol.iterator] = function() {
  2415. return {
  2416. next: function() {
  2417. return { done: false, value: null };
  2418. },
  2419. get return() {
  2420. throw new Test262Error();
  2421. }
  2422. };
  2423. };
  2424. assert.throws(Test262Error, function() {
  2425. for (var x of iterable) {
  2426. iterationCount += 1;
  2427. break;
  2428. }
  2429. });
  2430. assert.sameValue(iterationCount, 1, 'The loop body is evaluated');
  2431. `
  2432. testScriptWithTestLib(SCRIPT, _undefined, t)
  2433. }
  2434. func TestForOfLet(t *testing.T) {
  2435. const SCRIPT = `
  2436. var iterCount = 0;
  2437. function f() {}
  2438. for (var let of [23]) {
  2439. f(let);
  2440. if (let != 23) {
  2441. throw new Error("");
  2442. }
  2443. iterCount += 1;
  2444. }
  2445. iterCount;
  2446. `
  2447. testScript(SCRIPT, valueInt(1), t)
  2448. }
  2449. func TestForOfLetLet(t *testing.T) {
  2450. const SCRIPT = `
  2451. for (let let of [23]) {
  2452. }
  2453. `
  2454. _, err := Compile("", SCRIPT, false)
  2455. if err == nil {
  2456. t.Fatal("Expected error")
  2457. }
  2458. }
  2459. func TestForHeadLet(t *testing.T) {
  2460. const SCRIPT = `
  2461. for (let = 0; let < 2; let++);
  2462. `
  2463. testScript(SCRIPT, _undefined, t)
  2464. }
  2465. func TestLhsLet(t *testing.T) {
  2466. const SCRIPT = `
  2467. let = 1;
  2468. let;
  2469. `
  2470. testScript(SCRIPT, valueInt(1), t)
  2471. }
  2472. func TestLetPostfixASI(t *testing.T) {
  2473. const SCRIPT = `
  2474. let
  2475. ++
  2476. `
  2477. _, err := Compile("", SCRIPT, false)
  2478. if err == nil {
  2479. t.Fatal("Expected error")
  2480. }
  2481. }
  2482. func TestIteratorReturnNormal(t *testing.T) {
  2483. const SCRIPT = `
  2484. var iterable = {};
  2485. var iterationCount = 0;
  2486. iterable[Symbol.iterator] = function() {
  2487. return {
  2488. next: function() {
  2489. return { done: ++iterationCount > 2, value: null };
  2490. },
  2491. get return() {
  2492. throw new Test262Error();
  2493. }
  2494. };
  2495. };
  2496. for (var x of iterable) {
  2497. }
  2498. `
  2499. testScriptWithTestLib(SCRIPT, _undefined, t)
  2500. }
  2501. func TestIteratorReturnErrorNested(t *testing.T) {
  2502. const SCRIPT = `
  2503. var returnCalled = {};
  2504. function iter(id) {
  2505. return function() {
  2506. var count = 0;
  2507. return {
  2508. next: function () {
  2509. return {
  2510. value: null,
  2511. done: ++count > 2
  2512. };
  2513. },
  2514. return: function () {
  2515. returnCalled[id] = true;
  2516. throw new Error(id);
  2517. }
  2518. };
  2519. }
  2520. }
  2521. var iterable1 = {};
  2522. iterable1[Symbol.iterator] = iter("1");
  2523. var iterable2 = {};
  2524. iterable2[Symbol.iterator] = iter("2");
  2525. try {
  2526. for (var i of iterable1) {
  2527. for (var j of iterable2) {
  2528. break;
  2529. }
  2530. }
  2531. throw new Error("no exception was thrown");
  2532. } catch (e) {
  2533. if (e.message !== "2") {
  2534. throw e;
  2535. }
  2536. }
  2537. if (!returnCalled["1"]) {
  2538. throw new Error("no return 1");
  2539. }
  2540. if (!returnCalled["2"]) {
  2541. throw new Error("no return 2");
  2542. }
  2543. `
  2544. testScript(SCRIPT, _undefined, t)
  2545. }
  2546. func TestReturnFromForInLoop(t *testing.T) {
  2547. const SCRIPT = `
  2548. (function f() {
  2549. for (var i in {a: 1}) {
  2550. return true;
  2551. }
  2552. })();
  2553. `
  2554. testScript(SCRIPT, valueTrue, t)
  2555. }
  2556. func TestReturnFromForOfLoop(t *testing.T) {
  2557. const SCRIPT = `
  2558. (function f() {
  2559. for (var i of [1]) {
  2560. return true;
  2561. }
  2562. })();
  2563. `
  2564. testScript(SCRIPT, valueTrue, t)
  2565. }
  2566. func TestIfStackLeaks(t *testing.T) {
  2567. const SCRIPT = `
  2568. var t = 0;
  2569. if (t === 0) {
  2570. t;
  2571. }
  2572. `
  2573. testScript(SCRIPT, _positiveZero, t)
  2574. }
  2575. func TestWithCallee(t *testing.T) {
  2576. const SCRIPT = `
  2577. function O() {
  2578. var that = this;
  2579. this.m = function() {
  2580. return this === that;
  2581. }
  2582. }
  2583. with(new O()) {
  2584. m();
  2585. }
  2586. `
  2587. testScript(SCRIPT, valueTrue, t)
  2588. }
  2589. func TestWithScope(t *testing.T) {
  2590. const SCRIPT = `
  2591. function f(o) {
  2592. var x = 42;
  2593. function innerf(o) {
  2594. with (o) {
  2595. return x;
  2596. }
  2597. }
  2598. return innerf(o);
  2599. }
  2600. f({});
  2601. `
  2602. testScript(SCRIPT, valueInt(42), t)
  2603. }
  2604. func TestEvalCallee(t *testing.T) {
  2605. const SCRIPT = `
  2606. (function () {
  2607. 'use strict';
  2608. var v = function() {
  2609. return this === undefined;
  2610. };
  2611. return eval('v()');
  2612. })();
  2613. `
  2614. testScript(SCRIPT, valueTrue, t)
  2615. }
  2616. func TestEvalBindingDeleteVar(t *testing.T) {
  2617. const SCRIPT = `
  2618. (function () {
  2619. eval("var x = 1");
  2620. return x === 1 && delete x;
  2621. })();
  2622. `
  2623. testScript(SCRIPT, valueTrue, t)
  2624. }
  2625. func TestEvalBindingDeleteFunc(t *testing.T) {
  2626. const SCRIPT = `
  2627. (function () {
  2628. eval("function x(){}");
  2629. return typeof x === "function" && delete x;
  2630. })();
  2631. `
  2632. testScript(SCRIPT, valueTrue, t)
  2633. }
  2634. func TestDeleteGlobalLexical(t *testing.T) {
  2635. const SCRIPT = `
  2636. let x;
  2637. delete x;
  2638. `
  2639. testScript(SCRIPT, valueFalse, t)
  2640. }
  2641. func TestDeleteGlobalEval(t *testing.T) {
  2642. const SCRIPT = `
  2643. eval("var x");
  2644. delete x;
  2645. `
  2646. testScript(SCRIPT, valueTrue, t)
  2647. }
  2648. func TestGlobalVarNames(t *testing.T) {
  2649. vm := New()
  2650. _, err := vm.RunString("(0,eval)('var x')")
  2651. if err != nil {
  2652. t.Fatal(err)
  2653. }
  2654. _, err = vm.RunString("let x")
  2655. if err == nil {
  2656. t.Fatal("Expected error")
  2657. }
  2658. }
  2659. func TestTryResultEmpty(t *testing.T) {
  2660. const SCRIPT = `
  2661. 1; try { } finally { }
  2662. `
  2663. testScript(SCRIPT, _undefined, t)
  2664. }
  2665. func TestTryResultEmptyCatch(t *testing.T) {
  2666. const SCRIPT = `
  2667. 1; try { throw null } catch(e) { }
  2668. `
  2669. testScript(SCRIPT, _undefined, t)
  2670. }
  2671. func TestTryResultEmptyContinueLoop(t *testing.T) {
  2672. const SCRIPT = `
  2673. for (var i = 0; i < 2; i++) { try {throw null;} catch(e) {continue;} 'bad'}
  2674. `
  2675. testScript(SCRIPT, _undefined, t)
  2676. }
  2677. func TestTryEmptyCatchStackLeak(t *testing.T) {
  2678. const SCRIPT = `
  2679. (function() {
  2680. var f;
  2681. // Make sure the outer function is not stashless.
  2682. (function() {
  2683. f++;
  2684. })();
  2685. try {
  2686. throw new Error();
  2687. } catch(e) {}
  2688. })();
  2689. `
  2690. testScript(SCRIPT, _undefined, t)
  2691. }
  2692. func TestTryThrowEmptyCatch(t *testing.T) {
  2693. const SCRIPT = `
  2694. try {
  2695. throw new Error();
  2696. }
  2697. catch (e) {}
  2698. `
  2699. testScript(SCRIPT, _undefined, t)
  2700. }
  2701. func TestFalsyLoopBreak(t *testing.T) {
  2702. const SCRIPT = `
  2703. while(false) {
  2704. break;
  2705. }
  2706. for(;false;) {
  2707. break;
  2708. }
  2709. undefined;
  2710. `
  2711. MustCompile("", SCRIPT, false)
  2712. }
  2713. func TestFalsyLoopBreakWithResult(t *testing.T) {
  2714. const SCRIPT = `
  2715. while(false) {
  2716. break;
  2717. }
  2718. `
  2719. testScript(SCRIPT, _undefined, t)
  2720. }
  2721. func TestDummyCompile(t *testing.T) {
  2722. const SCRIPT = `
  2723. 'use strict';
  2724. for (;false;) {
  2725. eval = 1;
  2726. }
  2727. `
  2728. _, err := Compile("", SCRIPT, false)
  2729. if err == nil {
  2730. t.Fatal("expected error")
  2731. }
  2732. }
  2733. func TestDummyCompileForUpdate(t *testing.T) {
  2734. const SCRIPT = `
  2735. 'use strict';
  2736. for (;false;eval=1) {
  2737. }
  2738. `
  2739. _, err := Compile("", SCRIPT, false)
  2740. if err == nil {
  2741. t.Fatal("expected error")
  2742. }
  2743. }
  2744. func TestObjectLiteralWithNumericKeys(t *testing.T) {
  2745. const SCRIPT = `
  2746. var o = {1e3: true};
  2747. var keys = Object.keys(o);
  2748. var o1 = {get 1e3() {return true;}};
  2749. var keys1 = Object.keys(o1);
  2750. var o2 = {1e21: true};
  2751. var keys2 = Object.keys(o2);
  2752. let o3 = {0(){return true}};
  2753. keys.length === 1 && keys[0] === "1000" &&
  2754. keys1.length === 1 && keys1[0] === "1000" && o1[1e3] === true &&
  2755. keys2.length === 1 && keys2[0] === "1e+21" && o3[0]();
  2756. `
  2757. testScript(SCRIPT, valueTrue, t)
  2758. }
  2759. func TestEscapedObjectPropertyKeys(t *testing.T) {
  2760. const SCRIPT = `
  2761. var obj = {
  2762. w\u0069th: 42
  2763. };
  2764. var obj = {
  2765. with() {42}
  2766. };
  2767. `
  2768. _, err := Compile("", SCRIPT, false)
  2769. if err != nil {
  2770. t.Fatal(err)
  2771. }
  2772. }
  2773. func TestEscapedKeywords(t *testing.T) {
  2774. const SCRIPT = `r\u0065turn;`
  2775. _, err := Compile("", SCRIPT, false)
  2776. if err == nil {
  2777. t.Fatal("Expected error")
  2778. }
  2779. }
  2780. func TestEscapedLet(t *testing.T) {
  2781. const SCRIPT = `
  2782. this.let = 0;
  2783. l\u0065t // ASI
  2784. a;
  2785. // If the parser treated the previous escaped "let" as a lexical declaration,
  2786. // this variable declaration will result an early syntax error.
  2787. var a;
  2788. `
  2789. _, err := Compile("", SCRIPT, false)
  2790. if err != nil {
  2791. t.Fatal(err)
  2792. }
  2793. }
  2794. func TestObjectLiteralFuncProps(t *testing.T) {
  2795. const SCRIPT = `
  2796. (function() {
  2797. 'use strict';
  2798. var o = {
  2799. eval: function() {return 1;},
  2800. arguments() {return 2;},
  2801. test: function test1() {}
  2802. }
  2803. assert.sameValue(o.eval.name, "eval");
  2804. assert.sameValue(o.arguments.name, "arguments");
  2805. assert.sameValue(o.eval(), 1);
  2806. assert.sameValue(o.arguments(), 2);
  2807. assert.sameValue(o.test.name, "test1");
  2808. })();
  2809. `
  2810. testScriptWithTestLib(SCRIPT, _undefined, t)
  2811. }
  2812. func TestFuncName(t *testing.T) {
  2813. const SCRIPT = `
  2814. var method = 1;
  2815. var o = {
  2816. method: function() {
  2817. return method;
  2818. },
  2819. method1: function method() {
  2820. return method;
  2821. }
  2822. }
  2823. o.method() === 1 && o.method1() === o.method1;
  2824. `
  2825. testScript(SCRIPT, valueTrue, t)
  2826. }
  2827. func TestFuncNameAssign(t *testing.T) {
  2828. const SCRIPT = `
  2829. var f = function() {};
  2830. var f1;
  2831. f1 = function() {};
  2832. let f2 = function() {};
  2833. f.name === "f" && f1.name === "f1" && f2.name === "f2";
  2834. `
  2835. testScript(SCRIPT, valueTrue, t)
  2836. }
  2837. func TestLexicalDeclGlobal(t *testing.T) {
  2838. const SCRIPT = `
  2839. if (true) {
  2840. let it = "be";
  2841. if (it !== "be") {
  2842. throw new Error(it);
  2843. }
  2844. }
  2845. let thrown = false;
  2846. try {
  2847. it;
  2848. } catch(e) {
  2849. if (e instanceof ReferenceError) {
  2850. thrown = true;
  2851. }
  2852. }
  2853. thrown;
  2854. `
  2855. testScript(SCRIPT, valueTrue, t)
  2856. }
  2857. func TestLexicalDeclFunction(t *testing.T) {
  2858. const SCRIPT = `
  2859. function f() {
  2860. if (true) {
  2861. let it = "be";
  2862. if (it !== "be") {
  2863. throw new Error(it);
  2864. }
  2865. }
  2866. let thrown = false;
  2867. try {
  2868. it;
  2869. } catch(e) {
  2870. if (e instanceof ReferenceError) {
  2871. thrown = true;
  2872. }
  2873. }
  2874. return thrown;
  2875. }
  2876. f();
  2877. `
  2878. testScript(SCRIPT, valueTrue, t)
  2879. }
  2880. func TestLexicalDynamicScope(t *testing.T) {
  2881. const SCRIPT = `
  2882. const global = 1;
  2883. function f() {
  2884. const func = global + 1;
  2885. function inner() {
  2886. function assertThrows(fn) {
  2887. let thrown = false;
  2888. try {
  2889. fn();
  2890. } catch (e) {
  2891. if (e instanceof TypeError) {
  2892. thrown = true;
  2893. } else {
  2894. throw e;
  2895. }
  2896. }
  2897. if (!thrown) {
  2898. throw new Error("Did not throw");
  2899. }
  2900. }
  2901. assertThrows(function() {
  2902. func++;
  2903. });
  2904. assertThrows(function() {
  2905. global++;
  2906. });
  2907. assertThrows(function() {
  2908. eval("func++");
  2909. });
  2910. assertThrows(function() {
  2911. eval("global++");
  2912. });
  2913. return eval("func + 1");
  2914. }
  2915. return inner();
  2916. }
  2917. f();
  2918. `
  2919. testScript(SCRIPT, valueInt(3), t)
  2920. }
  2921. func TestLexicalDynamicScope1(t *testing.T) {
  2922. const SCRIPT = `
  2923. (function() {
  2924. const x = 1 * 4;
  2925. return (function() {
  2926. eval("");
  2927. return x;
  2928. })();
  2929. })();
  2930. `
  2931. testScript(SCRIPT, intToValue(4), t)
  2932. }
  2933. func TestLexicalDynamicScope2(t *testing.T) {
  2934. const SCRIPT = `
  2935. (function() {
  2936. const x = 1 + 3;
  2937. var y = 2 * 2;
  2938. eval("");
  2939. return x;
  2940. })();
  2941. `
  2942. testScript(SCRIPT, intToValue(4), t)
  2943. }
  2944. func TestNonStrictLet(t *testing.T) {
  2945. const SCRIPT = `
  2946. var let = 1;
  2947. `
  2948. testScript(SCRIPT, _undefined, t)
  2949. }
  2950. func TestStrictLet(t *testing.T) {
  2951. const SCRIPT = `
  2952. var let = 1;
  2953. `
  2954. _, err := Compile("", SCRIPT, true)
  2955. if err == nil {
  2956. t.Fatal("Expected an error")
  2957. }
  2958. }
  2959. func TestLetLet(t *testing.T) {
  2960. const SCRIPT = `
  2961. let let = 1;
  2962. `
  2963. _, err := Compile("", SCRIPT, false)
  2964. if err == nil {
  2965. t.Fatal("Expected an error")
  2966. }
  2967. }
  2968. func TestLetASI(t *testing.T) {
  2969. const SCRIPT = `
  2970. while (false) let // ASI
  2971. x = 1;
  2972. `
  2973. _, err := Compile("", SCRIPT, false)
  2974. if err != nil {
  2975. t.Fatal(err)
  2976. }
  2977. }
  2978. func TestLetASI1(t *testing.T) {
  2979. const SCRIPT = `
  2980. let
  2981. x = 1;
  2982. `
  2983. _, err := Compile("", SCRIPT, true)
  2984. if err != nil {
  2985. t.Fatal(err)
  2986. }
  2987. }
  2988. func TestLetNoASI(t *testing.T) {
  2989. const SCRIPT = `
  2990. function f() {}let
  2991. x = 1;
  2992. `
  2993. _, err := Compile("", SCRIPT, true)
  2994. if err != nil {
  2995. t.Fatal(err)
  2996. }
  2997. }
  2998. func TestLetNoASI1(t *testing.T) {
  2999. const SCRIPT = `
  3000. let
  3001. let = 1;
  3002. `
  3003. _, err := Compile("", SCRIPT, false)
  3004. if err == nil {
  3005. t.Fatal("Expected error")
  3006. }
  3007. }
  3008. func TestLetArrayWithNewline(t *testing.T) {
  3009. const SCRIPT = `
  3010. with ({}) let
  3011. [a] = 0;
  3012. `
  3013. _, err := Compile("", SCRIPT, false)
  3014. if err == nil {
  3015. t.Fatal("Expected error")
  3016. }
  3017. }
  3018. func TestDynamicUninitedVarAccess(t *testing.T) {
  3019. const SCRIPT = `
  3020. function f() {
  3021. var x;
  3022. return eval("x");
  3023. }
  3024. f();
  3025. `
  3026. testScript(SCRIPT, _undefined, t)
  3027. }
  3028. func TestLexicalForLoopNoClosure(t *testing.T) {
  3029. const SCRIPT = `
  3030. let sum = 0;
  3031. for (let i = 0; i < 3; i++) {
  3032. sum += i;
  3033. }
  3034. sum;
  3035. `
  3036. testScript(SCRIPT, valueInt(3), t)
  3037. }
  3038. func TestLexicalForLoopClosure(t *testing.T) {
  3039. const SCRIPT = `
  3040. var f = [];
  3041. for (let i = 0; i < 3; i++) {
  3042. f.push(function() {
  3043. return i;
  3044. });
  3045. }
  3046. f.length === 3 && f[0]() === 0 && f[1]() === 1 && f[2]() === 2;
  3047. `
  3048. testScript(SCRIPT, valueTrue, t)
  3049. }
  3050. func TestLexicalForLoopClosureInNext(t *testing.T) {
  3051. const SCRIPT = `
  3052. const a = [];
  3053. for (let i = 0; i < 5; a.push(function () { return i; }), ++i) { }
  3054. let res = "";
  3055. for (let k = 0; k < 5; ++k) {
  3056. res += ""+a[k]();
  3057. }
  3058. res;
  3059. `
  3060. testScript(SCRIPT, asciiString("12345"), t)
  3061. }
  3062. func TestVarForLoop(t *testing.T) {
  3063. const SCRIPT = `
  3064. var f = [];
  3065. for (var i = 0, j = 0; i < 3; i++) {
  3066. f.push(function() {
  3067. return i;
  3068. });
  3069. }
  3070. f.length === 3 && f[0]() === 3 && f[1]() === 3 && f[2]() === 3;
  3071. `
  3072. testScript(SCRIPT, valueTrue, t)
  3073. }
  3074. func TestLexicalForOfLoop(t *testing.T) {
  3075. const SCRIPT = `
  3076. var f = [];
  3077. for (let i of [0, 1, 2]) {
  3078. f.push(function() {
  3079. return i;
  3080. });
  3081. }
  3082. f.length === 3 && f[0]() === 0 && f[1]() === 1 && f[2]() === 2;
  3083. `
  3084. testScript(SCRIPT, valueTrue, t)
  3085. }
  3086. func TestLexicalForOfLoopContBreak(t *testing.T) {
  3087. const SCRIPT = `
  3088. const f = [];
  3089. for (let i of [0, 1, 2, 3, 4, 5]) {
  3090. if (i % 2) continue;
  3091. f.push(function() {
  3092. return i;
  3093. });
  3094. if (i > 2) break;
  3095. }
  3096. let res = "";
  3097. f.forEach(function(item) {res += item()});
  3098. f.length === 3 && res === "024";
  3099. `
  3100. testScript(SCRIPT, valueTrue, t)
  3101. }
  3102. func TestVarBlockConflict(t *testing.T) {
  3103. const SCRIPT = `
  3104. let x;
  3105. {
  3106. if (false) {
  3107. var x;
  3108. }
  3109. }
  3110. `
  3111. _, err := Compile("", SCRIPT, false)
  3112. if err == nil {
  3113. t.Fatal("Expected an error")
  3114. }
  3115. }
  3116. func TestVarBlockConflictEval(t *testing.T) {
  3117. const SCRIPT = `
  3118. assert.throws(SyntaxError, function() {
  3119. let x;
  3120. {
  3121. if (true) {
  3122. eval("var x");
  3123. }
  3124. }
  3125. });
  3126. `
  3127. testScriptWithTestLib(SCRIPT, _undefined, t)
  3128. }
  3129. func TestVarBlockNoConflict(t *testing.T) {
  3130. const SCRIPT = `
  3131. function f() {
  3132. let x;
  3133. function ff() {
  3134. {
  3135. var x = 3;
  3136. }
  3137. }
  3138. ff();
  3139. }
  3140. f();
  3141. `
  3142. testScript(SCRIPT, _undefined, t)
  3143. }
  3144. func TestVarBlockNoConflictEval(t *testing.T) {
  3145. const SCRIPT = `
  3146. function f() {
  3147. let x;
  3148. function ff() {
  3149. {
  3150. eval("var x = 3");
  3151. }
  3152. }
  3153. ff();
  3154. }
  3155. f();
  3156. `
  3157. testScript(SCRIPT, _undefined, t)
  3158. }
  3159. func TestVarDeclCorrectScope(t *testing.T) {
  3160. const SCRIPT = `
  3161. function f() {
  3162. {
  3163. let z;
  3164. eval("var x = 3");
  3165. }
  3166. return x;
  3167. }
  3168. f();
  3169. `
  3170. testScript(SCRIPT, valueInt(3), t)
  3171. }
  3172. func TestLexicalCatch(t *testing.T) {
  3173. const SCRIPT = `
  3174. try {
  3175. throw null;
  3176. } catch (e) {
  3177. let x = 1;
  3178. function f() {}
  3179. e;
  3180. }
  3181. `
  3182. testScript(SCRIPT, _null, t)
  3183. }
  3184. func TestArgumentsLexicalDecl(t *testing.T) {
  3185. const SCRIPT = `
  3186. function f1() {
  3187. let arguments;
  3188. return arguments;
  3189. }
  3190. f1(42);
  3191. `
  3192. testScript(SCRIPT, _undefined, t)
  3193. }
  3194. func TestArgumentsLexicalDeclAssign(t *testing.T) {
  3195. const SCRIPT = `
  3196. function f1() {
  3197. let arguments = arguments;
  3198. return a;
  3199. }
  3200. assert.throws(ReferenceError, function() {
  3201. f1(42);
  3202. });
  3203. `
  3204. testScriptWithTestLib(SCRIPT, _undefined, t)
  3205. }
  3206. func TestLexicalConstModifyFromEval(t *testing.T) {
  3207. const SCRIPT = `
  3208. const x = 1;
  3209. function f() {
  3210. eval("x = 2");
  3211. }
  3212. assert.throws(TypeError, function() {
  3213. f();
  3214. });
  3215. `
  3216. testScriptWithTestLib(SCRIPT, _undefined, t)
  3217. }
  3218. func TestLexicalStrictNames(t *testing.T) {
  3219. const SCRIPT = `let eval = 1;`
  3220. _, err := Compile("", SCRIPT, true)
  3221. if err == nil {
  3222. t.Fatal("Expected an error")
  3223. }
  3224. }
  3225. func TestAssignAfterStackExpand(t *testing.T) {
  3226. // make sure the reference to the variable x does not remain stale after the stack is copied
  3227. const SCRIPT = `
  3228. function f() {
  3229. let sum = 0;
  3230. for (let i = 0; i < arguments.length; i++) {
  3231. sum += arguments[i];
  3232. }
  3233. return sum;
  3234. }
  3235. function testAssignment() {
  3236. var x = 0;
  3237. var scope = {};
  3238. with (scope) {
  3239. x = (scope.x = f(0, 0, 0, 0, 0, 0, 1, 1), 1);
  3240. }
  3241. if (scope.x !== 2) {
  3242. throw new Error('#1: scope.x === 2. Actual: ' + (scope.x));
  3243. }
  3244. if (x !== 1) {
  3245. throw new Error('#2: x === 1. Actual: ' + (x));
  3246. }
  3247. }
  3248. testAssignment();
  3249. `
  3250. testScript(SCRIPT, _undefined, t)
  3251. }
  3252. func TestArgAccessFromDynamicStash(t *testing.T) {
  3253. const SCRIPT = `
  3254. function f(arg) {
  3255. function test() {
  3256. eval("");
  3257. return a;
  3258. }
  3259. return arg;
  3260. }
  3261. f(true);
  3262. `
  3263. testScript(SCRIPT, valueTrue, t)
  3264. }
  3265. func TestLoadMixedLex(t *testing.T) {
  3266. const SCRIPT = `
  3267. function f() {
  3268. let a = 1;
  3269. {
  3270. function inner() {
  3271. eval("var a = true");
  3272. return a;
  3273. }
  3274. return inner();
  3275. }
  3276. }
  3277. f();
  3278. `
  3279. testScript(SCRIPT, valueTrue, t)
  3280. }
  3281. func TestObjectLiteralSpread(t *testing.T) {
  3282. const SCRIPT = `
  3283. let src = {prop1: 1};
  3284. Object.defineProperty(src, "prop2", {value: 2, configurable: true});
  3285. Object.defineProperty(src, "prop3", {value: 3, enumerable: true, configurable: true});
  3286. let target = {prop4: 4, ...src};
  3287. assert(deepEqual(target, {prop1: 1, prop3: 3, prop4: 4}));
  3288. `
  3289. testScriptWithTestLibX(SCRIPT, _undefined, t)
  3290. }
  3291. func TestArrayLiteralSpread(t *testing.T) {
  3292. const SCRIPT = `
  3293. let a1 = [1, 2];
  3294. let a2 = [3, 4];
  3295. let a = [...a1, 0, ...a2, 1];
  3296. assert(compareArray(a, [1, 2, 0, 3, 4, 1]));
  3297. `
  3298. testScriptWithTestLib(SCRIPT, _undefined, t)
  3299. }
  3300. func TestObjectAssignmentPattern(t *testing.T) {
  3301. const SCRIPT = `
  3302. let a, b, c;
  3303. ({a, b, c=3} = {a: 1, b: 2});
  3304. assert.sameValue(a, 1, "a");
  3305. assert.sameValue(b, 2, "b");
  3306. assert.sameValue(c, 3, "c");
  3307. `
  3308. testScriptWithTestLib(SCRIPT, _undefined, t)
  3309. }
  3310. func TestObjectAssignmentPatternNoDyn(t *testing.T) {
  3311. const SCRIPT = `
  3312. (function() {
  3313. let a, b, c;
  3314. ({a, b, c=3} = {a: 1, b: 2});
  3315. assert.sameValue(a, 1, "a");
  3316. assert.sameValue(b, 2, "b");
  3317. assert.sameValue(c, 3, "c");
  3318. })();
  3319. `
  3320. testScriptWithTestLib(SCRIPT, _undefined, t)
  3321. }
  3322. func TestObjectAssignmentPatternNested(t *testing.T) {
  3323. const SCRIPT = `
  3324. let a, b, c, d;
  3325. ({a, b, c: {d} = 3} = {a: 1, b: 2, c: {d: 4}});
  3326. assert.sameValue(a, 1, "a");
  3327. assert.sameValue(b, 2, "b");
  3328. assert.sameValue(c, undefined, "c");
  3329. assert.sameValue(d, 4, "d");
  3330. `
  3331. testScriptWithTestLib(SCRIPT, _undefined, t)
  3332. }
  3333. func TestObjectAssignmentPatternEvalOrder(t *testing.T) {
  3334. const SCRIPT = `
  3335. let trace = "";
  3336. let target_obj = {};
  3337. function src() {
  3338. trace += "src(),";
  3339. return {
  3340. get a() {
  3341. trace += "get a,";
  3342. return "a";
  3343. }
  3344. }
  3345. }
  3346. function prop1() {
  3347. trace += "prop1(),"
  3348. return {
  3349. toString: function() {
  3350. trace += "prop1-to-string(),";
  3351. return "a";
  3352. }
  3353. }
  3354. }
  3355. function prop2() {
  3356. trace += "prop2(),";
  3357. return {
  3358. toString: function() {
  3359. trace += "prop2-to-string(),";
  3360. return "b";
  3361. }
  3362. }
  3363. }
  3364. function target() {
  3365. trace += "target(),"
  3366. return target_obj;
  3367. }
  3368. let a, b;
  3369. ({[prop1()]: target().a, [prop2()]: b} = src());
  3370. if (target_obj.a !== "a") {
  3371. throw new Error("target_obj.a="+target_obj.a);
  3372. }
  3373. trace;
  3374. `
  3375. testScript(SCRIPT, asciiString("src(),prop1(),prop1-to-string(),target(),get a,prop2(),prop2-to-string(),"), t)
  3376. }
  3377. func TestArrayAssignmentPatternEvalOrder(t *testing.T) {
  3378. const SCRIPT = `
  3379. let trace = "";
  3380. let src_arr = {
  3381. [Symbol.iterator]: function() {
  3382. let done = false;
  3383. return {
  3384. next: function() {
  3385. trace += "next,";
  3386. if (!done) {
  3387. done = true;
  3388. return {value: 0};
  3389. }
  3390. return {done: true};
  3391. },
  3392. return: function() {
  3393. trace += "return,";
  3394. }
  3395. }
  3396. }
  3397. }
  3398. function src() {
  3399. trace += "src(),";
  3400. return src_arr;
  3401. }
  3402. let tgt = {
  3403. get a() {
  3404. trace += "get a,";
  3405. return "a";
  3406. },
  3407. get b() {
  3408. trace += "get b,";
  3409. return "b";
  3410. }
  3411. }
  3412. function target() {
  3413. trace += "target(),";
  3414. return tgt;
  3415. }
  3416. function default_a() {
  3417. trace += "default a,";
  3418. return "def_a";
  3419. }
  3420. function default_b() {
  3421. trace += "default b,";
  3422. return "def_b";
  3423. }
  3424. ([target().a = default_a(), target().b = default_b()] = src());
  3425. trace;
  3426. `
  3427. testScript(SCRIPT, asciiString("src(),target(),next,target(),next,default b,"), t)
  3428. }
  3429. func TestObjectAssignPatternRest(t *testing.T) {
  3430. const SCRIPT = `
  3431. let a, b, c, d;
  3432. ({a, b, c, ...d} = {a: 1, b: 2, d: 4});
  3433. assert.sameValue(a, 1, "a");
  3434. assert.sameValue(b, 2, "b");
  3435. assert.sameValue(c, undefined, "c");
  3436. assert(deepEqual(d, {d: 4}), "d");
  3437. `
  3438. testScriptWithTestLibX(SCRIPT, _undefined, t)
  3439. }
  3440. func TestObjectBindPattern(t *testing.T) {
  3441. const SCRIPT = `
  3442. let {a, b, c, ...d} = {a: 1, b: 2, d: 4};
  3443. assert.sameValue(a, 1, "a");
  3444. assert.sameValue(b, 2, "b");
  3445. assert.sameValue(c, undefined, "c");
  3446. assert(deepEqual(d, {d: 4}), "d");
  3447. var { x: y, } = { x: 23 };
  3448. assert.sameValue(y, 23);
  3449. assert.throws(ReferenceError, function() {
  3450. x;
  3451. });
  3452. `
  3453. testScriptWithTestLibX(SCRIPT, _undefined, t)
  3454. }
  3455. func TestObjLiteralShorthandWithInitializer(t *testing.T) {
  3456. const SCRIPT = `
  3457. o = {a=1};
  3458. `
  3459. _, err := Compile("", SCRIPT, false)
  3460. if err == nil {
  3461. t.Fatal("Expected an error")
  3462. }
  3463. }
  3464. func TestObjLiteralShorthandLetStringLit(t *testing.T) {
  3465. const SCRIPT = `
  3466. o = {"let"};
  3467. `
  3468. _, err := Compile("", SCRIPT, false)
  3469. if err == nil {
  3470. t.Fatal("Expected an error")
  3471. }
  3472. }
  3473. func TestObjLiteralComputedKeys(t *testing.T) {
  3474. const SCRIPT = `
  3475. let o = {
  3476. get [Symbol.toString]() {
  3477. }
  3478. }
  3479. `
  3480. testScript(SCRIPT, _undefined, t)
  3481. }
  3482. func TestObjLiteralComputedKeysEvalOrder(t *testing.T) {
  3483. const SCRIPT = `
  3484. let trace = [];
  3485. function key() {
  3486. trace.push("key");
  3487. return {
  3488. toString: function() {
  3489. trace.push("key-toString");
  3490. return "key";
  3491. }
  3492. }
  3493. }
  3494. function val() {
  3495. trace.push("val");
  3496. return "val";
  3497. }
  3498. const _ = {
  3499. [key()]: val(),
  3500. }
  3501. trace.join(",");
  3502. `
  3503. testScript(SCRIPT, asciiString("key,key-toString,val"), t)
  3504. }
  3505. func TestArrayAssignPattern(t *testing.T) {
  3506. const SCRIPT = `
  3507. let a, b;
  3508. ([a, b] = [1, 2]);
  3509. a === 1 && b === 2;
  3510. `
  3511. testScript(SCRIPT, valueTrue, t)
  3512. }
  3513. func TestArrayAssignPattern1(t *testing.T) {
  3514. const SCRIPT = `
  3515. let a, b;
  3516. ([a = 3, b = 2] = [1]);
  3517. a === 1 && b === 2;
  3518. `
  3519. testScript(SCRIPT, valueTrue, t)
  3520. }
  3521. func TestArrayAssignPatternLHS(t *testing.T) {
  3522. const SCRIPT = `
  3523. let a = {};
  3524. [ a.b, a['c'] = 2 ] = [1];
  3525. a.b === 1 && a.c === 2;
  3526. `
  3527. testScript(SCRIPT, valueTrue, t)
  3528. }
  3529. func TestArrayAssignPatternElision(t *testing.T) {
  3530. const SCRIPT = `
  3531. let a, b;
  3532. ([a,, b] = [1, 4, 2]);
  3533. a === 1 && b === 2;
  3534. `
  3535. testScript(SCRIPT, valueTrue, t)
  3536. }
  3537. func TestArrayAssignPatternRestPattern(t *testing.T) {
  3538. const SCRIPT = `
  3539. let a, b, z;
  3540. [ z, ...[a, b] ] = [0, 1, 2];
  3541. z === 0 && a === 1 && b === 2;
  3542. `
  3543. testScript(SCRIPT, valueTrue, t)
  3544. }
  3545. func TestArrayBindingPattern(t *testing.T) {
  3546. const SCRIPT = `
  3547. let [a, b] = [1, 2];
  3548. a === 1 && b === 2;
  3549. `
  3550. testScript(SCRIPT, valueTrue, t)
  3551. }
  3552. func TestObjectPatternShorthandInit(t *testing.T) {
  3553. const SCRIPT = `
  3554. [...{ x = 1 }] = [];
  3555. x;
  3556. `
  3557. testScript(SCRIPT, valueInt(1), t)
  3558. }
  3559. func TestArrayBindingPatternRestPattern(t *testing.T) {
  3560. const SCRIPT = `
  3561. const [a, b, ...[c, d]] = [1, 2, 3, 4];
  3562. a === 1 && b === 2 && c === 3 && d === 4;
  3563. `
  3564. testScript(SCRIPT, valueTrue, t)
  3565. }
  3566. func TestForVarPattern(t *testing.T) {
  3567. const SCRIPT = `
  3568. var o = {a: 1};
  3569. var trace = "";
  3570. for (var [key, value] of Object.entries(o)) {
  3571. trace += key+":"+value;
  3572. }
  3573. trace;
  3574. `
  3575. testScript(SCRIPT, asciiString("a:1"), t)
  3576. }
  3577. func TestForLexPattern(t *testing.T) {
  3578. const SCRIPT = `
  3579. var o = {a: 1};
  3580. var trace = "";
  3581. for (const [key, value] of Object.entries(o)) {
  3582. trace += key+":"+value;
  3583. }
  3584. trace;
  3585. `
  3586. testScript(SCRIPT, asciiString("a:1"), t)
  3587. }
  3588. func TestBindingPatternRestTrailingComma(t *testing.T) {
  3589. const SCRIPT = `
  3590. const [a, b, ...rest,] = [];
  3591. `
  3592. _, err := Compile("", SCRIPT, false)
  3593. if err == nil {
  3594. t.Fatal("Expected an error")
  3595. }
  3596. }
  3597. func TestAssignPatternRestTrailingComma(t *testing.T) {
  3598. const SCRIPT = `
  3599. ([a, b, ...rest,] = []);
  3600. `
  3601. _, err := Compile("", SCRIPT, false)
  3602. if err == nil {
  3603. t.Fatal("Expected an error")
  3604. }
  3605. }
  3606. func TestFuncParamInitializerSimple(t *testing.T) {
  3607. const SCRIPT = `
  3608. function f(a = 1) {
  3609. return a;
  3610. }
  3611. ""+f()+f(2);
  3612. `
  3613. testScript(SCRIPT, asciiString("12"), t)
  3614. }
  3615. func TestFuncParamObjectPatternSimple(t *testing.T) {
  3616. const SCRIPT = `
  3617. function f({a, b} = {a: 1, b: 2}) {
  3618. return "" + a + b;
  3619. }
  3620. ""+f()+" "+f({a: 3, b: 4});
  3621. `
  3622. testScript(SCRIPT, asciiString("12 34"), t)
  3623. }
  3624. func TestFuncParamRestStackSimple(t *testing.T) {
  3625. const SCRIPT = `
  3626. function f(arg1, ...rest) {
  3627. return rest;
  3628. }
  3629. let ar = f(1, 2, 3);
  3630. ar.join(",");
  3631. `
  3632. testScript(SCRIPT, asciiString("2,3"), t)
  3633. }
  3634. func TestFuncParamRestStashSimple(t *testing.T) {
  3635. const SCRIPT = `
  3636. function f(arg1, ...rest) {
  3637. eval("true");
  3638. return rest;
  3639. }
  3640. let ar = f(1, 2, 3);
  3641. ar.join(",");
  3642. `
  3643. testScript(SCRIPT, asciiString("2,3"), t)
  3644. }
  3645. func TestRestArgsNotInStash(t *testing.T) {
  3646. const SCRIPT = `
  3647. function f(...rest) {
  3648. () => rest;
  3649. return rest.length;
  3650. }
  3651. f(1,2);
  3652. `
  3653. testScript(SCRIPT, valueInt(2), t)
  3654. }
  3655. func TestRestArgsInStash(t *testing.T) {
  3656. const SCRIPT = `
  3657. function f(first, ...rest) {
  3658. () => first;
  3659. () => rest;
  3660. return rest.length;
  3661. }
  3662. f(1,2);
  3663. `
  3664. testScript(SCRIPT, valueInt(1), t)
  3665. }
  3666. func TestRestArgsInStashFwdRef(t *testing.T) {
  3667. const SCRIPT = `
  3668. function f(first = eval(), ...rest) {
  3669. () => first;
  3670. () => rest;
  3671. return rest.length === 1 && rest[0] === 2;
  3672. }
  3673. f(1,2);
  3674. `
  3675. testScript(SCRIPT, valueTrue, t)
  3676. }
  3677. func TestFuncParamRestPattern(t *testing.T) {
  3678. const SCRIPT = `
  3679. function f(arg1, ...{0: rest1, 1: rest2}) {
  3680. return ""+arg1+" "+rest1+" "+rest2;
  3681. }
  3682. f(1, 2, 3);
  3683. `
  3684. testScript(SCRIPT, asciiString("1 2 3"), t)
  3685. }
  3686. func TestFuncParamForwardRef(t *testing.T) {
  3687. const SCRIPT = `
  3688. function f(a = b + 1, b) {
  3689. return ""+a+" "+b;
  3690. }
  3691. f(1, 2);
  3692. `
  3693. testScript(SCRIPT, asciiString("1 2"), t)
  3694. }
  3695. func TestFuncParamForwardRefMissing(t *testing.T) {
  3696. const SCRIPT = `
  3697. function f(a = b + 1, b) {
  3698. return ""+a+" "+b;
  3699. }
  3700. assert.throws(ReferenceError, function() {
  3701. f();
  3702. });
  3703. `
  3704. testScriptWithTestLib(SCRIPT, _undefined, t)
  3705. }
  3706. func TestFuncParamInnerRef(t *testing.T) {
  3707. const SCRIPT = `
  3708. function f(a = inner) {
  3709. var inner = 42;
  3710. return a;
  3711. }
  3712. assert.throws(ReferenceError, function() {
  3713. f();
  3714. });
  3715. `
  3716. testScriptWithTestLib(SCRIPT, _undefined, t)
  3717. }
  3718. func TestFuncParamInnerRefEval(t *testing.T) {
  3719. const SCRIPT = `
  3720. function f(a = eval("inner")) {
  3721. var inner = 42;
  3722. return a;
  3723. }
  3724. assert.throws(ReferenceError, function() {
  3725. f();
  3726. });
  3727. `
  3728. testScriptWithTestLib(SCRIPT, _undefined, t)
  3729. }
  3730. func TestFuncParamCalleeName(t *testing.T) {
  3731. const SCRIPT = `
  3732. function f(a = f) {
  3733. var f;
  3734. return f;
  3735. }
  3736. typeof f();
  3737. `
  3738. testScript(SCRIPT, asciiString("undefined"), t)
  3739. }
  3740. func TestFuncParamVarCopy(t *testing.T) {
  3741. const SCRIPT = `
  3742. function f(a = f) {
  3743. var a;
  3744. return a;
  3745. }
  3746. typeof f();
  3747. `
  3748. testScript(SCRIPT, asciiString("function"), t)
  3749. }
  3750. func TestFuncParamScope(t *testing.T) {
  3751. const SCRIPT = `
  3752. var x = 'outside';
  3753. var probe1, probe2;
  3754. function f(
  3755. _ = probe1 = function() { return x; },
  3756. __ = (eval('var x = "inside";'), probe2 = function() { return x; })
  3757. ) {
  3758. }
  3759. f();
  3760. probe1()+" "+probe2();
  3761. `
  3762. testScript(SCRIPT, asciiString("inside inside"), t)
  3763. }
  3764. func TestDefParamsStackPtr(t *testing.T) {
  3765. const SCRIPT = `
  3766. function A() {};
  3767. A.B = function () {};
  3768. function D(message = '') {
  3769. var C = A.B;
  3770. C([1,2,3]);
  3771. };
  3772. D();
  3773. `
  3774. testScript(SCRIPT, _undefined, t)
  3775. }
  3776. func TestNestedVariadicCalls(t *testing.T) {
  3777. const SCRIPT = `
  3778. function f() {
  3779. return Array.prototype.join.call(arguments, ",");
  3780. }
  3781. f(...[1], "a", f(...[2]));
  3782. `
  3783. testScript(SCRIPT, asciiString("1,a,2"), t)
  3784. }
  3785. func TestVariadicNew(t *testing.T) {
  3786. const SCRIPT = `
  3787. function C() {
  3788. this.res = Array.prototype.join.call(arguments, ",");
  3789. }
  3790. var c = new C(...[1], "a", new C(...[2]).res);
  3791. c.res;
  3792. `
  3793. testScript(SCRIPT, asciiString("1,a,2"), t)
  3794. }
  3795. func TestVariadicUseStackVars(t *testing.T) {
  3796. const SCRIPT = `
  3797. function A(message) { return message; }
  3798. function B(...args){
  3799. return A(...args);
  3800. }
  3801. B("C");
  3802. `
  3803. testScript(SCRIPT, asciiString("C"), t)
  3804. }
  3805. func TestCatchParamPattern(t *testing.T) {
  3806. const SCRIPT = `
  3807. function f() {
  3808. let x = 3;
  3809. try {
  3810. throw {a: 1, b: 2};
  3811. } catch ({a, b, c = x}) {
  3812. let x = 99;
  3813. return ""+a+" "+b+" "+c;
  3814. }
  3815. }
  3816. f();
  3817. `
  3818. testScript(SCRIPT, asciiString("1 2 3"), t)
  3819. }
  3820. func TestArrowUseStrict(t *testing.T) {
  3821. // simple parameter list -- ok
  3822. _, err := Compile("", "(a) => {'use strict';}", false)
  3823. if err != nil {
  3824. t.Fatal(err)
  3825. }
  3826. // non-simple parameter list -- syntax error
  3827. _, err = Compile("", "(a=0) => {'use strict';}", false)
  3828. if err == nil {
  3829. t.Fatal("expected error")
  3830. }
  3831. }
  3832. func TestArrowBoxedThis(t *testing.T) {
  3833. const SCRIPT = `
  3834. var context;
  3835. fn = function() {
  3836. return (arg) => { var local; context = this; };
  3837. };
  3838. fn()();
  3839. context === this;
  3840. `
  3841. testScript(SCRIPT, valueTrue, t)
  3842. }
  3843. func TestParameterOverride(t *testing.T) {
  3844. const SCRIPT = `
  3845. function f(arg) {
  3846. var arg = arg || "default"
  3847. return arg
  3848. }
  3849. f()
  3850. `
  3851. testScript(SCRIPT, asciiString("default"), t)
  3852. }
  3853. func TestEvalInIterScope(t *testing.T) {
  3854. const SCRIPT = `
  3855. for (let a = 0; a < 1; a++) {
  3856. eval("a");
  3857. }
  3858. `
  3859. testScript(SCRIPT, valueInt(0), t)
  3860. }
  3861. func TestTemplateLiterals(t *testing.T) {
  3862. vm := New()
  3863. _, err := vm.RunString("const a = 1, b = 'b';")
  3864. if err != nil {
  3865. t.Fatal(err)
  3866. }
  3867. f := func(t *testing.T, template, expected string) {
  3868. res, err := vm.RunString(template)
  3869. if err != nil {
  3870. t.Fatal(err)
  3871. }
  3872. if actual := res.Export(); actual != expected {
  3873. t.Fatalf("Expected: %q, actual: %q", expected, actual)
  3874. }
  3875. }
  3876. t.Run("empty", func(t *testing.T) {
  3877. f(t, "``", "")
  3878. })
  3879. t.Run("noSub", func(t *testing.T) {
  3880. f(t, "`test`", "test")
  3881. })
  3882. t.Run("emptyTail", func(t *testing.T) {
  3883. f(t, "`a=${a},b=${b}`", "a=1,b=b")
  3884. })
  3885. t.Run("emptyHead", func(t *testing.T) {
  3886. f(t, "`${a},b=${b}$`", "1,b=b$")
  3887. })
  3888. t.Run("headAndTail", func(t *testing.T) {
  3889. f(t, "`a=${a},b=${b}$`", "a=1,b=b$")
  3890. })
  3891. }
  3892. func TestTaggedTemplate(t *testing.T) {
  3893. const SCRIPT = `
  3894. let res;
  3895. const o = {
  3896. tmpl() {
  3897. res = this;
  3898. return () => {};
  3899. }
  3900. }
  3901. ` +
  3902. "o.tmpl()`test`;" + `
  3903. res === o;
  3904. `
  3905. testScript(SCRIPT, valueTrue, t)
  3906. }
  3907. func TestDuplicateGlobalFunc(t *testing.T) {
  3908. const SCRIPT = `
  3909. function a(){}
  3910. function b(){ return "b" }
  3911. function c(){ return "c" }
  3912. function a(){}
  3913. b();
  3914. `
  3915. testScript(SCRIPT, asciiString("b"), t)
  3916. }
  3917. func TestDuplicateFunc(t *testing.T) {
  3918. const SCRIPT = `
  3919. function f() {
  3920. function a(){}
  3921. function b(){ return "b" }
  3922. function c(){ return "c" }
  3923. function a(){}
  3924. return b();
  3925. }
  3926. f();
  3927. `
  3928. testScript(SCRIPT, asciiString("b"), t)
  3929. }
  3930. func TestSrcLocations(t *testing.T) {
  3931. // Do not reformat, assertions depend on the line and column numbers
  3932. const SCRIPT = `
  3933. let i = {
  3934. valueOf() {
  3935. throw new Error();
  3936. }
  3937. };
  3938. try {
  3939. i++;
  3940. } catch(e) {
  3941. assertStack(e, [["test.js", "valueOf", 4, 10],
  3942. ["test.js", "", 8, 3]
  3943. ]);
  3944. }
  3945. Object.defineProperty(globalThis, "x", {
  3946. get() {
  3947. throw new Error();
  3948. },
  3949. set() {
  3950. throw new Error();
  3951. }
  3952. });
  3953. try {
  3954. x;
  3955. } catch(e) {
  3956. assertStack(e, [["test.js", "get", 17, 10],
  3957. ["test.js", "", 25, 3]
  3958. ]);
  3959. }
  3960. try {
  3961. x++;
  3962. } catch(e) {
  3963. assertStack(e, [["test.js", "get", 17, 10],
  3964. ["test.js", "", 33, 3]
  3965. ]);
  3966. }
  3967. try {
  3968. x = 2;
  3969. } catch(e) {
  3970. assertStack(e, [["test.js", "set", 20, 10],
  3971. ["test.js", "", 41, 3]
  3972. ]);
  3973. }
  3974. try {
  3975. +i;
  3976. } catch(e) {
  3977. assertStack(e, [["test.js", "valueOf", 4, 10],
  3978. ["test.js", "", 49, 4]
  3979. ]);
  3980. }
  3981. try {
  3982. let n;
  3983. n.field = {
  3984. "key1": "test",
  3985. "key2": {},
  3986. }
  3987. } catch(e) {
  3988. assertStack(e, [["test.js", "", 58, 5]
  3989. ]);
  3990. }
  3991. `
  3992. testScriptWithTestLibX(SCRIPT, _undefined, t)
  3993. }
  3994. func TestSrcLocationThrowLiteral(t *testing.T) {
  3995. vm := New()
  3996. _, err := vm.RunString(`
  3997. const z = 1;
  3998. throw "";
  3999. `)
  4000. if ex, ok := err.(*Exception); ok {
  4001. pos := ex.stack[0].Position()
  4002. if pos.Line != 3 {
  4003. t.Fatal(pos)
  4004. }
  4005. } else {
  4006. t.Fatal(err)
  4007. }
  4008. }
  4009. func TestSrcLocation(t *testing.T) {
  4010. prg := MustCompile("test.js", `
  4011. f();
  4012. var x = 1;
  4013. let y = 1;
  4014. let [z1, z2] = [0, 0];
  4015. var [z3, z4] = [0, 0];
  4016. `, false)
  4017. const (
  4018. varLine = 3
  4019. letLine = 4
  4020. dstrLetLine = 5
  4021. dstrVarLine = 7
  4022. )
  4023. linesOfInterest := map[int]string{
  4024. varLine: "var",
  4025. letLine: "let",
  4026. dstrLetLine: "destruct let",
  4027. dstrVarLine: "destruct var",
  4028. }
  4029. for i := range prg.code {
  4030. loc := prg.src.Position(prg.sourceOffset(i))
  4031. delete(linesOfInterest, loc.Line)
  4032. if len(linesOfInterest) == 0 {
  4033. break
  4034. }
  4035. }
  4036. for _, v := range linesOfInterest {
  4037. t.Fatalf("no %s line", v)
  4038. }
  4039. }
  4040. func TestBadObjectKey(t *testing.T) {
  4041. _, err := Compile("", "({!:0})", false)
  4042. if err == nil {
  4043. t.Fatal("expected error")
  4044. }
  4045. }
  4046. func TestConstantFolding(t *testing.T) {
  4047. testValues := func(prg *Program, result Value, t *testing.T) {
  4048. if len(prg.values) != 1 || !prg.values[0].SameAs(result) {
  4049. prg.dumpCode(t.Logf)
  4050. t.Fatalf("values: %v", prg.values)
  4051. }
  4052. }
  4053. f := func(src string, result Value, t *testing.T) {
  4054. prg := MustCompile("test.js", src, false)
  4055. testValues(prg, result, t)
  4056. New().testPrg(prg, result, t)
  4057. }
  4058. ff := func(src string, result Value, t *testing.T) {
  4059. prg := MustCompile("test.js", src, false)
  4060. fl := prg.code[0].(*newFunc)
  4061. testValues(fl.prg, result, t)
  4062. New().testPrg(prg, result, t)
  4063. }
  4064. t.Run("lexical binding", func(t *testing.T) {
  4065. f("const x = 1 + 2; x", valueInt(3), t)
  4066. })
  4067. t.Run("var binding", func(t *testing.T) {
  4068. f("var x = 1 + 2; x", valueInt(3), t)
  4069. })
  4070. t.Run("assignment", func(t *testing.T) {
  4071. f("x = 1 + 2; x", valueInt(3), t)
  4072. })
  4073. t.Run("object pattern", func(t *testing.T) {
  4074. f("const {x = 1 + 2} = {}; x", valueInt(3), t)
  4075. })
  4076. t.Run("array pattern", func(t *testing.T) {
  4077. f("const [x = 1 + 2] = []; x", valueInt(3), t)
  4078. })
  4079. t.Run("object literal", func(t *testing.T) {
  4080. f("var o = {x: 1 + 2}; o.x", valueInt(3), t)
  4081. })
  4082. t.Run("array literal", func(t *testing.T) {
  4083. f("var a = [3, 3, 3, 1 + 2]; a[3]", valueInt(3), t)
  4084. })
  4085. t.Run("default function parameter", func(t *testing.T) {
  4086. ff("function f(arg = 1 + 2) {return arg}; f()", valueInt(3), t)
  4087. })
  4088. t.Run("return", func(t *testing.T) {
  4089. ff("function f() {return 1 + 2}; f()", valueInt(3), t)
  4090. })
  4091. }
  4092. func TestAssignBeforeInit(t *testing.T) {
  4093. const SCRIPT = `
  4094. assert.throws(ReferenceError, () => {
  4095. a = 1;
  4096. let a;
  4097. });
  4098. assert.throws(ReferenceError, () => {
  4099. ({a, b} = {a: 1, b: 2});
  4100. let a, b;
  4101. });
  4102. assert.throws(ReferenceError, () => {
  4103. (function() {
  4104. eval("");
  4105. ({a} = {a: 1});
  4106. })();
  4107. let a;
  4108. });
  4109. assert.throws(ReferenceError, () => {
  4110. const ctx = {x: 1};
  4111. function t() {
  4112. delete ctx.x;
  4113. return 42;
  4114. }
  4115. with(ctx) {
  4116. (function() {
  4117. 'use strict';
  4118. ({x} = {x: t()});
  4119. })();
  4120. }
  4121. return ctx.x;
  4122. });
  4123. assert.throws(ReferenceError, () => {
  4124. const ctx = {x: 1};
  4125. function t() {
  4126. delete ctx.x;
  4127. return 42;
  4128. }
  4129. with(ctx) {
  4130. (function() {
  4131. 'use strict';
  4132. const src = {};
  4133. Object.defineProperty(src, "x", {
  4134. get() {
  4135. return t();
  4136. }
  4137. });
  4138. ({x} = src);
  4139. })();
  4140. }
  4141. return ctx.x;
  4142. });
  4143. `
  4144. testScriptWithTestLib(SCRIPT, _undefined, t)
  4145. }
  4146. func TestOptChainCallee(t *testing.T) {
  4147. const SCRIPT = `
  4148. var a;
  4149. assert.sameValue(a?.(true), undefined);
  4150. a = null;
  4151. assert.sameValue(a?.(), undefined);
  4152. var o = {n: null};
  4153. assert.sameValue(o.m?.(true), undefined);
  4154. assert.sameValue(o.n?.(true), undefined);
  4155. `
  4156. testScriptWithTestLib(SCRIPT, _undefined, t)
  4157. }
  4158. func TestObjectLiteralSuper(t *testing.T) {
  4159. const SCRIPT = `
  4160. const proto = {
  4161. m() {
  4162. return 40;
  4163. }
  4164. }
  4165. const o = {
  4166. m() {
  4167. return super.m() + 2;
  4168. }
  4169. }
  4170. o.__proto__ = proto;
  4171. o.m();
  4172. `
  4173. testScript(SCRIPT, intToValue(42), t)
  4174. }
  4175. func TestClassCaptureThisInFieldInit(t *testing.T) {
  4176. const SCRIPT = `
  4177. let capture;
  4178. class C {
  4179. a = () => this
  4180. }
  4181. let c = new C();
  4182. c.a() === c;
  4183. `
  4184. testScript(SCRIPT, valueTrue, t)
  4185. }
  4186. func TestClassUseThisInFieldInit(t *testing.T) {
  4187. const SCRIPT = `
  4188. let capture;
  4189. class C {
  4190. a = this
  4191. }
  4192. let c = new C();
  4193. c.a === c;
  4194. `
  4195. testScript(SCRIPT, valueTrue, t)
  4196. }
  4197. func TestClassCaptureThisInStaticFieldInit(t *testing.T) {
  4198. const SCRIPT = `
  4199. let capture;
  4200. class C {
  4201. static a = (capture = () => this, 0)
  4202. }
  4203. let c = new C();
  4204. capture() === C;
  4205. `
  4206. testScript(SCRIPT, valueTrue, t)
  4207. }
  4208. func TestClassDynCaptureThisInStaticFieldInit(t *testing.T) {
  4209. const SCRIPT = `
  4210. class C {
  4211. static a = eval("this")
  4212. }
  4213. C.a === C;
  4214. `
  4215. testScript(SCRIPT, valueTrue, t)
  4216. }
  4217. func TestCompileClass(t *testing.T) {
  4218. const SCRIPT = `
  4219. class C extends Error {
  4220. a = true;
  4221. b = 1;
  4222. ["b".toUpperCase()] = 2
  4223. static A = Math.random() < 1
  4224. constructor(message = "My Error") {
  4225. super(message);
  4226. }
  4227. static M() {
  4228. }
  4229. static M1() {
  4230. }
  4231. m() {
  4232. //return C.a;
  4233. }
  4234. m1() {
  4235. return true;
  4236. }
  4237. static {
  4238. this.supername = super.name;
  4239. }
  4240. }
  4241. let c = new C();
  4242. c.a === true && c.b === 1 && c.B === 2 && c.m1() && C.A && C.supername === "Error";
  4243. `
  4244. testScript(SCRIPT, valueTrue, t)
  4245. }
  4246. func TestSuperInEval(t *testing.T) {
  4247. const SCRIPT = `
  4248. class C extends Error {
  4249. constructor() {
  4250. eval("super()");
  4251. }
  4252. m() {
  4253. return eval("super.name");
  4254. }
  4255. }
  4256. let c = new C();
  4257. c.m() === "Error";
  4258. `
  4259. testScript(SCRIPT, valueTrue, t)
  4260. }
  4261. func TestSuperRefDot(t *testing.T) {
  4262. const SCRIPT = `
  4263. let thisGet, thisSet;
  4264. class P {
  4265. _p = 0
  4266. get p() {
  4267. thisGet = this;
  4268. return this._p;
  4269. }
  4270. set p(v) {
  4271. thisSet = this;
  4272. this._p = v;
  4273. }
  4274. }
  4275. class C extends P {
  4276. g() {
  4277. return super.p;
  4278. }
  4279. s(v) {
  4280. super.p = v;
  4281. }
  4282. inc() {
  4283. super.p++;
  4284. }
  4285. incR() {
  4286. return super.p++;
  4287. }
  4288. inc1() {
  4289. ++super.p;
  4290. }
  4291. inc1R() {
  4292. return ++super.p;
  4293. }
  4294. unary() {
  4295. return +super.p;
  4296. }
  4297. pattern() {
  4298. [super.p] = [9];
  4299. }
  4300. }
  4301. let o = new C();
  4302. assert.sameValue(o.g(), 0, "get value");
  4303. assert.sameValue(thisGet, o, "get this");
  4304. o.s(1);
  4305. assert.sameValue(o._p, 1, "set value");
  4306. assert.sameValue(thisSet, o, "set this");
  4307. thisGet = undefined;
  4308. thisSet = undefined;
  4309. o.inc();
  4310. assert.sameValue(o._p, 2, "inc value");
  4311. assert.sameValue(thisGet, o, "inc thisGet");
  4312. assert.sameValue(thisSet, o, "inc thisSet");
  4313. thisGet = undefined;
  4314. thisSet = undefined;
  4315. assert.sameValue(o.incR(), 2, "incR result");
  4316. assert.sameValue(o._p, 3, "incR value");
  4317. assert.sameValue(thisGet, o, "incR thisGet");
  4318. assert.sameValue(thisSet, o, "incR thisSet");
  4319. thisGet = undefined;
  4320. thisSet = undefined;
  4321. o.inc1();
  4322. assert.sameValue(o._p, 4, "inc1 value");
  4323. assert.sameValue(thisGet, o, "inc1 thisGet");
  4324. assert.sameValue(thisSet, o, "inc1 thisSet");
  4325. thisGet = undefined;
  4326. thisSet = undefined;
  4327. assert.sameValue(o.inc1R(), 5, "inc1R result");
  4328. assert.sameValue(o._p, 5, "inc1R value");
  4329. assert.sameValue(thisGet, o, "inc1R thisGet");
  4330. assert.sameValue(thisSet, o, "inc1R thisSet");
  4331. assert.sameValue(o.unary(), 5, "unary");
  4332. o.pattern();
  4333. assert.sameValue(o._p, 9, "pattern");
  4334. `
  4335. testScriptWithTestLib(SCRIPT, _undefined, t)
  4336. }
  4337. func TestPrivateRefDot(t *testing.T) {
  4338. const SCRIPT = `
  4339. class C {
  4340. #p = 0;
  4341. g() {
  4342. return this.#p;
  4343. }
  4344. s(v) {
  4345. this.#p = v;
  4346. }
  4347. inc() {
  4348. this.#p++;
  4349. }
  4350. incR() {
  4351. return this.#p++;
  4352. }
  4353. inc1() {
  4354. ++this.#p;
  4355. }
  4356. inc1R() {
  4357. return ++this.#p;
  4358. }
  4359. pattern() {
  4360. [this.#p] = [9];
  4361. }
  4362. }
  4363. let o = new C();
  4364. assert.sameValue(o.g(), 0, "get value");
  4365. o.s(1);
  4366. assert.sameValue(o.g(), 1, "set value");
  4367. o.inc();
  4368. assert.sameValue(o.g(), 2, "inc value");
  4369. assert.sameValue(o.incR(), 2, "incR result");
  4370. assert.sameValue(o.g(), 3, "incR value");
  4371. o.inc1();
  4372. assert.sameValue(o.g(), 4, "inc1 value");
  4373. assert.sameValue(o.inc1R(), 5, "inc1R result");
  4374. assert.sameValue(o.g(), 5, "inc1R value");
  4375. o.pattern();
  4376. assert.sameValue(o.g(), 9, "pattern");
  4377. `
  4378. testScriptWithTestLib(SCRIPT, _undefined, t)
  4379. }
  4380. func TestPrivateRefDotEval(t *testing.T) {
  4381. const SCRIPT = `
  4382. class C {
  4383. #p = 0;
  4384. g() {
  4385. return eval("this.#p");
  4386. }
  4387. s(v) {
  4388. eval("this.#p = v");
  4389. }
  4390. incR() {
  4391. return eval("this.#p++");
  4392. }
  4393. inc1R() {
  4394. return eval("++this.#p");
  4395. }
  4396. pattern() {
  4397. eval("[this.#p] = [9]");
  4398. }
  4399. }
  4400. let o = new C();
  4401. assert.sameValue(o.g(), 0, "get value");
  4402. o.s(1);
  4403. assert.sameValue(o.g(), 1, "set value");
  4404. assert.sameValue(o.incR(), 1, "incR result");
  4405. assert.sameValue(o.g(), 2, "incR value");
  4406. assert.sameValue(o.inc1R(), 3, "inc1R result");
  4407. assert.sameValue(o.g(), 3, "inc1R value");
  4408. o.pattern();
  4409. assert.sameValue(o.g(), 9, "pattern");
  4410. `
  4411. testScriptWithTestLib(SCRIPT, _undefined, t)
  4412. }
  4413. func TestSuperRefDotCallee(t *testing.T) {
  4414. const SCRIPT = `
  4415. class P {
  4416. get p() {
  4417. return function() {
  4418. return this;
  4419. };
  4420. }
  4421. }
  4422. class C extends P {
  4423. m() {
  4424. return super.p();
  4425. }
  4426. }
  4427. let o = new C();
  4428. o.m() === o;
  4429. `
  4430. testScript(SCRIPT, valueTrue, t)
  4431. }
  4432. func TestSuperRefBracket(t *testing.T) {
  4433. const SCRIPT = `
  4434. let PROP = "p";
  4435. let thisGet, thisSet;
  4436. class P {
  4437. _p = 0
  4438. get p() {
  4439. thisGet = this;
  4440. return this._p;
  4441. }
  4442. set p(v) {
  4443. thisSet = this;
  4444. this._p = v;
  4445. }
  4446. }
  4447. class C extends P {
  4448. g() {
  4449. return super[PROP];
  4450. }
  4451. s(v) {
  4452. super[PROP] = v;
  4453. }
  4454. inc() {
  4455. super[PROP]++;
  4456. }
  4457. incR() {
  4458. return super[PROP]++;
  4459. }
  4460. inc1() {
  4461. ++super[PROP];
  4462. }
  4463. inc1R() {
  4464. return ++super[PROP];
  4465. }
  4466. pattern() {
  4467. [super[PROP]] = [9];
  4468. }
  4469. }
  4470. let o = new C();
  4471. assert.sameValue(o.g(), 0, "get value");
  4472. assert.sameValue(thisGet, o, "get this");
  4473. o.s(1);
  4474. assert.sameValue(o._p, 1, "set value");
  4475. assert.sameValue(thisSet, o, "set this");
  4476. thisGet = undefined;
  4477. thisSet = undefined;
  4478. o.inc();
  4479. assert.sameValue(o._p, 2, "inc value");
  4480. assert.sameValue(thisGet, o, "inc thisGet");
  4481. assert.sameValue(thisSet, o, "inc thisSet");
  4482. thisGet = undefined;
  4483. thisSet = undefined;
  4484. assert.sameValue(o.incR(), 2, "incR result");
  4485. assert.sameValue(o._p, 3, "incR value");
  4486. assert.sameValue(thisGet, o, "incR thisGet");
  4487. assert.sameValue(thisSet, o, "incR thisSet");
  4488. thisGet = undefined;
  4489. thisSet = undefined;
  4490. o.inc1();
  4491. assert.sameValue(o._p, 4, "inc1 value");
  4492. assert.sameValue(thisGet, o, "inc1 thisGet");
  4493. assert.sameValue(thisSet, o, "inc1 thisSet");
  4494. thisGet = undefined;
  4495. thisSet = undefined;
  4496. assert.sameValue(o.inc1R(), 5, "inc1R result");
  4497. assert.sameValue(o._p, 5, "inc1R value");
  4498. assert.sameValue(thisGet, o, "inc1R thisGet");
  4499. assert.sameValue(thisSet, o, "inc1R thisSet");
  4500. o.pattern();
  4501. assert.sameValue(o._p, 9, "pattern");
  4502. `
  4503. testScriptWithTestLib(SCRIPT, _undefined, t)
  4504. }
  4505. func TestSuperRefBracketEvalOrder(t *testing.T) {
  4506. const SCRIPT = `
  4507. let keyCallCount = 0;
  4508. function key() {
  4509. keyCallCount++;
  4510. C.prototype.__proto__ = null;
  4511. return "k";
  4512. }
  4513. class C {
  4514. constructor() {
  4515. super[key()]++;
  4516. }
  4517. }
  4518. assert.throws(TypeError, () => new C());
  4519. assert.sameValue(keyCallCount, 1);
  4520. `
  4521. testScriptWithTestLib(SCRIPT, _undefined, t)
  4522. }
  4523. func TestSuperRefBracketCallee(t *testing.T) {
  4524. const SCRIPT = `
  4525. let PROP = "p";
  4526. class P {
  4527. get p() {
  4528. return function() {
  4529. return this;
  4530. };
  4531. }
  4532. }
  4533. class C extends P {
  4534. m() {
  4535. return super[PROP]();
  4536. }
  4537. }
  4538. let o = new C();
  4539. o.m() === o;
  4540. `
  4541. testScript(SCRIPT, valueTrue, t)
  4542. }
  4543. func TestSuperBaseInCtor(t *testing.T) {
  4544. const SCRIPT = `
  4545. let result;
  4546. class Derived extends Object {
  4547. constructor() {
  4548. super();
  4549. result = super.constructor === Object;
  4550. }
  4551. }
  4552. new Derived();
  4553. result;
  4554. `
  4555. testScript(SCRIPT, valueTrue, t)
  4556. }
  4557. func TestClassNamedEval(t *testing.T) {
  4558. const SCRIPT = `
  4559. const C = class {
  4560. }
  4561. C.name === "C";
  4562. `
  4563. testScript(SCRIPT, valueTrue, t)
  4564. }
  4565. func TestClassNonDerived(t *testing.T) {
  4566. const SCRIPT = `
  4567. function initF() {
  4568. }
  4569. class C {
  4570. f = initF()
  4571. }
  4572. let c = new C();
  4573. `
  4574. testScript(SCRIPT, _undefined, t)
  4575. }
  4576. func TestClassExpr(t *testing.T) {
  4577. const SCRIPT = `
  4578. typeof Object.getOwnPropertyDescriptor(class {get f() {}}.prototype, "f").get === "function";
  4579. `
  4580. testScript(SCRIPT, valueTrue, t)
  4581. }
  4582. func TestClassSuperInHeritage(t *testing.T) {
  4583. const SCRIPT = `
  4584. class P {
  4585. a() {
  4586. return Error;
  4587. }
  4588. }
  4589. class C extends P {
  4590. m() {
  4591. class Inner extends super.a() {
  4592. }
  4593. return new Inner();
  4594. }
  4595. }
  4596. new C().m() instanceof Error;
  4597. `
  4598. testScript(SCRIPT, valueTrue, t)
  4599. }
  4600. func TestClassSuperInHeritageExpr(t *testing.T) {
  4601. const SCRIPT = `
  4602. class P {
  4603. a() {
  4604. return Error;
  4605. }
  4606. }
  4607. class C extends P {
  4608. m() {
  4609. function f(cls) {
  4610. return new cls();
  4611. }
  4612. return f(class Inner extends super.a() {
  4613. })
  4614. }
  4615. }
  4616. new C().m() instanceof Error;
  4617. `
  4618. testScript(SCRIPT, valueTrue, t)
  4619. }
  4620. func TestClassReferToBinding(t *testing.T) {
  4621. const SCRIPT = `
  4622. const CC = class C {
  4623. static T = 40
  4624. f = C.T + 2
  4625. }
  4626. let c = new CC();
  4627. c.f;
  4628. `
  4629. testScript(SCRIPT, intToValue(42), t)
  4630. }
  4631. func TestClassReferToBindingInStaticDecl(t *testing.T) {
  4632. const SCRIPT = `
  4633. class C {
  4634. static T = C.name
  4635. }
  4636. C.T;
  4637. `
  4638. testScript(SCRIPT, asciiString("C"), t)
  4639. }
  4640. func TestClassReferToBindingInStaticEval(t *testing.T) {
  4641. const SCRIPT = `
  4642. const CC = class C {
  4643. static T = eval("C.name")
  4644. }
  4645. CC.T;
  4646. `
  4647. testScript(SCRIPT, asciiString("C"), t)
  4648. }
  4649. func TestClassReferToBindingFromHeritage(t *testing.T) {
  4650. const SCRIPT = `
  4651. assert.throws(ReferenceError, () => {
  4652. class C extends C {
  4653. }
  4654. });
  4655. `
  4656. testScriptWithTestLib(SCRIPT, _undefined, t)
  4657. }
  4658. func TestClassCaptureSuperCallInArrowFunc(t *testing.T) {
  4659. const SCRIPT = `
  4660. let f;
  4661. class C extends class {} {
  4662. constructor() {
  4663. f = () => super();
  4664. f();
  4665. }
  4666. }
  4667. let c = new C();
  4668. `
  4669. testScript(SCRIPT, _undefined, t)
  4670. }
  4671. func TestClassCaptureSuperCallInNestedArrowFunc(t *testing.T) {
  4672. const SCRIPT = `
  4673. let f;
  4674. class P {
  4675. }
  4676. class C extends P {
  4677. constructor() {
  4678. f = () => () => super();
  4679. f()();
  4680. }
  4681. }
  4682. new C() instanceof P;
  4683. `
  4684. testScript(SCRIPT, valueTrue, t)
  4685. }
  4686. func TestThisInEval(t *testing.T) {
  4687. const SCRIPT = `
  4688. assert.sameValue(eval("this"), this, "global");
  4689. let o = {
  4690. f() {
  4691. return eval("this");
  4692. }
  4693. }
  4694. assert.sameValue(o.f(), o, "obj literal");
  4695. `
  4696. testScriptWithTestLib(SCRIPT, _undefined, t)
  4697. }
  4698. func TestStaticAsBindingTarget(t *testing.T) {
  4699. const SCRIPT = `
  4700. let [static] = [];
  4701. `
  4702. testScript(SCRIPT, _undefined, t)
  4703. }
  4704. func TestEvalInStaticFieldInit(t *testing.T) {
  4705. const SCRIPT = `
  4706. var C = class {
  4707. static f = 'test';
  4708. static g = this.f + '262';
  4709. static h = eval('this.g') + 'test';
  4710. }
  4711. C.f === "test" && C.g === "test262" && C.h === "test262test";
  4712. `
  4713. testScript(SCRIPT, valueTrue, t)
  4714. }
  4715. func TestClassPrivateElemInEval(t *testing.T) {
  4716. const SCRIPT = `
  4717. let f1, f2;
  4718. class C extends (f1 = o => eval("o.#a"), Object) {
  4719. static #a = 42;
  4720. static {
  4721. f2 = o => eval("o.#a");
  4722. assert.sameValue(C.#a, 42);
  4723. assert.sameValue((() => C.#a)(), 42);
  4724. }
  4725. }
  4726. assert.throws(SyntaxError, () => f1(C));
  4727. assert.sameValue(f2(C), 42);
  4728. `
  4729. testScriptWithTestLib(SCRIPT, _undefined, t)
  4730. }
  4731. func TestClassPrivateElemInIndirectEval(t *testing.T) {
  4732. const SCRIPT = `
  4733. let f1, f2;
  4734. class C extends (f1 = o => (0, eval)("o.#a"), Object) {
  4735. static #a = 42;
  4736. static {
  4737. f2 = o => (0, eval)("o.#a");
  4738. assert.throws(SyntaxError, () => (0, eval)("C.#a"));
  4739. }
  4740. }
  4741. assert.throws(SyntaxError, () => f1(C));
  4742. assert.throws(SyntaxError, () => f2(C));
  4743. `
  4744. testScriptWithTestLib(SCRIPT, _undefined, t)
  4745. }
  4746. func TestClassPrivateElemInFunction(t *testing.T) {
  4747. const SCRIPT = `
  4748. assert.throws(SyntaxError, () => {
  4749. class C {
  4750. static #a = 42;
  4751. static {
  4752. Function("o", "return o.#a");
  4753. }
  4754. }
  4755. });
  4756. `
  4757. testScriptWithTestLib(SCRIPT, _undefined, t)
  4758. }
  4759. func TestClassPrivateElementsDecl(t *testing.T) {
  4760. const SCRIPT = `
  4761. class C {
  4762. #a = 42;
  4763. get #b() {}
  4764. set #b(_) {}
  4765. get c() {
  4766. return this.#a;
  4767. }
  4768. #m() {
  4769. return this.#a;
  4770. }
  4771. static getter(inst) {
  4772. return inst.#m();
  4773. }
  4774. }
  4775. let c = new C();
  4776. c.c + C.getter(c);
  4777. `
  4778. testScript(SCRIPT, intToValue(84), t)
  4779. }
  4780. func TestPrivateIn(t *testing.T) {
  4781. const SCRIPT = `
  4782. class C {
  4783. #a = 42;
  4784. static check(inst) {
  4785. return #a in inst;
  4786. }
  4787. }
  4788. let c = new C();
  4789. C.check(c);
  4790. `
  4791. testScript(SCRIPT, valueTrue, t)
  4792. }
  4793. func TestDeletePropOfNonObject(t *testing.T) {
  4794. const SCRIPT = `
  4795. delete 'Test262'[100] && delete 'Test262'.a && delete 'Test262'['@'];
  4796. `
  4797. testScript(SCRIPT, valueTrue, t)
  4798. }
  4799. func TestKeywordsAsLabels(t *testing.T) {
  4800. const SCRIPT = `
  4801. let: for (let i = 0; i < 2; i++) {
  4802. if (i === 0) continue let;
  4803. break let;
  4804. }
  4805. \u006Cet: for (let i = 0; i < 2; i++) {
  4806. if (i === 0) continue \u006Cet;
  4807. break \u006Cet;
  4808. }
  4809. yield: for (let i = 0; i < 2; i++) {
  4810. if (i === 0) continue yield;
  4811. break yield;
  4812. }
  4813. yi\u0065ld: for (let i = 0; i < 2; i++) {
  4814. if (i === 0) continue yi\u0065ld;
  4815. break yi\u0065ld;
  4816. }
  4817. `
  4818. testScript(SCRIPT, _undefined, t)
  4819. }
  4820. func TestThisResolutionWithArg(t *testing.T) {
  4821. const SCRIPT = `
  4822. let capture;
  4823. function f(arg) {
  4824. capture = () => this; // move 'this' to stash
  4825. return [this, arg];
  4826. }
  4827. const _this = {};
  4828. const arg = {};
  4829. const [_this1, arg1] = f.call(_this, arg);
  4830. _this1 === _this && arg1 === arg;
  4831. `
  4832. testScript(SCRIPT, valueTrue, t)
  4833. }
  4834. func TestThisResolutionArgInStash(t *testing.T) {
  4835. const SCRIPT = `
  4836. let capture;
  4837. function f(arg) {
  4838. capture = () => this + arg; // move 'this' and arguments to stash
  4839. return [this, arg];
  4840. }
  4841. const _this = {};
  4842. const arg = {};
  4843. const [_this1, arg1] = f.call(_this, arg);
  4844. _this1 === _this && arg1 === arg;
  4845. `
  4846. testScript(SCRIPT, valueTrue, t)
  4847. }
  4848. func TestThisResolutionWithStackVar(t *testing.T) {
  4849. const SCRIPT = `
  4850. let capture;
  4851. function f(arg) {
  4852. const _ = 1; // a stack variable
  4853. capture = () => this + arg; // move 'this' and arguments to stash
  4854. return [this, arg];
  4855. }
  4856. const _this = {};
  4857. const arg = {};
  4858. const [_this1, arg1] = f.call(_this, arg);
  4859. _this1 === _this && arg1 === arg;
  4860. `
  4861. testScript(SCRIPT, valueTrue, t)
  4862. }
  4863. func TestForInLoopContinue(t *testing.T) {
  4864. const SCRIPT = `
  4865. var globalSink;
  4866. (function() {
  4867. const data = [{disabled: true}, {}];
  4868. function dummy() {}
  4869. function f1() {}
  4870. function f() {
  4871. dummy(); // move dummy to stash (so that f1 is at index 1)
  4872. for (const d of data) {
  4873. if (d.disabled) continue;
  4874. globalSink = () => d; // move d to stash
  4875. f1();
  4876. }
  4877. }
  4878. f();
  4879. })();
  4880. `
  4881. testScript(SCRIPT, _undefined, t)
  4882. }
  4883. func TestForInLoopContinueOuter(t *testing.T) {
  4884. const SCRIPT = `
  4885. var globalSink;
  4886. (function() {
  4887. const data = [{disabled: true}, {}];
  4888. function dummy1() {}
  4889. function f1() {}
  4890. function f() {
  4891. dummy1();
  4892. let counter = 0;
  4893. OUTER: for (let i = 0; i < 1; i++) {
  4894. for (const d of data) {
  4895. if (d.disabled) continue OUTER;
  4896. globalSink = () => d;
  4897. }
  4898. counter++;
  4899. }
  4900. f1();
  4901. if (counter !== 0) {
  4902. throw new Error(counter);
  4903. }
  4904. }
  4905. f();
  4906. })();
  4907. `
  4908. testScript(SCRIPT, _undefined, t)
  4909. }
  4910. func TestLexicalDeclInSwitch(t *testing.T) {
  4911. const SCRIPT = `
  4912. switch(0) {
  4913. case 1:
  4914. if (false) b = 3;
  4915. case 2:
  4916. const c = 1;
  4917. }
  4918. `
  4919. testScript(SCRIPT, _undefined, t)
  4920. }
  4921. func TestClassFieldSpecial(t *testing.T) {
  4922. const SCRIPT = `
  4923. class C {
  4924. get;
  4925. set;
  4926. async;
  4927. static;
  4928. }
  4929. `
  4930. testScript(SCRIPT, _undefined, t)
  4931. }
  4932. func TestClassMethodSpecial(t *testing.T) {
  4933. const SCRIPT = `
  4934. class C {
  4935. get() {}
  4936. set() {}
  4937. async() {}
  4938. static() {}
  4939. }
  4940. `
  4941. testScript(SCRIPT, _undefined, t)
  4942. }
  4943. func TestClassMethodNumLiteral(t *testing.T) {
  4944. const SCRIPT = `
  4945. class C {
  4946. 0() {
  4947. return true;
  4948. }
  4949. }
  4950. new C()[0]();
  4951. `
  4952. testScript(SCRIPT, valueTrue, t)
  4953. }
  4954. func TestAsyncFunc(t *testing.T) {
  4955. const SCRIPT = `
  4956. async (x = true, y) => {};
  4957. async x => {};
  4958. let passed = false;
  4959. async function f() {
  4960. return true;
  4961. }
  4962. async function f1(arg = true) {
  4963. passed = await f();
  4964. }
  4965. await f1();
  4966. return passed;
  4967. `
  4968. testAsyncFunc(SCRIPT, valueTrue, t)
  4969. }
  4970. func TestObjectLiteralComputedMethodKeys(t *testing.T) {
  4971. _, err := Compile("", `
  4972. ({
  4973. ["__proto__"]() {},
  4974. ["__proto__"]() {}
  4975. })
  4976. `, false)
  4977. if err != nil {
  4978. t.Fatal(err)
  4979. }
  4980. _, err = Compile("", `
  4981. ({
  4982. get ["__proto__"]() {},
  4983. get ["__proto__"]() {}
  4984. })
  4985. `, false)
  4986. if err != nil {
  4987. t.Fatal(err)
  4988. }
  4989. }
  4990. func TestGeneratorFunc(t *testing.T) {
  4991. const SCRIPT = `
  4992. let trace = "";
  4993. function defParam() {
  4994. trace += "1";
  4995. return "def";
  4996. }
  4997. function* g(param = defParam()) {
  4998. const THREE = 3;
  4999. trace += "2";
  5000. assert.sameValue(Math.floor(yield 1), THREE);
  5001. return 42;
  5002. }
  5003. let iter = g();
  5004. assert.sameValue(trace, "1");
  5005. let next = iter.next();
  5006. assert.sameValue(next.value, 1);
  5007. assert.sameValue(next.done, false);
  5008. next = iter.next(Math.PI);
  5009. assert.sameValue(next.value, 42);
  5010. assert.sameValue(next.done, true);
  5011. assert.sameValue(trace, "12");
  5012. `
  5013. testScriptWithTestLib(SCRIPT, _undefined, t)
  5014. }
  5015. func TestGeneratorMethods(t *testing.T) {
  5016. const SCRIPT = `
  5017. class C {
  5018. *g(param) {
  5019. yield 1;
  5020. yield 2;
  5021. }
  5022. }
  5023. let c = new C();
  5024. let iter = c.g();
  5025. let res = iter.next();
  5026. assert.sameValue(res.value, 1);
  5027. assert.sameValue(res.done, false);
  5028. res = iter.next();
  5029. assert.sameValue(res.value, 2);
  5030. assert.sameValue(res.done, false);
  5031. res = iter.next();
  5032. assert.sameValue(res.value, undefined);
  5033. assert.sameValue(res.done, true);
  5034. `
  5035. testScriptWithTestLib(SCRIPT, _undefined, t)
  5036. }
  5037. func TestFunctionBodyClassDecl(t *testing.T) {
  5038. const SCRIPT = `
  5039. function as(requiredArgument = {}) {
  5040. class something { }
  5041. };
  5042. `
  5043. _, err := Compile("", SCRIPT, false)
  5044. if err != nil {
  5045. t.Fatal(err)
  5046. }
  5047. }
  5048. /*
  5049. func TestBabel(t *testing.T) {
  5050. src, err := os.ReadFile("babel7.js")
  5051. if err != nil {
  5052. t.Fatal(err)
  5053. }
  5054. vm := New()
  5055. _, err = vm.RunString(string(src))
  5056. if err != nil {
  5057. t.Fatal(err)
  5058. }
  5059. _, err = vm.RunString(`var result = Babel.transform("", {presets: ["es2015"]});`)
  5060. if err != nil {
  5061. t.Fatal(err)
  5062. }
  5063. }*/
  5064. func BenchmarkCompile(b *testing.B) {
  5065. data, err := os.ReadFile("testdata/S15.10.2.12_A1_T1.js")
  5066. if err != nil {
  5067. b.Fatal(err)
  5068. }
  5069. src := string(data)
  5070. for i := 0; i < b.N; i++ {
  5071. _, err := Compile("test.js", src, false)
  5072. if err != nil {
  5073. b.Fatal(err)
  5074. }
  5075. }
  5076. }