compiler_test.go 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750
  1. package goja
  2. import (
  3. "io/ioutil"
  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. `
  115. var (
  116. // The reason it's implemented this way rather than just as _testLib = MustCompile(...)
  117. // is because when you try to debug the compiler and set a breakpoint it gets triggered during the
  118. // initialisation which is annoying.
  119. _testLib, _testLibX *Program
  120. testLibOnce, testLibXOnce sync.Once
  121. )
  122. func testLib() *Program {
  123. testLibOnce.Do(func() {
  124. _testLib = MustCompile("testlib.js", TESTLIB, false)
  125. })
  126. return _testLib
  127. }
  128. func testLibX() *Program {
  129. testLibXOnce.Do(func() {
  130. _testLibX = MustCompile("testlibx.js", TESTLIBX, false)
  131. })
  132. return _testLibX
  133. }
  134. func (r *Runtime) testPrg(p *Program, expectedResult Value, t *testing.T) {
  135. vm := r.vm
  136. vm.prg = p
  137. vm.pc = 0
  138. vm.prg.dumpCode(t.Logf)
  139. vm.result = _undefined
  140. vm.run()
  141. v := vm.result
  142. t.Logf("stack size: %d", len(vm.stack))
  143. t.Logf("stashAllocs: %d", vm.stashAllocs)
  144. if v == nil && expectedResult != nil || !v.SameAs(expectedResult) {
  145. t.Fatalf("Result: %+v, expected: %+v", v, expectedResult)
  146. }
  147. if vm.sp != 0 {
  148. t.Fatalf("sp: %d", vm.sp)
  149. }
  150. if l := len(vm.iterStack); l > 0 {
  151. t.Fatalf("iter stack is not empty: %d", l)
  152. }
  153. }
  154. func (r *Runtime) testScriptWithTestLib(script string, expectedResult Value, t *testing.T) {
  155. _, err := r.RunProgram(testLib())
  156. if err != nil {
  157. t.Fatal(err)
  158. }
  159. r.testScript(script, expectedResult, t)
  160. }
  161. func (r *Runtime) testScriptWithTestLibX(script string, expectedResult Value, t *testing.T) {
  162. _, err := r.RunProgram(testLib())
  163. if err != nil {
  164. t.Fatal(err)
  165. }
  166. _, err = r.RunProgram(testLibX())
  167. if err != nil {
  168. t.Fatal(err)
  169. }
  170. r.testScript(script, expectedResult, t)
  171. }
  172. func (r *Runtime) testScript(script string, expectedResult Value, t *testing.T) {
  173. r.testPrg(MustCompile("test.js", script, false), expectedResult, t)
  174. }
  175. func testScript(script string, expectedResult Value, t *testing.T) {
  176. New().testScript(script, expectedResult, t)
  177. }
  178. func testScriptWithTestLib(script string, expectedResult Value, t *testing.T) {
  179. New().testScriptWithTestLib(script, expectedResult, t)
  180. }
  181. func testScriptWithTestLibX(script string, expectedResult Value, t *testing.T) {
  182. New().testScriptWithTestLibX(script, expectedResult, t)
  183. }
  184. func TestEmptyProgram(t *testing.T) {
  185. const SCRIPT = `
  186. `
  187. testScript(SCRIPT, _undefined, t)
  188. }
  189. func TestResultEmptyBlock(t *testing.T) {
  190. const SCRIPT = `
  191. undefined;
  192. {}
  193. `
  194. testScript(SCRIPT, _undefined, t)
  195. }
  196. func TestResultVarDecl(t *testing.T) {
  197. const SCRIPT = `
  198. 7; var x = 1;
  199. `
  200. testScript(SCRIPT, valueInt(7), t)
  201. }
  202. func TestResultLexDecl(t *testing.T) {
  203. const SCRIPT = `
  204. 7; {let x = 1};
  205. `
  206. testScript(SCRIPT, valueInt(7), t)
  207. }
  208. func TestResultLexDeclBreak(t *testing.T) {
  209. const SCRIPT = `
  210. L:{ 7; {let x = 1; break L;}};
  211. `
  212. testScript(SCRIPT, valueInt(7), t)
  213. }
  214. func TestResultLexDeclNested(t *testing.T) {
  215. const SCRIPT = `
  216. 7; {let x = (function() { return eval("8; {let y = 9}")})()};
  217. `
  218. testScript(SCRIPT, valueInt(7), t)
  219. }
  220. func TestErrorProto(t *testing.T) {
  221. const SCRIPT = `
  222. var e = new TypeError();
  223. e.name;
  224. `
  225. testScript(SCRIPT, asciiString("TypeError"), t)
  226. }
  227. func TestThis1(t *testing.T) {
  228. const SCRIPT = `
  229. function independent() {
  230. return this.prop;
  231. }
  232. var o = {};
  233. o.b = {g: independent, prop: 42};
  234. o.b.g();
  235. `
  236. testScript(SCRIPT, intToValue(42), t)
  237. }
  238. func TestThis2(t *testing.T) {
  239. const SCRIPT = `
  240. var o = {
  241. prop: 37,
  242. f: function() {
  243. return this.prop;
  244. }
  245. };
  246. o.f();
  247. `
  248. testScript(SCRIPT, intToValue(37), t)
  249. }
  250. func TestThisStrict(t *testing.T) {
  251. const SCRIPT = `
  252. "use strict";
  253. Object.defineProperty(Object.prototype, "x", { get: function () { return this; } });
  254. (5).x === 5;
  255. `
  256. testScript(SCRIPT, valueTrue, t)
  257. }
  258. func TestThisNoStrict(t *testing.T) {
  259. const SCRIPT = `
  260. Object.defineProperty(Object.prototype, "x", { get: function () { return this; } });
  261. (5).x == 5;
  262. `
  263. testScript(SCRIPT, valueTrue, t)
  264. }
  265. func TestNestedFuncVarResolution(t *testing.T) {
  266. const SCRIPT = `
  267. (function outer() {
  268. var v = 42;
  269. function inner() {
  270. return v;
  271. }
  272. return inner();
  273. })();
  274. `
  275. testScript(SCRIPT, valueInt(42), t)
  276. }
  277. func TestNestedFuncVarResolution1(t *testing.T) {
  278. const SCRIPT = `
  279. function outer(argOuter) {
  280. var called = 0;
  281. var inner = function(argInner) {
  282. if (arguments.length !== 1) {
  283. throw new Error();
  284. }
  285. called++;
  286. if (argOuter !== 1) {
  287. throw new Error("argOuter");
  288. }
  289. if (argInner !== 2) {
  290. throw new Error("argInner");
  291. }
  292. };
  293. inner(2);
  294. }
  295. outer(1);
  296. `
  297. testScript(SCRIPT, _undefined, t)
  298. }
  299. func TestCallFewerArgs(t *testing.T) {
  300. const SCRIPT = `
  301. function A(a, b, c) {
  302. return String(a) + " " + String(b) + " " + String(c);
  303. }
  304. A(1, 2);
  305. `
  306. testScript(SCRIPT, asciiString("1 2 undefined"), t)
  307. }
  308. func TestCallFewerArgsClosureNoArgs(t *testing.T) {
  309. const SCRIPT = `
  310. var x;
  311. function A(a, b, c) {
  312. var y = a;
  313. x = function() { return " " + y };
  314. return String(a) + " " + String(b) + " " + String(c);
  315. }
  316. A(1, 2) + x();
  317. `
  318. testScript(SCRIPT, asciiString("1 2 undefined 1"), t)
  319. }
  320. func TestCallFewerArgsClosureArgs(t *testing.T) {
  321. const SCRIPT = `
  322. var x;
  323. function A(a, b, c) {
  324. var y = b;
  325. x = function() { return " " + a + " " + y };
  326. return String(a) + " " + String(b) + " " + String(c);
  327. }
  328. A(1, 2) + x();
  329. `
  330. testScript(SCRIPT, asciiString("1 2 undefined 1 2"), t)
  331. }
  332. func TestCallMoreArgs(t *testing.T) {
  333. const SCRIPT = `
  334. function A(a, b) {
  335. var c = 4;
  336. return a - b + c;
  337. }
  338. A(1, 2, 3);
  339. `
  340. testScript(SCRIPT, intToValue(3), t)
  341. }
  342. func TestCallMoreArgsDynamic(t *testing.T) {
  343. const SCRIPT = `
  344. function A(a, b) {
  345. var c = 4;
  346. if (false) {
  347. eval("");
  348. }
  349. return a - b + c;
  350. }
  351. A(1, 2, 3);
  352. `
  353. testScript(SCRIPT, intToValue(3), t)
  354. }
  355. func TestCallLessArgsDynamic(t *testing.T) {
  356. const SCRIPT = `
  357. function A(a, b, c) {
  358. // Make it stashful
  359. function B() {
  360. return a;
  361. }
  362. return String(a) + " " + String(b) + " " + String(c);
  363. }
  364. A(1, 2);
  365. `
  366. testScript(SCRIPT, asciiString("1 2 undefined"), t)
  367. }
  368. func TestCallLessArgsDynamicLocalVar(t *testing.T) {
  369. const SCRIPT = `
  370. function f(param) {
  371. var a = 42;
  372. if (false) {
  373. eval("");
  374. }
  375. return a;
  376. }
  377. f();
  378. `
  379. testScript(SCRIPT, intToValue(42), t)
  380. }
  381. /*
  382. func TestFib(t *testing.T) {
  383. testScript(TEST_FIB, valueInt(9227465), t)
  384. }
  385. */
  386. func TestNativeCall(t *testing.T) {
  387. const SCRIPT = `
  388. var o = Object(1);
  389. Object.defineProperty(o, "test", {value: 42});
  390. o.test;
  391. `
  392. testScript(SCRIPT, intToValue(42), t)
  393. }
  394. func TestJSCall(t *testing.T) {
  395. const SCRIPT = `
  396. function getter() {
  397. return this.x;
  398. }
  399. var o = Object(1);
  400. o.x = 42;
  401. Object.defineProperty(o, "test", {get: getter});
  402. o.test;
  403. `
  404. testScript(SCRIPT, intToValue(42), t)
  405. }
  406. func TestLoop1(t *testing.T) {
  407. const SCRIPT = `
  408. function A() {
  409. var x = 1;
  410. for (var i = 0; i < 1; i++) {
  411. var x = 2;
  412. }
  413. return x;
  414. }
  415. A();
  416. `
  417. testScript(SCRIPT, intToValue(2), t)
  418. }
  419. func TestLoopBreak(t *testing.T) {
  420. const SCRIPT = `
  421. function A() {
  422. var x = 1;
  423. for (var i = 0; i < 1; i++) {
  424. break;
  425. var x = 2;
  426. }
  427. return x;
  428. }
  429. A();
  430. `
  431. testScript(SCRIPT, intToValue(1), t)
  432. }
  433. func TestForLoopOptionalExpr(t *testing.T) {
  434. const SCRIPT = `
  435. function A() {
  436. var x = 1;
  437. for (;;) {
  438. break;
  439. var x = 2;
  440. }
  441. return x;
  442. }
  443. A();
  444. `
  445. testScript(SCRIPT, intToValue(1), t)
  446. }
  447. func TestBlockBreak(t *testing.T) {
  448. const SCRIPT = `
  449. var rv = 0;
  450. B1: {
  451. rv = 1;
  452. B2: {
  453. rv = 2;
  454. break B1;
  455. }
  456. rv = 3;
  457. }
  458. rv;
  459. `
  460. testScript(SCRIPT, intToValue(2), t)
  461. }
  462. func TestTry(t *testing.T) {
  463. const SCRIPT = `
  464. function A() {
  465. var x = 1;
  466. try {
  467. x = 2;
  468. } catch(e) {
  469. x = 3;
  470. } finally {
  471. x = 4;
  472. }
  473. return x;
  474. }
  475. A();
  476. `
  477. testScript(SCRIPT, intToValue(4), t)
  478. }
  479. func TestTryOptionalCatchBinding(t *testing.T) {
  480. const SCRIPT = `
  481. try {
  482. throw null;
  483. } catch {
  484. }
  485. `
  486. testScript(SCRIPT, _undefined, t)
  487. }
  488. func TestTryCatch(t *testing.T) {
  489. const SCRIPT = `
  490. function A() {
  491. var x;
  492. try {
  493. throw 4;
  494. } catch(e) {
  495. x = e;
  496. }
  497. return x;
  498. }
  499. A();
  500. `
  501. testScript(SCRIPT, intToValue(4), t)
  502. }
  503. func TestTryCatchDirectEval(t *testing.T) {
  504. const SCRIPT = `
  505. function A() {
  506. var x;
  507. try {
  508. throw 4;
  509. } catch(e) {
  510. eval("x = e");
  511. }
  512. return x;
  513. }
  514. A();
  515. `
  516. testScript(SCRIPT, intToValue(4), t)
  517. }
  518. func TestTryExceptionInCatch(t *testing.T) {
  519. const SCRIPT = `
  520. function A() {
  521. var x;
  522. try {
  523. throw 4;
  524. } catch(e) {
  525. throw 5;
  526. }
  527. return x;
  528. }
  529. var rv;
  530. try {
  531. A();
  532. } catch (e) {
  533. rv = e;
  534. }
  535. rv;
  536. `
  537. testScript(SCRIPT, intToValue(5), t)
  538. }
  539. func TestTryContinueInCatch(t *testing.T) {
  540. const SCRIPT = `
  541. var c3 = 0, fin3 = 0;
  542. while (c3 < 2) {
  543. try {
  544. throw "ex1";
  545. } catch(er1) {
  546. c3 += 1;
  547. continue;
  548. } finally {
  549. fin3 = 1;
  550. }
  551. fin3 = 0;
  552. }
  553. fin3;
  554. `
  555. testScript(SCRIPT, intToValue(1), t)
  556. }
  557. func TestContinueInWith(t *testing.T) {
  558. const SCRIPT = `
  559. var x;
  560. var o = {x: 0};
  561. for (var i = 0; i < 2; i++) {
  562. with(o) {
  563. x = i;
  564. if (i === 0) {
  565. continue;
  566. }
  567. }
  568. break;
  569. }
  570. x;
  571. `
  572. testScript(SCRIPT, _undefined, t)
  573. }
  574. func TestTryContinueInFinally(t *testing.T) {
  575. const SCRIPT = `
  576. var c3 = 0, fin3 = 0;
  577. while (c3 < 2) {
  578. try {
  579. throw "ex1";
  580. } catch(er1) {
  581. c3 += 1;
  582. } finally {
  583. fin3 = 1;
  584. continue;
  585. }
  586. fin3 = 0;
  587. }
  588. fin3;
  589. `
  590. testScript(SCRIPT, intToValue(1), t)
  591. }
  592. func TestTryBreakFinallyContinue(t *testing.T) {
  593. const SCRIPT = `
  594. for (var i = 0; i < 3; i++) {
  595. try {
  596. break;
  597. } finally {
  598. continue;
  599. }
  600. }
  601. `
  602. testScript(SCRIPT, _undefined, t)
  603. }
  604. func TestTryBreakFinallyContinueWithResult(t *testing.T) {
  605. const SCRIPT = `
  606. for (var i = 0; i < 3; i++) {
  607. try {
  608. true;
  609. break;
  610. } finally {
  611. continue;
  612. }
  613. }
  614. `
  615. testScript(SCRIPT, _undefined, t)
  616. }
  617. func TestTryBreakFinallyContinueWithResult1(t *testing.T) {
  618. const SCRIPT = `
  619. for (var i = 0; i < 3; i++) {
  620. try {
  621. true;
  622. break;
  623. } finally {
  624. var x = 1;
  625. continue;
  626. }
  627. }
  628. `
  629. testScript(SCRIPT, _undefined, t)
  630. }
  631. func TestTryBreakFinallyContinueWithResultNested(t *testing.T) {
  632. const SCRIPT = `
  633. LOOP:
  634. for (var i = 0; i < 3; i++) {
  635. try {
  636. if (true) {
  637. false; break;
  638. }
  639. } finally {
  640. if (true) {
  641. true; continue;
  642. }
  643. }
  644. }
  645. `
  646. testScript(SCRIPT, valueTrue, t)
  647. }
  648. func TestTryBreakOuterFinallyContinue(t *testing.T) {
  649. const SCRIPT = `
  650. let iCount = 0, jCount = 0;
  651. OUTER: for (let i = 0; i < 1; i++) {
  652. iCount++;
  653. for (let j = 0; j < 2; j++) {
  654. jCount++;
  655. try {
  656. break OUTER;
  657. } finally {
  658. continue;
  659. }
  660. }
  661. }
  662. ""+iCount+jCount;
  663. `
  664. testScript(SCRIPT, asciiString("12"), t)
  665. }
  666. func TestTryIllegalContinueWithFinallyOverride(t *testing.T) {
  667. const SCRIPT = `
  668. L: {
  669. while (Math.random() > 0.5) {
  670. try {
  671. continue L;
  672. } finally {
  673. break;
  674. }
  675. }
  676. }
  677. `
  678. _, err := Compile("", SCRIPT, false)
  679. if err == nil {
  680. t.Fatal("expected error")
  681. }
  682. }
  683. func TestTryIllegalContinueWithFinallyOverrideNoLabel(t *testing.T) {
  684. const SCRIPT = `
  685. L: {
  686. try {
  687. continue;
  688. } finally {
  689. break L;
  690. }
  691. }
  692. `
  693. _, err := Compile("", SCRIPT, false)
  694. if err == nil {
  695. t.Fatal("expected error")
  696. }
  697. }
  698. func TestTryIllegalContinueWithFinallyOverrideDummy(t *testing.T) {
  699. const SCRIPT = `
  700. L: {
  701. while (false) {
  702. try {
  703. continue L;
  704. } finally {
  705. break;
  706. }
  707. }
  708. }
  709. `
  710. _, err := Compile("", SCRIPT, false)
  711. if err == nil {
  712. t.Fatal("expected error")
  713. }
  714. }
  715. func TestTryNoResult(t *testing.T) {
  716. const SCRIPT = `
  717. true;
  718. L:
  719. try {
  720. break L;
  721. } finally {
  722. }
  723. `
  724. testScript(SCRIPT, _undefined, t)
  725. }
  726. func TestCatchLexicalEnv(t *testing.T) {
  727. const SCRIPT = `
  728. function F() {
  729. try {
  730. throw 1;
  731. } catch (e) {
  732. var x = e;
  733. }
  734. return x;
  735. }
  736. F();
  737. `
  738. testScript(SCRIPT, intToValue(1), t)
  739. }
  740. func TestThrowType(t *testing.T) {
  741. const SCRIPT = `
  742. function Exception(message) {
  743. this.message = message;
  744. }
  745. function A() {
  746. try {
  747. throw new Exception("boo!");
  748. } catch(e) {
  749. return e;
  750. }
  751. }
  752. var thrown = A();
  753. thrown !== null && typeof thrown === "object" && thrown.constructor === Exception;
  754. `
  755. testScript(SCRIPT, valueTrue, t)
  756. }
  757. func TestThrowConstructorName(t *testing.T) {
  758. const SCRIPT = `
  759. function Exception(message) {
  760. this.message = message;
  761. }
  762. function A() {
  763. try {
  764. throw new Exception("boo!");
  765. } catch(e) {
  766. return e;
  767. }
  768. }
  769. A().constructor.name;
  770. `
  771. testScript(SCRIPT, asciiString("Exception"), t)
  772. }
  773. func TestThrowNativeConstructorName(t *testing.T) {
  774. const SCRIPT = `
  775. function A() {
  776. try {
  777. throw new TypeError();
  778. } catch(e) {
  779. return e;
  780. }
  781. }
  782. A().constructor.name;
  783. `
  784. testScript(SCRIPT, asciiString("TypeError"), t)
  785. }
  786. func TestEmptyTryNoCatch(t *testing.T) {
  787. const SCRIPT = `
  788. var called = false;
  789. try {
  790. } finally {
  791. called = true;
  792. }
  793. called;
  794. `
  795. testScript(SCRIPT, valueTrue, t)
  796. }
  797. func TestTryReturnFromCatch(t *testing.T) {
  798. const SCRIPT = `
  799. function f(o) {
  800. var x = 42;
  801. function innerf(o) {
  802. try {
  803. throw o;
  804. } catch (e) {
  805. return x;
  806. }
  807. }
  808. return innerf(o);
  809. }
  810. f({});
  811. `
  812. testScript(SCRIPT, valueInt(42), t)
  813. }
  814. func TestTryCompletionResult(t *testing.T) {
  815. const SCRIPT = `
  816. 99; do { -99; try { 39 } catch (e) { -1 } finally { break; -2 }; } while (false);
  817. `
  818. testScript(SCRIPT, _undefined, t)
  819. }
  820. func TestIfElse(t *testing.T) {
  821. const SCRIPT = `
  822. var rv;
  823. if (rv === undefined) {
  824. rv = "passed";
  825. } else {
  826. rv = "failed";
  827. }
  828. rv;
  829. `
  830. testScript(SCRIPT, asciiString("passed"), t)
  831. }
  832. func TestIfElseRetVal(t *testing.T) {
  833. const SCRIPT = `
  834. var x;
  835. if (x === undefined) {
  836. "passed";
  837. } else {
  838. "failed";
  839. }
  840. `
  841. testScript(SCRIPT, asciiString("passed"), t)
  842. }
  843. func TestWhileReturnValue(t *testing.T) {
  844. const SCRIPT = `
  845. var x = 0;
  846. while(true) {
  847. x = 1;
  848. break;
  849. }
  850. `
  851. testScript(SCRIPT, intToValue(1), t)
  852. }
  853. func TestIfElseLabel(t *testing.T) {
  854. const SCRIPT = `
  855. var x = 0;
  856. abc: if (true) {
  857. x = 1;
  858. break abc;
  859. }
  860. `
  861. testScript(SCRIPT, intToValue(1), t)
  862. }
  863. func TestIfMultipleLabels(t *testing.T) {
  864. const SCRIPT = `
  865. var x = 0;
  866. xyz:abc: if (true) {
  867. break xyz;
  868. }
  869. `
  870. testScript(SCRIPT, _undefined, t)
  871. }
  872. func TestBreakOutOfTry(t *testing.T) {
  873. const SCRIPT = `
  874. function A() {
  875. var x = 1;
  876. B: {
  877. try {
  878. x = 2;
  879. } catch(e) {
  880. x = 3;
  881. } finally {
  882. break B;
  883. x = 4;
  884. }
  885. }
  886. return x;
  887. }
  888. A();
  889. `
  890. testScript(SCRIPT, intToValue(2), t)
  891. }
  892. func TestReturnOutOfTryNested(t *testing.T) {
  893. const SCRIPT = `
  894. function A() {
  895. function nested() {
  896. try {
  897. return 1;
  898. } catch(e) {
  899. return 2;
  900. }
  901. }
  902. return nested();
  903. }
  904. A();
  905. `
  906. testScript(SCRIPT, intToValue(1), t)
  907. }
  908. func TestContinueLoop(t *testing.T) {
  909. const SCRIPT = `
  910. function A() {
  911. var r = 0;
  912. for (var i = 0; i < 5; i++) {
  913. if (i > 1) {
  914. continue;
  915. }
  916. r++;
  917. }
  918. return r;
  919. }
  920. A();
  921. `
  922. testScript(SCRIPT, intToValue(2), t)
  923. }
  924. func TestContinueOutOfTry(t *testing.T) {
  925. const SCRIPT = `
  926. function A() {
  927. var r = 0;
  928. for (var i = 0; i < 5; i++) {
  929. try {
  930. if (i > 1) {
  931. continue;
  932. }
  933. } catch(e) {
  934. return 99;
  935. }
  936. r++;
  937. }
  938. return r;
  939. }
  940. A();
  941. `
  942. testScript(SCRIPT, intToValue(2), t)
  943. }
  944. func TestThisInCatch(t *testing.T) {
  945. const SCRIPT = `
  946. function O() {
  947. try {
  948. f();
  949. } catch (e) {
  950. this.value = e.toString();
  951. }
  952. }
  953. function f() {
  954. throw "ex";
  955. }
  956. var o = new O();
  957. o.value;
  958. `
  959. testScript(SCRIPT, asciiString("ex"), t)
  960. }
  961. func TestNestedTry(t *testing.T) {
  962. const SCRIPT = `
  963. var ex;
  964. try {
  965. throw "ex1";
  966. } catch (er1) {
  967. try {
  968. throw "ex2";
  969. } catch (er1) {
  970. ex = er1;
  971. }
  972. }
  973. ex;
  974. `
  975. testScript(SCRIPT, asciiString("ex2"), t)
  976. }
  977. func TestNestedTryInStashlessFunc(t *testing.T) {
  978. const SCRIPT = `
  979. function f() {
  980. var ex1, ex2;
  981. try {
  982. throw "ex1";
  983. } catch (er1) {
  984. try {
  985. throw "ex2";
  986. } catch (er1) {
  987. ex2 = er1;
  988. }
  989. ex1 = er1;
  990. }
  991. return ex1 == "ex1" && ex2 == "ex2";
  992. }
  993. f();
  994. `
  995. testScript(SCRIPT, valueTrue, t)
  996. }
  997. func TestEvalLexicalDecl(t *testing.T) {
  998. const SCRIPT = `
  999. eval("let x = true; x;");
  1000. `
  1001. testScript(SCRIPT, valueTrue, t)
  1002. }
  1003. func TestEvalInCatchInStashlessFunc(t *testing.T) {
  1004. const SCRIPT = `
  1005. function f() {
  1006. var ex;
  1007. try {
  1008. throw "ex1";
  1009. } catch (er1) {
  1010. eval("ex = er1");
  1011. }
  1012. return ex;
  1013. }
  1014. f();
  1015. `
  1016. testScript(SCRIPT, asciiString("ex1"), t)
  1017. }
  1018. func TestCatchClosureInStashlessFunc(t *testing.T) {
  1019. const SCRIPT = `
  1020. function f() {
  1021. var ex;
  1022. try {
  1023. throw "ex1";
  1024. } catch (er1) {
  1025. return function() {
  1026. return er1;
  1027. }
  1028. }
  1029. }
  1030. f()();
  1031. `
  1032. testScript(SCRIPT, asciiString("ex1"), t)
  1033. }
  1034. func TestCatchVarNotUsedInStashlessFunc(t *testing.T) {
  1035. const SCRIPT = `
  1036. function f() {
  1037. var ex;
  1038. try {
  1039. throw "ex1";
  1040. } catch (er1) {
  1041. ex = "ok";
  1042. }
  1043. return ex;
  1044. }
  1045. f();
  1046. `
  1047. testScript(SCRIPT, asciiString("ok"), t)
  1048. }
  1049. func TestNew(t *testing.T) {
  1050. const SCRIPT = `
  1051. function O() {
  1052. this.x = 42;
  1053. }
  1054. new O().x;
  1055. `
  1056. testScript(SCRIPT, intToValue(42), t)
  1057. }
  1058. func TestStringConstructor(t *testing.T) {
  1059. const SCRIPT = `
  1060. function F() {
  1061. return String(33) + " " + String("cows");
  1062. }
  1063. F();
  1064. `
  1065. testScript(SCRIPT, asciiString("33 cows"), t)
  1066. }
  1067. func TestError(t *testing.T) {
  1068. const SCRIPT = `
  1069. function F() {
  1070. return new Error("test");
  1071. }
  1072. var e = F();
  1073. e.message == "test" && e.name == "Error";
  1074. `
  1075. testScript(SCRIPT, valueTrue, t)
  1076. }
  1077. func TestTypeError(t *testing.T) {
  1078. const SCRIPT = `
  1079. function F() {
  1080. return new TypeError("test");
  1081. }
  1082. var e = F();
  1083. e.message == "test" && e.name == "TypeError";
  1084. `
  1085. testScript(SCRIPT, valueTrue, t)
  1086. }
  1087. func TestToString(t *testing.T) {
  1088. const SCRIPT = `
  1089. var o = {x: 42};
  1090. o.toString = function() {
  1091. return String(this.x);
  1092. }
  1093. var o1 = {};
  1094. o.toString() + " ### " + o1.toString();
  1095. `
  1096. testScript(SCRIPT, asciiString("42 ### [object Object]"), t)
  1097. }
  1098. func TestEvalOrder(t *testing.T) {
  1099. const SCRIPT = `
  1100. var o = {f: function() {return 42}, x: 0};
  1101. var trace = "";
  1102. function F1() {
  1103. trace += "First!";
  1104. return o;
  1105. }
  1106. function F2() {
  1107. trace += "Second!";
  1108. return "f";
  1109. }
  1110. function F3() {
  1111. trace += "Third!";
  1112. }
  1113. var rv = F1()[F2()](F3());
  1114. rv += trace;
  1115. rv;
  1116. `
  1117. testScript(SCRIPT, asciiString("42First!Second!Third!"), t)
  1118. }
  1119. func TestPostfixIncBracket(t *testing.T) {
  1120. const SCRIPT = `
  1121. var o = {x: 42};
  1122. var trace = "";
  1123. function F1() {
  1124. trace += "First!";
  1125. return o;
  1126. }
  1127. function F2() {
  1128. trace += "Second!";
  1129. return "x";
  1130. }
  1131. var rv = F1()[F2()]++;
  1132. rv + trace + o.x;
  1133. `
  1134. testScript(SCRIPT, asciiString("42First!Second!43"), t)
  1135. }
  1136. func TestPostfixIncDot(t *testing.T) {
  1137. const SCRIPT = `
  1138. var o = {x: 42};
  1139. var trace = "";
  1140. function F1() {
  1141. trace += "First!";
  1142. return o;
  1143. }
  1144. var rv = F1().x++;
  1145. rv + trace + o.x;
  1146. `
  1147. testScript(SCRIPT, asciiString("42First!43"), t)
  1148. }
  1149. func TestPrefixIncBracket(t *testing.T) {
  1150. const SCRIPT = `
  1151. var o = {x: 42};
  1152. var trace = "";
  1153. function F1() {
  1154. trace += "First!";
  1155. return o;
  1156. }
  1157. function F2() {
  1158. trace += "Second!";
  1159. return "x";
  1160. }
  1161. var rv = ++F1()[F2()];
  1162. rv + trace + o.x;
  1163. `
  1164. testScript(SCRIPT, asciiString("43First!Second!43"), t)
  1165. }
  1166. func TestPrefixIncDot(t *testing.T) {
  1167. const SCRIPT = `
  1168. var o = {x: 42};
  1169. var trace = "";
  1170. function F1() {
  1171. trace += "First!";
  1172. return o;
  1173. }
  1174. var rv = ++F1().x;
  1175. rv + trace + o.x;
  1176. `
  1177. testScript(SCRIPT, asciiString("43First!43"), t)
  1178. }
  1179. func TestPostDecObj(t *testing.T) {
  1180. const SCRIPT = `
  1181. var object = {valueOf: function() {return 1}};
  1182. var y = object--;
  1183. var ok = false;
  1184. if (y === 1) {
  1185. ok = true;
  1186. }
  1187. ok;
  1188. `
  1189. testScript(SCRIPT, valueTrue, t)
  1190. }
  1191. func TestPropAcc1(t *testing.T) {
  1192. const SCRIPT = `
  1193. 1..toString()
  1194. `
  1195. testScript(SCRIPT, asciiString("1"), t)
  1196. }
  1197. func TestEvalDirect(t *testing.T) {
  1198. const SCRIPT = `
  1199. var rv = false;
  1200. function foo(){ rv = true; }
  1201. var o = { };
  1202. function f() {
  1203. try {
  1204. eval("o.bar( foo() );");
  1205. } catch (e) {
  1206. }
  1207. }
  1208. f();
  1209. rv;
  1210. `
  1211. testScript(SCRIPT, valueTrue, t)
  1212. }
  1213. func TestEvalRet(t *testing.T) {
  1214. const SCRIPT = `
  1215. eval("for (var i = 0; i < 3; i++) {i}")
  1216. `
  1217. testScript(SCRIPT, valueInt(2), t)
  1218. }
  1219. func TestEvalFunctionDecl(t *testing.T) {
  1220. const SCRIPT = `
  1221. eval("function F() {}")
  1222. `
  1223. testScript(SCRIPT, _undefined, t)
  1224. }
  1225. func TestEvalFunctionExpr(t *testing.T) {
  1226. const SCRIPT = `
  1227. eval("(function F() {return 42;})")()
  1228. `
  1229. testScript(SCRIPT, intToValue(42), t)
  1230. }
  1231. func TestEvalDirectScope(t *testing.T) {
  1232. const SCRIPT = `
  1233. var __10_4_2_1_3 = "str";
  1234. function testcase() {
  1235. var __10_4_2_1_3 = "str1";
  1236. try {
  1237. throw "error";
  1238. } catch (e) {
  1239. var __10_4_2_1_3 = "str2";
  1240. return eval("__10_4_2_1_3");
  1241. }
  1242. }
  1243. testcase();
  1244. `
  1245. testScript(SCRIPT, asciiString("str2"), t)
  1246. }
  1247. func TestEvalDirectScope1(t *testing.T) {
  1248. const SCRIPT = `
  1249. 'use strict';
  1250. var __10_4_2_1_5 = "str";
  1251. function testcase() {
  1252. var __10_4_2_1_5 = "str1";
  1253. var r = eval("\
  1254. var __10_4_2_1_5 = \'str2\'; \
  1255. eval(\"\'str2\' === __10_4_2_1_5\")\
  1256. ");
  1257. return r;
  1258. }
  1259. testcase();
  1260. `
  1261. testScript(SCRIPT, valueTrue, t)
  1262. }
  1263. func TestEvalDirectCreateBinding(t *testing.T) {
  1264. const SCRIPT = `
  1265. function f() {
  1266. eval("var x = true");
  1267. return x;
  1268. }
  1269. var res = f();
  1270. var thrown = false;
  1271. try {
  1272. x;
  1273. } catch(e) {
  1274. if (e instanceof ReferenceError) {
  1275. thrown = true;
  1276. } else {
  1277. throw e;
  1278. }
  1279. }
  1280. res && thrown;
  1281. `
  1282. testScript(SCRIPT, valueTrue, t)
  1283. }
  1284. func TestEvalDirectCreateBinding1(t *testing.T) {
  1285. const SCRIPT = `
  1286. function f() {
  1287. eval("let x = 1; var y = 2; function f1() {return x};");
  1288. assert.throws(ReferenceError, function() { x });
  1289. return ""+y+f1();
  1290. }
  1291. f();
  1292. `
  1293. testScriptWithTestLib(SCRIPT, asciiString("21"), t)
  1294. }
  1295. func TestEvalDirectCreateBinding3(t *testing.T) {
  1296. const SCRIPT = `
  1297. function f() {
  1298. let x;
  1299. try {
  1300. eval("var y=1, x=2");
  1301. } catch(e) {}
  1302. return y;
  1303. }
  1304. assert.throws(ReferenceError, f);
  1305. `
  1306. testScriptWithTestLib(SCRIPT, _undefined, t)
  1307. }
  1308. func TestEvalGlobalStrict(t *testing.T) {
  1309. const SCRIPT = `
  1310. 'use strict';
  1311. var evalStr =
  1312. 'for (var x in this) {\n'+
  1313. ' if ( x === \'Math\' ) {\n'+
  1314. ' }\n'+
  1315. '}\n';
  1316. eval(evalStr);
  1317. `
  1318. testScript(SCRIPT, _undefined, t)
  1319. }
  1320. func TestEvalEmptyStrict(t *testing.T) {
  1321. const SCRIPT = `
  1322. 'use strict';
  1323. eval("");
  1324. `
  1325. testScript(SCRIPT, _undefined, t)
  1326. }
  1327. func TestEvalFuncDecl(t *testing.T) {
  1328. const SCRIPT = `
  1329. 'use strict';
  1330. var funcA = eval("function __funcA(__arg){return __arg;}; __funcA");
  1331. typeof funcA;
  1332. `
  1333. testScript(SCRIPT, asciiString("function"), t)
  1334. }
  1335. func TestGetAfterSet(t *testing.T) {
  1336. const SCRIPT = `
  1337. function f() {
  1338. var x = 1;
  1339. return x;
  1340. }
  1341. `
  1342. testScript(SCRIPT, _undefined, t)
  1343. }
  1344. func TestForLoopRet(t *testing.T) {
  1345. const SCRIPT = `
  1346. for (var i = 0; i < 20; i++) { if (i > 2) {break;} else { i }}
  1347. `
  1348. testScript(SCRIPT, _undefined, t)
  1349. }
  1350. func TestForLoopRet1(t *testing.T) {
  1351. const SCRIPT = `
  1352. for (var i = 0; i < 20; i++) { if (i > 2) {42;; {L:{break;}}} else { i }}
  1353. `
  1354. testScript(SCRIPT, intToValue(42), t)
  1355. }
  1356. func TestForInLoopRet(t *testing.T) {
  1357. const SCRIPT = `
  1358. var o = [1, 2, 3, 4];
  1359. for (var i in o) { if (i > 2) {break;} else { i }}
  1360. `
  1361. testScript(SCRIPT, _undefined, t)
  1362. }
  1363. func TestForInLoopRet1(t *testing.T) {
  1364. const SCRIPT = `
  1365. var o = {};
  1366. o.x = 1;
  1367. o.y = 2;
  1368. for (var i in o) {
  1369. true;
  1370. }
  1371. `
  1372. testScript(SCRIPT, valueTrue, t)
  1373. }
  1374. func TestDoWhileLoopRet(t *testing.T) {
  1375. const SCRIPT = `
  1376. var i = 0;
  1377. do {
  1378. if (i > 2) {
  1379. break;
  1380. } else {
  1381. i;
  1382. }
  1383. } while (i++ < 20);
  1384. `
  1385. testScript(SCRIPT, _undefined, t)
  1386. }
  1387. func TestDoWhileContinueRet(t *testing.T) {
  1388. const SCRIPT = `
  1389. var i = 0;
  1390. do {
  1391. if (i > 2) {
  1392. true;
  1393. continue;
  1394. } else {
  1395. i;
  1396. }
  1397. } while (i++ < 20);
  1398. `
  1399. testScript(SCRIPT, valueTrue, t)
  1400. }
  1401. func TestWhileLoopRet(t *testing.T) {
  1402. const SCRIPT = `
  1403. var i; while (i < 20) { if (i > 2) {break;} else { i++ }}
  1404. `
  1405. testScript(SCRIPT, _undefined, t)
  1406. }
  1407. func TestLoopRet1(t *testing.T) {
  1408. const SCRIPT = `
  1409. for (var i = 0; i < 20; i++) { }
  1410. `
  1411. testScript(SCRIPT, _undefined, t)
  1412. }
  1413. func TestInstanceof(t *testing.T) {
  1414. const SCRIPT = `
  1415. var rv;
  1416. try {
  1417. true();
  1418. } catch (e) {
  1419. rv = e instanceof TypeError;
  1420. }
  1421. rv;
  1422. `
  1423. testScript(SCRIPT, valueTrue, t)
  1424. }
  1425. func TestStrictAssign(t *testing.T) {
  1426. const SCRIPT = `
  1427. 'use strict';
  1428. var rv;
  1429. var called = false;
  1430. function F() {
  1431. called = true;
  1432. return 1;
  1433. }
  1434. try {
  1435. x = F();
  1436. } catch (e) {
  1437. rv = e instanceof ReferenceError;
  1438. }
  1439. rv + " " + called;
  1440. `
  1441. testScript(SCRIPT, asciiString("true true"), t)
  1442. }
  1443. func TestStrictScope(t *testing.T) {
  1444. const SCRIPT = `
  1445. var rv;
  1446. var called = false;
  1447. function F() {
  1448. 'use strict';
  1449. x = 1;
  1450. }
  1451. try {
  1452. F();
  1453. } catch (e) {
  1454. rv = e instanceof ReferenceError;
  1455. }
  1456. x = 1;
  1457. rv + " " + x;
  1458. `
  1459. testScript(SCRIPT, asciiString("true 1"), t)
  1460. }
  1461. func TestStringObj(t *testing.T) {
  1462. const SCRIPT = `
  1463. var s = new String("test");
  1464. s[0] + s[2] + s[1];
  1465. `
  1466. testScript(SCRIPT, asciiString("tse"), t)
  1467. }
  1468. func TestStringPrimitive(t *testing.T) {
  1469. const SCRIPT = `
  1470. var s = "test";
  1471. s[0] + s[2] + s[1];
  1472. `
  1473. testScript(SCRIPT, asciiString("tse"), t)
  1474. }
  1475. func TestCallGlobalObject(t *testing.T) {
  1476. const SCRIPT = `
  1477. var rv;
  1478. try {
  1479. this();
  1480. } catch (e) {
  1481. rv = e instanceof TypeError
  1482. }
  1483. rv;
  1484. `
  1485. testScript(SCRIPT, valueTrue, t)
  1486. }
  1487. func TestFuncLength(t *testing.T) {
  1488. const SCRIPT = `
  1489. function F(x, y) {
  1490. }
  1491. F.length
  1492. `
  1493. testScript(SCRIPT, intToValue(2), t)
  1494. }
  1495. func TestNativeFuncLength(t *testing.T) {
  1496. const SCRIPT = `
  1497. eval.length + Object.defineProperty.length + String.length
  1498. `
  1499. testScript(SCRIPT, intToValue(5), t)
  1500. }
  1501. func TestArguments(t *testing.T) {
  1502. const SCRIPT = `
  1503. function F() {
  1504. return arguments.length + " " + arguments[1];
  1505. }
  1506. F(1,2,3)
  1507. `
  1508. testScript(SCRIPT, asciiString("3 2"), t)
  1509. }
  1510. func TestArgumentsPut(t *testing.T) {
  1511. const SCRIPT = `
  1512. function F(x, y) {
  1513. arguments[0] -= arguments[1];
  1514. return x;
  1515. }
  1516. F(5, 2)
  1517. `
  1518. testScript(SCRIPT, intToValue(3), t)
  1519. }
  1520. func TestArgumentsPutStrict(t *testing.T) {
  1521. const SCRIPT = `
  1522. function F(x, y) {
  1523. 'use strict';
  1524. arguments[0] -= arguments[1];
  1525. return x;
  1526. }
  1527. F(5, 2)
  1528. `
  1529. testScript(SCRIPT, intToValue(5), t)
  1530. }
  1531. func TestArgumentsExtra(t *testing.T) {
  1532. const SCRIPT = `
  1533. function F(x, y) {
  1534. return arguments[2];
  1535. }
  1536. F(1, 2, 42)
  1537. `
  1538. testScript(SCRIPT, intToValue(42), t)
  1539. }
  1540. func TestArgumentsExist(t *testing.T) {
  1541. const SCRIPT = `
  1542. function F(x, arguments) {
  1543. return arguments;
  1544. }
  1545. F(1, 42)
  1546. `
  1547. testScript(SCRIPT, intToValue(42), t)
  1548. }
  1549. func TestArgumentsDelete(t *testing.T) {
  1550. const SCRIPT = `
  1551. function f(x) {
  1552. delete arguments[0];
  1553. arguments[0] = 42;
  1554. return x;
  1555. }
  1556. f(1)
  1557. `
  1558. testScript(SCRIPT, intToValue(1), t)
  1559. }
  1560. func TestArgumentsInEval(t *testing.T) {
  1561. const SCRIPT = `
  1562. function f() {
  1563. return eval("arguments");
  1564. }
  1565. f(1)[0];
  1566. `
  1567. testScript(SCRIPT, intToValue(1), t)
  1568. }
  1569. func TestArgumentsRedeclareInEval(t *testing.T) {
  1570. const SCRIPT = `
  1571. assert.sameValue("arguments" in this, false, "No global 'arguments' binding");
  1572. function f(p = eval("var arguments = 'param'"), arguments) {}
  1573. assert.throws(SyntaxError, f);
  1574. assert.sameValue("arguments" in this, false, "No global 'arguments' binding");
  1575. `
  1576. testScriptWithTestLib(SCRIPT, _undefined, t)
  1577. }
  1578. func TestArgumentsRedeclareArrow(t *testing.T) {
  1579. const SCRIPT = `
  1580. const oldArguments = globalThis.arguments;
  1581. let count = 0;
  1582. const f = (p = eval("var arguments = 'param'"), q = () => arguments) => {
  1583. var arguments = "local";
  1584. assert.sameValue(arguments, "local", "arguments");
  1585. assert.sameValue(q(), "param", "q");
  1586. count++;
  1587. }
  1588. f();
  1589. assert.sameValue(count, 1);
  1590. assert.sameValue(globalThis.arguments, oldArguments, "globalThis.arguments unchanged");
  1591. `
  1592. testScriptWithTestLib(SCRIPT, _undefined, t)
  1593. }
  1594. func TestEvalParamWithDef(t *testing.T) {
  1595. const SCRIPT = `
  1596. function f(param = 0) {
  1597. eval("var param = 1");
  1598. return param;
  1599. }
  1600. f();
  1601. `
  1602. testScript(SCRIPT, valueInt(1), t)
  1603. }
  1604. func TestArgumentsRedefinedAsLetDyn(t *testing.T) {
  1605. const SCRIPT = `
  1606. function f() {
  1607. let arguments;
  1608. eval(""); // force dynamic scope
  1609. return arguments;
  1610. }
  1611. f(1,2);
  1612. `
  1613. testScript(SCRIPT, _undefined, t)
  1614. }
  1615. func TestWith(t *testing.T) {
  1616. const SCRIPT = `
  1617. var b = 1;
  1618. var o = {a: 41};
  1619. with(o) {
  1620. a += b;
  1621. }
  1622. o.a;
  1623. `
  1624. testScript(SCRIPT, intToValue(42), t)
  1625. }
  1626. func TestWithInFunc(t *testing.T) {
  1627. const SCRIPT = `
  1628. function F() {
  1629. var b = 1;
  1630. var c = 0;
  1631. var o = {a: 40, c: 1};
  1632. with(o) {
  1633. a += b + c;
  1634. }
  1635. return o.a;
  1636. }
  1637. F();
  1638. `
  1639. testScript(SCRIPT, intToValue(42), t)
  1640. }
  1641. func TestAssignNonExtendable(t *testing.T) {
  1642. const SCRIPT = `
  1643. 'use strict';
  1644. function F() {
  1645. this.x = 1;
  1646. }
  1647. var o = new F();
  1648. Object.preventExtensions(o);
  1649. o.x = 42;
  1650. o.x;
  1651. `
  1652. testScript(SCRIPT, intToValue(42), t)
  1653. }
  1654. func TestAssignNonExtendable1(t *testing.T) {
  1655. const SCRIPT = `
  1656. 'use strict';
  1657. function F() {
  1658. }
  1659. var o = new F();
  1660. var rv;
  1661. Object.preventExtensions(o);
  1662. try {
  1663. o.x = 42;
  1664. } catch (e) {
  1665. rv = e.constructor === TypeError;
  1666. }
  1667. rv += " " + o.x;
  1668. rv;
  1669. `
  1670. testScript(SCRIPT, asciiString("true undefined"), t)
  1671. }
  1672. func TestAssignStrict(t *testing.T) {
  1673. const SCRIPT = `
  1674. 'use strict';
  1675. try {
  1676. eval("eval = 42");
  1677. } catch(e) {
  1678. var rv = e instanceof SyntaxError
  1679. }
  1680. rv;
  1681. `
  1682. testScript(SCRIPT, valueTrue, t)
  1683. }
  1684. func TestIllegalArgmentName(t *testing.T) {
  1685. const SCRIPT = `
  1686. 'use strict';
  1687. try {
  1688. eval("function F(eval) {}");
  1689. } catch (e) {
  1690. var rv = e instanceof SyntaxError
  1691. }
  1692. rv;
  1693. `
  1694. testScript(SCRIPT, valueTrue, t)
  1695. }
  1696. func TestFunction(t *testing.T) {
  1697. const SCRIPT = `
  1698. var f0 = Function("");
  1699. var f1 = Function("return ' one'");
  1700. var f2 = Function("arg", "return ' ' + arg");
  1701. f0() + f1() + f2("two");
  1702. `
  1703. testScript(SCRIPT, asciiString("undefined one two"), t)
  1704. }
  1705. func TestFunction1(t *testing.T) {
  1706. const SCRIPT = `
  1707. var f = function f1(count) {
  1708. if (count == 0) {
  1709. return true;
  1710. }
  1711. return f1(count-1);
  1712. }
  1713. f(1);
  1714. `
  1715. testScript(SCRIPT, valueTrue, t)
  1716. }
  1717. func TestFunction2(t *testing.T) {
  1718. const SCRIPT = `
  1719. var trace = "";
  1720. function f(count) {
  1721. trace += "f("+count+")";
  1722. if (count == 0) {
  1723. return;
  1724. }
  1725. return f(count-1);
  1726. }
  1727. function f1() {
  1728. trace += "f1";
  1729. }
  1730. var f2 = f;
  1731. f = f1;
  1732. f2(1);
  1733. trace;
  1734. `
  1735. testScript(SCRIPT, asciiString("f(1)f1"), t)
  1736. }
  1737. func TestFunctionToString(t *testing.T) {
  1738. const SCRIPT = `
  1739. Function("arg1", "arg2", "return 42").toString();
  1740. `
  1741. testScript(SCRIPT, asciiString("function anonymous(arg1,arg2\n) {\nreturn 42\n}"), t)
  1742. }
  1743. func TestObjectLiteral(t *testing.T) {
  1744. const SCRIPT = `
  1745. var getterCalled = false;
  1746. var setterCalled = false;
  1747. var o = {get x() {getterCalled = true}, set x() {setterCalled = true}};
  1748. o.x;
  1749. o.x = 42;
  1750. getterCalled && setterCalled;
  1751. `
  1752. testScript(SCRIPT, valueTrue, t)
  1753. }
  1754. func TestConst(t *testing.T) {
  1755. const SCRIPT = `
  1756. var v1 = true && true;
  1757. var v2 = 1/(-1 * 0);
  1758. var v3 = 1 == 2 || v1;
  1759. var v4 = true && false
  1760. v1 === true && v2 === -Infinity && v3 === v1 && v4 === false;
  1761. `
  1762. testScript(SCRIPT, valueTrue, t)
  1763. }
  1764. func TestConstWhile(t *testing.T) {
  1765. const SCRIPT = `
  1766. var c = 0;
  1767. while (2 + 2 === 4) {
  1768. if (++c > 9) {
  1769. break;
  1770. }
  1771. }
  1772. c === 10;
  1773. `
  1774. testScript(SCRIPT, valueTrue, t)
  1775. }
  1776. func TestConstWhileThrow(t *testing.T) {
  1777. const SCRIPT = `
  1778. var thrown = false;
  1779. try {
  1780. while ('s' in true) {
  1781. break;
  1782. }
  1783. } catch (e) {
  1784. thrown = e instanceof TypeError
  1785. }
  1786. thrown;
  1787. `
  1788. testScript(SCRIPT, valueTrue, t)
  1789. }
  1790. func TestDupParams(t *testing.T) {
  1791. const SCRIPT = `
  1792. function F(x, y, x) {
  1793. return x;
  1794. }
  1795. F(1, 2);
  1796. `
  1797. testScript(SCRIPT, _undefined, t)
  1798. }
  1799. func TestUseUnsuppliedParam(t *testing.T) {
  1800. const SCRIPT = `
  1801. function getMessage(message) {
  1802. if (message === undefined) {
  1803. message = '';
  1804. }
  1805. message += " 123 456";
  1806. return message;
  1807. }
  1808. getMessage();
  1809. `
  1810. testScript(SCRIPT, asciiString(" 123 456"), t)
  1811. }
  1812. func TestForInLetWithInitializer(t *testing.T) {
  1813. const SCRIPT = `for (let x = 3 in {}) { }`
  1814. _, err := Compile("", SCRIPT, false)
  1815. if err == nil {
  1816. t.Fatal("Expected error")
  1817. }
  1818. }
  1819. func TestForInLoop(t *testing.T) {
  1820. const SCRIPT = `
  1821. function Proto() {}
  1822. Proto.prototype.x = 42;
  1823. var o = new Proto();
  1824. o.y = 44;
  1825. o.x = 45;
  1826. var hasX = false;
  1827. var hasY = false;
  1828. for (var i in o) {
  1829. switch(i) {
  1830. case "x":
  1831. if (hasX) {
  1832. throw new Error("Already has X");
  1833. }
  1834. hasX = true;
  1835. break;
  1836. case "y":
  1837. if (hasY) {
  1838. throw new Error("Already has Y");
  1839. }
  1840. hasY = true;
  1841. break;
  1842. }
  1843. }
  1844. hasX && hasY;
  1845. `
  1846. testScript(SCRIPT, valueTrue, t)
  1847. }
  1848. func TestWhileLoopResult(t *testing.T) {
  1849. const SCRIPT = `
  1850. while(false);
  1851. `
  1852. testScript(SCRIPT, _undefined, t)
  1853. }
  1854. func TestEmptySwitch(t *testing.T) {
  1855. const SCRIPT = `
  1856. switch(1){}
  1857. `
  1858. testScript(SCRIPT, _undefined, t)
  1859. }
  1860. func TestEmptyDoWhile(t *testing.T) {
  1861. const SCRIPT = `
  1862. do {} while(false)
  1863. `
  1864. testScript(SCRIPT, _undefined, t)
  1865. }
  1866. func TestSwitch(t *testing.T) {
  1867. const SCRIPT = `
  1868. function F(x) {
  1869. var i = 0;
  1870. switch (x) {
  1871. case 0:
  1872. i++;
  1873. case 1:
  1874. i++;
  1875. default:
  1876. i++;
  1877. case 2:
  1878. i++;
  1879. break;
  1880. case 3:
  1881. i++;
  1882. }
  1883. return i;
  1884. }
  1885. F(0) + F(1) + F(2) + F(4);
  1886. `
  1887. testScript(SCRIPT, intToValue(10), t)
  1888. }
  1889. func TestSwitchDefFirst(t *testing.T) {
  1890. const SCRIPT = `
  1891. function F(x) {
  1892. var i = 0;
  1893. switch (x) {
  1894. default:
  1895. i++;
  1896. case 0:
  1897. i++;
  1898. case 1:
  1899. i++;
  1900. case 2:
  1901. i++;
  1902. break;
  1903. case 3:
  1904. i++;
  1905. }
  1906. return i;
  1907. }
  1908. F(0) + F(1) + F(2) + F(4);
  1909. `
  1910. testScript(SCRIPT, intToValue(10), t)
  1911. }
  1912. func TestSwitchResult(t *testing.T) {
  1913. const SCRIPT = `
  1914. var x = 2;
  1915. switch (x) {
  1916. case 0:
  1917. "zero";
  1918. case 1:
  1919. "one";
  1920. case 2:
  1921. "two";
  1922. break;
  1923. case 3:
  1924. "three";
  1925. default:
  1926. "default";
  1927. }
  1928. `
  1929. testScript(SCRIPT, asciiString("two"), t)
  1930. }
  1931. func TestSwitchResult1(t *testing.T) {
  1932. const SCRIPT = `
  1933. var x = 0;
  1934. switch (x) { case 0: "two"; case 1: break}
  1935. `
  1936. testScript(SCRIPT, asciiString("two"), t)
  1937. }
  1938. func TestSwitchResult2(t *testing.T) {
  1939. const SCRIPT = `
  1940. 6; switch ("a") { case "a": 7; case "b": }
  1941. `
  1942. testScript(SCRIPT, valueInt(7), t)
  1943. }
  1944. func TestSwitchResultJumpIntoEmptyEval(t *testing.T) {
  1945. const SCRIPT = `
  1946. function t(x) {
  1947. return eval("switch(x) { case 1: 2; break; case 2: let x = 1; case 3: x+2; break; case 4: default: 9}");
  1948. }
  1949. ""+t(2)+t();
  1950. `
  1951. testScript(SCRIPT, asciiString("39"), t)
  1952. }
  1953. func TestSwitchResultJumpIntoEmpty(t *testing.T) {
  1954. const SCRIPT = `
  1955. switch(2) { case 1: 2; break; case 2: let x = 1; case 3: x+2; case 4: {let y = 2}; break; default: 9};
  1956. `
  1957. testScript(SCRIPT, valueInt(3), t)
  1958. }
  1959. func TestSwitchLexical(t *testing.T) {
  1960. const SCRIPT = `
  1961. switch (true) { case true: let x = 1; }
  1962. `
  1963. testScript(SCRIPT, _undefined, t)
  1964. }
  1965. func TestSwitchBreakOuter(t *testing.T) {
  1966. const SCRIPT = `
  1967. LOOP:
  1968. for (let i = 0; i < 10; i++) {
  1969. switch (i) {
  1970. case 0:
  1971. continue;
  1972. case 1:
  1973. let x = 1;
  1974. continue;
  1975. case 2:
  1976. try {
  1977. x++;
  1978. } catch (e) {
  1979. if (e instanceof ReferenceError) {
  1980. break LOOP;
  1981. }
  1982. }
  1983. throw new Error("Exception was not thrown");
  1984. }
  1985. }
  1986. `
  1987. testScript(SCRIPT, _undefined, t)
  1988. }
  1989. func TestIfBreakResult(t *testing.T) {
  1990. const SCRIPT = `
  1991. L: {if (true) {42;} break L;}
  1992. `
  1993. testScript(SCRIPT, intToValue(42), t)
  1994. }
  1995. func TestSwitchNoMatch(t *testing.T) {
  1996. const SCRIPT = `
  1997. var result;
  1998. var x;
  1999. switch (x) {
  2000. case 0:
  2001. result = "2";
  2002. break;
  2003. }
  2004. result;
  2005. `
  2006. testScript(SCRIPT, _undefined, t)
  2007. }
  2008. func TestSwitchNoMatchNoDefault(t *testing.T) {
  2009. const SCRIPT = `
  2010. switch (1) {
  2011. case 0:
  2012. }
  2013. `
  2014. testScript(SCRIPT, _undefined, t)
  2015. }
  2016. func TestSwitchNoMatchNoDefaultNoResult(t *testing.T) {
  2017. const SCRIPT = `
  2018. switch (1) {
  2019. case 0:
  2020. }
  2021. 42;
  2022. `
  2023. testScript(SCRIPT, intToValue(42), t)
  2024. }
  2025. func TestSwitchNoMatchNoDefaultNoResultMatch(t *testing.T) {
  2026. const SCRIPT = `
  2027. switch (1) {
  2028. case 1:
  2029. }
  2030. 42;
  2031. `
  2032. testScript(SCRIPT, intToValue(42), t)
  2033. }
  2034. func TestEmptySwitchNoResult(t *testing.T) {
  2035. const SCRIPT = `
  2036. switch (1) {}
  2037. 42;
  2038. `
  2039. testScript(SCRIPT, intToValue(42), t)
  2040. }
  2041. func TestGetOwnPropertyNames(t *testing.T) {
  2042. const SCRIPT = `
  2043. var o = {
  2044. prop1: 42,
  2045. prop2: "test"
  2046. }
  2047. var hasProp1 = false;
  2048. var hasProp2 = false;
  2049. var names = Object.getOwnPropertyNames(o);
  2050. for (var i in names) {
  2051. var p = names[i];
  2052. switch(p) {
  2053. case "prop1":
  2054. hasProp1 = true;
  2055. break;
  2056. case "prop2":
  2057. hasProp2 = true;
  2058. break;
  2059. }
  2060. }
  2061. hasProp1 && hasProp2;
  2062. `
  2063. testScript(SCRIPT, valueTrue, t)
  2064. }
  2065. func TestArrayLiteral(t *testing.T) {
  2066. const SCRIPT = `
  2067. var f1Called = false;
  2068. var f2Called = false;
  2069. var f3Called = false;
  2070. var errorThrown = false;
  2071. function F1() {
  2072. f1Called = true;
  2073. }
  2074. function F2() {
  2075. f2Called = true;
  2076. }
  2077. function F3() {
  2078. f3Called = true;
  2079. }
  2080. try {
  2081. var a = [F1(), x(F3()), F2()];
  2082. } catch(e) {
  2083. if (e instanceof ReferenceError) {
  2084. errorThrown = true;
  2085. } else {
  2086. throw e;
  2087. }
  2088. }
  2089. f1Called && !f2Called && f3Called && errorThrown && a === undefined;
  2090. `
  2091. testScript(SCRIPT, valueTrue, t)
  2092. }
  2093. func TestJumpOutOfReturn(t *testing.T) {
  2094. const SCRIPT = `
  2095. function f() {
  2096. var a;
  2097. if (a == 0) {
  2098. return true;
  2099. }
  2100. }
  2101. f();
  2102. `
  2103. testScript(SCRIPT, _undefined, t)
  2104. }
  2105. func TestSwitchJumpOutOfReturn(t *testing.T) {
  2106. const SCRIPT = `
  2107. function f(x) {
  2108. switch(x) {
  2109. case 0:
  2110. break;
  2111. default:
  2112. return x;
  2113. }
  2114. }
  2115. f(0);
  2116. `
  2117. testScript(SCRIPT, _undefined, t)
  2118. }
  2119. func TestSetToReadOnlyPropertyStrictBracket(t *testing.T) {
  2120. const SCRIPT = `
  2121. 'use strict';
  2122. var o = {};
  2123. var thrown = false;
  2124. Object.defineProperty(o, "test", {value: 42, configurable: true});
  2125. try {
  2126. o["test"] = 43;
  2127. } catch (e) {
  2128. thrown = e instanceof TypeError;
  2129. }
  2130. thrown;
  2131. `
  2132. testScript(SCRIPT, valueTrue, t)
  2133. }
  2134. func TestSetToReadOnlyPropertyStrictDot(t *testing.T) {
  2135. const SCRIPT = `
  2136. 'use strict';
  2137. var o = {};
  2138. var thrown = false;
  2139. Object.defineProperty(o, "test", {value: 42, configurable: true});
  2140. try {
  2141. o.test = 43;
  2142. } catch (e) {
  2143. thrown = e instanceof TypeError;
  2144. }
  2145. thrown;
  2146. `
  2147. testScript(SCRIPT, valueTrue, t)
  2148. }
  2149. func TestDeleteNonConfigurablePropertyStrictBracket(t *testing.T) {
  2150. const SCRIPT = `
  2151. 'use strict';
  2152. var o = {};
  2153. var thrown = false;
  2154. Object.defineProperty(o, "test", {value: 42});
  2155. try {
  2156. delete o["test"];
  2157. } catch (e) {
  2158. thrown = e instanceof TypeError;
  2159. }
  2160. thrown;
  2161. `
  2162. testScript(SCRIPT, valueTrue, t)
  2163. }
  2164. func TestDeleteNonConfigurablePropertyStrictDot(t *testing.T) {
  2165. const SCRIPT = `
  2166. 'use strict';
  2167. var o = {};
  2168. var thrown = false;
  2169. Object.defineProperty(o, "test", {value: 42});
  2170. try {
  2171. delete o.test;
  2172. } catch (e) {
  2173. thrown = e instanceof TypeError;
  2174. }
  2175. thrown;
  2176. `
  2177. testScript(SCRIPT, valueTrue, t)
  2178. }
  2179. func TestCompound1(t *testing.T) {
  2180. const SCRIPT = `
  2181. var x = 0;
  2182. var scope = {x: 1};
  2183. var f;
  2184. with (scope) {
  2185. f = function() {
  2186. x *= (delete scope.x, 2);
  2187. }
  2188. }
  2189. f();
  2190. scope.x === 2 && x === 0;
  2191. `
  2192. testScript(SCRIPT, valueTrue, t)
  2193. }
  2194. func TestCompound2(t *testing.T) {
  2195. const SCRIPT = `
  2196. var x;
  2197. x = "x";
  2198. x ^= "1";
  2199. `
  2200. testScript(SCRIPT, intToValue(1), t)
  2201. }
  2202. func TestDeleteArguments(t *testing.T) {
  2203. defer func() {
  2204. if _, ok := recover().(*CompilerSyntaxError); !ok {
  2205. t.Fatal("Expected syntax error")
  2206. }
  2207. }()
  2208. const SCRIPT = `
  2209. 'use strict';
  2210. function f() {
  2211. delete arguments;
  2212. }
  2213. `
  2214. testScript(SCRIPT, _undefined, t)
  2215. }
  2216. func TestReturnUndefined(t *testing.T) {
  2217. const SCRIPT = `
  2218. function f() {
  2219. return x;
  2220. }
  2221. var thrown = false;
  2222. try {
  2223. f();
  2224. } catch (e) {
  2225. thrown = e instanceof ReferenceError;
  2226. }
  2227. thrown;
  2228. `
  2229. testScript(SCRIPT, valueTrue, t)
  2230. }
  2231. func TestForBreak(t *testing.T) {
  2232. const SCRIPT = `
  2233. var supreme, count;
  2234. supreme = 5;
  2235. var __evaluated = eval("for(count=0;;) {if (count===supreme)break;else count++; }");
  2236. if (__evaluated !== void 0) {
  2237. throw new Error('#1: __evaluated === 4. Actual: __evaluated ==='+ __evaluated );
  2238. }
  2239. `
  2240. testScript(SCRIPT, _undefined, t)
  2241. }
  2242. func TestLargeNumberLiteral(t *testing.T) {
  2243. const SCRIPT = `
  2244. var x = 0x800000000000000000000;
  2245. x.toString();
  2246. `
  2247. testScript(SCRIPT, asciiString("9.671406556917033e+24"), t)
  2248. }
  2249. func TestIncDelete(t *testing.T) {
  2250. const SCRIPT = `
  2251. var o = {x: 1};
  2252. o.x += (delete o.x, 1);
  2253. o.x;
  2254. `
  2255. testScript(SCRIPT, intToValue(2), t)
  2256. }
  2257. func TestCompoundAssignRefError(t *testing.T) {
  2258. const SCRIPT = `
  2259. var thrown = false;
  2260. try {
  2261. a *= 1;
  2262. } catch (e) {
  2263. if (e instanceof ReferenceError) {
  2264. thrown = true;
  2265. } else {
  2266. throw e;
  2267. }
  2268. }
  2269. thrown;
  2270. `
  2271. testScript(SCRIPT, valueTrue, t)
  2272. }
  2273. func TestObjectLiteral__Proto__(t *testing.T) {
  2274. const SCRIPT = `
  2275. var o = {
  2276. __proto__: null,
  2277. test: 42
  2278. }
  2279. Object.getPrototypeOf(o);
  2280. `
  2281. testScript(SCRIPT, _null, t)
  2282. }
  2283. func TestEmptyCodeError(t *testing.T) {
  2284. if _, err := New().RunString(`i`); err == nil {
  2285. t.Fatal("Expected an error")
  2286. } else {
  2287. if e := err.Error(); e != "ReferenceError: i is not defined at <eval>:1:1(0)" {
  2288. t.Fatalf("Unexpected error: '%s'", e)
  2289. }
  2290. }
  2291. }
  2292. func TestForOfArray(t *testing.T) {
  2293. const SCRIPT = `
  2294. var array = [0, 'a', true, false, null, /* hole */, undefined, NaN];
  2295. var i = 0;
  2296. for (var value of array) {
  2297. assert.sameValue(value, array[i], 'element at index ' + i);
  2298. i++;
  2299. }
  2300. assert.sameValue(i, 8, 'Visits all elements');
  2301. `
  2302. testScriptWithTestLib(SCRIPT, _undefined, t)
  2303. }
  2304. func TestForOfReturn(t *testing.T) {
  2305. const SCRIPT = `
  2306. var callCount = 0;
  2307. var iterationCount = 0;
  2308. var iterable = {};
  2309. var x = {
  2310. set attr(_) {
  2311. throw new Test262Error();
  2312. }
  2313. };
  2314. iterable[Symbol.iterator] = function() {
  2315. return {
  2316. next: function() {
  2317. return { done: false, value: 0 };
  2318. },
  2319. return: function() {
  2320. callCount += 1;
  2321. }
  2322. }
  2323. };
  2324. assert.throws(Test262Error, function() {
  2325. for (x.attr of iterable) {
  2326. iterationCount += 1;
  2327. }
  2328. });
  2329. assert.sameValue(iterationCount, 0, 'The loop body is not evaluated');
  2330. assert.sameValue(callCount, 1, 'Iterator is closed');
  2331. `
  2332. testScriptWithTestLib(SCRIPT, _undefined, t)
  2333. }
  2334. func TestForOfReturn1(t *testing.T) {
  2335. const SCRIPT = `
  2336. var iterable = {};
  2337. var iterationCount = 0;
  2338. iterable[Symbol.iterator] = function() {
  2339. return {
  2340. next: function() {
  2341. return { done: false, value: null };
  2342. },
  2343. get return() {
  2344. throw new Test262Error();
  2345. }
  2346. };
  2347. };
  2348. assert.throws(Test262Error, function() {
  2349. for (var x of iterable) {
  2350. iterationCount += 1;
  2351. break;
  2352. }
  2353. });
  2354. assert.sameValue(iterationCount, 1, 'The loop body is evaluated');
  2355. `
  2356. testScriptWithTestLib(SCRIPT, _undefined, t)
  2357. }
  2358. func TestForOfLet(t *testing.T) {
  2359. const SCRIPT = `
  2360. var iterCount = 0;
  2361. function f() {}
  2362. for (var let of [23]) {
  2363. f(let);
  2364. if (let != 23) {
  2365. throw new Error("");
  2366. }
  2367. iterCount += 1;
  2368. }
  2369. iterCount;
  2370. `
  2371. testScript(SCRIPT, valueInt(1), t)
  2372. }
  2373. func TestForOfLetLet(t *testing.T) {
  2374. const SCRIPT = `
  2375. for (let let of [23]) {
  2376. }
  2377. `
  2378. _, err := Compile("", SCRIPT, false)
  2379. if err == nil {
  2380. t.Fatal("Expected error")
  2381. }
  2382. }
  2383. func TestForHeadLet(t *testing.T) {
  2384. const SCRIPT = `
  2385. for (let = 0; let < 2; let++);
  2386. `
  2387. testScript(SCRIPT, _undefined, t)
  2388. }
  2389. func TestLhsLet(t *testing.T) {
  2390. const SCRIPT = `
  2391. let = 1;
  2392. let;
  2393. `
  2394. testScript(SCRIPT, valueInt(1), t)
  2395. }
  2396. func TestLetPostfixASI(t *testing.T) {
  2397. const SCRIPT = `
  2398. let
  2399. ++
  2400. `
  2401. _, err := Compile("", SCRIPT, false)
  2402. if err == nil {
  2403. t.Fatal("Expected error")
  2404. }
  2405. }
  2406. func TestIteratorReturnNormal(t *testing.T) {
  2407. const SCRIPT = `
  2408. var iterable = {};
  2409. var iterationCount = 0;
  2410. iterable[Symbol.iterator] = function() {
  2411. return {
  2412. next: function() {
  2413. return { done: ++iterationCount > 2, value: null };
  2414. },
  2415. get return() {
  2416. throw new Test262Error();
  2417. }
  2418. };
  2419. };
  2420. for (var x of iterable) {
  2421. }
  2422. `
  2423. testScriptWithTestLib(SCRIPT, _undefined, t)
  2424. }
  2425. func TestIteratorReturnErrorNested(t *testing.T) {
  2426. const SCRIPT = `
  2427. var returnCalled = {};
  2428. function iter(id) {
  2429. return function() {
  2430. var count = 0;
  2431. return {
  2432. next: function () {
  2433. return {
  2434. value: null,
  2435. done: ++count > 2
  2436. };
  2437. },
  2438. return: function () {
  2439. returnCalled[id] = true;
  2440. throw new Error(id);
  2441. }
  2442. };
  2443. }
  2444. }
  2445. var iterable1 = {};
  2446. iterable1[Symbol.iterator] = iter("1");
  2447. var iterable2 = {};
  2448. iterable2[Symbol.iterator] = iter("2");
  2449. try {
  2450. for (var i of iterable1) {
  2451. for (var j of iterable2) {
  2452. break;
  2453. }
  2454. }
  2455. throw new Error("no exception was thrown");
  2456. } catch (e) {
  2457. if (e.message !== "2") {
  2458. throw e;
  2459. }
  2460. }
  2461. if (!returnCalled["1"]) {
  2462. throw new Error("no return 1");
  2463. }
  2464. if (!returnCalled["2"]) {
  2465. throw new Error("no return 2");
  2466. }
  2467. `
  2468. testScript(SCRIPT, _undefined, t)
  2469. }
  2470. func TestReturnFromForInLoop(t *testing.T) {
  2471. const SCRIPT = `
  2472. (function f() {
  2473. for (var i in {a: 1}) {
  2474. return true;
  2475. }
  2476. })();
  2477. `
  2478. testScript(SCRIPT, valueTrue, t)
  2479. }
  2480. func TestReturnFromForOfLoop(t *testing.T) {
  2481. const SCRIPT = `
  2482. (function f() {
  2483. for (var i of [1]) {
  2484. return true;
  2485. }
  2486. })();
  2487. `
  2488. testScript(SCRIPT, valueTrue, t)
  2489. }
  2490. func TestIfStackLeaks(t *testing.T) {
  2491. const SCRIPT = `
  2492. var t = 0;
  2493. if (t === 0) {
  2494. t;
  2495. }
  2496. `
  2497. testScript(SCRIPT, _positiveZero, t)
  2498. }
  2499. func TestWithCallee(t *testing.T) {
  2500. const SCRIPT = `
  2501. function O() {
  2502. var that = this;
  2503. this.m = function() {
  2504. return this === that;
  2505. }
  2506. }
  2507. with(new O()) {
  2508. m();
  2509. }
  2510. `
  2511. testScript(SCRIPT, valueTrue, t)
  2512. }
  2513. func TestWithScope(t *testing.T) {
  2514. const SCRIPT = `
  2515. function f(o) {
  2516. var x = 42;
  2517. function innerf(o) {
  2518. with (o) {
  2519. return x;
  2520. }
  2521. }
  2522. return innerf(o);
  2523. }
  2524. f({});
  2525. `
  2526. testScript(SCRIPT, valueInt(42), t)
  2527. }
  2528. func TestEvalCallee(t *testing.T) {
  2529. const SCRIPT = `
  2530. (function () {
  2531. 'use strict';
  2532. var v = function() {
  2533. return this === undefined;
  2534. };
  2535. return eval('v()');
  2536. })();
  2537. `
  2538. testScript(SCRIPT, valueTrue, t)
  2539. }
  2540. func TestEvalBindingDeleteVar(t *testing.T) {
  2541. const SCRIPT = `
  2542. (function () {
  2543. eval("var x = 1");
  2544. return x === 1 && delete x;
  2545. })();
  2546. `
  2547. testScript(SCRIPT, valueTrue, t)
  2548. }
  2549. func TestEvalBindingDeleteFunc(t *testing.T) {
  2550. const SCRIPT = `
  2551. (function () {
  2552. eval("function x(){}");
  2553. return typeof x === "function" && delete x;
  2554. })();
  2555. `
  2556. testScript(SCRIPT, valueTrue, t)
  2557. }
  2558. func TestDeleteGlobalLexical(t *testing.T) {
  2559. const SCRIPT = `
  2560. let x;
  2561. delete x;
  2562. `
  2563. testScript(SCRIPT, valueFalse, t)
  2564. }
  2565. func TestDeleteGlobalEval(t *testing.T) {
  2566. const SCRIPT = `
  2567. eval("var x");
  2568. delete x;
  2569. `
  2570. testScript(SCRIPT, valueTrue, t)
  2571. }
  2572. func TestGlobalVarNames(t *testing.T) {
  2573. vm := New()
  2574. _, err := vm.RunString("(0,eval)('var x')")
  2575. if err != nil {
  2576. t.Fatal(err)
  2577. }
  2578. _, err = vm.RunString("let x")
  2579. if err == nil {
  2580. t.Fatal("Expected error")
  2581. }
  2582. }
  2583. func TestTryResultEmpty(t *testing.T) {
  2584. const SCRIPT = `
  2585. 1; try { } finally { }
  2586. `
  2587. testScript(SCRIPT, _undefined, t)
  2588. }
  2589. func TestTryResultEmptyCatch(t *testing.T) {
  2590. const SCRIPT = `
  2591. 1; try { throw null } catch(e) { }
  2592. `
  2593. testScript(SCRIPT, _undefined, t)
  2594. }
  2595. func TestTryResultEmptyContinueLoop(t *testing.T) {
  2596. const SCRIPT = `
  2597. for (var i = 0; i < 2; i++) { try {throw null;} catch(e) {continue;} 'bad'}
  2598. `
  2599. testScript(SCRIPT, _undefined, t)
  2600. }
  2601. func TestTryEmptyCatchStackLeak(t *testing.T) {
  2602. const SCRIPT = `
  2603. (function() {
  2604. var f;
  2605. // Make sure the outer function is not stashless.
  2606. (function() {
  2607. f++;
  2608. })();
  2609. try {
  2610. throw new Error();
  2611. } catch(e) {}
  2612. })();
  2613. `
  2614. testScript(SCRIPT, _undefined, t)
  2615. }
  2616. func TestTryThrowEmptyCatch(t *testing.T) {
  2617. const SCRIPT = `
  2618. try {
  2619. throw new Error();
  2620. }
  2621. catch (e) {}
  2622. `
  2623. testScript(SCRIPT, _undefined, t)
  2624. }
  2625. func TestFalsyLoopBreak(t *testing.T) {
  2626. const SCRIPT = `
  2627. while(false) {
  2628. break;
  2629. }
  2630. for(;false;) {
  2631. break;
  2632. }
  2633. undefined;
  2634. `
  2635. MustCompile("", SCRIPT, false)
  2636. }
  2637. func TestFalsyLoopBreakWithResult(t *testing.T) {
  2638. const SCRIPT = `
  2639. while(false) {
  2640. break;
  2641. }
  2642. `
  2643. testScript(SCRIPT, _undefined, t)
  2644. }
  2645. func TestDummyCompile(t *testing.T) {
  2646. const SCRIPT = `
  2647. 'use strict';
  2648. for (;false;) {
  2649. eval = 1;
  2650. }
  2651. `
  2652. _, err := Compile("", SCRIPT, false)
  2653. if err == nil {
  2654. t.Fatal("expected error")
  2655. }
  2656. }
  2657. func TestDummyCompileForUpdate(t *testing.T) {
  2658. const SCRIPT = `
  2659. 'use strict';
  2660. for (;false;eval=1) {
  2661. }
  2662. `
  2663. _, err := Compile("", SCRIPT, false)
  2664. if err == nil {
  2665. t.Fatal("expected error")
  2666. }
  2667. }
  2668. func TestObjectLiteralWithNumericKeys(t *testing.T) {
  2669. const SCRIPT = `
  2670. var o = {1e3: true};
  2671. var keys = Object.keys(o);
  2672. var o1 = {get 1e3() {return true;}};
  2673. var keys1 = Object.keys(o1);
  2674. var o2 = {1e21: true};
  2675. var keys2 = Object.keys(o2);
  2676. keys.length === 1 && keys[0] === "1000" &&
  2677. keys1.length === 1 && keys1[0] === "1000" && o1[1e3] === true &&
  2678. keys2.length === 1 && keys2[0] === "1e+21";
  2679. `
  2680. testScript(SCRIPT, valueTrue, t)
  2681. }
  2682. func TestEscapedObjectPropertyKeys(t *testing.T) {
  2683. const SCRIPT = `
  2684. var obj = {
  2685. w\u0069th: 42
  2686. };
  2687. var obj = {
  2688. with() {42}
  2689. };
  2690. `
  2691. _, err := Compile("", SCRIPT, false)
  2692. if err != nil {
  2693. t.Fatal(err)
  2694. }
  2695. }
  2696. func TestEscapedKeywords(t *testing.T) {
  2697. const SCRIPT = `r\u0065turn;`
  2698. _, err := Compile("", SCRIPT, false)
  2699. if err == nil {
  2700. t.Fatal("Expected error")
  2701. }
  2702. }
  2703. func TestEscapedLet(t *testing.T) {
  2704. const SCRIPT = `
  2705. this.let = 0;
  2706. l\u0065t // ASI
  2707. a;
  2708. // If the parser treated the previous escaped "let" as a lexical declaration,
  2709. // this variable declaration will result an early syntax error.
  2710. var a;
  2711. `
  2712. _, err := Compile("", SCRIPT, false)
  2713. if err != nil {
  2714. t.Fatal(err)
  2715. }
  2716. }
  2717. func TestObjectLiteralFuncProps(t *testing.T) {
  2718. const SCRIPT = `
  2719. (function() {
  2720. 'use strict';
  2721. var o = {
  2722. eval: function() {return 1;},
  2723. arguments() {return 2;},
  2724. test: function test1() {}
  2725. }
  2726. assert.sameValue(o.eval.name, "eval");
  2727. assert.sameValue(o.arguments.name, "arguments");
  2728. assert.sameValue(o.eval(), 1);
  2729. assert.sameValue(o.arguments(), 2);
  2730. assert.sameValue(o.test.name, "test1");
  2731. })();
  2732. `
  2733. testScriptWithTestLib(SCRIPT, _undefined, t)
  2734. }
  2735. func TestFuncName(t *testing.T) {
  2736. const SCRIPT = `
  2737. var method = 1;
  2738. var o = {
  2739. method: function() {
  2740. return method;
  2741. },
  2742. method1: function method() {
  2743. return method;
  2744. }
  2745. }
  2746. o.method() === 1 && o.method1() === o.method1;
  2747. `
  2748. testScript(SCRIPT, valueTrue, t)
  2749. }
  2750. func TestFuncNameAssign(t *testing.T) {
  2751. const SCRIPT = `
  2752. var f = function() {};
  2753. var f1;
  2754. f1 = function() {};
  2755. let f2 = function() {};
  2756. f.name === "f" && f1.name === "f1" && f2.name === "f2";
  2757. `
  2758. testScript(SCRIPT, valueTrue, t)
  2759. }
  2760. func TestLexicalDeclGlobal(t *testing.T) {
  2761. const SCRIPT = `
  2762. if (true) {
  2763. let it = "be";
  2764. if (it !== "be") {
  2765. throw new Error(it);
  2766. }
  2767. }
  2768. let thrown = false;
  2769. try {
  2770. it;
  2771. } catch(e) {
  2772. if (e instanceof ReferenceError) {
  2773. thrown = true;
  2774. }
  2775. }
  2776. thrown;
  2777. `
  2778. testScript(SCRIPT, valueTrue, t)
  2779. }
  2780. func TestLexicalDeclFunction(t *testing.T) {
  2781. const SCRIPT = `
  2782. function f() {
  2783. if (true) {
  2784. let it = "be";
  2785. if (it !== "be") {
  2786. throw new Error(it);
  2787. }
  2788. }
  2789. let thrown = false;
  2790. try {
  2791. it;
  2792. } catch(e) {
  2793. if (e instanceof ReferenceError) {
  2794. thrown = true;
  2795. }
  2796. }
  2797. return thrown;
  2798. }
  2799. f();
  2800. `
  2801. testScript(SCRIPT, valueTrue, t)
  2802. }
  2803. func TestLexicalDynamicScope(t *testing.T) {
  2804. const SCRIPT = `
  2805. const global = 1;
  2806. function f() {
  2807. const func = global + 1;
  2808. function inner() {
  2809. function assertThrows(fn) {
  2810. let thrown = false;
  2811. try {
  2812. fn();
  2813. } catch (e) {
  2814. if (e instanceof TypeError) {
  2815. thrown = true;
  2816. } else {
  2817. throw e;
  2818. }
  2819. }
  2820. if (!thrown) {
  2821. throw new Error("Did not throw");
  2822. }
  2823. }
  2824. assertThrows(function() {
  2825. func++;
  2826. });
  2827. assertThrows(function() {
  2828. global++;
  2829. });
  2830. assertThrows(function() {
  2831. eval("func++");
  2832. });
  2833. assertThrows(function() {
  2834. eval("global++");
  2835. });
  2836. return eval("func + 1");
  2837. }
  2838. return inner();
  2839. }
  2840. f();
  2841. `
  2842. testScript(SCRIPT, valueInt(3), t)
  2843. }
  2844. func TestLexicalDynamicScope1(t *testing.T) {
  2845. const SCRIPT = `
  2846. (function() {
  2847. const x = 1 * 4;
  2848. return (function() {
  2849. eval("");
  2850. return x;
  2851. })();
  2852. })();
  2853. `
  2854. testScript(SCRIPT, intToValue(4), t)
  2855. }
  2856. func TestLexicalDynamicScope2(t *testing.T) {
  2857. const SCRIPT = `
  2858. (function() {
  2859. const x = 1 + 3;
  2860. var y = 2 * 2;
  2861. eval("");
  2862. return x;
  2863. })();
  2864. `
  2865. testScript(SCRIPT, intToValue(4), t)
  2866. }
  2867. func TestNonStrictLet(t *testing.T) {
  2868. const SCRIPT = `
  2869. var let = 1;
  2870. `
  2871. testScript(SCRIPT, _undefined, t)
  2872. }
  2873. func TestStrictLet(t *testing.T) {
  2874. const SCRIPT = `
  2875. var let = 1;
  2876. `
  2877. _, err := Compile("", SCRIPT, true)
  2878. if err == nil {
  2879. t.Fatal("Expected an error")
  2880. }
  2881. }
  2882. func TestLetLet(t *testing.T) {
  2883. const SCRIPT = `
  2884. let let = 1;
  2885. `
  2886. _, err := Compile("", SCRIPT, false)
  2887. if err == nil {
  2888. t.Fatal("Expected an error")
  2889. }
  2890. }
  2891. func TestLetASI(t *testing.T) {
  2892. const SCRIPT = `
  2893. while (false) let // ASI
  2894. x = 1;
  2895. `
  2896. _, err := Compile("", SCRIPT, false)
  2897. if err != nil {
  2898. t.Fatal(err)
  2899. }
  2900. }
  2901. func TestLetASI1(t *testing.T) {
  2902. const SCRIPT = `
  2903. let
  2904. x = 1;
  2905. `
  2906. _, err := Compile("", SCRIPT, true)
  2907. if err != nil {
  2908. t.Fatal(err)
  2909. }
  2910. }
  2911. func TestLetNoASI(t *testing.T) {
  2912. const SCRIPT = `
  2913. function f() {}let
  2914. x = 1;
  2915. `
  2916. _, err := Compile("", SCRIPT, true)
  2917. if err != nil {
  2918. t.Fatal(err)
  2919. }
  2920. }
  2921. func TestLetNoASI1(t *testing.T) {
  2922. const SCRIPT = `
  2923. let
  2924. let = 1;
  2925. `
  2926. _, err := Compile("", SCRIPT, false)
  2927. if err == nil {
  2928. t.Fatal("Expected error")
  2929. }
  2930. }
  2931. func TestLetArrayWithNewline(t *testing.T) {
  2932. const SCRIPT = `
  2933. with ({}) let
  2934. [a] = 0;
  2935. `
  2936. _, err := Compile("", SCRIPT, false)
  2937. if err == nil {
  2938. t.Fatal("Expected error")
  2939. }
  2940. }
  2941. func TestDynamicUninitedVarAccess(t *testing.T) {
  2942. const SCRIPT = `
  2943. function f() {
  2944. var x;
  2945. return eval("x");
  2946. }
  2947. f();
  2948. `
  2949. testScript(SCRIPT, _undefined, t)
  2950. }
  2951. func TestLexicalForLoopNoClosure(t *testing.T) {
  2952. const SCRIPT = `
  2953. let sum = 0;
  2954. for (let i = 0; i < 3; i++) {
  2955. sum += i;
  2956. }
  2957. sum;
  2958. `
  2959. testScript(SCRIPT, valueInt(3), t)
  2960. }
  2961. func TestLexicalForLoopClosure(t *testing.T) {
  2962. const SCRIPT = `
  2963. var f = [];
  2964. for (let i = 0; i < 3; i++) {
  2965. f.push(function() {
  2966. return i;
  2967. });
  2968. }
  2969. f.length === 3 && f[0]() === 0 && f[1]() === 1 && f[2]() === 2;
  2970. `
  2971. testScript(SCRIPT, valueTrue, t)
  2972. }
  2973. func TestLexicalForLoopClosureInNext(t *testing.T) {
  2974. const SCRIPT = `
  2975. const a = [];
  2976. for (let i = 0; i < 5; a.push(function () { return i; }), ++i) { }
  2977. let res = "";
  2978. for (let k = 0; k < 5; ++k) {
  2979. res += ""+a[k]();
  2980. }
  2981. res;
  2982. `
  2983. testScript(SCRIPT, asciiString("12345"), t)
  2984. }
  2985. func TestVarForLoop(t *testing.T) {
  2986. const SCRIPT = `
  2987. var f = [];
  2988. for (var i = 0, j = 0; i < 3; i++) {
  2989. f.push(function() {
  2990. return i;
  2991. });
  2992. }
  2993. f.length === 3 && f[0]() === 3 && f[1]() === 3 && f[2]() === 3;
  2994. `
  2995. testScript(SCRIPT, valueTrue, t)
  2996. }
  2997. func TestLexicalForOfLoop(t *testing.T) {
  2998. const SCRIPT = `
  2999. var f = [];
  3000. for (let i of [0, 1, 2]) {
  3001. f.push(function() {
  3002. return i;
  3003. });
  3004. }
  3005. f.length === 3 && f[0]() === 0 && f[1]() === 1 && f[2]() === 2;
  3006. `
  3007. testScript(SCRIPT, valueTrue, t)
  3008. }
  3009. func TestLexicalForOfLoopContBreak(t *testing.T) {
  3010. const SCRIPT = `
  3011. const f = [];
  3012. for (let i of [0, 1, 2, 3, 4, 5]) {
  3013. if (i % 2) continue;
  3014. f.push(function() {
  3015. return i;
  3016. });
  3017. if (i > 2) break;
  3018. }
  3019. let res = "";
  3020. f.forEach(function(item) {res += item()});
  3021. f.length === 3 && res === "024";
  3022. `
  3023. testScript(SCRIPT, valueTrue, t)
  3024. }
  3025. func TestVarBlockConflict(t *testing.T) {
  3026. const SCRIPT = `
  3027. let x;
  3028. {
  3029. if (false) {
  3030. var x;
  3031. }
  3032. }
  3033. `
  3034. _, err := Compile("", SCRIPT, false)
  3035. if err == nil {
  3036. t.Fatal("Expected an error")
  3037. }
  3038. }
  3039. func TestVarBlockConflictEval(t *testing.T) {
  3040. const SCRIPT = `
  3041. assert.throws(SyntaxError, function() {
  3042. let x;
  3043. {
  3044. if (true) {
  3045. eval("var x");
  3046. }
  3047. }
  3048. });
  3049. `
  3050. testScriptWithTestLib(SCRIPT, _undefined, t)
  3051. }
  3052. func TestVarBlockNoConflict(t *testing.T) {
  3053. const SCRIPT = `
  3054. function f() {
  3055. let x;
  3056. function ff() {
  3057. {
  3058. var x = 3;
  3059. }
  3060. }
  3061. ff();
  3062. }
  3063. f();
  3064. `
  3065. testScript(SCRIPT, _undefined, t)
  3066. }
  3067. func TestVarBlockNoConflictEval(t *testing.T) {
  3068. const SCRIPT = `
  3069. function f() {
  3070. let x;
  3071. function ff() {
  3072. {
  3073. eval("var x = 3");
  3074. }
  3075. }
  3076. ff();
  3077. }
  3078. f();
  3079. `
  3080. testScript(SCRIPT, _undefined, t)
  3081. }
  3082. func TestVarDeclCorrectScope(t *testing.T) {
  3083. const SCRIPT = `
  3084. function f() {
  3085. {
  3086. let z;
  3087. eval("var x = 3");
  3088. }
  3089. return x;
  3090. }
  3091. f();
  3092. `
  3093. testScript(SCRIPT, valueInt(3), t)
  3094. }
  3095. func TestLexicalCatch(t *testing.T) {
  3096. const SCRIPT = `
  3097. try {
  3098. throw null;
  3099. } catch (e) {
  3100. let x = 1;
  3101. function f() {}
  3102. e;
  3103. }
  3104. `
  3105. testScript(SCRIPT, _null, t)
  3106. }
  3107. func TestArgumentsLexicalDecl(t *testing.T) {
  3108. const SCRIPT = `
  3109. function f1() {
  3110. let arguments;
  3111. return arguments;
  3112. }
  3113. f1(42);
  3114. `
  3115. testScript(SCRIPT, _undefined, t)
  3116. }
  3117. func TestArgumentsLexicalDeclAssign(t *testing.T) {
  3118. const SCRIPT = `
  3119. function f1() {
  3120. let arguments = arguments;
  3121. return a;
  3122. }
  3123. assert.throws(ReferenceError, function() {
  3124. f1(42);
  3125. });
  3126. `
  3127. testScriptWithTestLib(SCRIPT, _undefined, t)
  3128. }
  3129. func TestLexicalConstModifyFromEval(t *testing.T) {
  3130. const SCRIPT = `
  3131. const x = 1;
  3132. function f() {
  3133. eval("x = 2");
  3134. }
  3135. assert.throws(TypeError, function() {
  3136. f();
  3137. });
  3138. `
  3139. testScriptWithTestLib(SCRIPT, _undefined, t)
  3140. }
  3141. func TestLexicalStrictNames(t *testing.T) {
  3142. const SCRIPT = `let eval = 1;`
  3143. _, err := Compile("", SCRIPT, true)
  3144. if err == nil {
  3145. t.Fatal("Expected an error")
  3146. }
  3147. }
  3148. func TestAssignAfterStackExpand(t *testing.T) {
  3149. // make sure the reference to the variable x does not remain stale after the stack is copied
  3150. const SCRIPT = `
  3151. function f() {
  3152. let sum = 0;
  3153. for (let i = 0; i < arguments.length; i++) {
  3154. sum += arguments[i];
  3155. }
  3156. return sum;
  3157. }
  3158. function testAssignment() {
  3159. var x = 0;
  3160. var scope = {};
  3161. with (scope) {
  3162. x = (scope.x = f(0, 0, 0, 0, 0, 0, 1, 1), 1);
  3163. }
  3164. if (scope.x !== 2) {
  3165. throw new Error('#1: scope.x === 2. Actual: ' + (scope.x));
  3166. }
  3167. if (x !== 1) {
  3168. throw new Error('#2: x === 1. Actual: ' + (x));
  3169. }
  3170. }
  3171. testAssignment();
  3172. `
  3173. testScript(SCRIPT, _undefined, t)
  3174. }
  3175. func TestArgAccessFromDynamicStash(t *testing.T) {
  3176. const SCRIPT = `
  3177. function f(arg) {
  3178. function test() {
  3179. eval("");
  3180. return a;
  3181. }
  3182. return arg;
  3183. }
  3184. f(true);
  3185. `
  3186. testScript(SCRIPT, valueTrue, t)
  3187. }
  3188. func TestLoadMixedLex(t *testing.T) {
  3189. const SCRIPT = `
  3190. function f() {
  3191. let a = 1;
  3192. {
  3193. function inner() {
  3194. eval("var a = true");
  3195. return a;
  3196. }
  3197. return inner();
  3198. }
  3199. }
  3200. f();
  3201. `
  3202. testScript(SCRIPT, valueTrue, t)
  3203. }
  3204. func TestObjectLiteralSpread(t *testing.T) {
  3205. const SCRIPT = `
  3206. let src = {prop1: 1};
  3207. Object.defineProperty(src, "prop2", {value: 2, configurable: true});
  3208. Object.defineProperty(src, "prop3", {value: 3, enumerable: true, configurable: true});
  3209. let target = {prop4: 4, ...src};
  3210. assert(deepEqual(target, {prop1: 1, prop3: 3, prop4: 4}));
  3211. `
  3212. testScriptWithTestLibX(SCRIPT, _undefined, t)
  3213. }
  3214. func TestArrayLiteralSpread(t *testing.T) {
  3215. const SCRIPT = `
  3216. let a1 = [1, 2];
  3217. let a2 = [3, 4];
  3218. let a = [...a1, 0, ...a2, 1];
  3219. assert(compareArray(a, [1, 2, 0, 3, 4, 1]));
  3220. `
  3221. testScriptWithTestLib(SCRIPT, _undefined, t)
  3222. }
  3223. func TestObjectAssignmentPattern(t *testing.T) {
  3224. const SCRIPT = `
  3225. let a, b, c;
  3226. ({a, b, c=3} = {a: 1, b: 2});
  3227. assert.sameValue(a, 1, "a");
  3228. assert.sameValue(b, 2, "b");
  3229. assert.sameValue(c, 3, "c");
  3230. `
  3231. testScriptWithTestLib(SCRIPT, _undefined, t)
  3232. }
  3233. func TestObjectAssignmentPatternNoDyn(t *testing.T) {
  3234. const SCRIPT = `
  3235. (function() {
  3236. let a, b, c;
  3237. ({a, b, c=3} = {a: 1, b: 2});
  3238. assert.sameValue(a, 1, "a");
  3239. assert.sameValue(b, 2, "b");
  3240. assert.sameValue(c, 3, "c");
  3241. })();
  3242. `
  3243. testScriptWithTestLib(SCRIPT, _undefined, t)
  3244. }
  3245. func TestObjectAssignmentPatternNested(t *testing.T) {
  3246. const SCRIPT = `
  3247. let a, b, c, d;
  3248. ({a, b, c: {d} = 3} = {a: 1, b: 2, c: {d: 4}});
  3249. assert.sameValue(a, 1, "a");
  3250. assert.sameValue(b, 2, "b");
  3251. assert.sameValue(c, undefined, "c");
  3252. assert.sameValue(d, 4, "d");
  3253. `
  3254. testScriptWithTestLib(SCRIPT, _undefined, t)
  3255. }
  3256. func TestObjectAssignmentPatternEvalOrder(t *testing.T) {
  3257. const SCRIPT = `
  3258. let trace = "";
  3259. let target_obj = {};
  3260. function src() {
  3261. trace += "src(),";
  3262. return {
  3263. get a() {
  3264. trace += "get a,";
  3265. return "a";
  3266. }
  3267. }
  3268. }
  3269. function prop1() {
  3270. trace += "prop1(),"
  3271. return {
  3272. toString: function() {
  3273. trace += "prop1-to-string(),";
  3274. return "a";
  3275. }
  3276. }
  3277. }
  3278. function prop2() {
  3279. trace += "prop2(),";
  3280. return {
  3281. toString: function() {
  3282. trace += "prop2-to-string(),";
  3283. return "b";
  3284. }
  3285. }
  3286. }
  3287. function target() {
  3288. trace += "target(),"
  3289. return target_obj;
  3290. }
  3291. let a, b;
  3292. ({[prop1()]: target().a, [prop2()]: b} = src());
  3293. if (target_obj.a !== "a") {
  3294. throw new Error("target_obj.a="+target_obj.a);
  3295. }
  3296. trace;
  3297. `
  3298. testScript(SCRIPT, asciiString("src(),prop1(),prop1-to-string(),target(),get a,prop2(),prop2-to-string(),"), t)
  3299. }
  3300. func TestArrayAssignmentPatternEvalOrder(t *testing.T) {
  3301. const SCRIPT = `
  3302. let trace = "";
  3303. let src_arr = {
  3304. [Symbol.iterator]: function() {
  3305. let done = false;
  3306. return {
  3307. next: function() {
  3308. trace += "next,";
  3309. if (!done) {
  3310. done = true;
  3311. return {value: 0};
  3312. }
  3313. return {done: true};
  3314. },
  3315. return: function() {
  3316. trace += "return,";
  3317. }
  3318. }
  3319. }
  3320. }
  3321. function src() {
  3322. trace += "src(),";
  3323. return src_arr;
  3324. }
  3325. let tgt = {
  3326. get a() {
  3327. trace += "get a,";
  3328. return "a";
  3329. },
  3330. get b() {
  3331. trace += "get b,";
  3332. return "b";
  3333. }
  3334. }
  3335. function target() {
  3336. trace += "target(),";
  3337. return tgt;
  3338. }
  3339. function default_a() {
  3340. trace += "default a,";
  3341. return "def_a";
  3342. }
  3343. function default_b() {
  3344. trace += "default b,";
  3345. return "def_b";
  3346. }
  3347. ([target().a = default_a(), target().b = default_b()] = src());
  3348. trace;
  3349. `
  3350. testScript(SCRIPT, asciiString("src(),target(),next,target(),next,default b,"), t)
  3351. }
  3352. func TestObjectAssignPatternRest(t *testing.T) {
  3353. const SCRIPT = `
  3354. let a, b, c, d;
  3355. ({a, b, c, ...d} = {a: 1, b: 2, d: 4});
  3356. assert.sameValue(a, 1, "a");
  3357. assert.sameValue(b, 2, "b");
  3358. assert.sameValue(c, undefined, "c");
  3359. assert(deepEqual(d, {d: 4}), "d");
  3360. `
  3361. testScriptWithTestLibX(SCRIPT, _undefined, t)
  3362. }
  3363. func TestObjectBindPattern(t *testing.T) {
  3364. const SCRIPT = `
  3365. let {a, b, c, ...d} = {a: 1, b: 2, d: 4};
  3366. assert.sameValue(a, 1, "a");
  3367. assert.sameValue(b, 2, "b");
  3368. assert.sameValue(c, undefined, "c");
  3369. assert(deepEqual(d, {d: 4}), "d");
  3370. var { x: y, } = { x: 23 };
  3371. assert.sameValue(y, 23);
  3372. assert.throws(ReferenceError, function() {
  3373. x;
  3374. });
  3375. `
  3376. testScriptWithTestLibX(SCRIPT, _undefined, t)
  3377. }
  3378. func TestObjLiteralShorthandWithInitializer(t *testing.T) {
  3379. const SCRIPT = `
  3380. o = {a=1};
  3381. `
  3382. _, err := Compile("", SCRIPT, false)
  3383. if err == nil {
  3384. t.Fatal("Expected an error")
  3385. }
  3386. }
  3387. func TestObjLiteralShorthandLetStringLit(t *testing.T) {
  3388. const SCRIPT = `
  3389. o = {"let"};
  3390. `
  3391. _, err := Compile("", SCRIPT, false)
  3392. if err == nil {
  3393. t.Fatal("Expected an error")
  3394. }
  3395. }
  3396. func TestObjLiteralComputedKeys(t *testing.T) {
  3397. const SCRIPT = `
  3398. let o = {
  3399. get [Symbol.toString]() {
  3400. }
  3401. }
  3402. `
  3403. testScript(SCRIPT, _undefined, t)
  3404. }
  3405. func TestObjLiteralComputedKeysEvalOrder(t *testing.T) {
  3406. const SCRIPT = `
  3407. let trace = [];
  3408. function key() {
  3409. trace.push("key");
  3410. return {
  3411. toString: function() {
  3412. trace.push("key-toString");
  3413. return "key";
  3414. }
  3415. }
  3416. }
  3417. function val() {
  3418. trace.push("val");
  3419. return "val";
  3420. }
  3421. const _ = {
  3422. [key()]: val(),
  3423. }
  3424. trace.join(",");
  3425. `
  3426. testScript(SCRIPT, asciiString("key,key-toString,val"), t)
  3427. }
  3428. func TestArrayAssignPattern(t *testing.T) {
  3429. const SCRIPT = `
  3430. let a, b;
  3431. ([a, b] = [1, 2]);
  3432. a === 1 && b === 2;
  3433. `
  3434. testScript(SCRIPT, valueTrue, t)
  3435. }
  3436. func TestArrayAssignPattern1(t *testing.T) {
  3437. const SCRIPT = `
  3438. let a, b;
  3439. ([a = 3, b = 2] = [1]);
  3440. a === 1 && b === 2;
  3441. `
  3442. testScript(SCRIPT, valueTrue, t)
  3443. }
  3444. func TestArrayAssignPatternLHS(t *testing.T) {
  3445. const SCRIPT = `
  3446. let a = {};
  3447. [ a.b, a['c'] = 2 ] = [1];
  3448. a.b === 1 && a.c === 2;
  3449. `
  3450. testScript(SCRIPT, valueTrue, t)
  3451. }
  3452. func TestArrayAssignPatternElision(t *testing.T) {
  3453. const SCRIPT = `
  3454. let a, b;
  3455. ([a,, b] = [1, 4, 2]);
  3456. a === 1 && b === 2;
  3457. `
  3458. testScript(SCRIPT, valueTrue, t)
  3459. }
  3460. func TestArrayAssignPatternRestPattern(t *testing.T) {
  3461. const SCRIPT = `
  3462. let a, b, z;
  3463. [ z, ...[a, b] ] = [0, 1, 2];
  3464. z === 0 && a === 1 && b === 2;
  3465. `
  3466. testScript(SCRIPT, valueTrue, t)
  3467. }
  3468. func TestArrayBindingPattern(t *testing.T) {
  3469. const SCRIPT = `
  3470. let [a, b] = [1, 2];
  3471. a === 1 && b === 2;
  3472. `
  3473. testScript(SCRIPT, valueTrue, t)
  3474. }
  3475. func TestObjectPatternShorthandInit(t *testing.T) {
  3476. const SCRIPT = `
  3477. [...{ x = 1 }] = [];
  3478. x;
  3479. `
  3480. testScript(SCRIPT, valueInt(1), t)
  3481. }
  3482. func TestArrayBindingPatternRestPattern(t *testing.T) {
  3483. const SCRIPT = `
  3484. const [a, b, ...[c, d]] = [1, 2, 3, 4];
  3485. a === 1 && b === 2 && c === 3 && d === 4;
  3486. `
  3487. testScript(SCRIPT, valueTrue, t)
  3488. }
  3489. func TestForVarPattern(t *testing.T) {
  3490. const SCRIPT = `
  3491. var o = {a: 1};
  3492. var trace = "";
  3493. for (var [key, value] of Object.entries(o)) {
  3494. trace += key+":"+value;
  3495. }
  3496. trace;
  3497. `
  3498. testScript(SCRIPT, asciiString("a:1"), t)
  3499. }
  3500. func TestForLexPattern(t *testing.T) {
  3501. const SCRIPT = `
  3502. var o = {a: 1};
  3503. var trace = "";
  3504. for (const [key, value] of Object.entries(o)) {
  3505. trace += key+":"+value;
  3506. }
  3507. trace;
  3508. `
  3509. testScript(SCRIPT, asciiString("a:1"), t)
  3510. }
  3511. func TestBindingPatternRestTrailingComma(t *testing.T) {
  3512. const SCRIPT = `
  3513. const [a, b, ...rest,] = [];
  3514. `
  3515. _, err := Compile("", SCRIPT, false)
  3516. if err == nil {
  3517. t.Fatal("Expected an error")
  3518. }
  3519. }
  3520. func TestAssignPatternRestTrailingComma(t *testing.T) {
  3521. const SCRIPT = `
  3522. ([a, b, ...rest,] = []);
  3523. `
  3524. _, err := Compile("", SCRIPT, false)
  3525. if err == nil {
  3526. t.Fatal("Expected an error")
  3527. }
  3528. }
  3529. func TestFuncParamInitializerSimple(t *testing.T) {
  3530. const SCRIPT = `
  3531. function f(a = 1) {
  3532. return a;
  3533. }
  3534. ""+f()+f(2);
  3535. `
  3536. testScript(SCRIPT, asciiString("12"), t)
  3537. }
  3538. func TestFuncParamObjectPatternSimple(t *testing.T) {
  3539. const SCRIPT = `
  3540. function f({a, b} = {a: 1, b: 2}) {
  3541. return "" + a + b;
  3542. }
  3543. ""+f()+" "+f({a: 3, b: 4});
  3544. `
  3545. testScript(SCRIPT, asciiString("12 34"), t)
  3546. }
  3547. func TestFuncParamRestStackSimple(t *testing.T) {
  3548. const SCRIPT = `
  3549. function f(arg1, ...rest) {
  3550. return rest;
  3551. }
  3552. let ar = f(1, 2, 3);
  3553. ar.join(",");
  3554. `
  3555. testScript(SCRIPT, asciiString("2,3"), t)
  3556. }
  3557. func TestFuncParamRestStashSimple(t *testing.T) {
  3558. const SCRIPT = `
  3559. function f(arg1, ...rest) {
  3560. eval("true");
  3561. return rest;
  3562. }
  3563. let ar = f(1, 2, 3);
  3564. ar.join(",");
  3565. `
  3566. testScript(SCRIPT, asciiString("2,3"), t)
  3567. }
  3568. func TestRestArgsNotInStash(t *testing.T) {
  3569. const SCRIPT = `
  3570. function f(...rest) {
  3571. () => rest;
  3572. return rest.length;
  3573. }
  3574. f(1,2);
  3575. `
  3576. testScript(SCRIPT, valueInt(2), t)
  3577. }
  3578. func TestRestArgsInStash(t *testing.T) {
  3579. const SCRIPT = `
  3580. function f(first, ...rest) {
  3581. () => first;
  3582. () => rest;
  3583. return rest.length;
  3584. }
  3585. f(1,2);
  3586. `
  3587. testScript(SCRIPT, valueInt(1), t)
  3588. }
  3589. func TestRestArgsInStashFwdRef(t *testing.T) {
  3590. const SCRIPT = `
  3591. function f(first = eval(), ...rest) {
  3592. () => first;
  3593. () => rest;
  3594. return rest.length === 1 && rest[0] === 2;
  3595. }
  3596. f(1,2);
  3597. `
  3598. testScript(SCRIPT, valueTrue, t)
  3599. }
  3600. func TestFuncParamRestPattern(t *testing.T) {
  3601. const SCRIPT = `
  3602. function f(arg1, ...{0: rest1, 1: rest2}) {
  3603. return ""+arg1+" "+rest1+" "+rest2;
  3604. }
  3605. f(1, 2, 3);
  3606. `
  3607. testScript(SCRIPT, asciiString("1 2 3"), t)
  3608. }
  3609. func TestFuncParamForwardRef(t *testing.T) {
  3610. const SCRIPT = `
  3611. function f(a = b + 1, b) {
  3612. return ""+a+" "+b;
  3613. }
  3614. f(1, 2);
  3615. `
  3616. testScript(SCRIPT, asciiString("1 2"), t)
  3617. }
  3618. func TestFuncParamForwardRefMissing(t *testing.T) {
  3619. const SCRIPT = `
  3620. function f(a = b + 1, b) {
  3621. return ""+a+" "+b;
  3622. }
  3623. assert.throws(ReferenceError, function() {
  3624. f();
  3625. });
  3626. `
  3627. testScriptWithTestLib(SCRIPT, _undefined, t)
  3628. }
  3629. func TestFuncParamInnerRef(t *testing.T) {
  3630. const SCRIPT = `
  3631. function f(a = inner) {
  3632. var inner = 42;
  3633. return a;
  3634. }
  3635. assert.throws(ReferenceError, function() {
  3636. f();
  3637. });
  3638. `
  3639. testScriptWithTestLib(SCRIPT, _undefined, t)
  3640. }
  3641. func TestFuncParamInnerRefEval(t *testing.T) {
  3642. const SCRIPT = `
  3643. function f(a = eval("inner")) {
  3644. var inner = 42;
  3645. return a;
  3646. }
  3647. assert.throws(ReferenceError, function() {
  3648. f();
  3649. });
  3650. `
  3651. testScriptWithTestLib(SCRIPT, _undefined, t)
  3652. }
  3653. func TestFuncParamCalleeName(t *testing.T) {
  3654. const SCRIPT = `
  3655. function f(a = f) {
  3656. var f;
  3657. return f;
  3658. }
  3659. typeof f();
  3660. `
  3661. testScript(SCRIPT, asciiString("undefined"), t)
  3662. }
  3663. func TestFuncParamVarCopy(t *testing.T) {
  3664. const SCRIPT = `
  3665. function f(a = f) {
  3666. var a;
  3667. return a;
  3668. }
  3669. typeof f();
  3670. `
  3671. testScript(SCRIPT, asciiString("function"), t)
  3672. }
  3673. func TestFuncParamScope(t *testing.T) {
  3674. const SCRIPT = `
  3675. var x = 'outside';
  3676. var probe1, probe2;
  3677. function f(
  3678. _ = probe1 = function() { return x; },
  3679. __ = (eval('var x = "inside";'), probe2 = function() { return x; })
  3680. ) {
  3681. }
  3682. f();
  3683. probe1()+" "+probe2();
  3684. `
  3685. testScript(SCRIPT, asciiString("inside inside"), t)
  3686. }
  3687. func TestDefParamsStackPtr(t *testing.T) {
  3688. const SCRIPT = `
  3689. function A() {};
  3690. A.B = function () {};
  3691. function D(message = '') {
  3692. var C = A.B;
  3693. C([1,2,3]);
  3694. };
  3695. D();
  3696. `
  3697. testScript(SCRIPT, _undefined, t)
  3698. }
  3699. func TestNestedVariadicCalls(t *testing.T) {
  3700. const SCRIPT = `
  3701. function f() {
  3702. return Array.prototype.join.call(arguments, ",");
  3703. }
  3704. f(...[1], "a", f(...[2]));
  3705. `
  3706. testScript(SCRIPT, asciiString("1,a,2"), t)
  3707. }
  3708. func TestVariadicNew(t *testing.T) {
  3709. const SCRIPT = `
  3710. function C() {
  3711. this.res = Array.prototype.join.call(arguments, ",");
  3712. }
  3713. var c = new C(...[1], "a", new C(...[2]).res);
  3714. c.res;
  3715. `
  3716. testScript(SCRIPT, asciiString("1,a,2"), t)
  3717. }
  3718. func TestVariadicUseStackVars(t *testing.T) {
  3719. const SCRIPT = `
  3720. function A(message) { return message; }
  3721. function B(...args){
  3722. return A(...args);
  3723. }
  3724. B("C");
  3725. `
  3726. testScript(SCRIPT, asciiString("C"), t)
  3727. }
  3728. func TestCatchParamPattern(t *testing.T) {
  3729. const SCRIPT = `
  3730. function f() {
  3731. let x = 3;
  3732. try {
  3733. throw {a: 1, b: 2};
  3734. } catch ({a, b, c = x}) {
  3735. let x = 99;
  3736. return ""+a+" "+b+" "+c;
  3737. }
  3738. }
  3739. f();
  3740. `
  3741. testScript(SCRIPT, asciiString("1 2 3"), t)
  3742. }
  3743. func TestArrowUseStrict(t *testing.T) {
  3744. // simple parameter list -- ok
  3745. _, err := Compile("", "(a) => {'use strict';}", false)
  3746. if err != nil {
  3747. t.Fatal(err)
  3748. }
  3749. // non-simple parameter list -- syntax error
  3750. _, err = Compile("", "(a=0) => {'use strict';}", false)
  3751. if err == nil {
  3752. t.Fatal("expected error")
  3753. }
  3754. }
  3755. func TestArrowBoxedThis(t *testing.T) {
  3756. const SCRIPT = `
  3757. var context;
  3758. fn = function() {
  3759. return (arg) => { var local; context = this; };
  3760. };
  3761. fn()();
  3762. context === this;
  3763. `
  3764. testScript(SCRIPT, valueTrue, t)
  3765. }
  3766. func TestParameterOverride(t *testing.T) {
  3767. const SCRIPT = `
  3768. function f(arg) {
  3769. var arg = arg || "default"
  3770. return arg
  3771. }
  3772. f()
  3773. `
  3774. testScript(SCRIPT, asciiString("default"), t)
  3775. }
  3776. func TestEvalInIterScope(t *testing.T) {
  3777. const SCRIPT = `
  3778. for (let a = 0; a < 1; a++) {
  3779. eval("a");
  3780. }
  3781. `
  3782. testScript(SCRIPT, valueInt(0), t)
  3783. }
  3784. func TestTemplateLiterals(t *testing.T) {
  3785. vm := New()
  3786. _, err := vm.RunString("const a = 1, b = 'b';")
  3787. if err != nil {
  3788. t.Fatal(err)
  3789. }
  3790. f := func(t *testing.T, template, expected string) {
  3791. res, err := vm.RunString(template)
  3792. if err != nil {
  3793. t.Fatal(err)
  3794. }
  3795. if actual := res.Export(); actual != expected {
  3796. t.Fatalf("Expected: %q, actual: %q", expected, actual)
  3797. }
  3798. }
  3799. t.Run("empty", func(t *testing.T) {
  3800. f(t, "``", "")
  3801. })
  3802. t.Run("noSub", func(t *testing.T) {
  3803. f(t, "`test`", "test")
  3804. })
  3805. t.Run("emptyTail", func(t *testing.T) {
  3806. f(t, "`a=${a},b=${b}`", "a=1,b=b")
  3807. })
  3808. t.Run("emptyHead", func(t *testing.T) {
  3809. f(t, "`${a},b=${b}$`", "1,b=b$")
  3810. })
  3811. t.Run("headAndTail", func(t *testing.T) {
  3812. f(t, "`a=${a},b=${b}$`", "a=1,b=b$")
  3813. })
  3814. }
  3815. func TestTaggedTemplate(t *testing.T) {
  3816. const SCRIPT = `
  3817. let res;
  3818. const o = {
  3819. tmpl() {
  3820. res = this;
  3821. return () => {};
  3822. }
  3823. }
  3824. ` +
  3825. "o.tmpl()`test`;" + `
  3826. res === o;
  3827. `
  3828. testScript(SCRIPT, valueTrue, t)
  3829. }
  3830. func TestDuplicateGlobalFunc(t *testing.T) {
  3831. const SCRIPT = `
  3832. function a(){}
  3833. function b(){ return "b" }
  3834. function c(){ return "c" }
  3835. function a(){}
  3836. b();
  3837. `
  3838. testScript(SCRIPT, asciiString("b"), t)
  3839. }
  3840. func TestDuplicateFunc(t *testing.T) {
  3841. const SCRIPT = `
  3842. function f() {
  3843. function a(){}
  3844. function b(){ return "b" }
  3845. function c(){ return "c" }
  3846. function a(){}
  3847. return b();
  3848. }
  3849. f();
  3850. `
  3851. testScript(SCRIPT, asciiString("b"), t)
  3852. }
  3853. func TestSrcLocations(t *testing.T) {
  3854. // Do not reformat, assertions depend on line and column numbers
  3855. const SCRIPT = `
  3856. let i = {
  3857. valueOf() {
  3858. throw new Error();
  3859. }
  3860. };
  3861. try {
  3862. i++;
  3863. } catch(e) {
  3864. assertStack(e, [["test.js", "valueOf", 4, 10],
  3865. ["test.js", "", 8, 3]
  3866. ]);
  3867. }
  3868. Object.defineProperty(globalThis, "x", {
  3869. get() {
  3870. throw new Error();
  3871. },
  3872. set() {
  3873. throw new Error();
  3874. }
  3875. });
  3876. try {
  3877. x;
  3878. } catch(e) {
  3879. assertStack(e, [["test.js", "get", 17, 10],
  3880. ["test.js", "", 25, 3]
  3881. ]);
  3882. }
  3883. try {
  3884. x++;
  3885. } catch(e) {
  3886. assertStack(e, [["test.js", "get", 17, 10],
  3887. ["test.js", "", 33, 3]
  3888. ]);
  3889. }
  3890. try {
  3891. x = 2;
  3892. } catch(e) {
  3893. assertStack(e, [["test.js", "set", 20, 10],
  3894. ["test.js", "", 41, 3]
  3895. ]);
  3896. }
  3897. try {
  3898. +i;
  3899. } catch(e) {
  3900. assertStack(e, [["test.js", "valueOf", 4, 10],
  3901. ["test.js", "", 49, 4]
  3902. ]);
  3903. }
  3904. function assertStack(e, expected) {
  3905. const lines = e.stack.split('\n');
  3906. let lnum = 1;
  3907. for (const [file, func, line, col] of expected) {
  3908. const expLine = func === "" ?
  3909. "\tat " + file + ":" + line + ":" + col + "(" :
  3910. "\tat " + func + " (" + file + ":" + line + ":" + col + "(";
  3911. assert.sameValue(lines[lnum].substring(0, expLine.length), expLine, "line " + lnum);
  3912. lnum++;
  3913. }
  3914. }
  3915. `
  3916. testScriptWithTestLib(SCRIPT, _undefined, t)
  3917. }
  3918. func TestSrcLocationThrowLiteral(t *testing.T) {
  3919. vm := New()
  3920. _, err := vm.RunString(`
  3921. const z = 1;
  3922. throw "";
  3923. `)
  3924. if ex, ok := err.(*Exception); ok {
  3925. pos := ex.stack[0].Position()
  3926. if pos.Line != 3 {
  3927. t.Fatal(pos)
  3928. }
  3929. } else {
  3930. t.Fatal(err)
  3931. }
  3932. }
  3933. func TestSrcLocation(t *testing.T) {
  3934. prg := MustCompile("test.js", `
  3935. f();
  3936. var x = 1;
  3937. let y = 1;
  3938. let [z1, z2] = [0, 0];
  3939. var [z3, z4] = [0, 0];
  3940. `, false)
  3941. const (
  3942. varLine = 3
  3943. letLine = 4
  3944. dstrLetLine = 5
  3945. dstrVarLine = 7
  3946. )
  3947. linesOfInterest := map[int]string{
  3948. varLine: "var",
  3949. letLine: "let",
  3950. dstrLetLine: "destruct let",
  3951. dstrVarLine: "destruct var",
  3952. }
  3953. for i := range prg.code {
  3954. loc := prg.src.Position(prg.sourceOffset(i))
  3955. delete(linesOfInterest, loc.Line)
  3956. if len(linesOfInterest) == 0 {
  3957. break
  3958. }
  3959. }
  3960. for _, v := range linesOfInterest {
  3961. t.Fatalf("no %s line", v)
  3962. }
  3963. }
  3964. func TestBadObjectKey(t *testing.T) {
  3965. _, err := Compile("", "({!:0})", false)
  3966. if err == nil {
  3967. t.Fatal("expected error")
  3968. }
  3969. }
  3970. func TestConstantFolding(t *testing.T) {
  3971. testValues := func(prg *Program, result Value, t *testing.T) {
  3972. if len(prg.values) != 1 || !prg.values[0].SameAs(result) {
  3973. prg.dumpCode(t.Logf)
  3974. t.Fatalf("values: %v", prg.values)
  3975. }
  3976. }
  3977. f := func(src string, result Value, t *testing.T) {
  3978. prg := MustCompile("test.js", src, false)
  3979. testValues(prg, result, t)
  3980. New().testPrg(prg, result, t)
  3981. }
  3982. ff := func(src string, result Value, t *testing.T) {
  3983. prg := MustCompile("test.js", src, false)
  3984. fl := prg.code[0].(*newFunc)
  3985. testValues(fl.prg, result, t)
  3986. New().testPrg(prg, result, t)
  3987. }
  3988. t.Run("lexical binding", func(t *testing.T) {
  3989. f("const x = 1 + 2; x", valueInt(3), t)
  3990. })
  3991. t.Run("var binding", func(t *testing.T) {
  3992. f("var x = 1 + 2; x", valueInt(3), t)
  3993. })
  3994. t.Run("assignment", func(t *testing.T) {
  3995. f("x = 1 + 2; x", valueInt(3), t)
  3996. })
  3997. t.Run("object pattern", func(t *testing.T) {
  3998. f("const {x = 1 + 2} = {}; x", valueInt(3), t)
  3999. })
  4000. t.Run("array pattern", func(t *testing.T) {
  4001. f("const [x = 1 + 2] = []; x", valueInt(3), t)
  4002. })
  4003. t.Run("object literal", func(t *testing.T) {
  4004. f("var o = {x: 1 + 2}; o.x", valueInt(3), t)
  4005. })
  4006. t.Run("array literal", func(t *testing.T) {
  4007. f("var a = [3, 3, 3, 1 + 2]; a[3]", valueInt(3), t)
  4008. })
  4009. t.Run("default function parameter", func(t *testing.T) {
  4010. ff("function f(arg = 1 + 2) {return arg}; f()", valueInt(3), t)
  4011. })
  4012. t.Run("return", func(t *testing.T) {
  4013. ff("function f() {return 1 + 2}; f()", valueInt(3), t)
  4014. })
  4015. }
  4016. func TestAssignBeforeInit(t *testing.T) {
  4017. const SCRIPT = `
  4018. assert.throws(ReferenceError, () => {
  4019. a = 1;
  4020. let a;
  4021. });
  4022. assert.throws(ReferenceError, () => {
  4023. ({a, b} = {a: 1, b: 2});
  4024. let a, b;
  4025. });
  4026. assert.throws(ReferenceError, () => {
  4027. (function() {
  4028. eval("");
  4029. ({a} = {a: 1});
  4030. })();
  4031. let a;
  4032. });
  4033. assert.throws(ReferenceError, () => {
  4034. const ctx = {x: 1};
  4035. function t() {
  4036. delete ctx.x;
  4037. return 42;
  4038. }
  4039. with(ctx) {
  4040. (function() {
  4041. 'use strict';
  4042. ({x} = {x: t()});
  4043. })();
  4044. }
  4045. return ctx.x;
  4046. });
  4047. assert.throws(ReferenceError, () => {
  4048. const ctx = {x: 1};
  4049. function t() {
  4050. delete ctx.x;
  4051. return 42;
  4052. }
  4053. with(ctx) {
  4054. (function() {
  4055. 'use strict';
  4056. const src = {};
  4057. Object.defineProperty(src, "x", {
  4058. get() {
  4059. return t();
  4060. }
  4061. });
  4062. ({x} = src);
  4063. })();
  4064. }
  4065. return ctx.x;
  4066. });
  4067. `
  4068. testScriptWithTestLib(SCRIPT, _undefined, t)
  4069. }
  4070. /*
  4071. func TestBabel(t *testing.T) {
  4072. src, err := ioutil.ReadFile("babel7.js")
  4073. if err != nil {
  4074. t.Fatal(err)
  4075. }
  4076. vm := New()
  4077. _, err = vm.RunString(string(src))
  4078. if err != nil {
  4079. t.Fatal(err)
  4080. }
  4081. _, err = vm.RunString(`var result = Babel.transform("", {presets: ["es2015"]});`)
  4082. if err != nil {
  4083. t.Fatal(err)
  4084. }
  4085. }*/
  4086. func BenchmarkCompile(b *testing.B) {
  4087. data, err := ioutil.ReadFile("testdata/S15.10.2.12_A1_T1.js")
  4088. if err != nil {
  4089. b.Fatal(err)
  4090. }
  4091. src := string(data)
  4092. for i := 0; i < b.N; i++ {
  4093. _, err := Compile("test.js", src, false)
  4094. if err != nil {
  4095. b.Fatal(err)
  4096. }
  4097. }
  4098. }