2
0

compiler_test.go 97 KB

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