compiler_test.go 65 KB

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