compiler_test.go 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549
  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 TestNonStrictLet(t *testing.T) {
  2845. const SCRIPT = `
  2846. var let = 1;
  2847. `
  2848. testScript(SCRIPT, _undefined, t)
  2849. }
  2850. func TestStrictLet(t *testing.T) {
  2851. const SCRIPT = `
  2852. var let = 1;
  2853. `
  2854. _, err := Compile("", SCRIPT, true)
  2855. if err == nil {
  2856. t.Fatal("Expected an error")
  2857. }
  2858. }
  2859. func TestLetLet(t *testing.T) {
  2860. const SCRIPT = `
  2861. let let = 1;
  2862. `
  2863. _, err := Compile("", SCRIPT, false)
  2864. if err == nil {
  2865. t.Fatal("Expected an error")
  2866. }
  2867. }
  2868. func TestLetASI(t *testing.T) {
  2869. const SCRIPT = `
  2870. while (false) let // ASI
  2871. x = 1;
  2872. `
  2873. _, err := Compile("", SCRIPT, false)
  2874. if err != nil {
  2875. t.Fatal(err)
  2876. }
  2877. }
  2878. func TestLetASI1(t *testing.T) {
  2879. const SCRIPT = `
  2880. let
  2881. x = 1;
  2882. `
  2883. _, err := Compile("", SCRIPT, true)
  2884. if err != nil {
  2885. t.Fatal(err)
  2886. }
  2887. }
  2888. func TestLetNoASI(t *testing.T) {
  2889. const SCRIPT = `
  2890. function f() {}let
  2891. x = 1;
  2892. `
  2893. _, err := Compile("", SCRIPT, true)
  2894. if err != nil {
  2895. t.Fatal(err)
  2896. }
  2897. }
  2898. func TestLetNoASI1(t *testing.T) {
  2899. const SCRIPT = `
  2900. let
  2901. let = 1;
  2902. `
  2903. _, err := Compile("", SCRIPT, false)
  2904. if err == nil {
  2905. t.Fatal("Expected error")
  2906. }
  2907. }
  2908. func TestLetArrayWithNewline(t *testing.T) {
  2909. const SCRIPT = `
  2910. with ({}) let
  2911. [a] = 0;
  2912. `
  2913. _, err := Compile("", SCRIPT, false)
  2914. if err == nil {
  2915. t.Fatal("Expected error")
  2916. }
  2917. }
  2918. func TestDynamicUninitedVarAccess(t *testing.T) {
  2919. const SCRIPT = `
  2920. function f() {
  2921. var x;
  2922. return eval("x");
  2923. }
  2924. f();
  2925. `
  2926. testScript(SCRIPT, _undefined, t)
  2927. }
  2928. func TestLexicalForLoopNoClosure(t *testing.T) {
  2929. const SCRIPT = `
  2930. let sum = 0;
  2931. for (let i = 0; i < 3; i++) {
  2932. sum += i;
  2933. }
  2934. sum;
  2935. `
  2936. testScript(SCRIPT, valueInt(3), t)
  2937. }
  2938. func TestLexicalForLoopClosure(t *testing.T) {
  2939. const SCRIPT = `
  2940. var f = [];
  2941. for (let i = 0; i < 3; i++) {
  2942. f.push(function() {
  2943. return i;
  2944. });
  2945. }
  2946. f.length === 3 && f[0]() === 0 && f[1]() === 1 && f[2]() === 2;
  2947. `
  2948. testScript(SCRIPT, valueTrue, t)
  2949. }
  2950. func TestLexicalForLoopClosureInNext(t *testing.T) {
  2951. const SCRIPT = `
  2952. const a = [];
  2953. for (let i = 0; i < 5; a.push(function () { return i; }), ++i) { }
  2954. let res = "";
  2955. for (let k = 0; k < 5; ++k) {
  2956. res += ""+a[k]();
  2957. }
  2958. res;
  2959. `
  2960. testScript(SCRIPT, asciiString("12345"), t)
  2961. }
  2962. func TestVarForLoop(t *testing.T) {
  2963. const SCRIPT = `
  2964. var f = [];
  2965. for (var i = 0, j = 0; i < 3; i++) {
  2966. f.push(function() {
  2967. return i;
  2968. });
  2969. }
  2970. f.length === 3 && f[0]() === 3 && f[1]() === 3 && f[2]() === 3;
  2971. `
  2972. testScript(SCRIPT, valueTrue, t)
  2973. }
  2974. func TestLexicalForOfLoop(t *testing.T) {
  2975. const SCRIPT = `
  2976. var f = [];
  2977. for (let i of [0, 1, 2]) {
  2978. f.push(function() {
  2979. return i;
  2980. });
  2981. }
  2982. f.length === 3 && f[0]() === 0 && f[1]() === 1 && f[2]() === 2;
  2983. `
  2984. testScript(SCRIPT, valueTrue, t)
  2985. }
  2986. func TestLexicalForOfLoopContBreak(t *testing.T) {
  2987. const SCRIPT = `
  2988. const f = [];
  2989. for (let i of [0, 1, 2, 3, 4, 5]) {
  2990. if (i % 2) continue;
  2991. f.push(function() {
  2992. return i;
  2993. });
  2994. if (i > 2) break;
  2995. }
  2996. let res = "";
  2997. f.forEach(function(item) {res += item()});
  2998. f.length === 3 && res === "024";
  2999. `
  3000. testScript(SCRIPT, valueTrue, t)
  3001. }
  3002. func TestVarBlockConflict(t *testing.T) {
  3003. const SCRIPT = `
  3004. let x;
  3005. {
  3006. if (false) {
  3007. var x;
  3008. }
  3009. }
  3010. `
  3011. _, err := Compile("", SCRIPT, false)
  3012. if err == nil {
  3013. t.Fatal("Expected an error")
  3014. }
  3015. }
  3016. func TestVarBlockConflictEval(t *testing.T) {
  3017. const SCRIPT = `
  3018. assert.throws(SyntaxError, function() {
  3019. let x;
  3020. {
  3021. if (true) {
  3022. eval("var x");
  3023. }
  3024. }
  3025. });
  3026. `
  3027. testScriptWithTestLib(SCRIPT, _undefined, t)
  3028. }
  3029. func TestVarBlockNoConflict(t *testing.T) {
  3030. const SCRIPT = `
  3031. function f() {
  3032. let x;
  3033. function ff() {
  3034. {
  3035. var x = 3;
  3036. }
  3037. }
  3038. ff();
  3039. }
  3040. f();
  3041. `
  3042. testScript(SCRIPT, _undefined, t)
  3043. }
  3044. func TestVarBlockNoConflictEval(t *testing.T) {
  3045. const SCRIPT = `
  3046. function f() {
  3047. let x;
  3048. function ff() {
  3049. {
  3050. eval("var x = 3");
  3051. }
  3052. }
  3053. ff();
  3054. }
  3055. f();
  3056. `
  3057. testScript(SCRIPT, _undefined, t)
  3058. }
  3059. func TestVarDeclCorrectScope(t *testing.T) {
  3060. const SCRIPT = `
  3061. function f() {
  3062. {
  3063. let z;
  3064. eval("var x = 3");
  3065. }
  3066. return x;
  3067. }
  3068. f();
  3069. `
  3070. testScript(SCRIPT, valueInt(3), t)
  3071. }
  3072. func TestLexicalCatch(t *testing.T) {
  3073. const SCRIPT = `
  3074. try {
  3075. throw null;
  3076. } catch (e) {
  3077. let x = 1;
  3078. function f() {}
  3079. e;
  3080. }
  3081. `
  3082. testScript(SCRIPT, _null, t)
  3083. }
  3084. func TestArgumentsLexicalDecl(t *testing.T) {
  3085. const SCRIPT = `
  3086. function f1() {
  3087. let arguments;
  3088. return arguments;
  3089. }
  3090. f1(42);
  3091. `
  3092. testScript(SCRIPT, _undefined, t)
  3093. }
  3094. func TestArgumentsLexicalDeclAssign(t *testing.T) {
  3095. const SCRIPT = `
  3096. function f1() {
  3097. let arguments = arguments;
  3098. return a;
  3099. }
  3100. assert.throws(ReferenceError, function() {
  3101. f1(42);
  3102. });
  3103. `
  3104. testScriptWithTestLib(SCRIPT, _undefined, t)
  3105. }
  3106. func TestLexicalConstModifyFromEval(t *testing.T) {
  3107. const SCRIPT = `
  3108. const x = 1;
  3109. function f() {
  3110. eval("x = 2");
  3111. }
  3112. assert.throws(TypeError, function() {
  3113. f();
  3114. });
  3115. `
  3116. testScriptWithTestLib(SCRIPT, _undefined, t)
  3117. }
  3118. func TestLexicalStrictNames(t *testing.T) {
  3119. const SCRIPT = `let eval = 1;`
  3120. _, err := Compile("", SCRIPT, true)
  3121. if err == nil {
  3122. t.Fatal("Expected an error")
  3123. }
  3124. }
  3125. func TestAssignAfterStackExpand(t *testing.T) {
  3126. // make sure the reference to the variable x does not remain stale after the stack is copied
  3127. const SCRIPT = `
  3128. function f() {
  3129. let sum = 0;
  3130. for (let i = 0; i < arguments.length; i++) {
  3131. sum += arguments[i];
  3132. }
  3133. return sum;
  3134. }
  3135. function testAssignment() {
  3136. var x = 0;
  3137. var scope = {};
  3138. with (scope) {
  3139. x = (scope.x = f(0, 0, 0, 0, 0, 0, 1, 1), 1);
  3140. }
  3141. if (scope.x !== 2) {
  3142. throw new Error('#1: scope.x === 2. Actual: ' + (scope.x));
  3143. }
  3144. if (x !== 1) {
  3145. throw new Error('#2: x === 1. Actual: ' + (x));
  3146. }
  3147. }
  3148. testAssignment();
  3149. `
  3150. testScript(SCRIPT, _undefined, t)
  3151. }
  3152. func TestArgAccessFromDynamicStash(t *testing.T) {
  3153. const SCRIPT = `
  3154. function f(arg) {
  3155. function test() {
  3156. eval("");
  3157. return a;
  3158. }
  3159. return arg;
  3160. }
  3161. f(true);
  3162. `
  3163. testScript(SCRIPT, valueTrue, t)
  3164. }
  3165. func TestLoadMixedLex(t *testing.T) {
  3166. const SCRIPT = `
  3167. function f() {
  3168. let a = 1;
  3169. {
  3170. function inner() {
  3171. eval("var a = true");
  3172. return a;
  3173. }
  3174. return inner();
  3175. }
  3176. }
  3177. f();
  3178. `
  3179. testScript(SCRIPT, valueTrue, t)
  3180. }
  3181. func TestObjectLiteralSpread(t *testing.T) {
  3182. const SCRIPT = `
  3183. let src = {prop1: 1};
  3184. Object.defineProperty(src, "prop2", {value: 2, configurable: true});
  3185. Object.defineProperty(src, "prop3", {value: 3, enumerable: true, configurable: true});
  3186. let target = {prop4: 4, ...src};
  3187. assert(deepEqual(target, {prop1: 1, prop3: 3, prop4: 4}));
  3188. `
  3189. testScriptWithTestLibX(SCRIPT, _undefined, t)
  3190. }
  3191. func TestArrayLiteralSpread(t *testing.T) {
  3192. const SCRIPT = `
  3193. let a1 = [1, 2];
  3194. let a2 = [3, 4];
  3195. let a = [...a1, 0, ...a2, 1];
  3196. assert(compareArray(a, [1, 2, 0, 3, 4, 1]));
  3197. `
  3198. testScriptWithTestLib(SCRIPT, _undefined, t)
  3199. }
  3200. func TestObjectAssignmentPattern(t *testing.T) {
  3201. const SCRIPT = `
  3202. let a, b, c;
  3203. ({a, b, c=3} = {a: 1, b: 2});
  3204. assert.sameValue(a, 1, "a");
  3205. assert.sameValue(b, 2, "b");
  3206. assert.sameValue(c, 3, "c");
  3207. `
  3208. testScriptWithTestLib(SCRIPT, _undefined, t)
  3209. }
  3210. func TestObjectAssignmentPatternNested(t *testing.T) {
  3211. const SCRIPT = `
  3212. let a, b, c, d;
  3213. ({a, b, c: {d} = 3} = {a: 1, b: 2, c: {d: 4}});
  3214. assert.sameValue(a, 1, "a");
  3215. assert.sameValue(b, 2, "b");
  3216. assert.sameValue(c, undefined, "c");
  3217. assert.sameValue(d, 4, "d");
  3218. `
  3219. testScriptWithTestLib(SCRIPT, _undefined, t)
  3220. }
  3221. func TestObjectAssignmentPatternEvalOrder(t *testing.T) {
  3222. const SCRIPT = `
  3223. let trace = "";
  3224. let target_obj = {};
  3225. function src() {
  3226. trace += "src(),";
  3227. return {
  3228. get a() {
  3229. trace += "get a,";
  3230. return "a";
  3231. }
  3232. }
  3233. }
  3234. function prop1() {
  3235. trace += "prop1(),"
  3236. return {
  3237. toString: function() {
  3238. trace += "prop1-to-string(),";
  3239. return "a";
  3240. }
  3241. }
  3242. }
  3243. function prop2() {
  3244. trace += "prop2(),";
  3245. return {
  3246. toString: function() {
  3247. trace += "prop2-to-string(),";
  3248. return "b";
  3249. }
  3250. }
  3251. }
  3252. function target() {
  3253. trace += "target(),"
  3254. return target_obj;
  3255. }
  3256. let a, b;
  3257. ({[prop1()]: target().a, [prop2()]: b} = src());
  3258. if (target_obj.a !== "a") {
  3259. throw new Error("target_obj.a="+target_obj.a);
  3260. }
  3261. trace;
  3262. `
  3263. testScript(SCRIPT, asciiString("src(),prop1(),prop1-to-string(),target(),get a,prop2(),prop2-to-string(),"), t)
  3264. }
  3265. func TestArrayAssignmentPatternEvalOrder(t *testing.T) {
  3266. const SCRIPT = `
  3267. let trace = "";
  3268. let src_arr = {
  3269. [Symbol.iterator]: function() {
  3270. let done = false;
  3271. return {
  3272. next: function() {
  3273. trace += "next,";
  3274. if (!done) {
  3275. done = true;
  3276. return {value: 0};
  3277. }
  3278. return {done: true};
  3279. },
  3280. return: function() {
  3281. trace += "return,";
  3282. }
  3283. }
  3284. }
  3285. }
  3286. function src() {
  3287. trace += "src(),";
  3288. return src_arr;
  3289. }
  3290. let tgt = {
  3291. get a() {
  3292. trace += "get a,";
  3293. return "a";
  3294. },
  3295. get b() {
  3296. trace += "get b,";
  3297. return "b";
  3298. }
  3299. }
  3300. function target() {
  3301. trace += "target(),";
  3302. return tgt;
  3303. }
  3304. function default_a() {
  3305. trace += "default a,";
  3306. return "def_a";
  3307. }
  3308. function default_b() {
  3309. trace += "default b,";
  3310. return "def_b";
  3311. }
  3312. ([target().a = default_a(), target().b = default_b()] = src());
  3313. trace;
  3314. `
  3315. testScript(SCRIPT, asciiString("src(),target(),next,target(),next,default b,"), t)
  3316. }
  3317. func TestObjectAssignPatternRest(t *testing.T) {
  3318. const SCRIPT = `
  3319. let a, b, c, d;
  3320. ({a, b, c, ...d} = {a: 1, b: 2, d: 4});
  3321. assert.sameValue(a, 1, "a");
  3322. assert.sameValue(b, 2, "b");
  3323. assert.sameValue(c, undefined, "c");
  3324. assert(deepEqual(d, {d: 4}), "d");
  3325. `
  3326. testScriptWithTestLibX(SCRIPT, _undefined, t)
  3327. }
  3328. func TestObjectBindPattern(t *testing.T) {
  3329. const SCRIPT = `
  3330. let {a, b, c, ...d} = {a: 1, b: 2, d: 4};
  3331. assert.sameValue(a, 1, "a");
  3332. assert.sameValue(b, 2, "b");
  3333. assert.sameValue(c, undefined, "c");
  3334. assert(deepEqual(d, {d: 4}), "d");
  3335. var { x: y, } = { x: 23 };
  3336. assert.sameValue(y, 23);
  3337. assert.throws(ReferenceError, function() {
  3338. x;
  3339. });
  3340. `
  3341. testScriptWithTestLibX(SCRIPT, _undefined, t)
  3342. }
  3343. func TestObjLiteralShorthandWithInitializer(t *testing.T) {
  3344. const SCRIPT = `
  3345. o = {a=1};
  3346. `
  3347. _, err := Compile("", SCRIPT, false)
  3348. if err == nil {
  3349. t.Fatal("Expected an error")
  3350. }
  3351. }
  3352. func TestObjLiteralShorthandLetStringLit(t *testing.T) {
  3353. const SCRIPT = `
  3354. o = {"let"};
  3355. `
  3356. _, err := Compile("", SCRIPT, false)
  3357. if err == nil {
  3358. t.Fatal("Expected an error")
  3359. }
  3360. }
  3361. func TestObjLiteralComputedKeys(t *testing.T) {
  3362. const SCRIPT = `
  3363. let o = {
  3364. get [Symbol.toString]() {
  3365. }
  3366. }
  3367. `
  3368. testScript(SCRIPT, _undefined, t)
  3369. }
  3370. func TestObjLiteralComputedKeysEvalOrder(t *testing.T) {
  3371. const SCRIPT = `
  3372. let trace = [];
  3373. function key() {
  3374. trace.push("key");
  3375. return {
  3376. toString: function() {
  3377. trace.push("key-toString");
  3378. return "key";
  3379. }
  3380. }
  3381. }
  3382. function val() {
  3383. trace.push("val");
  3384. return "val";
  3385. }
  3386. const _ = {
  3387. [key()]: val(),
  3388. }
  3389. trace.join(",");
  3390. `
  3391. testScript(SCRIPT, asciiString("key,key-toString,val"), t)
  3392. }
  3393. func TestArrayAssignPattern(t *testing.T) {
  3394. const SCRIPT = `
  3395. let a, b;
  3396. ([a, b] = [1, 2]);
  3397. a === 1 && b === 2;
  3398. `
  3399. testScript(SCRIPT, valueTrue, t)
  3400. }
  3401. func TestArrayAssignPattern1(t *testing.T) {
  3402. const SCRIPT = `
  3403. let a, b;
  3404. ([a = 3, b = 2] = [1]);
  3405. a === 1 && b === 2;
  3406. `
  3407. testScript(SCRIPT, valueTrue, t)
  3408. }
  3409. func TestArrayAssignPatternLHS(t *testing.T) {
  3410. const SCRIPT = `
  3411. let a = {};
  3412. [ a.b, a['c'] = 2 ] = [1];
  3413. a.b === 1 && a.c === 2;
  3414. `
  3415. testScript(SCRIPT, valueTrue, t)
  3416. }
  3417. func TestArrayAssignPatternElision(t *testing.T) {
  3418. const SCRIPT = `
  3419. let a, b;
  3420. ([a,, b] = [1, 4, 2]);
  3421. a === 1 && b === 2;
  3422. `
  3423. testScript(SCRIPT, valueTrue, t)
  3424. }
  3425. func TestArrayAssignPatternRestPattern(t *testing.T) {
  3426. const SCRIPT = `
  3427. let a, b, z;
  3428. [ z, ...[a, b] ] = [0, 1, 2];
  3429. z === 0 && a === 1 && b === 2;
  3430. `
  3431. testScript(SCRIPT, valueTrue, t)
  3432. }
  3433. func TestArrayBindingPattern(t *testing.T) {
  3434. const SCRIPT = `
  3435. let [a, b] = [1, 2];
  3436. a === 1 && b === 2;
  3437. `
  3438. testScript(SCRIPT, valueTrue, t)
  3439. }
  3440. func TestObjectPatternShorthandInit(t *testing.T) {
  3441. const SCRIPT = `
  3442. [...{ x = 1 }] = [];
  3443. x;
  3444. `
  3445. testScript(SCRIPT, valueInt(1), t)
  3446. }
  3447. func TestArrayBindingPatternRestPattern(t *testing.T) {
  3448. const SCRIPT = `
  3449. const [a, b, ...[c, d]] = [1, 2, 3, 4];
  3450. a === 1 && b === 2 && c === 3 && d === 4;
  3451. `
  3452. testScript(SCRIPT, valueTrue, t)
  3453. }
  3454. func TestForVarPattern(t *testing.T) {
  3455. const SCRIPT = `
  3456. var o = {a: 1};
  3457. var trace = "";
  3458. for (var [key, value] of Object.entries(o)) {
  3459. trace += key+":"+value;
  3460. }
  3461. trace;
  3462. `
  3463. testScript(SCRIPT, asciiString("a:1"), t)
  3464. }
  3465. func TestForLexPattern(t *testing.T) {
  3466. const SCRIPT = `
  3467. var o = {a: 1};
  3468. var trace = "";
  3469. for (const [key, value] of Object.entries(o)) {
  3470. trace += key+":"+value;
  3471. }
  3472. trace;
  3473. `
  3474. testScript(SCRIPT, asciiString("a:1"), t)
  3475. }
  3476. func TestBindingPatternRestTrailingComma(t *testing.T) {
  3477. const SCRIPT = `
  3478. const [a, b, ...rest,] = [];
  3479. `
  3480. _, err := Compile("", SCRIPT, false)
  3481. if err == nil {
  3482. t.Fatal("Expected an error")
  3483. }
  3484. }
  3485. func TestAssignPatternRestTrailingComma(t *testing.T) {
  3486. const SCRIPT = `
  3487. ([a, b, ...rest,] = []);
  3488. `
  3489. _, err := Compile("", SCRIPT, false)
  3490. if err == nil {
  3491. t.Fatal("Expected an error")
  3492. }
  3493. }
  3494. func TestFuncParamInitializerSimple(t *testing.T) {
  3495. const SCRIPT = `
  3496. function f(a = 1) {
  3497. return a;
  3498. }
  3499. ""+f()+f(2);
  3500. `
  3501. testScript(SCRIPT, asciiString("12"), t)
  3502. }
  3503. func TestFuncParamObjectPatternSimple(t *testing.T) {
  3504. const SCRIPT = `
  3505. function f({a, b} = {a: 1, b: 2}) {
  3506. return "" + a + b;
  3507. }
  3508. ""+f()+" "+f({a: 3, b: 4});
  3509. `
  3510. testScript(SCRIPT, asciiString("12 34"), t)
  3511. }
  3512. func TestFuncParamRestStackSimple(t *testing.T) {
  3513. const SCRIPT = `
  3514. function f(arg1, ...rest) {
  3515. return rest;
  3516. }
  3517. let ar = f(1, 2, 3);
  3518. ar.join(",");
  3519. `
  3520. testScript(SCRIPT, asciiString("2,3"), t)
  3521. }
  3522. func TestFuncParamRestStashSimple(t *testing.T) {
  3523. const SCRIPT = `
  3524. function f(arg1, ...rest) {
  3525. eval("true");
  3526. return rest;
  3527. }
  3528. let ar = f(1, 2, 3);
  3529. ar.join(",");
  3530. `
  3531. testScript(SCRIPT, asciiString("2,3"), t)
  3532. }
  3533. func TestRestArgsNotInStash(t *testing.T) {
  3534. const SCRIPT = `
  3535. function f(...rest) {
  3536. () => rest;
  3537. return rest.length;
  3538. }
  3539. f(1,2);
  3540. `
  3541. testScript(SCRIPT, valueInt(2), t)
  3542. }
  3543. func TestRestArgsInStash(t *testing.T) {
  3544. const SCRIPT = `
  3545. function f(first, ...rest) {
  3546. () => first;
  3547. () => rest;
  3548. return rest.length;
  3549. }
  3550. f(1,2);
  3551. `
  3552. testScript(SCRIPT, valueInt(1), t)
  3553. }
  3554. func TestRestArgsInStashFwdRef(t *testing.T) {
  3555. const SCRIPT = `
  3556. function f(first = eval(), ...rest) {
  3557. () => first;
  3558. () => rest;
  3559. return rest.length === 1 && rest[0] === 2;
  3560. }
  3561. f(1,2);
  3562. `
  3563. testScript(SCRIPT, valueTrue, t)
  3564. }
  3565. func TestFuncParamRestPattern(t *testing.T) {
  3566. const SCRIPT = `
  3567. function f(arg1, ...{0: rest1, 1: rest2}) {
  3568. return ""+arg1+" "+rest1+" "+rest2;
  3569. }
  3570. f(1, 2, 3);
  3571. `
  3572. testScript(SCRIPT, asciiString("1 2 3"), t)
  3573. }
  3574. func TestFuncParamForwardRef(t *testing.T) {
  3575. const SCRIPT = `
  3576. function f(a = b + 1, b) {
  3577. return ""+a+" "+b;
  3578. }
  3579. f(1, 2);
  3580. `
  3581. testScript(SCRIPT, asciiString("1 2"), t)
  3582. }
  3583. func TestFuncParamForwardRefMissing(t *testing.T) {
  3584. const SCRIPT = `
  3585. function f(a = b + 1, b) {
  3586. return ""+a+" "+b;
  3587. }
  3588. assert.throws(ReferenceError, function() {
  3589. f();
  3590. });
  3591. `
  3592. testScriptWithTestLib(SCRIPT, _undefined, t)
  3593. }
  3594. func TestFuncParamInnerRef(t *testing.T) {
  3595. const SCRIPT = `
  3596. function f(a = inner) {
  3597. var inner = 42;
  3598. return a;
  3599. }
  3600. assert.throws(ReferenceError, function() {
  3601. f();
  3602. });
  3603. `
  3604. testScriptWithTestLib(SCRIPT, _undefined, t)
  3605. }
  3606. func TestFuncParamInnerRefEval(t *testing.T) {
  3607. const SCRIPT = `
  3608. function f(a = eval("inner")) {
  3609. var inner = 42;
  3610. return a;
  3611. }
  3612. assert.throws(ReferenceError, function() {
  3613. f();
  3614. });
  3615. `
  3616. testScriptWithTestLib(SCRIPT, _undefined, t)
  3617. }
  3618. func TestFuncParamCalleeName(t *testing.T) {
  3619. const SCRIPT = `
  3620. function f(a = f) {
  3621. var f;
  3622. return f;
  3623. }
  3624. typeof f();
  3625. `
  3626. testScript(SCRIPT, asciiString("undefined"), t)
  3627. }
  3628. func TestFuncParamVarCopy(t *testing.T) {
  3629. const SCRIPT = `
  3630. function f(a = f) {
  3631. var a;
  3632. return a;
  3633. }
  3634. typeof f();
  3635. `
  3636. testScript(SCRIPT, asciiString("function"), t)
  3637. }
  3638. func TestFuncParamScope(t *testing.T) {
  3639. const SCRIPT = `
  3640. var x = 'outside';
  3641. var probe1, probe2;
  3642. function f(
  3643. _ = probe1 = function() { return x; },
  3644. __ = (eval('var x = "inside";'), probe2 = function() { return x; })
  3645. ) {
  3646. }
  3647. f();
  3648. probe1()+" "+probe2();
  3649. `
  3650. testScript(SCRIPT, asciiString("inside inside"), t)
  3651. }
  3652. func TestDefParamsStackPtr(t *testing.T) {
  3653. const SCRIPT = `
  3654. function A() {};
  3655. A.B = function () {};
  3656. function D(message = '') {
  3657. var C = A.B;
  3658. C([1,2,3]);
  3659. };
  3660. D();
  3661. `
  3662. testScript(SCRIPT, _undefined, t)
  3663. }
  3664. func TestNestedVariadicCalls(t *testing.T) {
  3665. const SCRIPT = `
  3666. function f() {
  3667. return Array.prototype.join.call(arguments, ",");
  3668. }
  3669. f(...[1], "a", f(...[2]));
  3670. `
  3671. testScript(SCRIPT, asciiString("1,a,2"), t)
  3672. }
  3673. func TestVariadicNew(t *testing.T) {
  3674. const SCRIPT = `
  3675. function C() {
  3676. this.res = Array.prototype.join.call(arguments, ",");
  3677. }
  3678. var c = new C(...[1], "a", new C(...[2]).res);
  3679. c.res;
  3680. `
  3681. testScript(SCRIPT, asciiString("1,a,2"), t)
  3682. }
  3683. func TestVariadicUseStackVars(t *testing.T) {
  3684. const SCRIPT = `
  3685. function A(message) { return message; }
  3686. function B(...args){
  3687. return A(...args);
  3688. }
  3689. B("C");
  3690. `
  3691. testScript(SCRIPT, asciiString("C"), t)
  3692. }
  3693. func TestCatchParamPattern(t *testing.T) {
  3694. const SCRIPT = `
  3695. function f() {
  3696. let x = 3;
  3697. try {
  3698. throw {a: 1, b: 2};
  3699. } catch ({a, b, c = x}) {
  3700. let x = 99;
  3701. return ""+a+" "+b+" "+c;
  3702. }
  3703. }
  3704. f();
  3705. `
  3706. testScript(SCRIPT, asciiString("1 2 3"), t)
  3707. }
  3708. func TestArrowUseStrict(t *testing.T) {
  3709. // simple parameter list -- ok
  3710. _, err := Compile("", "(a) => {'use strict';}", false)
  3711. if err != nil {
  3712. t.Fatal(err)
  3713. }
  3714. // non-simple parameter list -- syntax error
  3715. _, err = Compile("", "(a=0) => {'use strict';}", false)
  3716. if err == nil {
  3717. t.Fatal("expected error")
  3718. }
  3719. }
  3720. func TestArrowBoxedThis(t *testing.T) {
  3721. const SCRIPT = `
  3722. var context;
  3723. fn = function() {
  3724. return (arg) => { var local; context = this; };
  3725. };
  3726. fn()();
  3727. context === this;
  3728. `
  3729. testScript(SCRIPT, valueTrue, t)
  3730. }
  3731. func TestParameterOverride(t *testing.T) {
  3732. const SCRIPT = `
  3733. function f(arg) {
  3734. var arg = arg || "default"
  3735. return arg
  3736. }
  3737. f()
  3738. `
  3739. testScript(SCRIPT, asciiString("default"), t)
  3740. }
  3741. func TestEvalInIterScope(t *testing.T) {
  3742. const SCRIPT = `
  3743. for (let a = 0; a < 1; a++) {
  3744. eval("a");
  3745. }
  3746. `
  3747. testScript(SCRIPT, valueInt(0), t)
  3748. }
  3749. func TestTemplateLiterals(t *testing.T) {
  3750. vm := New()
  3751. _, err := vm.RunString("const a = 1, b = 'b';")
  3752. if err != nil {
  3753. t.Fatal(err)
  3754. }
  3755. f := func(t *testing.T, template, expected string) {
  3756. res, err := vm.RunString(template)
  3757. if err != nil {
  3758. t.Fatal(err)
  3759. }
  3760. if actual := res.Export(); actual != expected {
  3761. t.Fatalf("Expected: %q, actual: %q", expected, actual)
  3762. }
  3763. }
  3764. t.Run("empty", func(t *testing.T) {
  3765. f(t, "``", "")
  3766. })
  3767. t.Run("noSub", func(t *testing.T) {
  3768. f(t, "`test`", "test")
  3769. })
  3770. t.Run("emptyTail", func(t *testing.T) {
  3771. f(t, "`a=${a},b=${b}`", "a=1,b=b")
  3772. })
  3773. t.Run("emptyHead", func(t *testing.T) {
  3774. f(t, "`${a},b=${b}$`", "1,b=b$")
  3775. })
  3776. t.Run("headAndTail", func(t *testing.T) {
  3777. f(t, "`a=${a},b=${b}$`", "a=1,b=b$")
  3778. })
  3779. }
  3780. func TestTaggedTemplate(t *testing.T) {
  3781. const SCRIPT = `
  3782. let res;
  3783. const o = {
  3784. tmpl() {
  3785. res = this;
  3786. return () => {};
  3787. }
  3788. }
  3789. ` +
  3790. "o.tmpl()`test`;" + `
  3791. res === o;
  3792. `
  3793. testScript(SCRIPT, valueTrue, t)
  3794. }
  3795. func TestDuplicateGlobalFunc(t *testing.T) {
  3796. const SCRIPT = `
  3797. function a(){}
  3798. function b(){ return "b" }
  3799. function c(){ return "c" }
  3800. function a(){}
  3801. b();
  3802. `
  3803. testScript(SCRIPT, asciiString("b"), t)
  3804. }
  3805. func TestDuplicateFunc(t *testing.T) {
  3806. const SCRIPT = `
  3807. function f() {
  3808. function a(){}
  3809. function b(){ return "b" }
  3810. function c(){ return "c" }
  3811. function a(){}
  3812. return b();
  3813. }
  3814. f();
  3815. `
  3816. testScript(SCRIPT, asciiString("b"), t)
  3817. }
  3818. func TestSrcLocations(t *testing.T) {
  3819. // Do not reformat, assertions depend on line and column numbers
  3820. const SCRIPT = `
  3821. let i = {
  3822. valueOf() {
  3823. throw new Error();
  3824. }
  3825. };
  3826. try {
  3827. i++;
  3828. } catch(e) {
  3829. assertStack(e, [["test.js", "valueOf", 4, 10],
  3830. ["test.js", "", 8, 3]
  3831. ]);
  3832. }
  3833. Object.defineProperty(globalThis, "x", {
  3834. get() {
  3835. throw new Error();
  3836. },
  3837. set() {
  3838. throw new Error();
  3839. }
  3840. });
  3841. try {
  3842. x;
  3843. } catch(e) {
  3844. assertStack(e, [["test.js", "get", 17, 10],
  3845. ["test.js", "", 25, 3]
  3846. ]);
  3847. }
  3848. try {
  3849. x++;
  3850. } catch(e) {
  3851. assertStack(e, [["test.js", "get", 17, 10],
  3852. ["test.js", "", 33, 3]
  3853. ]);
  3854. }
  3855. try {
  3856. x = 2;
  3857. } catch(e) {
  3858. assertStack(e, [["test.js", "set", 20, 10],
  3859. ["test.js", "", 41, 3]
  3860. ]);
  3861. }
  3862. try {
  3863. +i;
  3864. } catch(e) {
  3865. assertStack(e, [["test.js", "valueOf", 4, 10],
  3866. ["test.js", "", 49, 4]
  3867. ]);
  3868. }
  3869. function assertStack(e, expected) {
  3870. const lines = e.stack.split('\n');
  3871. let lnum = 1;
  3872. for (const [file, func, line, col] of expected) {
  3873. const expLine = func === "" ?
  3874. "\tat " + file + ":" + line + ":" + col + "(" :
  3875. "\tat " + func + " (" + file + ":" + line + ":" + col + "(";
  3876. assert.sameValue(lines[lnum].substring(0, expLine.length), expLine, "line " + lnum);
  3877. lnum++;
  3878. }
  3879. }
  3880. `
  3881. testScriptWithTestLib(SCRIPT, _undefined, t)
  3882. }
  3883. /*
  3884. func TestBabel(t *testing.T) {
  3885. src, err := ioutil.ReadFile("babel7.js")
  3886. if err != nil {
  3887. t.Fatal(err)
  3888. }
  3889. vm := New()
  3890. _, err = vm.RunString(string(src))
  3891. if err != nil {
  3892. t.Fatal(err)
  3893. }
  3894. _, err = vm.RunString(`var result = Babel.transform("", {presets: ["es2015"]});`)
  3895. if err != nil {
  3896. t.Fatal(err)
  3897. }
  3898. }*/
  3899. func BenchmarkCompile(b *testing.B) {
  3900. data, err := ioutil.ReadFile("testdata/S15.10.2.12_A1_T1.js")
  3901. if err != nil {
  3902. b.Fatal(err)
  3903. }
  3904. src := string(data)
  3905. for i := 0; i < b.N; i++ {
  3906. _, err := Compile("test.js", src, false)
  3907. if err != nil {
  3908. b.Fatal(err)
  3909. }
  3910. }
  3911. }