compiler_test.go 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748
  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 TestContinueLoop(t *testing.T) {
  972. const SCRIPT = `
  973. function A() {
  974. var r = 0;
  975. for (var i = 0; i < 5; i++) {
  976. if (i > 1) {
  977. continue;
  978. }
  979. r++;
  980. }
  981. return r;
  982. }
  983. A();
  984. `
  985. testScript(SCRIPT, intToValue(2), t)
  986. }
  987. func TestContinueOutOfTry(t *testing.T) {
  988. const SCRIPT = `
  989. function A() {
  990. var r = 0;
  991. for (var i = 0; i < 5; i++) {
  992. try {
  993. if (i > 1) {
  994. continue;
  995. }
  996. } catch(e) {
  997. return 99;
  998. }
  999. r++;
  1000. }
  1001. return r;
  1002. }
  1003. A();
  1004. `
  1005. testScript(SCRIPT, intToValue(2), t)
  1006. }
  1007. func TestThisInCatch(t *testing.T) {
  1008. const SCRIPT = `
  1009. function O() {
  1010. try {
  1011. f();
  1012. } catch (e) {
  1013. this.value = e.toString();
  1014. }
  1015. }
  1016. function f() {
  1017. throw "ex";
  1018. }
  1019. var o = new O();
  1020. o.value;
  1021. `
  1022. testScript(SCRIPT, asciiString("ex"), t)
  1023. }
  1024. func TestNestedTry(t *testing.T) {
  1025. const SCRIPT = `
  1026. var ex;
  1027. try {
  1028. throw "ex1";
  1029. } catch (er1) {
  1030. try {
  1031. throw "ex2";
  1032. } catch (er1) {
  1033. ex = er1;
  1034. }
  1035. }
  1036. ex;
  1037. `
  1038. testScript(SCRIPT, asciiString("ex2"), t)
  1039. }
  1040. func TestNestedTryInStashlessFunc(t *testing.T) {
  1041. const SCRIPT = `
  1042. function f() {
  1043. var ex1, ex2;
  1044. try {
  1045. throw "ex1";
  1046. } catch (er1) {
  1047. try {
  1048. throw "ex2";
  1049. } catch (er1) {
  1050. ex2 = er1;
  1051. }
  1052. ex1 = er1;
  1053. }
  1054. return ex1 == "ex1" && ex2 == "ex2";
  1055. }
  1056. f();
  1057. `
  1058. testScript(SCRIPT, valueTrue, t)
  1059. }
  1060. func TestEvalLexicalDecl(t *testing.T) {
  1061. const SCRIPT = `
  1062. eval("let x = true; x;");
  1063. `
  1064. testScript(SCRIPT, valueTrue, t)
  1065. }
  1066. func TestEvalInCatchInStashlessFunc(t *testing.T) {
  1067. const SCRIPT = `
  1068. function f() {
  1069. var ex;
  1070. try {
  1071. throw "ex1";
  1072. } catch (er1) {
  1073. eval("ex = er1");
  1074. }
  1075. return ex;
  1076. }
  1077. f();
  1078. `
  1079. testScript(SCRIPT, asciiString("ex1"), t)
  1080. }
  1081. func TestCatchClosureInStashlessFunc(t *testing.T) {
  1082. const SCRIPT = `
  1083. function f() {
  1084. var ex;
  1085. try {
  1086. throw "ex1";
  1087. } catch (er1) {
  1088. return function() {
  1089. return er1;
  1090. }
  1091. }
  1092. }
  1093. f()();
  1094. `
  1095. testScript(SCRIPT, asciiString("ex1"), t)
  1096. }
  1097. func TestCatchVarNotUsedInStashlessFunc(t *testing.T) {
  1098. const SCRIPT = `
  1099. function f() {
  1100. var ex;
  1101. try {
  1102. throw "ex1";
  1103. } catch (er1) {
  1104. ex = "ok";
  1105. }
  1106. return ex;
  1107. }
  1108. f();
  1109. `
  1110. testScript(SCRIPT, asciiString("ok"), t)
  1111. }
  1112. func TestNew(t *testing.T) {
  1113. const SCRIPT = `
  1114. function O() {
  1115. this.x = 42;
  1116. }
  1117. new O().x;
  1118. `
  1119. testScript(SCRIPT, intToValue(42), t)
  1120. }
  1121. func TestStringConstructor(t *testing.T) {
  1122. const SCRIPT = `
  1123. function F() {
  1124. return String(33) + " " + String("cows");
  1125. }
  1126. F();
  1127. `
  1128. testScript(SCRIPT, asciiString("33 cows"), t)
  1129. }
  1130. func TestError(t *testing.T) {
  1131. const SCRIPT = `
  1132. function F() {
  1133. return new Error("test");
  1134. }
  1135. var e = F();
  1136. e.message == "test" && e.name == "Error";
  1137. `
  1138. testScript(SCRIPT, valueTrue, t)
  1139. }
  1140. func TestTypeError(t *testing.T) {
  1141. const SCRIPT = `
  1142. function F() {
  1143. return new TypeError("test");
  1144. }
  1145. var e = F();
  1146. e.message == "test" && e.name == "TypeError";
  1147. `
  1148. testScript(SCRIPT, valueTrue, t)
  1149. }
  1150. func TestToString(t *testing.T) {
  1151. const SCRIPT = `
  1152. var o = {x: 42};
  1153. o.toString = function() {
  1154. return String(this.x);
  1155. }
  1156. var o1 = {};
  1157. o.toString() + " ### " + o1.toString();
  1158. `
  1159. testScript(SCRIPT, asciiString("42 ### [object Object]"), t)
  1160. }
  1161. func TestEvalOrder(t *testing.T) {
  1162. const SCRIPT = `
  1163. var o = {f: function() {return 42}, x: 0};
  1164. var trace = "";
  1165. function F1() {
  1166. trace += "First!";
  1167. return o;
  1168. }
  1169. function F2() {
  1170. trace += "Second!";
  1171. return "f";
  1172. }
  1173. function F3() {
  1174. trace += "Third!";
  1175. }
  1176. var rv = F1()[F2()](F3());
  1177. rv += trace;
  1178. rv;
  1179. `
  1180. testScript(SCRIPT, asciiString("42First!Second!Third!"), t)
  1181. }
  1182. func TestPostfixIncBracket(t *testing.T) {
  1183. const SCRIPT = `
  1184. var o = {x: 42};
  1185. var trace = "";
  1186. function F1() {
  1187. trace += "First!";
  1188. return o;
  1189. }
  1190. function F2() {
  1191. trace += "Second!";
  1192. return "x";
  1193. }
  1194. var rv = F1()[F2()]++;
  1195. rv + trace + o.x;
  1196. `
  1197. testScript(SCRIPT, asciiString("42First!Second!43"), t)
  1198. }
  1199. func TestPostfixIncDot(t *testing.T) {
  1200. const SCRIPT = `
  1201. var o = {x: 42};
  1202. var trace = "";
  1203. function F1() {
  1204. trace += "First!";
  1205. return o;
  1206. }
  1207. var rv = F1().x++;
  1208. rv + trace + o.x;
  1209. `
  1210. testScript(SCRIPT, asciiString("42First!43"), t)
  1211. }
  1212. func TestPrefixIncBracket(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. function F2() {
  1221. trace += "Second!";
  1222. return "x";
  1223. }
  1224. var rv = ++F1()[F2()];
  1225. rv + trace + o.x;
  1226. `
  1227. testScript(SCRIPT, asciiString("43First!Second!43"), t)
  1228. }
  1229. func TestPrefixIncDot(t *testing.T) {
  1230. const SCRIPT = `
  1231. var o = {x: 42};
  1232. var trace = "";
  1233. function F1() {
  1234. trace += "First!";
  1235. return o;
  1236. }
  1237. var rv = ++F1().x;
  1238. rv + trace + o.x;
  1239. `
  1240. testScript(SCRIPT, asciiString("43First!43"), t)
  1241. }
  1242. func TestPostDecObj(t *testing.T) {
  1243. const SCRIPT = `
  1244. var object = {valueOf: function() {return 1}};
  1245. var y = object--;
  1246. var ok = false;
  1247. if (y === 1) {
  1248. ok = true;
  1249. }
  1250. ok;
  1251. `
  1252. testScript(SCRIPT, valueTrue, t)
  1253. }
  1254. func TestPropAcc1(t *testing.T) {
  1255. const SCRIPT = `
  1256. 1..toString()
  1257. `
  1258. testScript(SCRIPT, asciiString("1"), t)
  1259. }
  1260. func TestEvalDirect(t *testing.T) {
  1261. const SCRIPT = `
  1262. var rv = false;
  1263. function foo(){ rv = true; }
  1264. var o = { };
  1265. function f() {
  1266. try {
  1267. eval("o.bar( foo() );");
  1268. } catch (e) {
  1269. }
  1270. }
  1271. f();
  1272. rv;
  1273. `
  1274. testScript(SCRIPT, valueTrue, t)
  1275. }
  1276. func TestEvalRet(t *testing.T) {
  1277. const SCRIPT = `
  1278. eval("for (var i = 0; i < 3; i++) {i}")
  1279. `
  1280. testScript(SCRIPT, valueInt(2), t)
  1281. }
  1282. func TestEvalFunctionDecl(t *testing.T) {
  1283. const SCRIPT = `
  1284. eval("function F() {}")
  1285. `
  1286. testScript(SCRIPT, _undefined, t)
  1287. }
  1288. func TestEvalFunctionExpr(t *testing.T) {
  1289. const SCRIPT = `
  1290. eval("(function F() {return 42;})")()
  1291. `
  1292. testScript(SCRIPT, intToValue(42), t)
  1293. }
  1294. func TestEvalDirectScope(t *testing.T) {
  1295. const SCRIPT = `
  1296. var __10_4_2_1_3 = "str";
  1297. function testcase() {
  1298. var __10_4_2_1_3 = "str1";
  1299. try {
  1300. throw "error";
  1301. } catch (e) {
  1302. var __10_4_2_1_3 = "str2";
  1303. return eval("__10_4_2_1_3");
  1304. }
  1305. }
  1306. testcase();
  1307. `
  1308. testScript(SCRIPT, asciiString("str2"), t)
  1309. }
  1310. func TestEvalDirectScope1(t *testing.T) {
  1311. const SCRIPT = `
  1312. 'use strict';
  1313. var __10_4_2_1_5 = "str";
  1314. function testcase() {
  1315. var __10_4_2_1_5 = "str1";
  1316. var r = eval("\
  1317. var __10_4_2_1_5 = \'str2\'; \
  1318. eval(\"\'str2\' === __10_4_2_1_5\")\
  1319. ");
  1320. return r;
  1321. }
  1322. testcase();
  1323. `
  1324. testScript(SCRIPT, valueTrue, t)
  1325. }
  1326. func TestEvalDirectCreateBinding(t *testing.T) {
  1327. const SCRIPT = `
  1328. function f() {
  1329. eval("var x = true");
  1330. return x;
  1331. }
  1332. var res = f();
  1333. var thrown = false;
  1334. try {
  1335. x;
  1336. } catch(e) {
  1337. if (e instanceof ReferenceError) {
  1338. thrown = true;
  1339. } else {
  1340. throw e;
  1341. }
  1342. }
  1343. res && thrown;
  1344. `
  1345. testScript(SCRIPT, valueTrue, t)
  1346. }
  1347. func TestEvalDirectCreateBinding1(t *testing.T) {
  1348. const SCRIPT = `
  1349. function f() {
  1350. eval("let x = 1; var y = 2; function f1() {return x};");
  1351. assert.throws(ReferenceError, function() { x });
  1352. return ""+y+f1();
  1353. }
  1354. f();
  1355. `
  1356. testScriptWithTestLib(SCRIPT, asciiString("21"), t)
  1357. }
  1358. func TestEvalDirectCreateBinding3(t *testing.T) {
  1359. const SCRIPT = `
  1360. function f() {
  1361. let x;
  1362. try {
  1363. eval("var y=1, x=2");
  1364. } catch(e) {}
  1365. return y;
  1366. }
  1367. assert.throws(ReferenceError, f);
  1368. `
  1369. testScriptWithTestLib(SCRIPT, _undefined, t)
  1370. }
  1371. func TestEvalGlobalStrict(t *testing.T) {
  1372. const SCRIPT = `
  1373. 'use strict';
  1374. var evalStr =
  1375. 'for (var x in this) {\n'+
  1376. ' if ( x === \'Math\' ) {\n'+
  1377. ' }\n'+
  1378. '}\n';
  1379. eval(evalStr);
  1380. `
  1381. testScript(SCRIPT, _undefined, t)
  1382. }
  1383. func TestEvalEmptyStrict(t *testing.T) {
  1384. const SCRIPT = `
  1385. 'use strict';
  1386. eval("");
  1387. `
  1388. testScript(SCRIPT, _undefined, t)
  1389. }
  1390. func TestEvalFuncDecl(t *testing.T) {
  1391. const SCRIPT = `
  1392. 'use strict';
  1393. var funcA = eval("function __funcA(__arg){return __arg;}; __funcA");
  1394. typeof funcA;
  1395. `
  1396. testScript(SCRIPT, asciiString("function"), t)
  1397. }
  1398. func TestGetAfterSet(t *testing.T) {
  1399. const SCRIPT = `
  1400. function f() {
  1401. var x = 1;
  1402. return x;
  1403. }
  1404. `
  1405. testScript(SCRIPT, _undefined, t)
  1406. }
  1407. func TestForLoopRet(t *testing.T) {
  1408. const SCRIPT = `
  1409. for (var i = 0; i < 20; i++) { if (i > 2) {break;} else { i }}
  1410. `
  1411. testScript(SCRIPT, _undefined, t)
  1412. }
  1413. func TestForLoopRet1(t *testing.T) {
  1414. const SCRIPT = `
  1415. for (var i = 0; i < 20; i++) { if (i > 2) {42;; {L:{break;}}} else { i }}
  1416. `
  1417. testScript(SCRIPT, intToValue(42), t)
  1418. }
  1419. func TestForInLoopRet(t *testing.T) {
  1420. const SCRIPT = `
  1421. var o = [1, 2, 3, 4];
  1422. for (var i in o) { if (i > 2) {break;} else { i }}
  1423. `
  1424. testScript(SCRIPT, _undefined, t)
  1425. }
  1426. func TestForInLoopRet1(t *testing.T) {
  1427. const SCRIPT = `
  1428. var o = {};
  1429. o.x = 1;
  1430. o.y = 2;
  1431. for (var i in o) {
  1432. true;
  1433. }
  1434. `
  1435. testScript(SCRIPT, valueTrue, t)
  1436. }
  1437. func TestDoWhileLoopRet(t *testing.T) {
  1438. const SCRIPT = `
  1439. var i = 0;
  1440. do {
  1441. if (i > 2) {
  1442. break;
  1443. } else {
  1444. i;
  1445. }
  1446. } while (i++ < 20);
  1447. `
  1448. testScript(SCRIPT, _undefined, t)
  1449. }
  1450. func TestDoWhileContinueRet(t *testing.T) {
  1451. const SCRIPT = `
  1452. var i = 0;
  1453. do {
  1454. if (i > 2) {
  1455. true;
  1456. continue;
  1457. } else {
  1458. i;
  1459. }
  1460. } while (i++ < 20);
  1461. `
  1462. testScript(SCRIPT, valueTrue, t)
  1463. }
  1464. func TestWhileLoopRet(t *testing.T) {
  1465. const SCRIPT = `
  1466. var i; while (i < 20) { if (i > 2) {break;} else { i++ }}
  1467. `
  1468. testScript(SCRIPT, _undefined, t)
  1469. }
  1470. func TestLoopRet1(t *testing.T) {
  1471. const SCRIPT = `
  1472. for (var i = 0; i < 20; i++) { }
  1473. `
  1474. testScript(SCRIPT, _undefined, t)
  1475. }
  1476. func TestInstanceof(t *testing.T) {
  1477. const SCRIPT = `
  1478. var rv;
  1479. try {
  1480. true();
  1481. } catch (e) {
  1482. rv = e instanceof TypeError;
  1483. }
  1484. rv;
  1485. `
  1486. testScript(SCRIPT, valueTrue, t)
  1487. }
  1488. func TestStrictAssign(t *testing.T) {
  1489. const SCRIPT = `
  1490. 'use strict';
  1491. var rv;
  1492. var called = false;
  1493. function F() {
  1494. called = true;
  1495. return 1;
  1496. }
  1497. try {
  1498. x = F();
  1499. } catch (e) {
  1500. rv = e instanceof ReferenceError;
  1501. }
  1502. rv + " " + called;
  1503. `
  1504. testScript(SCRIPT, asciiString("true true"), t)
  1505. }
  1506. func TestStrictScope(t *testing.T) {
  1507. const SCRIPT = `
  1508. var rv;
  1509. var called = false;
  1510. function F() {
  1511. 'use strict';
  1512. x = 1;
  1513. }
  1514. try {
  1515. F();
  1516. } catch (e) {
  1517. rv = e instanceof ReferenceError;
  1518. }
  1519. x = 1;
  1520. rv + " " + x;
  1521. `
  1522. testScript(SCRIPT, asciiString("true 1"), t)
  1523. }
  1524. func TestStringObj(t *testing.T) {
  1525. const SCRIPT = `
  1526. var s = new String("test");
  1527. s[0] + s[2] + s[1];
  1528. `
  1529. testScript(SCRIPT, asciiString("tse"), t)
  1530. }
  1531. func TestStringPrimitive(t *testing.T) {
  1532. const SCRIPT = `
  1533. var s = "test";
  1534. s[0] + s[2] + s[1];
  1535. `
  1536. testScript(SCRIPT, asciiString("tse"), t)
  1537. }
  1538. func TestCallGlobalObject(t *testing.T) {
  1539. const SCRIPT = `
  1540. var rv;
  1541. try {
  1542. this();
  1543. } catch (e) {
  1544. rv = e instanceof TypeError
  1545. }
  1546. rv;
  1547. `
  1548. testScript(SCRIPT, valueTrue, t)
  1549. }
  1550. func TestFuncLength(t *testing.T) {
  1551. const SCRIPT = `
  1552. function F(x, y) {
  1553. }
  1554. F.length
  1555. `
  1556. testScript(SCRIPT, intToValue(2), t)
  1557. }
  1558. func TestNativeFuncLength(t *testing.T) {
  1559. const SCRIPT = `
  1560. eval.length + Object.defineProperty.length + String.length
  1561. `
  1562. testScript(SCRIPT, intToValue(5), t)
  1563. }
  1564. func TestArguments(t *testing.T) {
  1565. const SCRIPT = `
  1566. function F() {
  1567. return arguments.length + " " + arguments[1];
  1568. }
  1569. F(1,2,3)
  1570. `
  1571. testScript(SCRIPT, asciiString("3 2"), t)
  1572. }
  1573. func TestArgumentsPut(t *testing.T) {
  1574. const SCRIPT = `
  1575. function F(x, y) {
  1576. arguments[0] -= arguments[1];
  1577. return x;
  1578. }
  1579. F(5, 2)
  1580. `
  1581. testScript(SCRIPT, intToValue(3), t)
  1582. }
  1583. func TestArgumentsPutStrict(t *testing.T) {
  1584. const SCRIPT = `
  1585. function F(x, y) {
  1586. 'use strict';
  1587. arguments[0] -= arguments[1];
  1588. return x;
  1589. }
  1590. F(5, 2)
  1591. `
  1592. testScript(SCRIPT, intToValue(5), t)
  1593. }
  1594. func TestArgumentsExtra(t *testing.T) {
  1595. const SCRIPT = `
  1596. function F(x, y) {
  1597. return arguments[2];
  1598. }
  1599. F(1, 2, 42)
  1600. `
  1601. testScript(SCRIPT, intToValue(42), t)
  1602. }
  1603. func TestArgumentsExist(t *testing.T) {
  1604. const SCRIPT = `
  1605. function F(x, arguments) {
  1606. return arguments;
  1607. }
  1608. F(1, 42)
  1609. `
  1610. testScript(SCRIPT, intToValue(42), t)
  1611. }
  1612. func TestArgumentsDelete(t *testing.T) {
  1613. const SCRIPT = `
  1614. function f(x) {
  1615. delete arguments[0];
  1616. arguments[0] = 42;
  1617. return x;
  1618. }
  1619. f(1)
  1620. `
  1621. testScript(SCRIPT, intToValue(1), t)
  1622. }
  1623. func TestArgumentsInEval(t *testing.T) {
  1624. const SCRIPT = `
  1625. function f() {
  1626. return eval("arguments");
  1627. }
  1628. f(1)[0];
  1629. `
  1630. testScript(SCRIPT, intToValue(1), t)
  1631. }
  1632. func TestArgumentsRedeclareInEval(t *testing.T) {
  1633. const SCRIPT = `
  1634. assert.sameValue("arguments" in this, false, "No global 'arguments' binding");
  1635. function f(p = eval("var arguments = 'param'"), arguments) {}
  1636. assert.throws(SyntaxError, f);
  1637. assert.sameValue("arguments" in this, false, "No global 'arguments' binding");
  1638. `
  1639. testScriptWithTestLib(SCRIPT, _undefined, t)
  1640. }
  1641. func TestArgumentsRedeclareArrow(t *testing.T) {
  1642. const SCRIPT = `
  1643. const oldArguments = globalThis.arguments;
  1644. let count = 0;
  1645. const f = (p = eval("var arguments = 'param'"), q = () => arguments) => {
  1646. var arguments = "local";
  1647. assert.sameValue(arguments, "local", "arguments");
  1648. assert.sameValue(q(), "param", "q");
  1649. count++;
  1650. }
  1651. f();
  1652. assert.sameValue(count, 1);
  1653. assert.sameValue(globalThis.arguments, oldArguments, "globalThis.arguments unchanged");
  1654. `
  1655. testScriptWithTestLib(SCRIPT, _undefined, t)
  1656. }
  1657. func TestEvalParamWithDef(t *testing.T) {
  1658. const SCRIPT = `
  1659. function f(param = 0) {
  1660. eval("var param = 1");
  1661. return param;
  1662. }
  1663. f();
  1664. `
  1665. testScript(SCRIPT, valueInt(1), t)
  1666. }
  1667. func TestArgumentsRedefinedAsLetDyn(t *testing.T) {
  1668. const SCRIPT = `
  1669. function f() {
  1670. let arguments;
  1671. eval(""); // force dynamic scope
  1672. return arguments;
  1673. }
  1674. f(1,2);
  1675. `
  1676. testScript(SCRIPT, _undefined, t)
  1677. }
  1678. func TestWith(t *testing.T) {
  1679. const SCRIPT = `
  1680. var b = 1;
  1681. var o = {a: 41};
  1682. with(o) {
  1683. a += b;
  1684. }
  1685. o.a;
  1686. `
  1687. testScript(SCRIPT, intToValue(42), t)
  1688. }
  1689. func TestWithInFunc(t *testing.T) {
  1690. const SCRIPT = `
  1691. function F() {
  1692. var b = 1;
  1693. var c = 0;
  1694. var o = {a: 40, c: 1};
  1695. with(o) {
  1696. a += b + c;
  1697. }
  1698. return o.a;
  1699. }
  1700. F();
  1701. `
  1702. testScript(SCRIPT, intToValue(42), t)
  1703. }
  1704. func TestAssignNonExtendable(t *testing.T) {
  1705. const SCRIPT = `
  1706. 'use strict';
  1707. function F() {
  1708. this.x = 1;
  1709. }
  1710. var o = new F();
  1711. Object.preventExtensions(o);
  1712. o.x = 42;
  1713. o.x;
  1714. `
  1715. testScript(SCRIPT, intToValue(42), t)
  1716. }
  1717. func TestAssignNonExtendable1(t *testing.T) {
  1718. const SCRIPT = `
  1719. 'use strict';
  1720. function F() {
  1721. }
  1722. var o = new F();
  1723. var rv;
  1724. Object.preventExtensions(o);
  1725. try {
  1726. o.x = 42;
  1727. } catch (e) {
  1728. rv = e.constructor === TypeError;
  1729. }
  1730. rv += " " + o.x;
  1731. rv;
  1732. `
  1733. testScript(SCRIPT, asciiString("true undefined"), t)
  1734. }
  1735. func TestAssignStrict(t *testing.T) {
  1736. const SCRIPT = `
  1737. 'use strict';
  1738. try {
  1739. eval("eval = 42");
  1740. } catch(e) {
  1741. var rv = e instanceof SyntaxError
  1742. }
  1743. rv;
  1744. `
  1745. testScript(SCRIPT, valueTrue, t)
  1746. }
  1747. func TestIllegalArgmentName(t *testing.T) {
  1748. const SCRIPT = `
  1749. 'use strict';
  1750. try {
  1751. eval("function F(eval) {}");
  1752. } catch (e) {
  1753. var rv = e instanceof SyntaxError
  1754. }
  1755. rv;
  1756. `
  1757. testScript(SCRIPT, valueTrue, t)
  1758. }
  1759. func TestFunction(t *testing.T) {
  1760. const SCRIPT = `
  1761. var f0 = Function("");
  1762. var f1 = Function("return ' one'");
  1763. var f2 = Function("arg", "return ' ' + arg");
  1764. f0() + f1() + f2("two");
  1765. `
  1766. testScript(SCRIPT, asciiString("undefined one two"), t)
  1767. }
  1768. func TestFunction1(t *testing.T) {
  1769. const SCRIPT = `
  1770. var f = function f1(count) {
  1771. if (count == 0) {
  1772. return true;
  1773. }
  1774. return f1(count-1);
  1775. }
  1776. f(1);
  1777. `
  1778. testScript(SCRIPT, valueTrue, t)
  1779. }
  1780. func TestFunction2(t *testing.T) {
  1781. const SCRIPT = `
  1782. var trace = "";
  1783. function f(count) {
  1784. trace += "f("+count+")";
  1785. if (count == 0) {
  1786. return;
  1787. }
  1788. return f(count-1);
  1789. }
  1790. function f1() {
  1791. trace += "f1";
  1792. }
  1793. var f2 = f;
  1794. f = f1;
  1795. f2(1);
  1796. trace;
  1797. `
  1798. testScript(SCRIPT, asciiString("f(1)f1"), t)
  1799. }
  1800. func TestFunctionToString(t *testing.T) {
  1801. const SCRIPT = `
  1802. Function("arg1", "arg2", "return 42").toString();
  1803. `
  1804. testScript(SCRIPT, asciiString("function anonymous(arg1,arg2\n) {\nreturn 42\n}"), t)
  1805. }
  1806. func TestObjectLiteral(t *testing.T) {
  1807. const SCRIPT = `
  1808. var getterCalled = false;
  1809. var setterCalled = false;
  1810. var o = {get x() {getterCalled = true}, set x(_) {setterCalled = true}};
  1811. o.x;
  1812. o.x = 42;
  1813. getterCalled && setterCalled;
  1814. `
  1815. testScript(SCRIPT, valueTrue, t)
  1816. }
  1817. func TestConst(t *testing.T) {
  1818. const SCRIPT = `
  1819. var v1 = true && true;
  1820. var v2 = 1/(-1 * 0);
  1821. var v3 = 1 == 2 || v1;
  1822. var v4 = true && false
  1823. v1 === true && v2 === -Infinity && v3 === v1 && v4 === false;
  1824. `
  1825. testScript(SCRIPT, valueTrue, t)
  1826. }
  1827. func TestConstWhile(t *testing.T) {
  1828. const SCRIPT = `
  1829. var c = 0;
  1830. while (2 + 2 === 4) {
  1831. if (++c > 9) {
  1832. break;
  1833. }
  1834. }
  1835. c === 10;
  1836. `
  1837. testScript(SCRIPT, valueTrue, t)
  1838. }
  1839. func TestConstWhileThrow(t *testing.T) {
  1840. const SCRIPT = `
  1841. var thrown = false;
  1842. try {
  1843. while ('s' in true) {
  1844. break;
  1845. }
  1846. } catch (e) {
  1847. thrown = e instanceof TypeError
  1848. }
  1849. thrown;
  1850. `
  1851. testScript(SCRIPT, valueTrue, t)
  1852. }
  1853. func TestDupParams(t *testing.T) {
  1854. const SCRIPT = `
  1855. function F(x, y, x) {
  1856. return x;
  1857. }
  1858. F(1, 2);
  1859. `
  1860. testScript(SCRIPT, _undefined, t)
  1861. }
  1862. func TestUseUnsuppliedParam(t *testing.T) {
  1863. const SCRIPT = `
  1864. function getMessage(message) {
  1865. if (message === undefined) {
  1866. message = '';
  1867. }
  1868. message += " 123 456";
  1869. return message;
  1870. }
  1871. getMessage();
  1872. `
  1873. testScript(SCRIPT, asciiString(" 123 456"), t)
  1874. }
  1875. func TestForInLetWithInitializer(t *testing.T) {
  1876. const SCRIPT = `for (let x = 3 in {}) { }`
  1877. _, err := Compile("", SCRIPT, false)
  1878. if err == nil {
  1879. t.Fatal("Expected error")
  1880. }
  1881. }
  1882. func TestForInLoop(t *testing.T) {
  1883. const SCRIPT = `
  1884. function Proto() {}
  1885. Proto.prototype.x = 42;
  1886. var o = new Proto();
  1887. o.y = 44;
  1888. o.x = 45;
  1889. var hasX = false;
  1890. var hasY = false;
  1891. for (var i in o) {
  1892. switch(i) {
  1893. case "x":
  1894. if (hasX) {
  1895. throw new Error("Already has X");
  1896. }
  1897. hasX = true;
  1898. break;
  1899. case "y":
  1900. if (hasY) {
  1901. throw new Error("Already has Y");
  1902. }
  1903. hasY = true;
  1904. break;
  1905. }
  1906. }
  1907. hasX && hasY;
  1908. `
  1909. testScript(SCRIPT, valueTrue, t)
  1910. }
  1911. func TestWhileLoopResult(t *testing.T) {
  1912. const SCRIPT = `
  1913. while(false);
  1914. `
  1915. testScript(SCRIPT, _undefined, t)
  1916. }
  1917. func TestEmptySwitch(t *testing.T) {
  1918. const SCRIPT = `
  1919. switch(1){}
  1920. `
  1921. testScript(SCRIPT, _undefined, t)
  1922. }
  1923. func TestEmptyDoWhile(t *testing.T) {
  1924. const SCRIPT = `
  1925. do {} while(false)
  1926. `
  1927. testScript(SCRIPT, _undefined, t)
  1928. }
  1929. func TestSwitch(t *testing.T) {
  1930. const SCRIPT = `
  1931. function F(x) {
  1932. var i = 0;
  1933. switch (x) {
  1934. case 0:
  1935. i++;
  1936. case 1:
  1937. i++;
  1938. default:
  1939. i++;
  1940. case 2:
  1941. i++;
  1942. break;
  1943. case 3:
  1944. i++;
  1945. }
  1946. return i;
  1947. }
  1948. F(0) + F(1) + F(2) + F(4);
  1949. `
  1950. testScript(SCRIPT, intToValue(10), t)
  1951. }
  1952. func TestSwitchDefFirst(t *testing.T) {
  1953. const SCRIPT = `
  1954. function F(x) {
  1955. var i = 0;
  1956. switch (x) {
  1957. default:
  1958. i++;
  1959. case 0:
  1960. i++;
  1961. case 1:
  1962. i++;
  1963. case 2:
  1964. i++;
  1965. break;
  1966. case 3:
  1967. i++;
  1968. }
  1969. return i;
  1970. }
  1971. F(0) + F(1) + F(2) + F(4);
  1972. `
  1973. testScript(SCRIPT, intToValue(10), t)
  1974. }
  1975. func TestSwitchResult(t *testing.T) {
  1976. const SCRIPT = `
  1977. var x = 2;
  1978. switch (x) {
  1979. case 0:
  1980. "zero";
  1981. case 1:
  1982. "one";
  1983. case 2:
  1984. "two";
  1985. break;
  1986. case 3:
  1987. "three";
  1988. default:
  1989. "default";
  1990. }
  1991. `
  1992. testScript(SCRIPT, asciiString("two"), t)
  1993. }
  1994. func TestSwitchResult1(t *testing.T) {
  1995. const SCRIPT = `
  1996. var x = 0;
  1997. switch (x) { case 0: "two"; case 1: break}
  1998. `
  1999. testScript(SCRIPT, asciiString("two"), t)
  2000. }
  2001. func TestSwitchResult2(t *testing.T) {
  2002. const SCRIPT = `
  2003. 6; switch ("a") { case "a": 7; case "b": }
  2004. `
  2005. testScript(SCRIPT, valueInt(7), t)
  2006. }
  2007. func TestSwitchResultJumpIntoEmptyEval(t *testing.T) {
  2008. const SCRIPT = `
  2009. function t(x) {
  2010. return eval("switch(x) { case 1: 2; break; case 2: let x = 1; case 3: x+2; break; case 4: default: 9}");
  2011. }
  2012. ""+t(2)+t();
  2013. `
  2014. testScript(SCRIPT, asciiString("39"), t)
  2015. }
  2016. func TestSwitchResultJumpIntoEmpty(t *testing.T) {
  2017. const SCRIPT = `
  2018. switch(2) { case 1: 2; break; case 2: let x = 1; case 3: x+2; case 4: {let y = 2}; break; default: 9};
  2019. `
  2020. testScript(SCRIPT, valueInt(3), t)
  2021. }
  2022. func TestSwitchLexical(t *testing.T) {
  2023. const SCRIPT = `
  2024. switch (true) { case true: let x = 1; }
  2025. `
  2026. testScript(SCRIPT, _undefined, t)
  2027. }
  2028. func TestSwitchBreakOuter(t *testing.T) {
  2029. const SCRIPT = `
  2030. LOOP:
  2031. for (let i = 0; i < 10; i++) {
  2032. switch (i) {
  2033. case 0:
  2034. continue;
  2035. case 1:
  2036. let x = 1;
  2037. continue;
  2038. case 2:
  2039. try {
  2040. x++;
  2041. } catch (e) {
  2042. if (e instanceof ReferenceError) {
  2043. break LOOP;
  2044. }
  2045. }
  2046. throw new Error("Exception was not thrown");
  2047. }
  2048. }
  2049. `
  2050. testScript(SCRIPT, _undefined, t)
  2051. }
  2052. func TestIfBreakResult(t *testing.T) {
  2053. const SCRIPT = `
  2054. L: {if (true) {42;} break L;}
  2055. `
  2056. testScript(SCRIPT, intToValue(42), t)
  2057. }
  2058. func TestSwitchNoMatch(t *testing.T) {
  2059. const SCRIPT = `
  2060. var result;
  2061. var x;
  2062. switch (x) {
  2063. case 0:
  2064. result = "2";
  2065. break;
  2066. }
  2067. result;
  2068. `
  2069. testScript(SCRIPT, _undefined, t)
  2070. }
  2071. func TestSwitchNoMatchNoDefault(t *testing.T) {
  2072. const SCRIPT = `
  2073. switch (1) {
  2074. case 0:
  2075. }
  2076. `
  2077. testScript(SCRIPT, _undefined, t)
  2078. }
  2079. func TestSwitchNoMatchNoDefaultNoResult(t *testing.T) {
  2080. const SCRIPT = `
  2081. switch (1) {
  2082. case 0:
  2083. }
  2084. 42;
  2085. `
  2086. testScript(SCRIPT, intToValue(42), t)
  2087. }
  2088. func TestSwitchNoMatchNoDefaultNoResultMatch(t *testing.T) {
  2089. const SCRIPT = `
  2090. switch (1) {
  2091. case 1:
  2092. }
  2093. 42;
  2094. `
  2095. testScript(SCRIPT, intToValue(42), t)
  2096. }
  2097. func TestEmptySwitchNoResult(t *testing.T) {
  2098. const SCRIPT = `
  2099. switch (1) {}
  2100. 42;
  2101. `
  2102. testScript(SCRIPT, intToValue(42), t)
  2103. }
  2104. func TestGetOwnPropertyNames(t *testing.T) {
  2105. const SCRIPT = `
  2106. var o = {
  2107. prop1: 42,
  2108. prop2: "test"
  2109. }
  2110. var hasProp1 = false;
  2111. var hasProp2 = false;
  2112. var names = Object.getOwnPropertyNames(o);
  2113. for (var i in names) {
  2114. var p = names[i];
  2115. switch(p) {
  2116. case "prop1":
  2117. hasProp1 = true;
  2118. break;
  2119. case "prop2":
  2120. hasProp2 = true;
  2121. break;
  2122. }
  2123. }
  2124. hasProp1 && hasProp2;
  2125. `
  2126. testScript(SCRIPT, valueTrue, t)
  2127. }
  2128. func TestArrayLiteral(t *testing.T) {
  2129. const SCRIPT = `
  2130. var f1Called = false;
  2131. var f2Called = false;
  2132. var f3Called = false;
  2133. var errorThrown = false;
  2134. function F1() {
  2135. f1Called = true;
  2136. }
  2137. function F2() {
  2138. f2Called = true;
  2139. }
  2140. function F3() {
  2141. f3Called = true;
  2142. }
  2143. try {
  2144. var a = [F1(), x(F3()), F2()];
  2145. } catch(e) {
  2146. if (e instanceof ReferenceError) {
  2147. errorThrown = true;
  2148. } else {
  2149. throw e;
  2150. }
  2151. }
  2152. f1Called && !f2Called && f3Called && errorThrown && a === undefined;
  2153. `
  2154. testScript(SCRIPT, valueTrue, t)
  2155. }
  2156. func TestJumpOutOfReturn(t *testing.T) {
  2157. const SCRIPT = `
  2158. function f() {
  2159. var a;
  2160. if (a == 0) {
  2161. return true;
  2162. }
  2163. }
  2164. f();
  2165. `
  2166. testScript(SCRIPT, _undefined, t)
  2167. }
  2168. func TestSwitchJumpOutOfReturn(t *testing.T) {
  2169. const SCRIPT = `
  2170. function f(x) {
  2171. switch(x) {
  2172. case 0:
  2173. break;
  2174. default:
  2175. return x;
  2176. }
  2177. }
  2178. f(0);
  2179. `
  2180. testScript(SCRIPT, _undefined, t)
  2181. }
  2182. func TestSetToReadOnlyPropertyStrictBracket(t *testing.T) {
  2183. const SCRIPT = `
  2184. 'use strict';
  2185. var o = {};
  2186. var thrown = false;
  2187. Object.defineProperty(o, "test", {value: 42, configurable: true});
  2188. try {
  2189. o["test"] = 43;
  2190. } catch (e) {
  2191. thrown = e instanceof TypeError;
  2192. }
  2193. thrown;
  2194. `
  2195. testScript(SCRIPT, valueTrue, t)
  2196. }
  2197. func TestSetToReadOnlyPropertyStrictDot(t *testing.T) {
  2198. const SCRIPT = `
  2199. 'use strict';
  2200. var o = {};
  2201. var thrown = false;
  2202. Object.defineProperty(o, "test", {value: 42, configurable: true});
  2203. try {
  2204. o.test = 43;
  2205. } catch (e) {
  2206. thrown = e instanceof TypeError;
  2207. }
  2208. thrown;
  2209. `
  2210. testScript(SCRIPT, valueTrue, t)
  2211. }
  2212. func TestDeleteNonConfigurablePropertyStrictBracket(t *testing.T) {
  2213. const SCRIPT = `
  2214. 'use strict';
  2215. var o = {};
  2216. var thrown = false;
  2217. Object.defineProperty(o, "test", {value: 42});
  2218. try {
  2219. delete o["test"];
  2220. } catch (e) {
  2221. thrown = e instanceof TypeError;
  2222. }
  2223. thrown;
  2224. `
  2225. testScript(SCRIPT, valueTrue, t)
  2226. }
  2227. func TestDeleteNonConfigurablePropertyStrictDot(t *testing.T) {
  2228. const SCRIPT = `
  2229. 'use strict';
  2230. var o = {};
  2231. var thrown = false;
  2232. Object.defineProperty(o, "test", {value: 42});
  2233. try {
  2234. delete o.test;
  2235. } catch (e) {
  2236. thrown = e instanceof TypeError;
  2237. }
  2238. thrown;
  2239. `
  2240. testScript(SCRIPT, valueTrue, t)
  2241. }
  2242. func TestCompound1(t *testing.T) {
  2243. const SCRIPT = `
  2244. var x = 0;
  2245. var scope = {x: 1};
  2246. var f;
  2247. with (scope) {
  2248. f = function() {
  2249. x *= (delete scope.x, 2);
  2250. }
  2251. }
  2252. f();
  2253. scope.x === 2 && x === 0;
  2254. `
  2255. testScript(SCRIPT, valueTrue, t)
  2256. }
  2257. func TestCompound2(t *testing.T) {
  2258. const SCRIPT = `
  2259. var x;
  2260. x = "x";
  2261. x ^= "1";
  2262. `
  2263. testScript(SCRIPT, intToValue(1), t)
  2264. }
  2265. func TestDeleteArguments(t *testing.T) {
  2266. defer func() {
  2267. if _, ok := recover().(*CompilerSyntaxError); !ok {
  2268. t.Fatal("Expected syntax error")
  2269. }
  2270. }()
  2271. const SCRIPT = `
  2272. 'use strict';
  2273. function f() {
  2274. delete arguments;
  2275. }
  2276. `
  2277. testScript(SCRIPT, _undefined, t)
  2278. }
  2279. func TestReturnUndefined(t *testing.T) {
  2280. const SCRIPT = `
  2281. function f() {
  2282. return x;
  2283. }
  2284. var thrown = false;
  2285. try {
  2286. f();
  2287. } catch (e) {
  2288. thrown = e instanceof ReferenceError;
  2289. }
  2290. thrown;
  2291. `
  2292. testScript(SCRIPT, valueTrue, t)
  2293. }
  2294. func TestForBreak(t *testing.T) {
  2295. const SCRIPT = `
  2296. var supreme, count;
  2297. supreme = 5;
  2298. var __evaluated = eval("for(count=0;;) {if (count===supreme)break;else count++; }");
  2299. if (__evaluated !== void 0) {
  2300. throw new Error('#1: __evaluated === 4. Actual: __evaluated ==='+ __evaluated );
  2301. }
  2302. `
  2303. testScript(SCRIPT, _undefined, t)
  2304. }
  2305. func TestLargeNumberLiteral(t *testing.T) {
  2306. const SCRIPT = `
  2307. var x = 0x800000000000000000000;
  2308. x.toString();
  2309. `
  2310. testScript(SCRIPT, asciiString("9.671406556917033e+24"), t)
  2311. }
  2312. func TestIncDelete(t *testing.T) {
  2313. const SCRIPT = `
  2314. var o = {x: 1};
  2315. o.x += (delete o.x, 1);
  2316. o.x;
  2317. `
  2318. testScript(SCRIPT, intToValue(2), t)
  2319. }
  2320. func TestCompoundAssignRefError(t *testing.T) {
  2321. const SCRIPT = `
  2322. var thrown = false;
  2323. try {
  2324. a *= 1;
  2325. } catch (e) {
  2326. if (e instanceof ReferenceError) {
  2327. thrown = true;
  2328. } else {
  2329. throw e;
  2330. }
  2331. }
  2332. thrown;
  2333. `
  2334. testScript(SCRIPT, valueTrue, t)
  2335. }
  2336. func TestObjectLiteral__Proto__(t *testing.T) {
  2337. const SCRIPT = `
  2338. var o = {
  2339. __proto__: null,
  2340. test: 42
  2341. }
  2342. Object.getPrototypeOf(o);
  2343. `
  2344. testScript(SCRIPT, _null, t)
  2345. }
  2346. func TestEmptyCodeError(t *testing.T) {
  2347. if _, err := New().RunString(`i`); err == nil {
  2348. t.Fatal("Expected an error")
  2349. } else {
  2350. if e := err.Error(); e != "ReferenceError: i is not defined at <eval>:1:1(0)" {
  2351. t.Fatalf("Unexpected error: '%s'", e)
  2352. }
  2353. }
  2354. }
  2355. func TestForOfArray(t *testing.T) {
  2356. const SCRIPT = `
  2357. var array = [0, 'a', true, false, null, /* hole */, undefined, NaN];
  2358. var i = 0;
  2359. for (var value of array) {
  2360. assert.sameValue(value, array[i], 'element at index ' + i);
  2361. i++;
  2362. }
  2363. assert.sameValue(i, 8, 'Visits all elements');
  2364. `
  2365. testScriptWithTestLib(SCRIPT, _undefined, t)
  2366. }
  2367. func TestForOfReturn(t *testing.T) {
  2368. const SCRIPT = `
  2369. var callCount = 0;
  2370. var iterationCount = 0;
  2371. var iterable = {};
  2372. var x = {
  2373. set attr(_) {
  2374. throw new Test262Error();
  2375. }
  2376. };
  2377. iterable[Symbol.iterator] = function() {
  2378. return {
  2379. next: function() {
  2380. return { done: false, value: 0 };
  2381. },
  2382. return: function() {
  2383. callCount += 1;
  2384. }
  2385. }
  2386. };
  2387. assert.throws(Test262Error, function() {
  2388. for (x.attr of iterable) {
  2389. iterationCount += 1;
  2390. }
  2391. });
  2392. assert.sameValue(iterationCount, 0, 'The loop body is not evaluated');
  2393. assert.sameValue(callCount, 1, 'Iterator is closed');
  2394. `
  2395. testScriptWithTestLib(SCRIPT, _undefined, t)
  2396. }
  2397. func TestForOfReturn1(t *testing.T) {
  2398. const SCRIPT = `
  2399. var iterable = {};
  2400. var iterationCount = 0;
  2401. iterable[Symbol.iterator] = function() {
  2402. return {
  2403. next: function() {
  2404. return { done: false, value: null };
  2405. },
  2406. get return() {
  2407. throw new Test262Error();
  2408. }
  2409. };
  2410. };
  2411. assert.throws(Test262Error, function() {
  2412. for (var x of iterable) {
  2413. iterationCount += 1;
  2414. break;
  2415. }
  2416. });
  2417. assert.sameValue(iterationCount, 1, 'The loop body is evaluated');
  2418. `
  2419. testScriptWithTestLib(SCRIPT, _undefined, t)
  2420. }
  2421. func TestForOfLet(t *testing.T) {
  2422. const SCRIPT = `
  2423. var iterCount = 0;
  2424. function f() {}
  2425. for (var let of [23]) {
  2426. f(let);
  2427. if (let != 23) {
  2428. throw new Error("");
  2429. }
  2430. iterCount += 1;
  2431. }
  2432. iterCount;
  2433. `
  2434. testScript(SCRIPT, valueInt(1), t)
  2435. }
  2436. func TestForOfLetLet(t *testing.T) {
  2437. const SCRIPT = `
  2438. for (let let of [23]) {
  2439. }
  2440. `
  2441. _, err := Compile("", SCRIPT, false)
  2442. if err == nil {
  2443. t.Fatal("Expected error")
  2444. }
  2445. }
  2446. func TestForHeadLet(t *testing.T) {
  2447. const SCRIPT = `
  2448. for (let = 0; let < 2; let++);
  2449. `
  2450. testScript(SCRIPT, _undefined, t)
  2451. }
  2452. func TestLhsLet(t *testing.T) {
  2453. const SCRIPT = `
  2454. let = 1;
  2455. let;
  2456. `
  2457. testScript(SCRIPT, valueInt(1), t)
  2458. }
  2459. func TestLetPostfixASI(t *testing.T) {
  2460. const SCRIPT = `
  2461. let
  2462. ++
  2463. `
  2464. _, err := Compile("", SCRIPT, false)
  2465. if err == nil {
  2466. t.Fatal("Expected error")
  2467. }
  2468. }
  2469. func TestIteratorReturnNormal(t *testing.T) {
  2470. const SCRIPT = `
  2471. var iterable = {};
  2472. var iterationCount = 0;
  2473. iterable[Symbol.iterator] = function() {
  2474. return {
  2475. next: function() {
  2476. return { done: ++iterationCount > 2, value: null };
  2477. },
  2478. get return() {
  2479. throw new Test262Error();
  2480. }
  2481. };
  2482. };
  2483. for (var x of iterable) {
  2484. }
  2485. `
  2486. testScriptWithTestLib(SCRIPT, _undefined, t)
  2487. }
  2488. func TestIteratorReturnErrorNested(t *testing.T) {
  2489. const SCRIPT = `
  2490. var returnCalled = {};
  2491. function iter(id) {
  2492. return function() {
  2493. var count = 0;
  2494. return {
  2495. next: function () {
  2496. return {
  2497. value: null,
  2498. done: ++count > 2
  2499. };
  2500. },
  2501. return: function () {
  2502. returnCalled[id] = true;
  2503. throw new Error(id);
  2504. }
  2505. };
  2506. }
  2507. }
  2508. var iterable1 = {};
  2509. iterable1[Symbol.iterator] = iter("1");
  2510. var iterable2 = {};
  2511. iterable2[Symbol.iterator] = iter("2");
  2512. try {
  2513. for (var i of iterable1) {
  2514. for (var j of iterable2) {
  2515. break;
  2516. }
  2517. }
  2518. throw new Error("no exception was thrown");
  2519. } catch (e) {
  2520. if (e.message !== "2") {
  2521. throw e;
  2522. }
  2523. }
  2524. if (!returnCalled["1"]) {
  2525. throw new Error("no return 1");
  2526. }
  2527. if (!returnCalled["2"]) {
  2528. throw new Error("no return 2");
  2529. }
  2530. `
  2531. testScript(SCRIPT, _undefined, t)
  2532. }
  2533. func TestReturnFromForInLoop(t *testing.T) {
  2534. const SCRIPT = `
  2535. (function f() {
  2536. for (var i in {a: 1}) {
  2537. return true;
  2538. }
  2539. })();
  2540. `
  2541. testScript(SCRIPT, valueTrue, t)
  2542. }
  2543. func TestReturnFromForOfLoop(t *testing.T) {
  2544. const SCRIPT = `
  2545. (function f() {
  2546. for (var i of [1]) {
  2547. return true;
  2548. }
  2549. })();
  2550. `
  2551. testScript(SCRIPT, valueTrue, t)
  2552. }
  2553. func TestIfStackLeaks(t *testing.T) {
  2554. const SCRIPT = `
  2555. var t = 0;
  2556. if (t === 0) {
  2557. t;
  2558. }
  2559. `
  2560. testScript(SCRIPT, _positiveZero, t)
  2561. }
  2562. func TestWithCallee(t *testing.T) {
  2563. const SCRIPT = `
  2564. function O() {
  2565. var that = this;
  2566. this.m = function() {
  2567. return this === that;
  2568. }
  2569. }
  2570. with(new O()) {
  2571. m();
  2572. }
  2573. `
  2574. testScript(SCRIPT, valueTrue, t)
  2575. }
  2576. func TestWithScope(t *testing.T) {
  2577. const SCRIPT = `
  2578. function f(o) {
  2579. var x = 42;
  2580. function innerf(o) {
  2581. with (o) {
  2582. return x;
  2583. }
  2584. }
  2585. return innerf(o);
  2586. }
  2587. f({});
  2588. `
  2589. testScript(SCRIPT, valueInt(42), t)
  2590. }
  2591. func TestEvalCallee(t *testing.T) {
  2592. const SCRIPT = `
  2593. (function () {
  2594. 'use strict';
  2595. var v = function() {
  2596. return this === undefined;
  2597. };
  2598. return eval('v()');
  2599. })();
  2600. `
  2601. testScript(SCRIPT, valueTrue, t)
  2602. }
  2603. func TestEvalBindingDeleteVar(t *testing.T) {
  2604. const SCRIPT = `
  2605. (function () {
  2606. eval("var x = 1");
  2607. return x === 1 && delete x;
  2608. })();
  2609. `
  2610. testScript(SCRIPT, valueTrue, t)
  2611. }
  2612. func TestEvalBindingDeleteFunc(t *testing.T) {
  2613. const SCRIPT = `
  2614. (function () {
  2615. eval("function x(){}");
  2616. return typeof x === "function" && delete x;
  2617. })();
  2618. `
  2619. testScript(SCRIPT, valueTrue, t)
  2620. }
  2621. func TestDeleteGlobalLexical(t *testing.T) {
  2622. const SCRIPT = `
  2623. let x;
  2624. delete x;
  2625. `
  2626. testScript(SCRIPT, valueFalse, t)
  2627. }
  2628. func TestDeleteGlobalEval(t *testing.T) {
  2629. const SCRIPT = `
  2630. eval("var x");
  2631. delete x;
  2632. `
  2633. testScript(SCRIPT, valueTrue, t)
  2634. }
  2635. func TestGlobalVarNames(t *testing.T) {
  2636. vm := New()
  2637. _, err := vm.RunString("(0,eval)('var x')")
  2638. if err != nil {
  2639. t.Fatal(err)
  2640. }
  2641. _, err = vm.RunString("let x")
  2642. if err == nil {
  2643. t.Fatal("Expected error")
  2644. }
  2645. }
  2646. func TestTryResultEmpty(t *testing.T) {
  2647. const SCRIPT = `
  2648. 1; try { } finally { }
  2649. `
  2650. testScript(SCRIPT, _undefined, t)
  2651. }
  2652. func TestTryResultEmptyCatch(t *testing.T) {
  2653. const SCRIPT = `
  2654. 1; try { throw null } catch(e) { }
  2655. `
  2656. testScript(SCRIPT, _undefined, t)
  2657. }
  2658. func TestTryResultEmptyContinueLoop(t *testing.T) {
  2659. const SCRIPT = `
  2660. for (var i = 0; i < 2; i++) { try {throw null;} catch(e) {continue;} 'bad'}
  2661. `
  2662. testScript(SCRIPT, _undefined, t)
  2663. }
  2664. func TestTryEmptyCatchStackLeak(t *testing.T) {
  2665. const SCRIPT = `
  2666. (function() {
  2667. var f;
  2668. // Make sure the outer function is not stashless.
  2669. (function() {
  2670. f++;
  2671. })();
  2672. try {
  2673. throw new Error();
  2674. } catch(e) {}
  2675. })();
  2676. `
  2677. testScript(SCRIPT, _undefined, t)
  2678. }
  2679. func TestTryThrowEmptyCatch(t *testing.T) {
  2680. const SCRIPT = `
  2681. try {
  2682. throw new Error();
  2683. }
  2684. catch (e) {}
  2685. `
  2686. testScript(SCRIPT, _undefined, t)
  2687. }
  2688. func TestFalsyLoopBreak(t *testing.T) {
  2689. const SCRIPT = `
  2690. while(false) {
  2691. break;
  2692. }
  2693. for(;false;) {
  2694. break;
  2695. }
  2696. undefined;
  2697. `
  2698. MustCompile("", SCRIPT, false)
  2699. }
  2700. func TestFalsyLoopBreakWithResult(t *testing.T) {
  2701. const SCRIPT = `
  2702. while(false) {
  2703. break;
  2704. }
  2705. `
  2706. testScript(SCRIPT, _undefined, t)
  2707. }
  2708. func TestDummyCompile(t *testing.T) {
  2709. const SCRIPT = `
  2710. 'use strict';
  2711. for (;false;) {
  2712. eval = 1;
  2713. }
  2714. `
  2715. _, err := Compile("", SCRIPT, false)
  2716. if err == nil {
  2717. t.Fatal("expected error")
  2718. }
  2719. }
  2720. func TestDummyCompileForUpdate(t *testing.T) {
  2721. const SCRIPT = `
  2722. 'use strict';
  2723. for (;false;eval=1) {
  2724. }
  2725. `
  2726. _, err := Compile("", SCRIPT, false)
  2727. if err == nil {
  2728. t.Fatal("expected error")
  2729. }
  2730. }
  2731. func TestObjectLiteralWithNumericKeys(t *testing.T) {
  2732. const SCRIPT = `
  2733. var o = {1e3: true};
  2734. var keys = Object.keys(o);
  2735. var o1 = {get 1e3() {return true;}};
  2736. var keys1 = Object.keys(o1);
  2737. var o2 = {1e21: true};
  2738. var keys2 = Object.keys(o2);
  2739. keys.length === 1 && keys[0] === "1000" &&
  2740. keys1.length === 1 && keys1[0] === "1000" && o1[1e3] === true &&
  2741. keys2.length === 1 && keys2[0] === "1e+21";
  2742. `
  2743. testScript(SCRIPT, valueTrue, t)
  2744. }
  2745. func TestEscapedObjectPropertyKeys(t *testing.T) {
  2746. const SCRIPT = `
  2747. var obj = {
  2748. w\u0069th: 42
  2749. };
  2750. var obj = {
  2751. with() {42}
  2752. };
  2753. `
  2754. _, err := Compile("", SCRIPT, false)
  2755. if err != nil {
  2756. t.Fatal(err)
  2757. }
  2758. }
  2759. func TestEscapedKeywords(t *testing.T) {
  2760. const SCRIPT = `r\u0065turn;`
  2761. _, err := Compile("", SCRIPT, false)
  2762. if err == nil {
  2763. t.Fatal("Expected error")
  2764. }
  2765. }
  2766. func TestEscapedLet(t *testing.T) {
  2767. const SCRIPT = `
  2768. this.let = 0;
  2769. l\u0065t // ASI
  2770. a;
  2771. // If the parser treated the previous escaped "let" as a lexical declaration,
  2772. // this variable declaration will result an early syntax error.
  2773. var a;
  2774. `
  2775. _, err := Compile("", SCRIPT, false)
  2776. if err != nil {
  2777. t.Fatal(err)
  2778. }
  2779. }
  2780. func TestObjectLiteralFuncProps(t *testing.T) {
  2781. const SCRIPT = `
  2782. (function() {
  2783. 'use strict';
  2784. var o = {
  2785. eval: function() {return 1;},
  2786. arguments() {return 2;},
  2787. test: function test1() {}
  2788. }
  2789. assert.sameValue(o.eval.name, "eval");
  2790. assert.sameValue(o.arguments.name, "arguments");
  2791. assert.sameValue(o.eval(), 1);
  2792. assert.sameValue(o.arguments(), 2);
  2793. assert.sameValue(o.test.name, "test1");
  2794. })();
  2795. `
  2796. testScriptWithTestLib(SCRIPT, _undefined, t)
  2797. }
  2798. func TestFuncName(t *testing.T) {
  2799. const SCRIPT = `
  2800. var method = 1;
  2801. var o = {
  2802. method: function() {
  2803. return method;
  2804. },
  2805. method1: function method() {
  2806. return method;
  2807. }
  2808. }
  2809. o.method() === 1 && o.method1() === o.method1;
  2810. `
  2811. testScript(SCRIPT, valueTrue, t)
  2812. }
  2813. func TestFuncNameAssign(t *testing.T) {
  2814. const SCRIPT = `
  2815. var f = function() {};
  2816. var f1;
  2817. f1 = function() {};
  2818. let f2 = function() {};
  2819. f.name === "f" && f1.name === "f1" && f2.name === "f2";
  2820. `
  2821. testScript(SCRIPT, valueTrue, t)
  2822. }
  2823. func TestLexicalDeclGlobal(t *testing.T) {
  2824. const SCRIPT = `
  2825. if (true) {
  2826. let it = "be";
  2827. if (it !== "be") {
  2828. throw new Error(it);
  2829. }
  2830. }
  2831. let thrown = false;
  2832. try {
  2833. it;
  2834. } catch(e) {
  2835. if (e instanceof ReferenceError) {
  2836. thrown = true;
  2837. }
  2838. }
  2839. thrown;
  2840. `
  2841. testScript(SCRIPT, valueTrue, t)
  2842. }
  2843. func TestLexicalDeclFunction(t *testing.T) {
  2844. const SCRIPT = `
  2845. function f() {
  2846. if (true) {
  2847. let it = "be";
  2848. if (it !== "be") {
  2849. throw new Error(it);
  2850. }
  2851. }
  2852. let thrown = false;
  2853. try {
  2854. it;
  2855. } catch(e) {
  2856. if (e instanceof ReferenceError) {
  2857. thrown = true;
  2858. }
  2859. }
  2860. return thrown;
  2861. }
  2862. f();
  2863. `
  2864. testScript(SCRIPT, valueTrue, t)
  2865. }
  2866. func TestLexicalDynamicScope(t *testing.T) {
  2867. const SCRIPT = `
  2868. const global = 1;
  2869. function f() {
  2870. const func = global + 1;
  2871. function inner() {
  2872. function assertThrows(fn) {
  2873. let thrown = false;
  2874. try {
  2875. fn();
  2876. } catch (e) {
  2877. if (e instanceof TypeError) {
  2878. thrown = true;
  2879. } else {
  2880. throw e;
  2881. }
  2882. }
  2883. if (!thrown) {
  2884. throw new Error("Did not throw");
  2885. }
  2886. }
  2887. assertThrows(function() {
  2888. func++;
  2889. });
  2890. assertThrows(function() {
  2891. global++;
  2892. });
  2893. assertThrows(function() {
  2894. eval("func++");
  2895. });
  2896. assertThrows(function() {
  2897. eval("global++");
  2898. });
  2899. return eval("func + 1");
  2900. }
  2901. return inner();
  2902. }
  2903. f();
  2904. `
  2905. testScript(SCRIPT, valueInt(3), t)
  2906. }
  2907. func TestLexicalDynamicScope1(t *testing.T) {
  2908. const SCRIPT = `
  2909. (function() {
  2910. const x = 1 * 4;
  2911. return (function() {
  2912. eval("");
  2913. return x;
  2914. })();
  2915. })();
  2916. `
  2917. testScript(SCRIPT, intToValue(4), t)
  2918. }
  2919. func TestLexicalDynamicScope2(t *testing.T) {
  2920. const SCRIPT = `
  2921. (function() {
  2922. const x = 1 + 3;
  2923. var y = 2 * 2;
  2924. eval("");
  2925. return x;
  2926. })();
  2927. `
  2928. testScript(SCRIPT, intToValue(4), t)
  2929. }
  2930. func TestNonStrictLet(t *testing.T) {
  2931. const SCRIPT = `
  2932. var let = 1;
  2933. `
  2934. testScript(SCRIPT, _undefined, t)
  2935. }
  2936. func TestStrictLet(t *testing.T) {
  2937. const SCRIPT = `
  2938. var let = 1;
  2939. `
  2940. _, err := Compile("", SCRIPT, true)
  2941. if err == nil {
  2942. t.Fatal("Expected an error")
  2943. }
  2944. }
  2945. func TestLetLet(t *testing.T) {
  2946. const SCRIPT = `
  2947. let let = 1;
  2948. `
  2949. _, err := Compile("", SCRIPT, false)
  2950. if err == nil {
  2951. t.Fatal("Expected an error")
  2952. }
  2953. }
  2954. func TestLetASI(t *testing.T) {
  2955. const SCRIPT = `
  2956. while (false) let // ASI
  2957. x = 1;
  2958. `
  2959. _, err := Compile("", SCRIPT, false)
  2960. if err != nil {
  2961. t.Fatal(err)
  2962. }
  2963. }
  2964. func TestLetASI1(t *testing.T) {
  2965. const SCRIPT = `
  2966. let
  2967. x = 1;
  2968. `
  2969. _, err := Compile("", SCRIPT, true)
  2970. if err != nil {
  2971. t.Fatal(err)
  2972. }
  2973. }
  2974. func TestLetNoASI(t *testing.T) {
  2975. const SCRIPT = `
  2976. function f() {}let
  2977. x = 1;
  2978. `
  2979. _, err := Compile("", SCRIPT, true)
  2980. if err != nil {
  2981. t.Fatal(err)
  2982. }
  2983. }
  2984. func TestLetNoASI1(t *testing.T) {
  2985. const SCRIPT = `
  2986. let
  2987. let = 1;
  2988. `
  2989. _, err := Compile("", SCRIPT, false)
  2990. if err == nil {
  2991. t.Fatal("Expected error")
  2992. }
  2993. }
  2994. func TestLetArrayWithNewline(t *testing.T) {
  2995. const SCRIPT = `
  2996. with ({}) let
  2997. [a] = 0;
  2998. `
  2999. _, err := Compile("", SCRIPT, false)
  3000. if err == nil {
  3001. t.Fatal("Expected error")
  3002. }
  3003. }
  3004. func TestDynamicUninitedVarAccess(t *testing.T) {
  3005. const SCRIPT = `
  3006. function f() {
  3007. var x;
  3008. return eval("x");
  3009. }
  3010. f();
  3011. `
  3012. testScript(SCRIPT, _undefined, t)
  3013. }
  3014. func TestLexicalForLoopNoClosure(t *testing.T) {
  3015. const SCRIPT = `
  3016. let sum = 0;
  3017. for (let i = 0; i < 3; i++) {
  3018. sum += i;
  3019. }
  3020. sum;
  3021. `
  3022. testScript(SCRIPT, valueInt(3), t)
  3023. }
  3024. func TestLexicalForLoopClosure(t *testing.T) {
  3025. const SCRIPT = `
  3026. var f = [];
  3027. for (let i = 0; i < 3; i++) {
  3028. f.push(function() {
  3029. return i;
  3030. });
  3031. }
  3032. f.length === 3 && f[0]() === 0 && f[1]() === 1 && f[2]() === 2;
  3033. `
  3034. testScript(SCRIPT, valueTrue, t)
  3035. }
  3036. func TestLexicalForLoopClosureInNext(t *testing.T) {
  3037. const SCRIPT = `
  3038. const a = [];
  3039. for (let i = 0; i < 5; a.push(function () { return i; }), ++i) { }
  3040. let res = "";
  3041. for (let k = 0; k < 5; ++k) {
  3042. res += ""+a[k]();
  3043. }
  3044. res;
  3045. `
  3046. testScript(SCRIPT, asciiString("12345"), t)
  3047. }
  3048. func TestVarForLoop(t *testing.T) {
  3049. const SCRIPT = `
  3050. var f = [];
  3051. for (var i = 0, j = 0; i < 3; i++) {
  3052. f.push(function() {
  3053. return i;
  3054. });
  3055. }
  3056. f.length === 3 && f[0]() === 3 && f[1]() === 3 && f[2]() === 3;
  3057. `
  3058. testScript(SCRIPT, valueTrue, t)
  3059. }
  3060. func TestLexicalForOfLoop(t *testing.T) {
  3061. const SCRIPT = `
  3062. var f = [];
  3063. for (let i of [0, 1, 2]) {
  3064. f.push(function() {
  3065. return i;
  3066. });
  3067. }
  3068. f.length === 3 && f[0]() === 0 && f[1]() === 1 && f[2]() === 2;
  3069. `
  3070. testScript(SCRIPT, valueTrue, t)
  3071. }
  3072. func TestLexicalForOfLoopContBreak(t *testing.T) {
  3073. const SCRIPT = `
  3074. const f = [];
  3075. for (let i of [0, 1, 2, 3, 4, 5]) {
  3076. if (i % 2) continue;
  3077. f.push(function() {
  3078. return i;
  3079. });
  3080. if (i > 2) break;
  3081. }
  3082. let res = "";
  3083. f.forEach(function(item) {res += item()});
  3084. f.length === 3 && res === "024";
  3085. `
  3086. testScript(SCRIPT, valueTrue, t)
  3087. }
  3088. func TestVarBlockConflict(t *testing.T) {
  3089. const SCRIPT = `
  3090. let x;
  3091. {
  3092. if (false) {
  3093. var x;
  3094. }
  3095. }
  3096. `
  3097. _, err := Compile("", SCRIPT, false)
  3098. if err == nil {
  3099. t.Fatal("Expected an error")
  3100. }
  3101. }
  3102. func TestVarBlockConflictEval(t *testing.T) {
  3103. const SCRIPT = `
  3104. assert.throws(SyntaxError, function() {
  3105. let x;
  3106. {
  3107. if (true) {
  3108. eval("var x");
  3109. }
  3110. }
  3111. });
  3112. `
  3113. testScriptWithTestLib(SCRIPT, _undefined, t)
  3114. }
  3115. func TestVarBlockNoConflict(t *testing.T) {
  3116. const SCRIPT = `
  3117. function f() {
  3118. let x;
  3119. function ff() {
  3120. {
  3121. var x = 3;
  3122. }
  3123. }
  3124. ff();
  3125. }
  3126. f();
  3127. `
  3128. testScript(SCRIPT, _undefined, t)
  3129. }
  3130. func TestVarBlockNoConflictEval(t *testing.T) {
  3131. const SCRIPT = `
  3132. function f() {
  3133. let x;
  3134. function ff() {
  3135. {
  3136. eval("var x = 3");
  3137. }
  3138. }
  3139. ff();
  3140. }
  3141. f();
  3142. `
  3143. testScript(SCRIPT, _undefined, t)
  3144. }
  3145. func TestVarDeclCorrectScope(t *testing.T) {
  3146. const SCRIPT = `
  3147. function f() {
  3148. {
  3149. let z;
  3150. eval("var x = 3");
  3151. }
  3152. return x;
  3153. }
  3154. f();
  3155. `
  3156. testScript(SCRIPT, valueInt(3), t)
  3157. }
  3158. func TestLexicalCatch(t *testing.T) {
  3159. const SCRIPT = `
  3160. try {
  3161. throw null;
  3162. } catch (e) {
  3163. let x = 1;
  3164. function f() {}
  3165. e;
  3166. }
  3167. `
  3168. testScript(SCRIPT, _null, t)
  3169. }
  3170. func TestArgumentsLexicalDecl(t *testing.T) {
  3171. const SCRIPT = `
  3172. function f1() {
  3173. let arguments;
  3174. return arguments;
  3175. }
  3176. f1(42);
  3177. `
  3178. testScript(SCRIPT, _undefined, t)
  3179. }
  3180. func TestArgumentsLexicalDeclAssign(t *testing.T) {
  3181. const SCRIPT = `
  3182. function f1() {
  3183. let arguments = arguments;
  3184. return a;
  3185. }
  3186. assert.throws(ReferenceError, function() {
  3187. f1(42);
  3188. });
  3189. `
  3190. testScriptWithTestLib(SCRIPT, _undefined, t)
  3191. }
  3192. func TestLexicalConstModifyFromEval(t *testing.T) {
  3193. const SCRIPT = `
  3194. const x = 1;
  3195. function f() {
  3196. eval("x = 2");
  3197. }
  3198. assert.throws(TypeError, function() {
  3199. f();
  3200. });
  3201. `
  3202. testScriptWithTestLib(SCRIPT, _undefined, t)
  3203. }
  3204. func TestLexicalStrictNames(t *testing.T) {
  3205. const SCRIPT = `let eval = 1;`
  3206. _, err := Compile("", SCRIPT, true)
  3207. if err == nil {
  3208. t.Fatal("Expected an error")
  3209. }
  3210. }
  3211. func TestAssignAfterStackExpand(t *testing.T) {
  3212. // make sure the reference to the variable x does not remain stale after the stack is copied
  3213. const SCRIPT = `
  3214. function f() {
  3215. let sum = 0;
  3216. for (let i = 0; i < arguments.length; i++) {
  3217. sum += arguments[i];
  3218. }
  3219. return sum;
  3220. }
  3221. function testAssignment() {
  3222. var x = 0;
  3223. var scope = {};
  3224. with (scope) {
  3225. x = (scope.x = f(0, 0, 0, 0, 0, 0, 1, 1), 1);
  3226. }
  3227. if (scope.x !== 2) {
  3228. throw new Error('#1: scope.x === 2. Actual: ' + (scope.x));
  3229. }
  3230. if (x !== 1) {
  3231. throw new Error('#2: x === 1. Actual: ' + (x));
  3232. }
  3233. }
  3234. testAssignment();
  3235. `
  3236. testScript(SCRIPT, _undefined, t)
  3237. }
  3238. func TestArgAccessFromDynamicStash(t *testing.T) {
  3239. const SCRIPT = `
  3240. function f(arg) {
  3241. function test() {
  3242. eval("");
  3243. return a;
  3244. }
  3245. return arg;
  3246. }
  3247. f(true);
  3248. `
  3249. testScript(SCRIPT, valueTrue, t)
  3250. }
  3251. func TestLoadMixedLex(t *testing.T) {
  3252. const SCRIPT = `
  3253. function f() {
  3254. let a = 1;
  3255. {
  3256. function inner() {
  3257. eval("var a = true");
  3258. return a;
  3259. }
  3260. return inner();
  3261. }
  3262. }
  3263. f();
  3264. `
  3265. testScript(SCRIPT, valueTrue, t)
  3266. }
  3267. func TestObjectLiteralSpread(t *testing.T) {
  3268. const SCRIPT = `
  3269. let src = {prop1: 1};
  3270. Object.defineProperty(src, "prop2", {value: 2, configurable: true});
  3271. Object.defineProperty(src, "prop3", {value: 3, enumerable: true, configurable: true});
  3272. let target = {prop4: 4, ...src};
  3273. assert(deepEqual(target, {prop1: 1, prop3: 3, prop4: 4}));
  3274. `
  3275. testScriptWithTestLibX(SCRIPT, _undefined, t)
  3276. }
  3277. func TestArrayLiteralSpread(t *testing.T) {
  3278. const SCRIPT = `
  3279. let a1 = [1, 2];
  3280. let a2 = [3, 4];
  3281. let a = [...a1, 0, ...a2, 1];
  3282. assert(compareArray(a, [1, 2, 0, 3, 4, 1]));
  3283. `
  3284. testScriptWithTestLib(SCRIPT, _undefined, t)
  3285. }
  3286. func TestObjectAssignmentPattern(t *testing.T) {
  3287. const SCRIPT = `
  3288. let a, b, c;
  3289. ({a, b, c=3} = {a: 1, b: 2});
  3290. assert.sameValue(a, 1, "a");
  3291. assert.sameValue(b, 2, "b");
  3292. assert.sameValue(c, 3, "c");
  3293. `
  3294. testScriptWithTestLib(SCRIPT, _undefined, t)
  3295. }
  3296. func TestObjectAssignmentPatternNoDyn(t *testing.T) {
  3297. const SCRIPT = `
  3298. (function() {
  3299. let a, b, c;
  3300. ({a, b, c=3} = {a: 1, b: 2});
  3301. assert.sameValue(a, 1, "a");
  3302. assert.sameValue(b, 2, "b");
  3303. assert.sameValue(c, 3, "c");
  3304. })();
  3305. `
  3306. testScriptWithTestLib(SCRIPT, _undefined, t)
  3307. }
  3308. func TestObjectAssignmentPatternNested(t *testing.T) {
  3309. const SCRIPT = `
  3310. let a, b, c, d;
  3311. ({a, b, c: {d} = 3} = {a: 1, b: 2, c: {d: 4}});
  3312. assert.sameValue(a, 1, "a");
  3313. assert.sameValue(b, 2, "b");
  3314. assert.sameValue(c, undefined, "c");
  3315. assert.sameValue(d, 4, "d");
  3316. `
  3317. testScriptWithTestLib(SCRIPT, _undefined, t)
  3318. }
  3319. func TestObjectAssignmentPatternEvalOrder(t *testing.T) {
  3320. const SCRIPT = `
  3321. let trace = "";
  3322. let target_obj = {};
  3323. function src() {
  3324. trace += "src(),";
  3325. return {
  3326. get a() {
  3327. trace += "get a,";
  3328. return "a";
  3329. }
  3330. }
  3331. }
  3332. function prop1() {
  3333. trace += "prop1(),"
  3334. return {
  3335. toString: function() {
  3336. trace += "prop1-to-string(),";
  3337. return "a";
  3338. }
  3339. }
  3340. }
  3341. function prop2() {
  3342. trace += "prop2(),";
  3343. return {
  3344. toString: function() {
  3345. trace += "prop2-to-string(),";
  3346. return "b";
  3347. }
  3348. }
  3349. }
  3350. function target() {
  3351. trace += "target(),"
  3352. return target_obj;
  3353. }
  3354. let a, b;
  3355. ({[prop1()]: target().a, [prop2()]: b} = src());
  3356. if (target_obj.a !== "a") {
  3357. throw new Error("target_obj.a="+target_obj.a);
  3358. }
  3359. trace;
  3360. `
  3361. testScript(SCRIPT, asciiString("src(),prop1(),prop1-to-string(),target(),get a,prop2(),prop2-to-string(),"), t)
  3362. }
  3363. func TestArrayAssignmentPatternEvalOrder(t *testing.T) {
  3364. const SCRIPT = `
  3365. let trace = "";
  3366. let src_arr = {
  3367. [Symbol.iterator]: function() {
  3368. let done = false;
  3369. return {
  3370. next: function() {
  3371. trace += "next,";
  3372. if (!done) {
  3373. done = true;
  3374. return {value: 0};
  3375. }
  3376. return {done: true};
  3377. },
  3378. return: function() {
  3379. trace += "return,";
  3380. }
  3381. }
  3382. }
  3383. }
  3384. function src() {
  3385. trace += "src(),";
  3386. return src_arr;
  3387. }
  3388. let tgt = {
  3389. get a() {
  3390. trace += "get a,";
  3391. return "a";
  3392. },
  3393. get b() {
  3394. trace += "get b,";
  3395. return "b";
  3396. }
  3397. }
  3398. function target() {
  3399. trace += "target(),";
  3400. return tgt;
  3401. }
  3402. function default_a() {
  3403. trace += "default a,";
  3404. return "def_a";
  3405. }
  3406. function default_b() {
  3407. trace += "default b,";
  3408. return "def_b";
  3409. }
  3410. ([target().a = default_a(), target().b = default_b()] = src());
  3411. trace;
  3412. `
  3413. testScript(SCRIPT, asciiString("src(),target(),next,target(),next,default b,"), t)
  3414. }
  3415. func TestObjectAssignPatternRest(t *testing.T) {
  3416. const SCRIPT = `
  3417. let a, b, c, d;
  3418. ({a, b, c, ...d} = {a: 1, b: 2, d: 4});
  3419. assert.sameValue(a, 1, "a");
  3420. assert.sameValue(b, 2, "b");
  3421. assert.sameValue(c, undefined, "c");
  3422. assert(deepEqual(d, {d: 4}), "d");
  3423. `
  3424. testScriptWithTestLibX(SCRIPT, _undefined, t)
  3425. }
  3426. func TestObjectBindPattern(t *testing.T) {
  3427. const SCRIPT = `
  3428. let {a, b, c, ...d} = {a: 1, b: 2, d: 4};
  3429. assert.sameValue(a, 1, "a");
  3430. assert.sameValue(b, 2, "b");
  3431. assert.sameValue(c, undefined, "c");
  3432. assert(deepEqual(d, {d: 4}), "d");
  3433. var { x: y, } = { x: 23 };
  3434. assert.sameValue(y, 23);
  3435. assert.throws(ReferenceError, function() {
  3436. x;
  3437. });
  3438. `
  3439. testScriptWithTestLibX(SCRIPT, _undefined, t)
  3440. }
  3441. func TestObjLiteralShorthandWithInitializer(t *testing.T) {
  3442. const SCRIPT = `
  3443. o = {a=1};
  3444. `
  3445. _, err := Compile("", SCRIPT, false)
  3446. if err == nil {
  3447. t.Fatal("Expected an error")
  3448. }
  3449. }
  3450. func TestObjLiteralShorthandLetStringLit(t *testing.T) {
  3451. const SCRIPT = `
  3452. o = {"let"};
  3453. `
  3454. _, err := Compile("", SCRIPT, false)
  3455. if err == nil {
  3456. t.Fatal("Expected an error")
  3457. }
  3458. }
  3459. func TestObjLiteralComputedKeys(t *testing.T) {
  3460. const SCRIPT = `
  3461. let o = {
  3462. get [Symbol.toString]() {
  3463. }
  3464. }
  3465. `
  3466. testScript(SCRIPT, _undefined, t)
  3467. }
  3468. func TestObjLiteralComputedKeysEvalOrder(t *testing.T) {
  3469. const SCRIPT = `
  3470. let trace = [];
  3471. function key() {
  3472. trace.push("key");
  3473. return {
  3474. toString: function() {
  3475. trace.push("key-toString");
  3476. return "key";
  3477. }
  3478. }
  3479. }
  3480. function val() {
  3481. trace.push("val");
  3482. return "val";
  3483. }
  3484. const _ = {
  3485. [key()]: val(),
  3486. }
  3487. trace.join(",");
  3488. `
  3489. testScript(SCRIPT, asciiString("key,key-toString,val"), t)
  3490. }
  3491. func TestArrayAssignPattern(t *testing.T) {
  3492. const SCRIPT = `
  3493. let a, b;
  3494. ([a, b] = [1, 2]);
  3495. a === 1 && b === 2;
  3496. `
  3497. testScript(SCRIPT, valueTrue, t)
  3498. }
  3499. func TestArrayAssignPattern1(t *testing.T) {
  3500. const SCRIPT = `
  3501. let a, b;
  3502. ([a = 3, b = 2] = [1]);
  3503. a === 1 && b === 2;
  3504. `
  3505. testScript(SCRIPT, valueTrue, t)
  3506. }
  3507. func TestArrayAssignPatternLHS(t *testing.T) {
  3508. const SCRIPT = `
  3509. let a = {};
  3510. [ a.b, a['c'] = 2 ] = [1];
  3511. a.b === 1 && a.c === 2;
  3512. `
  3513. testScript(SCRIPT, valueTrue, t)
  3514. }
  3515. func TestArrayAssignPatternElision(t *testing.T) {
  3516. const SCRIPT = `
  3517. let a, b;
  3518. ([a,, b] = [1, 4, 2]);
  3519. a === 1 && b === 2;
  3520. `
  3521. testScript(SCRIPT, valueTrue, t)
  3522. }
  3523. func TestArrayAssignPatternRestPattern(t *testing.T) {
  3524. const SCRIPT = `
  3525. let a, b, z;
  3526. [ z, ...[a, b] ] = [0, 1, 2];
  3527. z === 0 && a === 1 && b === 2;
  3528. `
  3529. testScript(SCRIPT, valueTrue, t)
  3530. }
  3531. func TestArrayBindingPattern(t *testing.T) {
  3532. const SCRIPT = `
  3533. let [a, b] = [1, 2];
  3534. a === 1 && b === 2;
  3535. `
  3536. testScript(SCRIPT, valueTrue, t)
  3537. }
  3538. func TestObjectPatternShorthandInit(t *testing.T) {
  3539. const SCRIPT = `
  3540. [...{ x = 1 }] = [];
  3541. x;
  3542. `
  3543. testScript(SCRIPT, valueInt(1), t)
  3544. }
  3545. func TestArrayBindingPatternRestPattern(t *testing.T) {
  3546. const SCRIPT = `
  3547. const [a, b, ...[c, d]] = [1, 2, 3, 4];
  3548. a === 1 && b === 2 && c === 3 && d === 4;
  3549. `
  3550. testScript(SCRIPT, valueTrue, t)
  3551. }
  3552. func TestForVarPattern(t *testing.T) {
  3553. const SCRIPT = `
  3554. var o = {a: 1};
  3555. var trace = "";
  3556. for (var [key, value] of Object.entries(o)) {
  3557. trace += key+":"+value;
  3558. }
  3559. trace;
  3560. `
  3561. testScript(SCRIPT, asciiString("a:1"), t)
  3562. }
  3563. func TestForLexPattern(t *testing.T) {
  3564. const SCRIPT = `
  3565. var o = {a: 1};
  3566. var trace = "";
  3567. for (const [key, value] of Object.entries(o)) {
  3568. trace += key+":"+value;
  3569. }
  3570. trace;
  3571. `
  3572. testScript(SCRIPT, asciiString("a:1"), t)
  3573. }
  3574. func TestBindingPatternRestTrailingComma(t *testing.T) {
  3575. const SCRIPT = `
  3576. const [a, b, ...rest,] = [];
  3577. `
  3578. _, err := Compile("", SCRIPT, false)
  3579. if err == nil {
  3580. t.Fatal("Expected an error")
  3581. }
  3582. }
  3583. func TestAssignPatternRestTrailingComma(t *testing.T) {
  3584. const SCRIPT = `
  3585. ([a, b, ...rest,] = []);
  3586. `
  3587. _, err := Compile("", SCRIPT, false)
  3588. if err == nil {
  3589. t.Fatal("Expected an error")
  3590. }
  3591. }
  3592. func TestFuncParamInitializerSimple(t *testing.T) {
  3593. const SCRIPT = `
  3594. function f(a = 1) {
  3595. return a;
  3596. }
  3597. ""+f()+f(2);
  3598. `
  3599. testScript(SCRIPT, asciiString("12"), t)
  3600. }
  3601. func TestFuncParamObjectPatternSimple(t *testing.T) {
  3602. const SCRIPT = `
  3603. function f({a, b} = {a: 1, b: 2}) {
  3604. return "" + a + b;
  3605. }
  3606. ""+f()+" "+f({a: 3, b: 4});
  3607. `
  3608. testScript(SCRIPT, asciiString("12 34"), t)
  3609. }
  3610. func TestFuncParamRestStackSimple(t *testing.T) {
  3611. const SCRIPT = `
  3612. function f(arg1, ...rest) {
  3613. return rest;
  3614. }
  3615. let ar = f(1, 2, 3);
  3616. ar.join(",");
  3617. `
  3618. testScript(SCRIPT, asciiString("2,3"), t)
  3619. }
  3620. func TestFuncParamRestStashSimple(t *testing.T) {
  3621. const SCRIPT = `
  3622. function f(arg1, ...rest) {
  3623. eval("true");
  3624. return rest;
  3625. }
  3626. let ar = f(1, 2, 3);
  3627. ar.join(",");
  3628. `
  3629. testScript(SCRIPT, asciiString("2,3"), t)
  3630. }
  3631. func TestRestArgsNotInStash(t *testing.T) {
  3632. const SCRIPT = `
  3633. function f(...rest) {
  3634. () => rest;
  3635. return rest.length;
  3636. }
  3637. f(1,2);
  3638. `
  3639. testScript(SCRIPT, valueInt(2), t)
  3640. }
  3641. func TestRestArgsInStash(t *testing.T) {
  3642. const SCRIPT = `
  3643. function f(first, ...rest) {
  3644. () => first;
  3645. () => rest;
  3646. return rest.length;
  3647. }
  3648. f(1,2);
  3649. `
  3650. testScript(SCRIPT, valueInt(1), t)
  3651. }
  3652. func TestRestArgsInStashFwdRef(t *testing.T) {
  3653. const SCRIPT = `
  3654. function f(first = eval(), ...rest) {
  3655. () => first;
  3656. () => rest;
  3657. return rest.length === 1 && rest[0] === 2;
  3658. }
  3659. f(1,2);
  3660. `
  3661. testScript(SCRIPT, valueTrue, t)
  3662. }
  3663. func TestFuncParamRestPattern(t *testing.T) {
  3664. const SCRIPT = `
  3665. function f(arg1, ...{0: rest1, 1: rest2}) {
  3666. return ""+arg1+" "+rest1+" "+rest2;
  3667. }
  3668. f(1, 2, 3);
  3669. `
  3670. testScript(SCRIPT, asciiString("1 2 3"), t)
  3671. }
  3672. func TestFuncParamForwardRef(t *testing.T) {
  3673. const SCRIPT = `
  3674. function f(a = b + 1, b) {
  3675. return ""+a+" "+b;
  3676. }
  3677. f(1, 2);
  3678. `
  3679. testScript(SCRIPT, asciiString("1 2"), t)
  3680. }
  3681. func TestFuncParamForwardRefMissing(t *testing.T) {
  3682. const SCRIPT = `
  3683. function f(a = b + 1, b) {
  3684. return ""+a+" "+b;
  3685. }
  3686. assert.throws(ReferenceError, function() {
  3687. f();
  3688. });
  3689. `
  3690. testScriptWithTestLib(SCRIPT, _undefined, t)
  3691. }
  3692. func TestFuncParamInnerRef(t *testing.T) {
  3693. const SCRIPT = `
  3694. function f(a = inner) {
  3695. var inner = 42;
  3696. return a;
  3697. }
  3698. assert.throws(ReferenceError, function() {
  3699. f();
  3700. });
  3701. `
  3702. testScriptWithTestLib(SCRIPT, _undefined, t)
  3703. }
  3704. func TestFuncParamInnerRefEval(t *testing.T) {
  3705. const SCRIPT = `
  3706. function f(a = eval("inner")) {
  3707. var inner = 42;
  3708. return a;
  3709. }
  3710. assert.throws(ReferenceError, function() {
  3711. f();
  3712. });
  3713. `
  3714. testScriptWithTestLib(SCRIPT, _undefined, t)
  3715. }
  3716. func TestFuncParamCalleeName(t *testing.T) {
  3717. const SCRIPT = `
  3718. function f(a = f) {
  3719. var f;
  3720. return f;
  3721. }
  3722. typeof f();
  3723. `
  3724. testScript(SCRIPT, asciiString("undefined"), t)
  3725. }
  3726. func TestFuncParamVarCopy(t *testing.T) {
  3727. const SCRIPT = `
  3728. function f(a = f) {
  3729. var a;
  3730. return a;
  3731. }
  3732. typeof f();
  3733. `
  3734. testScript(SCRIPT, asciiString("function"), t)
  3735. }
  3736. func TestFuncParamScope(t *testing.T) {
  3737. const SCRIPT = `
  3738. var x = 'outside';
  3739. var probe1, probe2;
  3740. function f(
  3741. _ = probe1 = function() { return x; },
  3742. __ = (eval('var x = "inside";'), probe2 = function() { return x; })
  3743. ) {
  3744. }
  3745. f();
  3746. probe1()+" "+probe2();
  3747. `
  3748. testScript(SCRIPT, asciiString("inside inside"), t)
  3749. }
  3750. func TestDefParamsStackPtr(t *testing.T) {
  3751. const SCRIPT = `
  3752. function A() {};
  3753. A.B = function () {};
  3754. function D(message = '') {
  3755. var C = A.B;
  3756. C([1,2,3]);
  3757. };
  3758. D();
  3759. `
  3760. testScript(SCRIPT, _undefined, t)
  3761. }
  3762. func TestNestedVariadicCalls(t *testing.T) {
  3763. const SCRIPT = `
  3764. function f() {
  3765. return Array.prototype.join.call(arguments, ",");
  3766. }
  3767. f(...[1], "a", f(...[2]));
  3768. `
  3769. testScript(SCRIPT, asciiString("1,a,2"), t)
  3770. }
  3771. func TestVariadicNew(t *testing.T) {
  3772. const SCRIPT = `
  3773. function C() {
  3774. this.res = Array.prototype.join.call(arguments, ",");
  3775. }
  3776. var c = new C(...[1], "a", new C(...[2]).res);
  3777. c.res;
  3778. `
  3779. testScript(SCRIPT, asciiString("1,a,2"), t)
  3780. }
  3781. func TestVariadicUseStackVars(t *testing.T) {
  3782. const SCRIPT = `
  3783. function A(message) { return message; }
  3784. function B(...args){
  3785. return A(...args);
  3786. }
  3787. B("C");
  3788. `
  3789. testScript(SCRIPT, asciiString("C"), t)
  3790. }
  3791. func TestCatchParamPattern(t *testing.T) {
  3792. const SCRIPT = `
  3793. function f() {
  3794. let x = 3;
  3795. try {
  3796. throw {a: 1, b: 2};
  3797. } catch ({a, b, c = x}) {
  3798. let x = 99;
  3799. return ""+a+" "+b+" "+c;
  3800. }
  3801. }
  3802. f();
  3803. `
  3804. testScript(SCRIPT, asciiString("1 2 3"), t)
  3805. }
  3806. func TestArrowUseStrict(t *testing.T) {
  3807. // simple parameter list -- ok
  3808. _, err := Compile("", "(a) => {'use strict';}", false)
  3809. if err != nil {
  3810. t.Fatal(err)
  3811. }
  3812. // non-simple parameter list -- syntax error
  3813. _, err = Compile("", "(a=0) => {'use strict';}", false)
  3814. if err == nil {
  3815. t.Fatal("expected error")
  3816. }
  3817. }
  3818. func TestArrowBoxedThis(t *testing.T) {
  3819. const SCRIPT = `
  3820. var context;
  3821. fn = function() {
  3822. return (arg) => { var local; context = this; };
  3823. };
  3824. fn()();
  3825. context === this;
  3826. `
  3827. testScript(SCRIPT, valueTrue, t)
  3828. }
  3829. func TestParameterOverride(t *testing.T) {
  3830. const SCRIPT = `
  3831. function f(arg) {
  3832. var arg = arg || "default"
  3833. return arg
  3834. }
  3835. f()
  3836. `
  3837. testScript(SCRIPT, asciiString("default"), t)
  3838. }
  3839. func TestEvalInIterScope(t *testing.T) {
  3840. const SCRIPT = `
  3841. for (let a = 0; a < 1; a++) {
  3842. eval("a");
  3843. }
  3844. `
  3845. testScript(SCRIPT, valueInt(0), t)
  3846. }
  3847. func TestTemplateLiterals(t *testing.T) {
  3848. vm := New()
  3849. _, err := vm.RunString("const a = 1, b = 'b';")
  3850. if err != nil {
  3851. t.Fatal(err)
  3852. }
  3853. f := func(t *testing.T, template, expected string) {
  3854. res, err := vm.RunString(template)
  3855. if err != nil {
  3856. t.Fatal(err)
  3857. }
  3858. if actual := res.Export(); actual != expected {
  3859. t.Fatalf("Expected: %q, actual: %q", expected, actual)
  3860. }
  3861. }
  3862. t.Run("empty", func(t *testing.T) {
  3863. f(t, "``", "")
  3864. })
  3865. t.Run("noSub", func(t *testing.T) {
  3866. f(t, "`test`", "test")
  3867. })
  3868. t.Run("emptyTail", func(t *testing.T) {
  3869. f(t, "`a=${a},b=${b}`", "a=1,b=b")
  3870. })
  3871. t.Run("emptyHead", func(t *testing.T) {
  3872. f(t, "`${a},b=${b}$`", "1,b=b$")
  3873. })
  3874. t.Run("headAndTail", func(t *testing.T) {
  3875. f(t, "`a=${a},b=${b}$`", "a=1,b=b$")
  3876. })
  3877. }
  3878. func TestTaggedTemplate(t *testing.T) {
  3879. const SCRIPT = `
  3880. let res;
  3881. const o = {
  3882. tmpl() {
  3883. res = this;
  3884. return () => {};
  3885. }
  3886. }
  3887. ` +
  3888. "o.tmpl()`test`;" + `
  3889. res === o;
  3890. `
  3891. testScript(SCRIPT, valueTrue, t)
  3892. }
  3893. func TestDuplicateGlobalFunc(t *testing.T) {
  3894. const SCRIPT = `
  3895. function a(){}
  3896. function b(){ return "b" }
  3897. function c(){ return "c" }
  3898. function a(){}
  3899. b();
  3900. `
  3901. testScript(SCRIPT, asciiString("b"), t)
  3902. }
  3903. func TestDuplicateFunc(t *testing.T) {
  3904. const SCRIPT = `
  3905. function f() {
  3906. function a(){}
  3907. function b(){ return "b" }
  3908. function c(){ return "c" }
  3909. function a(){}
  3910. return b();
  3911. }
  3912. f();
  3913. `
  3914. testScript(SCRIPT, asciiString("b"), t)
  3915. }
  3916. func TestSrcLocations(t *testing.T) {
  3917. // Do not reformat, assertions depend on the line and column numbers
  3918. const SCRIPT = `
  3919. let i = {
  3920. valueOf() {
  3921. throw new Error();
  3922. }
  3923. };
  3924. try {
  3925. i++;
  3926. } catch(e) {
  3927. assertStack(e, [["test.js", "valueOf", 4, 10],
  3928. ["test.js", "", 8, 3]
  3929. ]);
  3930. }
  3931. Object.defineProperty(globalThis, "x", {
  3932. get() {
  3933. throw new Error();
  3934. },
  3935. set() {
  3936. throw new Error();
  3937. }
  3938. });
  3939. try {
  3940. x;
  3941. } catch(e) {
  3942. assertStack(e, [["test.js", "get", 17, 10],
  3943. ["test.js", "", 25, 3]
  3944. ]);
  3945. }
  3946. try {
  3947. x++;
  3948. } catch(e) {
  3949. assertStack(e, [["test.js", "get", 17, 10],
  3950. ["test.js", "", 33, 3]
  3951. ]);
  3952. }
  3953. try {
  3954. x = 2;
  3955. } catch(e) {
  3956. assertStack(e, [["test.js", "set", 20, 10],
  3957. ["test.js", "", 41, 3]
  3958. ]);
  3959. }
  3960. try {
  3961. +i;
  3962. } catch(e) {
  3963. assertStack(e, [["test.js", "valueOf", 4, 10],
  3964. ["test.js", "", 49, 4]
  3965. ]);
  3966. }
  3967. `
  3968. testScriptWithTestLibX(SCRIPT, _undefined, t)
  3969. }
  3970. func TestSrcLocationThrowLiteral(t *testing.T) {
  3971. vm := New()
  3972. _, err := vm.RunString(`
  3973. const z = 1;
  3974. throw "";
  3975. `)
  3976. if ex, ok := err.(*Exception); ok {
  3977. pos := ex.stack[0].Position()
  3978. if pos.Line != 3 {
  3979. t.Fatal(pos)
  3980. }
  3981. } else {
  3982. t.Fatal(err)
  3983. }
  3984. }
  3985. func TestSrcLocation(t *testing.T) {
  3986. prg := MustCompile("test.js", `
  3987. f();
  3988. var x = 1;
  3989. let y = 1;
  3990. let [z1, z2] = [0, 0];
  3991. var [z3, z4] = [0, 0];
  3992. `, false)
  3993. const (
  3994. varLine = 3
  3995. letLine = 4
  3996. dstrLetLine = 5
  3997. dstrVarLine = 7
  3998. )
  3999. linesOfInterest := map[int]string{
  4000. varLine: "var",
  4001. letLine: "let",
  4002. dstrLetLine: "destruct let",
  4003. dstrVarLine: "destruct var",
  4004. }
  4005. for i := range prg.code {
  4006. loc := prg.src.Position(prg.sourceOffset(i))
  4007. delete(linesOfInterest, loc.Line)
  4008. if len(linesOfInterest) == 0 {
  4009. break
  4010. }
  4011. }
  4012. for _, v := range linesOfInterest {
  4013. t.Fatalf("no %s line", v)
  4014. }
  4015. }
  4016. func TestBadObjectKey(t *testing.T) {
  4017. _, err := Compile("", "({!:0})", false)
  4018. if err == nil {
  4019. t.Fatal("expected error")
  4020. }
  4021. }
  4022. func TestConstantFolding(t *testing.T) {
  4023. testValues := func(prg *Program, result Value, t *testing.T) {
  4024. if len(prg.values) != 1 || !prg.values[0].SameAs(result) {
  4025. prg.dumpCode(t.Logf)
  4026. t.Fatalf("values: %v", prg.values)
  4027. }
  4028. }
  4029. f := func(src string, result Value, t *testing.T) {
  4030. prg := MustCompile("test.js", src, false)
  4031. testValues(prg, result, t)
  4032. New().testPrg(prg, result, t)
  4033. }
  4034. ff := func(src string, result Value, t *testing.T) {
  4035. prg := MustCompile("test.js", src, false)
  4036. fl := prg.code[0].(*newFunc)
  4037. testValues(fl.prg, result, t)
  4038. New().testPrg(prg, result, t)
  4039. }
  4040. t.Run("lexical binding", func(t *testing.T) {
  4041. f("const x = 1 + 2; x", valueInt(3), t)
  4042. })
  4043. t.Run("var binding", func(t *testing.T) {
  4044. f("var x = 1 + 2; x", valueInt(3), t)
  4045. })
  4046. t.Run("assignment", func(t *testing.T) {
  4047. f("x = 1 + 2; x", valueInt(3), t)
  4048. })
  4049. t.Run("object pattern", func(t *testing.T) {
  4050. f("const {x = 1 + 2} = {}; x", valueInt(3), t)
  4051. })
  4052. t.Run("array pattern", func(t *testing.T) {
  4053. f("const [x = 1 + 2] = []; x", valueInt(3), t)
  4054. })
  4055. t.Run("object literal", func(t *testing.T) {
  4056. f("var o = {x: 1 + 2}; o.x", valueInt(3), t)
  4057. })
  4058. t.Run("array literal", func(t *testing.T) {
  4059. f("var a = [3, 3, 3, 1 + 2]; a[3]", valueInt(3), t)
  4060. })
  4061. t.Run("default function parameter", func(t *testing.T) {
  4062. ff("function f(arg = 1 + 2) {return arg}; f()", valueInt(3), t)
  4063. })
  4064. t.Run("return", func(t *testing.T) {
  4065. ff("function f() {return 1 + 2}; f()", valueInt(3), t)
  4066. })
  4067. }
  4068. func TestAssignBeforeInit(t *testing.T) {
  4069. const SCRIPT = `
  4070. assert.throws(ReferenceError, () => {
  4071. a = 1;
  4072. let a;
  4073. });
  4074. assert.throws(ReferenceError, () => {
  4075. ({a, b} = {a: 1, b: 2});
  4076. let a, b;
  4077. });
  4078. assert.throws(ReferenceError, () => {
  4079. (function() {
  4080. eval("");
  4081. ({a} = {a: 1});
  4082. })();
  4083. let a;
  4084. });
  4085. assert.throws(ReferenceError, () => {
  4086. const ctx = {x: 1};
  4087. function t() {
  4088. delete ctx.x;
  4089. return 42;
  4090. }
  4091. with(ctx) {
  4092. (function() {
  4093. 'use strict';
  4094. ({x} = {x: t()});
  4095. })();
  4096. }
  4097. return ctx.x;
  4098. });
  4099. assert.throws(ReferenceError, () => {
  4100. const ctx = {x: 1};
  4101. function t() {
  4102. delete ctx.x;
  4103. return 42;
  4104. }
  4105. with(ctx) {
  4106. (function() {
  4107. 'use strict';
  4108. const src = {};
  4109. Object.defineProperty(src, "x", {
  4110. get() {
  4111. return t();
  4112. }
  4113. });
  4114. ({x} = src);
  4115. })();
  4116. }
  4117. return ctx.x;
  4118. });
  4119. `
  4120. testScriptWithTestLib(SCRIPT, _undefined, t)
  4121. }
  4122. func TestOptChainCallee(t *testing.T) {
  4123. const SCRIPT = `
  4124. var a;
  4125. assert.sameValue(a?.(true), undefined);
  4126. a = null;
  4127. assert.sameValue(a?.(), undefined);
  4128. var o = {n: null};
  4129. assert.sameValue(o.m?.(true), undefined);
  4130. assert.sameValue(o.n?.(true), undefined);
  4131. `
  4132. testScriptWithTestLib(SCRIPT, _undefined, t)
  4133. }
  4134. func TestObjectLiteralSuper(t *testing.T) {
  4135. const SCRIPT = `
  4136. const proto = {
  4137. m() {
  4138. return 40;
  4139. }
  4140. }
  4141. const o = {
  4142. m() {
  4143. return super.m() + 2;
  4144. }
  4145. }
  4146. o.__proto__ = proto;
  4147. o.m();
  4148. `
  4149. testScript(SCRIPT, intToValue(42), t)
  4150. }
  4151. func TestClassCaptureThisInFieldInit(t *testing.T) {
  4152. const SCRIPT = `
  4153. let capture;
  4154. class C {
  4155. a = () => this
  4156. }
  4157. let c = new C();
  4158. c.a() === c;
  4159. `
  4160. testScript(SCRIPT, valueTrue, t)
  4161. }
  4162. func TestClassUseThisInFieldInit(t *testing.T) {
  4163. const SCRIPT = `
  4164. let capture;
  4165. class C {
  4166. a = this
  4167. }
  4168. let c = new C();
  4169. c.a === c;
  4170. `
  4171. testScript(SCRIPT, valueTrue, t)
  4172. }
  4173. func TestClassCaptureThisInStaticFieldInit(t *testing.T) {
  4174. const SCRIPT = `
  4175. let capture;
  4176. class C {
  4177. static a = (capture = () => this, 0)
  4178. }
  4179. let c = new C();
  4180. capture() === C;
  4181. `
  4182. testScript(SCRIPT, valueTrue, t)
  4183. }
  4184. func TestClassDynCaptureThisInStaticFieldInit(t *testing.T) {
  4185. const SCRIPT = `
  4186. class C {
  4187. static a = eval("this")
  4188. }
  4189. C.a === C;
  4190. `
  4191. testScript(SCRIPT, valueTrue, t)
  4192. }
  4193. func TestCompileClass(t *testing.T) {
  4194. const SCRIPT = `
  4195. class C extends Error {
  4196. a = true;
  4197. b = 1;
  4198. ["b".toUpperCase()] = 2
  4199. static A = Math.random() < 1
  4200. constructor(message = "My Error") {
  4201. super(message);
  4202. }
  4203. static M() {
  4204. }
  4205. static M1() {
  4206. }
  4207. m() {
  4208. //return C.a;
  4209. }
  4210. m1() {
  4211. return true;
  4212. }
  4213. static {
  4214. this.supername = super.name;
  4215. }
  4216. }
  4217. let c = new C();
  4218. c.a === true && c.b === 1 && c.B === 2 && c.m1() && C.A && C.supername === "Error";
  4219. `
  4220. testScript(SCRIPT, valueTrue, t)
  4221. }
  4222. func TestSuperInEval(t *testing.T) {
  4223. const SCRIPT = `
  4224. class C extends Error {
  4225. constructor() {
  4226. eval("super()");
  4227. }
  4228. m() {
  4229. return eval("super.name");
  4230. }
  4231. }
  4232. let c = new C();
  4233. c.m() === "Error";
  4234. `
  4235. testScript(SCRIPT, valueTrue, t)
  4236. }
  4237. func TestSuperRefDot(t *testing.T) {
  4238. const SCRIPT = `
  4239. let thisGet, thisSet;
  4240. class P {
  4241. _p = 0
  4242. get p() {
  4243. thisGet = this;
  4244. return this._p;
  4245. }
  4246. set p(v) {
  4247. thisSet = this;
  4248. this._p = v;
  4249. }
  4250. }
  4251. class C extends P {
  4252. g() {
  4253. return super.p;
  4254. }
  4255. s(v) {
  4256. super.p = v;
  4257. }
  4258. inc() {
  4259. super.p++;
  4260. }
  4261. incR() {
  4262. return super.p++;
  4263. }
  4264. inc1() {
  4265. ++super.p;
  4266. }
  4267. inc1R() {
  4268. return ++super.p;
  4269. }
  4270. unary() {
  4271. return +super.p;
  4272. }
  4273. pattern() {
  4274. [super.p] = [9];
  4275. }
  4276. }
  4277. let o = new C();
  4278. assert.sameValue(o.g(), 0, "get value");
  4279. assert.sameValue(thisGet, o, "get this");
  4280. o.s(1);
  4281. assert.sameValue(o._p, 1, "set value");
  4282. assert.sameValue(thisSet, o, "set this");
  4283. thisGet = undefined;
  4284. thisSet = undefined;
  4285. o.inc();
  4286. assert.sameValue(o._p, 2, "inc value");
  4287. assert.sameValue(thisGet, o, "inc thisGet");
  4288. assert.sameValue(thisSet, o, "inc thisSet");
  4289. thisGet = undefined;
  4290. thisSet = undefined;
  4291. assert.sameValue(o.incR(), 2, "incR result");
  4292. assert.sameValue(o._p, 3, "incR value");
  4293. assert.sameValue(thisGet, o, "incR thisGet");
  4294. assert.sameValue(thisSet, o, "incR thisSet");
  4295. thisGet = undefined;
  4296. thisSet = undefined;
  4297. o.inc1();
  4298. assert.sameValue(o._p, 4, "inc1 value");
  4299. assert.sameValue(thisGet, o, "inc1 thisGet");
  4300. assert.sameValue(thisSet, o, "inc1 thisSet");
  4301. thisGet = undefined;
  4302. thisSet = undefined;
  4303. assert.sameValue(o.inc1R(), 5, "inc1R result");
  4304. assert.sameValue(o._p, 5, "inc1R value");
  4305. assert.sameValue(thisGet, o, "inc1R thisGet");
  4306. assert.sameValue(thisSet, o, "inc1R thisSet");
  4307. assert.sameValue(o.unary(), 5, "unary");
  4308. o.pattern();
  4309. assert.sameValue(o._p, 9, "pattern");
  4310. `
  4311. testScriptWithTestLib(SCRIPT, _undefined, t)
  4312. }
  4313. func TestPrivateRefDot(t *testing.T) {
  4314. const SCRIPT = `
  4315. class C {
  4316. #p = 0;
  4317. g() {
  4318. return this.#p;
  4319. }
  4320. s(v) {
  4321. this.#p = v;
  4322. }
  4323. inc() {
  4324. this.#p++;
  4325. }
  4326. incR() {
  4327. return this.#p++;
  4328. }
  4329. inc1() {
  4330. ++this.#p;
  4331. }
  4332. inc1R() {
  4333. return ++this.#p;
  4334. }
  4335. pattern() {
  4336. [this.#p] = [9];
  4337. }
  4338. }
  4339. let o = new C();
  4340. assert.sameValue(o.g(), 0, "get value");
  4341. o.s(1);
  4342. assert.sameValue(o.g(), 1, "set value");
  4343. o.inc();
  4344. assert.sameValue(o.g(), 2, "inc value");
  4345. assert.sameValue(o.incR(), 2, "incR result");
  4346. assert.sameValue(o.g(), 3, "incR value");
  4347. o.inc1();
  4348. assert.sameValue(o.g(), 4, "inc1 value");
  4349. assert.sameValue(o.inc1R(), 5, "inc1R result");
  4350. assert.sameValue(o.g(), 5, "inc1R value");
  4351. o.pattern();
  4352. assert.sameValue(o.g(), 9, "pattern");
  4353. `
  4354. testScriptWithTestLib(SCRIPT, _undefined, t)
  4355. }
  4356. func TestPrivateRefDotEval(t *testing.T) {
  4357. const SCRIPT = `
  4358. class C {
  4359. #p = 0;
  4360. g() {
  4361. return eval("this.#p");
  4362. }
  4363. s(v) {
  4364. eval("this.#p = v");
  4365. }
  4366. incR() {
  4367. return eval("this.#p++");
  4368. }
  4369. inc1R() {
  4370. return eval("++this.#p");
  4371. }
  4372. pattern() {
  4373. eval("[this.#p] = [9]");
  4374. }
  4375. }
  4376. let o = new C();
  4377. assert.sameValue(o.g(), 0, "get value");
  4378. o.s(1);
  4379. assert.sameValue(o.g(), 1, "set value");
  4380. assert.sameValue(o.incR(), 1, "incR result");
  4381. assert.sameValue(o.g(), 2, "incR value");
  4382. assert.sameValue(o.inc1R(), 3, "inc1R result");
  4383. assert.sameValue(o.g(), 3, "inc1R value");
  4384. o.pattern();
  4385. assert.sameValue(o.g(), 9, "pattern");
  4386. `
  4387. testScriptWithTestLib(SCRIPT, _undefined, t)
  4388. }
  4389. func TestSuperRefDotCallee(t *testing.T) {
  4390. const SCRIPT = `
  4391. class P {
  4392. get p() {
  4393. return function() {
  4394. return this;
  4395. };
  4396. }
  4397. }
  4398. class C extends P {
  4399. m() {
  4400. return super.p();
  4401. }
  4402. }
  4403. let o = new C();
  4404. o.m() === o;
  4405. `
  4406. testScript(SCRIPT, valueTrue, t)
  4407. }
  4408. func TestSuperRefBracket(t *testing.T) {
  4409. const SCRIPT = `
  4410. let PROP = "p";
  4411. let thisGet, thisSet;
  4412. class P {
  4413. _p = 0
  4414. get p() {
  4415. thisGet = this;
  4416. return this._p;
  4417. }
  4418. set p(v) {
  4419. thisSet = this;
  4420. this._p = v;
  4421. }
  4422. }
  4423. class C extends P {
  4424. g() {
  4425. return super[PROP];
  4426. }
  4427. s(v) {
  4428. super[PROP] = v;
  4429. }
  4430. inc() {
  4431. super[PROP]++;
  4432. }
  4433. incR() {
  4434. return super[PROP]++;
  4435. }
  4436. inc1() {
  4437. ++super[PROP];
  4438. }
  4439. inc1R() {
  4440. return ++super[PROP];
  4441. }
  4442. pattern() {
  4443. [super[PROP]] = [9];
  4444. }
  4445. }
  4446. let o = new C();
  4447. assert.sameValue(o.g(), 0, "get value");
  4448. assert.sameValue(thisGet, o, "get this");
  4449. o.s(1);
  4450. assert.sameValue(o._p, 1, "set value");
  4451. assert.sameValue(thisSet, o, "set this");
  4452. thisGet = undefined;
  4453. thisSet = undefined;
  4454. o.inc();
  4455. assert.sameValue(o._p, 2, "inc value");
  4456. assert.sameValue(thisGet, o, "inc thisGet");
  4457. assert.sameValue(thisSet, o, "inc thisSet");
  4458. thisGet = undefined;
  4459. thisSet = undefined;
  4460. assert.sameValue(o.incR(), 2, "incR result");
  4461. assert.sameValue(o._p, 3, "incR value");
  4462. assert.sameValue(thisGet, o, "incR thisGet");
  4463. assert.sameValue(thisSet, o, "incR thisSet");
  4464. thisGet = undefined;
  4465. thisSet = undefined;
  4466. o.inc1();
  4467. assert.sameValue(o._p, 4, "inc1 value");
  4468. assert.sameValue(thisGet, o, "inc1 thisGet");
  4469. assert.sameValue(thisSet, o, "inc1 thisSet");
  4470. thisGet = undefined;
  4471. thisSet = undefined;
  4472. assert.sameValue(o.inc1R(), 5, "inc1R result");
  4473. assert.sameValue(o._p, 5, "inc1R value");
  4474. assert.sameValue(thisGet, o, "inc1R thisGet");
  4475. assert.sameValue(thisSet, o, "inc1R thisSet");
  4476. o.pattern();
  4477. assert.sameValue(o._p, 9, "pattern");
  4478. `
  4479. testScriptWithTestLib(SCRIPT, _undefined, t)
  4480. }
  4481. func TestSuperRefBracketEvalOrder(t *testing.T) {
  4482. const SCRIPT = `
  4483. let keyCallCount = 0;
  4484. function key() {
  4485. keyCallCount++;
  4486. C.prototype.__proto__ = null;
  4487. return "k";
  4488. }
  4489. class C {
  4490. constructor() {
  4491. super[key()]++;
  4492. }
  4493. }
  4494. assert.throws(TypeError, () => new C());
  4495. assert.sameValue(keyCallCount, 1);
  4496. `
  4497. testScriptWithTestLib(SCRIPT, _undefined, t)
  4498. }
  4499. func TestSuperRefBracketCallee(t *testing.T) {
  4500. const SCRIPT = `
  4501. let PROP = "p";
  4502. class P {
  4503. get p() {
  4504. return function() {
  4505. return this;
  4506. };
  4507. }
  4508. }
  4509. class C extends P {
  4510. m() {
  4511. return super[PROP]();
  4512. }
  4513. }
  4514. let o = new C();
  4515. o.m() === o;
  4516. `
  4517. testScript(SCRIPT, valueTrue, t)
  4518. }
  4519. func TestSuperBaseInCtor(t *testing.T) {
  4520. const SCRIPT = `
  4521. let result;
  4522. class Derived extends Object {
  4523. constructor() {
  4524. super();
  4525. result = super.constructor === Object;
  4526. }
  4527. }
  4528. new Derived();
  4529. result;
  4530. `
  4531. testScript(SCRIPT, valueTrue, t)
  4532. }
  4533. func TestClassNamedEval(t *testing.T) {
  4534. const SCRIPT = `
  4535. const C = class {
  4536. }
  4537. C.name === "C";
  4538. `
  4539. testScript(SCRIPT, valueTrue, t)
  4540. }
  4541. func TestClassNonDerived(t *testing.T) {
  4542. const SCRIPT = `
  4543. function initF() {
  4544. }
  4545. class C {
  4546. f = initF()
  4547. }
  4548. let c = new C();
  4549. `
  4550. testScript(SCRIPT, _undefined, t)
  4551. }
  4552. func TestClassExpr(t *testing.T) {
  4553. const SCRIPT = `
  4554. typeof Object.getOwnPropertyDescriptor(class {get f() {}}.prototype, "f").get === "function";
  4555. `
  4556. testScript(SCRIPT, valueTrue, t)
  4557. }
  4558. func TestClassSuperInHeritage(t *testing.T) {
  4559. const SCRIPT = `
  4560. class P {
  4561. a() {
  4562. return Error;
  4563. }
  4564. }
  4565. class C extends P {
  4566. m() {
  4567. class Inner extends super.a() {
  4568. }
  4569. return new Inner();
  4570. }
  4571. }
  4572. new C().m() instanceof Error;
  4573. `
  4574. testScript(SCRIPT, valueTrue, t)
  4575. }
  4576. func TestClassSuperInHeritageExpr(t *testing.T) {
  4577. const SCRIPT = `
  4578. class P {
  4579. a() {
  4580. return Error;
  4581. }
  4582. }
  4583. class C extends P {
  4584. m() {
  4585. function f(cls) {
  4586. return new cls();
  4587. }
  4588. return f(class Inner extends super.a() {
  4589. })
  4590. }
  4591. }
  4592. new C().m() instanceof Error;
  4593. `
  4594. testScript(SCRIPT, valueTrue, t)
  4595. }
  4596. func TestClassReferToBinding(t *testing.T) {
  4597. const SCRIPT = `
  4598. const CC = class C {
  4599. static T = 40
  4600. f = C.T + 2
  4601. }
  4602. let c = new CC();
  4603. c.f;
  4604. `
  4605. testScript(SCRIPT, intToValue(42), t)
  4606. }
  4607. func TestClassReferToBindingInStaticDecl(t *testing.T) {
  4608. const SCRIPT = `
  4609. class C {
  4610. static T = C.name
  4611. }
  4612. C.T;
  4613. `
  4614. testScript(SCRIPT, asciiString("C"), t)
  4615. }
  4616. func TestClassReferToBindingInStaticEval(t *testing.T) {
  4617. const SCRIPT = `
  4618. const CC = class C {
  4619. static T = eval("C.name")
  4620. }
  4621. CC.T;
  4622. `
  4623. testScript(SCRIPT, asciiString("C"), t)
  4624. }
  4625. func TestClassReferToBindingFromHeritage(t *testing.T) {
  4626. const SCRIPT = `
  4627. assert.throws(ReferenceError, () => {
  4628. class C extends C {
  4629. }
  4630. });
  4631. `
  4632. testScriptWithTestLib(SCRIPT, _undefined, t)
  4633. }
  4634. func TestClassCaptureSuperCallInArrowFunc(t *testing.T) {
  4635. const SCRIPT = `
  4636. let f;
  4637. class C extends class {} {
  4638. constructor() {
  4639. f = () => super();
  4640. f();
  4641. }
  4642. }
  4643. let c = new C();
  4644. `
  4645. testScript(SCRIPT, _undefined, t)
  4646. }
  4647. func TestClassCaptureSuperCallInNestedArrowFunc(t *testing.T) {
  4648. const SCRIPT = `
  4649. let f;
  4650. class P {
  4651. }
  4652. class C extends P {
  4653. constructor() {
  4654. f = () => () => super();
  4655. f()();
  4656. }
  4657. }
  4658. new C() instanceof P;
  4659. `
  4660. testScript(SCRIPT, valueTrue, t)
  4661. }
  4662. func TestThisInEval(t *testing.T) {
  4663. const SCRIPT = `
  4664. assert.sameValue(eval("this"), this, "global");
  4665. let o = {
  4666. f() {
  4667. return eval("this");
  4668. }
  4669. }
  4670. assert.sameValue(o.f(), o, "obj literal");
  4671. `
  4672. testScriptWithTestLib(SCRIPT, _undefined, t)
  4673. }
  4674. func TestStaticAsBindingTarget(t *testing.T) {
  4675. const SCRIPT = `
  4676. let [static] = [];
  4677. `
  4678. testScript(SCRIPT, _undefined, t)
  4679. }
  4680. func TestEvalInStaticFieldInit(t *testing.T) {
  4681. const SCRIPT = `
  4682. var C = class {
  4683. static f = 'test';
  4684. static g = this.f + '262';
  4685. static h = eval('this.g') + 'test';
  4686. }
  4687. C.f === "test" && C.g === "test262" && C.h === "test262test";
  4688. `
  4689. testScript(SCRIPT, valueTrue, t)
  4690. }
  4691. func TestClassPrivateElemInEval(t *testing.T) {
  4692. const SCRIPT = `
  4693. let f1, f2;
  4694. class C extends (f1 = o => eval("o.#a"), Object) {
  4695. static #a = 42;
  4696. static {
  4697. f2 = o => eval("o.#a");
  4698. assert.sameValue(C.#a, 42);
  4699. assert.sameValue((() => C.#a)(), 42);
  4700. }
  4701. }
  4702. assert.throws(SyntaxError, () => f1(C));
  4703. assert.sameValue(f2(C), 42);
  4704. `
  4705. testScriptWithTestLib(SCRIPT, _undefined, t)
  4706. }
  4707. func TestClassPrivateElemInIndirectEval(t *testing.T) {
  4708. const SCRIPT = `
  4709. let f1, f2;
  4710. class C extends (f1 = o => (0, eval)("o.#a"), Object) {
  4711. static #a = 42;
  4712. static {
  4713. f2 = o => (0, eval)("o.#a");
  4714. assert.throws(SyntaxError, () => (0, eval)("C.#a"));
  4715. }
  4716. }
  4717. assert.throws(SyntaxError, () => f1(C));
  4718. assert.throws(SyntaxError, () => f2(C));
  4719. `
  4720. testScriptWithTestLib(SCRIPT, _undefined, t)
  4721. }
  4722. func TestClassPrivateElemInFunction(t *testing.T) {
  4723. const SCRIPT = `
  4724. assert.throws(SyntaxError, () => {
  4725. class C {
  4726. static #a = 42;
  4727. static {
  4728. Function("o", "return o.#a");
  4729. }
  4730. }
  4731. });
  4732. `
  4733. testScriptWithTestLib(SCRIPT, _undefined, t)
  4734. }
  4735. func TestClassPrivateElementsDecl(t *testing.T) {
  4736. const SCRIPT = `
  4737. class C {
  4738. #a = 42;
  4739. get #b() {}
  4740. set #b(_) {}
  4741. get c() {
  4742. return this.#a;
  4743. }
  4744. #m() {
  4745. return this.#a;
  4746. }
  4747. static getter(inst) {
  4748. return inst.#m();
  4749. }
  4750. }
  4751. let c = new C();
  4752. c.c + C.getter(c);
  4753. `
  4754. testScript(SCRIPT, intToValue(84), t)
  4755. }
  4756. func TestPrivateIn(t *testing.T) {
  4757. const SCRIPT = `
  4758. class C {
  4759. #a = 42;
  4760. static check(inst) {
  4761. return #a in inst;
  4762. }
  4763. }
  4764. let c = new C();
  4765. C.check(c);
  4766. `
  4767. testScript(SCRIPT, valueTrue, t)
  4768. }
  4769. func TestDeletePropOfNonObject(t *testing.T) {
  4770. const SCRIPT = `
  4771. delete 'Test262'[100] && delete 'Test262'.a && delete 'Test262'['@'];
  4772. `
  4773. testScript(SCRIPT, valueTrue, t)
  4774. }
  4775. func TestKeywordsAsLabels(t *testing.T) {
  4776. const SCRIPT = `
  4777. let: for (let i = 0; i < 2; i++) {
  4778. if (i === 0) continue let;
  4779. break let;
  4780. }
  4781. \u006Cet: for (let i = 0; i < 2; i++) {
  4782. if (i === 0) continue \u006Cet;
  4783. break \u006Cet;
  4784. }
  4785. yield: for (let i = 0; i < 2; i++) {
  4786. if (i === 0) continue yield;
  4787. break yield;
  4788. }
  4789. yi\u0065ld: for (let i = 0; i < 2; i++) {
  4790. if (i === 0) continue yi\u0065ld;
  4791. break yi\u0065ld;
  4792. }
  4793. `
  4794. testScript(SCRIPT, _undefined, t)
  4795. }
  4796. func TestThisResolutionWithArg(t *testing.T) {
  4797. const SCRIPT = `
  4798. let capture;
  4799. function f(arg) {
  4800. capture = () => this; // move 'this' to stash
  4801. return [this, arg];
  4802. }
  4803. const _this = {};
  4804. const arg = {};
  4805. const [_this1, arg1] = f.call(_this, arg);
  4806. _this1 === _this && arg1 === arg;
  4807. `
  4808. testScript(SCRIPT, valueTrue, t)
  4809. }
  4810. func TestThisResolutionArgInStash(t *testing.T) {
  4811. const SCRIPT = `
  4812. let capture;
  4813. function f(arg) {
  4814. capture = () => this + arg; // move 'this' and arguments to stash
  4815. return [this, arg];
  4816. }
  4817. const _this = {};
  4818. const arg = {};
  4819. const [_this1, arg1] = f.call(_this, arg);
  4820. _this1 === _this && arg1 === arg;
  4821. `
  4822. testScript(SCRIPT, valueTrue, t)
  4823. }
  4824. func TestThisResolutionWithStackVar(t *testing.T) {
  4825. const SCRIPT = `
  4826. let capture;
  4827. function f(arg) {
  4828. const _ = 1; // a stack variable
  4829. capture = () => this + arg; // move 'this' and arguments to stash
  4830. return [this, arg];
  4831. }
  4832. const _this = {};
  4833. const arg = {};
  4834. const [_this1, arg1] = f.call(_this, arg);
  4835. _this1 === _this && arg1 === arg;
  4836. `
  4837. testScript(SCRIPT, valueTrue, t)
  4838. }
  4839. func TestForInLoopContinue(t *testing.T) {
  4840. const SCRIPT = `
  4841. var globalSink;
  4842. (function() {
  4843. const data = [{disabled: true}, {}];
  4844. function dummy() {}
  4845. function f1() {}
  4846. function f() {
  4847. dummy(); // move dummy to stash (so that f1 is at index 1)
  4848. for (const d of data) {
  4849. if (d.disabled) continue;
  4850. globalSink = () => d; // move d to stash
  4851. f1();
  4852. }
  4853. }
  4854. f();
  4855. })();
  4856. `
  4857. testScript(SCRIPT, _undefined, t)
  4858. }
  4859. func TestForInLoopContinueOuter(t *testing.T) {
  4860. const SCRIPT = `
  4861. var globalSink;
  4862. (function() {
  4863. const data = [{disabled: true}, {}];
  4864. function dummy1() {}
  4865. function f1() {}
  4866. function f() {
  4867. dummy1();
  4868. let counter = 0;
  4869. OUTER: for (let i = 0; i < 1; i++) {
  4870. for (const d of data) {
  4871. if (d.disabled) continue OUTER;
  4872. globalSink = () => d;
  4873. }
  4874. counter++;
  4875. }
  4876. f1();
  4877. if (counter !== 0) {
  4878. throw new Error(counter);
  4879. }
  4880. }
  4881. f();
  4882. })();
  4883. `
  4884. testScript(SCRIPT, _undefined, t)
  4885. }
  4886. func TestLexicalDeclInSwitch(t *testing.T) {
  4887. const SCRIPT = `
  4888. switch(0) {
  4889. case 1:
  4890. if (false) b = 3;
  4891. case 2:
  4892. const c = 1;
  4893. }
  4894. `
  4895. testScript(SCRIPT, _undefined, t)
  4896. }
  4897. func TestClassFieldSpecial(t *testing.T) {
  4898. const SCRIPT = `
  4899. class C {
  4900. get;
  4901. set;
  4902. async;
  4903. static;
  4904. }
  4905. `
  4906. testScript(SCRIPT, _undefined, t)
  4907. }
  4908. func TestClassMethodSpecial(t *testing.T) {
  4909. const SCRIPT = `
  4910. class C {
  4911. get() {}
  4912. set() {}
  4913. async() {}
  4914. static() {}
  4915. }
  4916. `
  4917. testScript(SCRIPT, _undefined, t)
  4918. }
  4919. func TestAsyncFunc(t *testing.T) {
  4920. const SCRIPT = `
  4921. async (x = true, y) => {};
  4922. async x => {};
  4923. let passed = false;
  4924. async function f() {
  4925. return true;
  4926. }
  4927. async function f1(arg = true) {
  4928. passed = await f();
  4929. }
  4930. await f1();
  4931. return passed;
  4932. `
  4933. testAsyncFunc(SCRIPT, valueTrue, t)
  4934. }
  4935. /*
  4936. func TestBabel(t *testing.T) {
  4937. src, err := os.ReadFile("babel7.js")
  4938. if err != nil {
  4939. t.Fatal(err)
  4940. }
  4941. vm := New()
  4942. _, err = vm.RunString(string(src))
  4943. if err != nil {
  4944. t.Fatal(err)
  4945. }
  4946. _, err = vm.RunString(`var result = Babel.transform("", {presets: ["es2015"]});`)
  4947. if err != nil {
  4948. t.Fatal(err)
  4949. }
  4950. }*/
  4951. func BenchmarkCompile(b *testing.B) {
  4952. data, err := os.ReadFile("testdata/S15.10.2.12_A1_T1.js")
  4953. if err != nil {
  4954. b.Fatal(err)
  4955. }
  4956. src := string(data)
  4957. for i := 0; i < b.N; i++ {
  4958. _, err := Compile("test.js", src, false)
  4959. if err != nil {
  4960. b.Fatal(err)
  4961. }
  4962. }
  4963. }