compiler_test.go 101 KB

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