compiler_test.go 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326
  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, _undefined, 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, _undefined, 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 TestTryCompletionResult(t *testing.T) {
  689. const SCRIPT = `
  690. 99; do { -99; try { 39 } catch (e) { -1 } finally { break; -2 }; } while (false);
  691. `
  692. testScript1(SCRIPT, _undefined, t)
  693. }
  694. func TestIfElse(t *testing.T) {
  695. const SCRIPT = `
  696. var rv;
  697. if (rv === undefined) {
  698. rv = "passed";
  699. } else {
  700. rv = "failed";
  701. }
  702. `
  703. testScript(SCRIPT, asciiString("passed"), t)
  704. }
  705. func TestIfElseRetVal(t *testing.T) {
  706. const SCRIPT = `
  707. var x;
  708. if (x === undefined) {
  709. "passed";
  710. } else {
  711. "failed";
  712. }
  713. `
  714. testScript1(SCRIPT, asciiString("passed"), t)
  715. }
  716. func TestWhileReturnValue(t *testing.T) {
  717. const SCRIPT = `
  718. var x = 0;
  719. while(true) {
  720. x = 1;
  721. break;
  722. }
  723. `
  724. testScript1(SCRIPT, intToValue(1), t)
  725. }
  726. func TestIfElseLabel(t *testing.T) {
  727. const SCRIPT = `
  728. var x = 0;
  729. abc: if (true) {
  730. x = 1;
  731. break abc;
  732. }
  733. `
  734. testScript1(SCRIPT, intToValue(1), t)
  735. }
  736. func TestIfMultipleLabels(t *testing.T) {
  737. const SCRIPT = `
  738. var x = 0;
  739. xyz:abc: if (true) {
  740. break xyz;
  741. }
  742. `
  743. testScript1(SCRIPT, _undefined, t)
  744. }
  745. func TestBreakOutOfTry(t *testing.T) {
  746. const SCRIPT = `
  747. function A() {
  748. var x = 1;
  749. B: {
  750. try {
  751. x = 2;
  752. } catch(e) {
  753. x = 3;
  754. } finally {
  755. break B;
  756. x = 4;
  757. }
  758. }
  759. return x;
  760. }
  761. A();
  762. `
  763. testScript1(SCRIPT, intToValue(2), t)
  764. }
  765. func TestReturnOutOfTryNested(t *testing.T) {
  766. const SCRIPT = `
  767. function A() {
  768. function nested() {
  769. try {
  770. return 1;
  771. } catch(e) {
  772. return 2;
  773. }
  774. }
  775. return nested();
  776. }
  777. A();
  778. `
  779. testScript1(SCRIPT, intToValue(1), t)
  780. }
  781. func TestContinueLoop(t *testing.T) {
  782. const SCRIPT = `
  783. function A() {
  784. var r = 0;
  785. for (var i = 0; i < 5; i++) {
  786. if (i > 1) {
  787. continue;
  788. }
  789. r++;
  790. }
  791. return r;
  792. }
  793. A();
  794. `
  795. testScript1(SCRIPT, intToValue(2), t)
  796. }
  797. func TestContinueOutOfTry(t *testing.T) {
  798. const SCRIPT = `
  799. function A() {
  800. var r = 0;
  801. for (var i = 0; i < 5; i++) {
  802. try {
  803. if (i > 1) {
  804. continue;
  805. }
  806. } catch(e) {
  807. return 99;
  808. }
  809. r++;
  810. }
  811. return r;
  812. }
  813. A();
  814. `
  815. testScript1(SCRIPT, intToValue(2), t)
  816. }
  817. func TestThisInCatch(t *testing.T) {
  818. const SCRIPT = `
  819. function O() {
  820. try {
  821. f();
  822. } catch (e) {
  823. this.value = e.toString();
  824. }
  825. }
  826. function f() {
  827. throw "ex";
  828. }
  829. var o = new O();
  830. o.value;
  831. `
  832. testScript1(SCRIPT, asciiString("ex"), t)
  833. }
  834. func TestNestedTry(t *testing.T) {
  835. const SCRIPT = `
  836. var ex;
  837. try {
  838. throw "ex1";
  839. } catch (er1) {
  840. try {
  841. throw "ex2";
  842. } catch (er1) {
  843. ex = er1;
  844. }
  845. }
  846. ex;
  847. `
  848. testScript1(SCRIPT, asciiString("ex2"), t)
  849. }
  850. func TestNestedTryInStashlessFunc(t *testing.T) {
  851. const SCRIPT = `
  852. function f() {
  853. var ex1, ex2;
  854. try {
  855. throw "ex1";
  856. } catch (er1) {
  857. try {
  858. throw "ex2";
  859. } catch (er1) {
  860. ex2 = er1;
  861. }
  862. ex1 = er1;
  863. }
  864. return ex1 == "ex1" && ex2 == "ex2";
  865. }
  866. f();
  867. `
  868. testScript1(SCRIPT, valueTrue, t)
  869. }
  870. func TestEvalLexicalDecl(t *testing.T) {
  871. const SCRIPT = `
  872. eval("let x = true; x;");
  873. `
  874. testScript1(SCRIPT, valueTrue, t)
  875. }
  876. func TestEvalInCatchInStashlessFunc(t *testing.T) {
  877. const SCRIPT = `
  878. function f() {
  879. var ex;
  880. try {
  881. throw "ex1";
  882. } catch (er1) {
  883. eval("ex = er1");
  884. }
  885. return ex;
  886. }
  887. f();
  888. `
  889. testScript1(SCRIPT, asciiString("ex1"), t)
  890. }
  891. func TestCatchClosureInStashlessFunc(t *testing.T) {
  892. const SCRIPT = `
  893. function f() {
  894. var ex;
  895. try {
  896. throw "ex1";
  897. } catch (er1) {
  898. return function() {
  899. return er1;
  900. }
  901. }
  902. }
  903. f()();
  904. `
  905. testScript1(SCRIPT, asciiString("ex1"), t)
  906. }
  907. func TestCatchVarNotUsedInStashlessFunc(t *testing.T) {
  908. const SCRIPT = `
  909. function f() {
  910. var ex;
  911. try {
  912. throw "ex1";
  913. } catch (er1) {
  914. ex = "ok";
  915. }
  916. return ex;
  917. }
  918. f();
  919. `
  920. testScript1(SCRIPT, asciiString("ok"), t)
  921. }
  922. func TestNew(t *testing.T) {
  923. const SCRIPT = `
  924. function O() {
  925. this.x = 42;
  926. }
  927. new O().x;
  928. `
  929. testScript1(SCRIPT, intToValue(42), t)
  930. }
  931. func TestStringConstructor(t *testing.T) {
  932. const SCRIPT = `
  933. function F() {
  934. return String(33) + " " + String("cows");
  935. }
  936. F();
  937. `
  938. testScript1(SCRIPT, asciiString("33 cows"), t)
  939. }
  940. func TestError(t *testing.T) {
  941. const SCRIPT = `
  942. function F() {
  943. return new Error("test");
  944. }
  945. var e = F();
  946. var rv = e.message == "test" && e.name == "Error";
  947. `
  948. testScript(SCRIPT, valueTrue, t)
  949. }
  950. func TestTypeError(t *testing.T) {
  951. const SCRIPT = `
  952. function F() {
  953. return new TypeError("test");
  954. }
  955. var e = F();
  956. e.message == "test" && e.name == "TypeError";
  957. `
  958. testScript1(SCRIPT, valueTrue, t)
  959. }
  960. func TestToString(t *testing.T) {
  961. const SCRIPT = `
  962. var o = {x: 42};
  963. o.toString = function() {
  964. return String(this.x);
  965. }
  966. var o1 = {};
  967. o.toString() + " ### " + o1.toString();
  968. `
  969. testScript1(SCRIPT, asciiString("42 ### [object Object]"), t)
  970. }
  971. func TestEvalOrder(t *testing.T) {
  972. const SCRIPT = `
  973. var o = {f: function() {return 42}, x: 0};
  974. var trace = "";
  975. function F1() {
  976. trace += "First!";
  977. return o;
  978. }
  979. function F2() {
  980. trace += "Second!";
  981. return "f";
  982. }
  983. function F3() {
  984. trace += "Third!";
  985. }
  986. var rv = F1()[F2()](F3());
  987. rv += trace;
  988. `
  989. testScript(SCRIPT, asciiString("42First!Second!Third!"), t)
  990. }
  991. func TestPostfixIncBracket(t *testing.T) {
  992. const SCRIPT = `
  993. var o = {x: 42};
  994. var trace = "";
  995. function F1() {
  996. trace += "First!";
  997. return o;
  998. }
  999. function F2() {
  1000. trace += "Second!";
  1001. return "x";
  1002. }
  1003. var rv = F1()[F2()]++;
  1004. rv += trace + o.x;
  1005. `
  1006. testScript(SCRIPT, asciiString("42First!Second!43"), t)
  1007. }
  1008. func TestPostfixIncDot(t *testing.T) {
  1009. const SCRIPT = `
  1010. var o = {x: 42};
  1011. var trace = "";
  1012. function F1() {
  1013. trace += "First!";
  1014. return o;
  1015. }
  1016. var rv = F1().x++;
  1017. rv += trace + o.x;
  1018. `
  1019. testScript(SCRIPT, asciiString("42First!43"), t)
  1020. }
  1021. func TestPrefixIncBracket(t *testing.T) {
  1022. const SCRIPT = `
  1023. var o = {x: 42};
  1024. var trace = "";
  1025. function F1() {
  1026. trace += "First!";
  1027. return o;
  1028. }
  1029. function F2() {
  1030. trace += "Second!";
  1031. return "x";
  1032. }
  1033. var rv = ++F1()[F2()];
  1034. rv += trace + o.x;
  1035. `
  1036. testScript(SCRIPT, asciiString("43First!Second!43"), t)
  1037. }
  1038. func TestPrefixIncDot(t *testing.T) {
  1039. const SCRIPT = `
  1040. var o = {x: 42};
  1041. var trace = "";
  1042. function F1() {
  1043. trace += "First!";
  1044. return o;
  1045. }
  1046. var rv = ++F1().x;
  1047. rv += trace + o.x;
  1048. `
  1049. testScript(SCRIPT, asciiString("43First!43"), t)
  1050. }
  1051. func TestPostDecObj(t *testing.T) {
  1052. const SCRIPT = `
  1053. var object = {valueOf: function() {return 1}};
  1054. var y = object--;
  1055. var ok = false;
  1056. if (y === 1) {
  1057. ok = true;
  1058. }
  1059. ok;
  1060. `
  1061. testScript1(SCRIPT, valueTrue, t)
  1062. }
  1063. func TestPropAcc1(t *testing.T) {
  1064. const SCRIPT = `
  1065. 1..toString()
  1066. `
  1067. testScript1(SCRIPT, asciiString("1"), t)
  1068. }
  1069. func TestEvalDirect(t *testing.T) {
  1070. const SCRIPT = `
  1071. var rv = false;
  1072. function foo(){ rv = true; }
  1073. var o = { };
  1074. function f() {
  1075. try {
  1076. eval("o.bar( foo() );");
  1077. } catch (e) {
  1078. }
  1079. }
  1080. f();
  1081. `
  1082. testScript(SCRIPT, valueTrue, t)
  1083. }
  1084. func TestEvalRet(t *testing.T) {
  1085. const SCRIPT = `
  1086. eval("for (var i = 0; i < 3; i++) {i}")
  1087. `
  1088. testScript1(SCRIPT, valueInt(2), t)
  1089. }
  1090. func TestEvalFunctionDecl(t *testing.T) {
  1091. const SCRIPT = `
  1092. eval("function F() {}")
  1093. `
  1094. testScript1(SCRIPT, _undefined, t)
  1095. }
  1096. func TestEvalFunctionExpr(t *testing.T) {
  1097. const SCRIPT = `
  1098. eval("(function F() {return 42;})")()
  1099. `
  1100. testScript1(SCRIPT, intToValue(42), t)
  1101. }
  1102. func TestEvalDirectScope(t *testing.T) {
  1103. const SCRIPT = `
  1104. var __10_4_2_1_3 = "str";
  1105. function testcase() {
  1106. var __10_4_2_1_3 = "str1";
  1107. try {
  1108. throw "error";
  1109. } catch (e) {
  1110. var __10_4_2_1_3 = "str2";
  1111. return eval("__10_4_2_1_3");
  1112. }
  1113. }
  1114. testcase();
  1115. `
  1116. testScript1(SCRIPT, asciiString("str2"), t)
  1117. }
  1118. func TestEvalDirectScope1(t *testing.T) {
  1119. const SCRIPT = `
  1120. 'use strict';
  1121. var __10_4_2_1_5 = "str";
  1122. function testcase() {
  1123. var __10_4_2_1_5 = "str1";
  1124. var r = eval("\
  1125. var __10_4_2_1_5 = \'str2\'; \
  1126. eval(\"\'str2\' === __10_4_2_1_5\")\
  1127. ");
  1128. return r;
  1129. }
  1130. testcase();
  1131. `
  1132. testScript1(SCRIPT, valueTrue, t)
  1133. }
  1134. func TestEvalDirectCreateBinding(t *testing.T) {
  1135. const SCRIPT = `
  1136. function f() {
  1137. eval("var x = true");
  1138. return x;
  1139. }
  1140. var res = f();
  1141. var thrown = false;
  1142. try {
  1143. x;
  1144. } catch(e) {
  1145. if (e instanceof ReferenceError) {
  1146. thrown = true;
  1147. } else {
  1148. throw e;
  1149. }
  1150. }
  1151. res && thrown;
  1152. `
  1153. testScript1(SCRIPT, valueTrue, t)
  1154. }
  1155. func TestEvalDirectCreateBinding1(t *testing.T) {
  1156. const SCRIPT = `
  1157. function f() {
  1158. eval("let x = 1; var y = 2; function f1() {return x};");
  1159. assert.throws(ReferenceError, function() { x });
  1160. return ""+y+f1();
  1161. }
  1162. f();
  1163. `
  1164. testScript1(TESTLIB+SCRIPT, asciiString("21"), t)
  1165. }
  1166. func TestEvalDirectCreateBinding3(t *testing.T) {
  1167. const SCRIPT = `
  1168. function f() {
  1169. let x;
  1170. try {
  1171. eval("var y=1, x=2");
  1172. } catch(e) {}
  1173. return y;
  1174. }
  1175. assert.throws(ReferenceError, f);
  1176. `
  1177. testScript1(TESTLIB+SCRIPT, _undefined, t)
  1178. }
  1179. func TestEvalGlobalStrict(t *testing.T) {
  1180. const SCRIPT = `
  1181. 'use strict';
  1182. var evalStr =
  1183. 'for (var x in this) {\n'+
  1184. ' if ( x === \'Math\' ) {\n'+
  1185. ' }\n'+
  1186. '}\n';
  1187. eval(evalStr);
  1188. `
  1189. testScript1(SCRIPT, _undefined, t)
  1190. }
  1191. func TestEvalEmptyStrict(t *testing.T) {
  1192. const SCRIPT = `
  1193. 'use strict';
  1194. eval("");
  1195. `
  1196. testScript1(SCRIPT, _undefined, t)
  1197. }
  1198. func TestEvalFuncDecl(t *testing.T) {
  1199. const SCRIPT = `
  1200. 'use strict';
  1201. var funcA = eval("function __funcA(__arg){return __arg;}; __funcA");
  1202. typeof funcA;
  1203. `
  1204. testScript1(SCRIPT, asciiString("function"), t)
  1205. }
  1206. func TestGetAfterSet(t *testing.T) {
  1207. const SCRIPT = `
  1208. function f() {
  1209. var x = 1;
  1210. return x;
  1211. }
  1212. `
  1213. testScript1(SCRIPT, _undefined, t)
  1214. }
  1215. func TestForLoopRet(t *testing.T) {
  1216. const SCRIPT = `
  1217. for (var i = 0; i < 20; i++) { if (i > 2) {break;} else { i }}
  1218. `
  1219. testScript1(SCRIPT, _undefined, t)
  1220. }
  1221. func TestForLoopRet1(t *testing.T) {
  1222. const SCRIPT = `
  1223. for (var i = 0; i < 20; i++) { if (i > 2) {42;; {L:{break;}}} else { i }}
  1224. `
  1225. testScript1(SCRIPT, intToValue(42), t)
  1226. }
  1227. func TestForInLoopRet(t *testing.T) {
  1228. const SCRIPT = `
  1229. var o = [1, 2, 3, 4];
  1230. for (var i in o) { if (i > 2) {break;} else { i }}
  1231. `
  1232. testScript1(SCRIPT, _undefined, t)
  1233. }
  1234. func TestForInLoopRet1(t *testing.T) {
  1235. const SCRIPT = `
  1236. var o = {};
  1237. o.x = 1;
  1238. o.y = 2;
  1239. for (var i in o) {
  1240. true;
  1241. }
  1242. `
  1243. testScript1(SCRIPT, valueTrue, t)
  1244. }
  1245. func TestDoWhileLoopRet(t *testing.T) {
  1246. const SCRIPT = `
  1247. var i = 0;
  1248. do {
  1249. if (i > 2) {
  1250. break;
  1251. } else {
  1252. i;
  1253. }
  1254. } while (i++ < 20);
  1255. `
  1256. testScript1(SCRIPT, _undefined, t)
  1257. }
  1258. func TestDoWhileContinueRet(t *testing.T) {
  1259. const SCRIPT = `
  1260. var i = 0;
  1261. do {
  1262. if (i > 2) {
  1263. true;
  1264. continue;
  1265. } else {
  1266. i;
  1267. }
  1268. } while (i++ < 20);
  1269. `
  1270. testScript1(SCRIPT, valueTrue, t)
  1271. }
  1272. func TestWhileLoopRet(t *testing.T) {
  1273. const SCRIPT = `
  1274. var i; while (i < 20) { if (i > 2) {break;} else { i++ }}
  1275. `
  1276. testScript1(SCRIPT, _undefined, t)
  1277. }
  1278. func TestLoopRet1(t *testing.T) {
  1279. const SCRIPT = `
  1280. for (var i = 0; i < 20; i++) { }
  1281. `
  1282. testScript1(SCRIPT, _undefined, t)
  1283. }
  1284. func TestInstanceof(t *testing.T) {
  1285. const SCRIPT = `
  1286. var rv;
  1287. try {
  1288. true();
  1289. } catch (e) {
  1290. rv = e instanceof TypeError;
  1291. }
  1292. `
  1293. testScript(SCRIPT, valueTrue, t)
  1294. }
  1295. func TestStrictAssign(t *testing.T) {
  1296. const SCRIPT = `
  1297. 'use strict';
  1298. var rv;
  1299. var called = false;
  1300. function F() {
  1301. called = true;
  1302. return 1;
  1303. }
  1304. try {
  1305. x = F();
  1306. } catch (e) {
  1307. rv = e instanceof ReferenceError;
  1308. }
  1309. rv += " " + called;
  1310. `
  1311. testScript(SCRIPT, asciiString("true true"), t)
  1312. }
  1313. func TestStrictScope(t *testing.T) {
  1314. const SCRIPT = `
  1315. var rv;
  1316. var called = false;
  1317. function F() {
  1318. 'use strict';
  1319. x = 1;
  1320. }
  1321. try {
  1322. F();
  1323. } catch (e) {
  1324. rv = e instanceof ReferenceError;
  1325. }
  1326. x = 1;
  1327. rv += " " + x;
  1328. `
  1329. testScript(SCRIPT, asciiString("true 1"), t)
  1330. }
  1331. func TestStringObj(t *testing.T) {
  1332. const SCRIPT = `
  1333. var s = new String("test");
  1334. s[0] + s[2] + s[1];
  1335. `
  1336. testScript1(SCRIPT, asciiString("tse"), t)
  1337. }
  1338. func TestStringPrimitive(t *testing.T) {
  1339. const SCRIPT = `
  1340. var s = "test";
  1341. s[0] + s[2] + s[1];
  1342. `
  1343. testScript1(SCRIPT, asciiString("tse"), t)
  1344. }
  1345. func TestCallGlobalObject(t *testing.T) {
  1346. const SCRIPT = `
  1347. var rv;
  1348. try {
  1349. this();
  1350. } catch (e) {
  1351. rv = e instanceof TypeError
  1352. }
  1353. `
  1354. testScript(SCRIPT, valueTrue, t)
  1355. }
  1356. func TestFuncLength(t *testing.T) {
  1357. const SCRIPT = `
  1358. function F(x, y) {
  1359. }
  1360. F.length
  1361. `
  1362. testScript1(SCRIPT, intToValue(2), t)
  1363. }
  1364. func TestNativeFuncLength(t *testing.T) {
  1365. const SCRIPT = `
  1366. eval.length + Object.defineProperty.length + String.length
  1367. `
  1368. testScript1(SCRIPT, intToValue(5), t)
  1369. }
  1370. func TestArguments(t *testing.T) {
  1371. const SCRIPT = `
  1372. function F() {
  1373. return arguments.length + " " + arguments[1];
  1374. }
  1375. F(1,2,3)
  1376. `
  1377. testScript1(SCRIPT, asciiString("3 2"), t)
  1378. }
  1379. func TestArgumentsPut(t *testing.T) {
  1380. const SCRIPT = `
  1381. function F(x, y) {
  1382. arguments[0] -= arguments[1];
  1383. return x;
  1384. }
  1385. F(5, 2)
  1386. `
  1387. testScript1(SCRIPT, intToValue(3), t)
  1388. }
  1389. func TestArgumentsPutStrict(t *testing.T) {
  1390. const SCRIPT = `
  1391. function F(x, y) {
  1392. 'use strict';
  1393. arguments[0] -= arguments[1];
  1394. return x;
  1395. }
  1396. F(5, 2)
  1397. `
  1398. testScript1(SCRIPT, intToValue(5), t)
  1399. }
  1400. func TestArgumentsExtra(t *testing.T) {
  1401. const SCRIPT = `
  1402. function F(x, y) {
  1403. return arguments[2];
  1404. }
  1405. F(1, 2, 42)
  1406. `
  1407. testScript1(SCRIPT, intToValue(42), t)
  1408. }
  1409. func TestArgumentsExist(t *testing.T) {
  1410. const SCRIPT = `
  1411. function F(x, arguments) {
  1412. return arguments;
  1413. }
  1414. F(1, 42)
  1415. `
  1416. testScript1(SCRIPT, intToValue(42), t)
  1417. }
  1418. func TestArgumentsDelete(t *testing.T) {
  1419. const SCRIPT = `
  1420. function f(x) {
  1421. delete arguments[0];
  1422. arguments[0] = 42;
  1423. return x;
  1424. }
  1425. f(1)
  1426. `
  1427. testScript1(SCRIPT, intToValue(1), t)
  1428. }
  1429. func TestArgumentsInEval(t *testing.T) {
  1430. const SCRIPT = `
  1431. function f() {
  1432. return eval("arguments");
  1433. }
  1434. f(1)[0];
  1435. `
  1436. testScript1(SCRIPT, intToValue(1), t)
  1437. }
  1438. func TestArgumentsRedeclareInEval(t *testing.T) {
  1439. const SCRIPT = `
  1440. assert.sameValue("arguments" in this, false, "No global 'arguments' binding");
  1441. function f(p = eval("var arguments = 'param'"), arguments) {}
  1442. assert.throws(SyntaxError, f);
  1443. assert.sameValue("arguments" in this, false, "No global 'arguments' binding");
  1444. `
  1445. testScript1(TESTLIB+SCRIPT, _undefined, t)
  1446. }
  1447. func TestArgumentsRedeclareArrow(t *testing.T) {
  1448. const SCRIPT = `
  1449. const oldArguments = globalThis.arguments;
  1450. let count = 0;
  1451. const f = (p = eval("var arguments = 'param'"), q = () => arguments) => {
  1452. var arguments = "local";
  1453. assert.sameValue(arguments, "local", "arguments");
  1454. assert.sameValue(q(), "param", "q");
  1455. count++;
  1456. }
  1457. f();
  1458. assert.sameValue(count, 1);
  1459. assert.sameValue(globalThis.arguments, oldArguments, "globalThis.arguments unchanged");
  1460. `
  1461. testScript1(TESTLIB+SCRIPT, _undefined, t)
  1462. }
  1463. func TestEvalParamWithDef(t *testing.T) {
  1464. const SCRIPT = `
  1465. function f(param = 0) {
  1466. eval("var param = 1");
  1467. return param;
  1468. }
  1469. f();
  1470. `
  1471. testScript1(SCRIPT, valueInt(1), t)
  1472. }
  1473. func TestArgumentsRedefinedAsLetDyn(t *testing.T) {
  1474. const SCRIPT = `
  1475. function f() {
  1476. let arguments;
  1477. eval(""); // force dynamic scope
  1478. return arguments;
  1479. }
  1480. f(1,2);
  1481. `
  1482. testScript1(SCRIPT, _undefined, t)
  1483. }
  1484. func TestWith(t *testing.T) {
  1485. const SCRIPT = `
  1486. var b = 1;
  1487. var o = {a: 41};
  1488. with(o) {
  1489. a += b;
  1490. }
  1491. o.a;
  1492. `
  1493. testScript1(SCRIPT, intToValue(42), t)
  1494. }
  1495. func TestWithInFunc(t *testing.T) {
  1496. const SCRIPT = `
  1497. function F() {
  1498. var b = 1;
  1499. var c = 0;
  1500. var o = {a: 40, c: 1};
  1501. with(o) {
  1502. a += b + c;
  1503. }
  1504. return o.a;
  1505. }
  1506. F();
  1507. `
  1508. testScript1(SCRIPT, intToValue(42), t)
  1509. }
  1510. func TestAssignNonExtendable(t *testing.T) {
  1511. const SCRIPT = `
  1512. 'use strict';
  1513. function F() {
  1514. this.x = 1;
  1515. }
  1516. var o = new F();
  1517. Object.preventExtensions(o);
  1518. o.x = 42;
  1519. o.x;
  1520. `
  1521. testScript1(SCRIPT, intToValue(42), t)
  1522. }
  1523. func TestAssignNonExtendable1(t *testing.T) {
  1524. const SCRIPT = `
  1525. 'use strict';
  1526. function F() {
  1527. }
  1528. var o = new F();
  1529. var rv;
  1530. Object.preventExtensions(o);
  1531. try {
  1532. o.x = 42;
  1533. } catch (e) {
  1534. rv = e.constructor === TypeError;
  1535. }
  1536. rv += " " + o.x;
  1537. `
  1538. testScript(SCRIPT, asciiString("true undefined"), t)
  1539. }
  1540. func TestAssignStrict(t *testing.T) {
  1541. const SCRIPT = `
  1542. 'use strict';
  1543. try {
  1544. eval("eval = 42");
  1545. } catch(e) {
  1546. var rv = e instanceof SyntaxError
  1547. }
  1548. `
  1549. testScript(SCRIPT, valueTrue, t)
  1550. }
  1551. func TestIllegalArgmentName(t *testing.T) {
  1552. const SCRIPT = `
  1553. 'use strict';
  1554. try {
  1555. eval("function F(eval) {}");
  1556. } catch (e) {
  1557. var rv = e instanceof SyntaxError
  1558. }
  1559. `
  1560. testScript(SCRIPT, valueTrue, t)
  1561. }
  1562. func TestFunction(t *testing.T) {
  1563. const SCRIPT = `
  1564. var f0 = Function("");
  1565. var f1 = Function("return ' one'");
  1566. var f2 = Function("arg", "return ' ' + arg");
  1567. f0() + f1() + f2("two");
  1568. `
  1569. testScript1(SCRIPT, asciiString("undefined one two"), t)
  1570. }
  1571. func TestFunction1(t *testing.T) {
  1572. const SCRIPT = `
  1573. var f = function f1(count) {
  1574. if (count == 0) {
  1575. return true;
  1576. }
  1577. return f1(count-1);
  1578. }
  1579. f(1);
  1580. `
  1581. testScript1(SCRIPT, valueTrue, t)
  1582. }
  1583. func TestFunction2(t *testing.T) {
  1584. const SCRIPT = `
  1585. var trace = "";
  1586. function f(count) {
  1587. trace += "f("+count+")";
  1588. if (count == 0) {
  1589. return;
  1590. }
  1591. return f(count-1);
  1592. }
  1593. function f1() {
  1594. trace += "f1";
  1595. }
  1596. var f2 = f;
  1597. f = f1;
  1598. f2(1);
  1599. trace;
  1600. `
  1601. testScript1(SCRIPT, asciiString("f(1)f1"), t)
  1602. }
  1603. func TestFunctionToString(t *testing.T) {
  1604. const SCRIPT = `
  1605. Function("arg1", "arg2", "return 42").toString();
  1606. `
  1607. testScript1(SCRIPT, asciiString("function anonymous(arg1,arg2\n) {\nreturn 42\n}"), t)
  1608. }
  1609. func TestObjectLiteral(t *testing.T) {
  1610. const SCRIPT = `
  1611. var getterCalled = false;
  1612. var setterCalled = false;
  1613. var o = {get x() {getterCalled = true}, set x() {setterCalled = true}};
  1614. o.x;
  1615. o.x = 42;
  1616. getterCalled && setterCalled;
  1617. `
  1618. testScript1(SCRIPT, valueTrue, t)
  1619. }
  1620. func TestConst(t *testing.T) {
  1621. const SCRIPT = `
  1622. var v1 = true && true;
  1623. var v2 = 1/(-1 * 0);
  1624. var v3 = 1 == 2 || v1;
  1625. var v4 = true && false
  1626. v1 === true && v2 === -Infinity && v3 === v1 && v4 === false;
  1627. `
  1628. testScript1(SCRIPT, valueTrue, t)
  1629. }
  1630. func TestConstWhile(t *testing.T) {
  1631. const SCRIPT = `
  1632. var c = 0;
  1633. while (2 + 2 === 4) {
  1634. if (++c > 9) {
  1635. break;
  1636. }
  1637. }
  1638. c === 10;
  1639. `
  1640. testScript1(SCRIPT, valueTrue, t)
  1641. }
  1642. func TestConstWhileThrow(t *testing.T) {
  1643. const SCRIPT = `
  1644. var thrown = false;
  1645. try {
  1646. while ('s' in true) {
  1647. break;
  1648. }
  1649. } catch (e) {
  1650. thrown = e instanceof TypeError
  1651. }
  1652. thrown;
  1653. `
  1654. testScript1(SCRIPT, valueTrue, t)
  1655. }
  1656. func TestDupParams(t *testing.T) {
  1657. const SCRIPT = `
  1658. function F(x, y, x) {
  1659. return x;
  1660. }
  1661. F(1, 2);
  1662. `
  1663. testScript1(SCRIPT, _undefined, t)
  1664. }
  1665. func TestUseUnsuppliedParam(t *testing.T) {
  1666. const SCRIPT = `
  1667. function getMessage(message) {
  1668. if (message === undefined) {
  1669. message = '';
  1670. }
  1671. message += " 123 456";
  1672. return message;
  1673. }
  1674. getMessage();
  1675. `
  1676. testScript1(SCRIPT, asciiString(" 123 456"), t)
  1677. }
  1678. func TestForInLetWithInitializer(t *testing.T) {
  1679. const SCRIPT = `for (let x = 3 in {}) { }`
  1680. _, err := Compile("", SCRIPT, false)
  1681. if err == nil {
  1682. t.Fatal("Expected error")
  1683. }
  1684. }
  1685. func TestForInLoop(t *testing.T) {
  1686. const SCRIPT = `
  1687. function Proto() {}
  1688. Proto.prototype.x = 42;
  1689. var o = new Proto();
  1690. o.y = 44;
  1691. o.x = 45;
  1692. var hasX = false;
  1693. var hasY = false;
  1694. for (var i in o) {
  1695. switch(i) {
  1696. case "x":
  1697. if (hasX) {
  1698. throw new Error("Already has X");
  1699. }
  1700. hasX = true;
  1701. break;
  1702. case "y":
  1703. if (hasY) {
  1704. throw new Error("Already has Y");
  1705. }
  1706. hasY = true;
  1707. break;
  1708. }
  1709. }
  1710. hasX && hasY;
  1711. `
  1712. testScript1(SCRIPT, valueTrue, t)
  1713. }
  1714. func TestWhileLoopResult(t *testing.T) {
  1715. const SCRIPT = `
  1716. while(false);
  1717. `
  1718. testScript1(SCRIPT, _undefined, t)
  1719. }
  1720. func TestEmptySwitch(t *testing.T) {
  1721. const SCRIPT = `
  1722. switch(1){}
  1723. `
  1724. testScript1(SCRIPT, _undefined, t)
  1725. }
  1726. func TestEmptyDoWhile(t *testing.T) {
  1727. const SCRIPT = `
  1728. do {} while(false)
  1729. `
  1730. testScript1(SCRIPT, _undefined, t)
  1731. }
  1732. func TestSwitch(t *testing.T) {
  1733. const SCRIPT = `
  1734. function F(x) {
  1735. var i = 0;
  1736. switch (x) {
  1737. case 0:
  1738. i++;
  1739. case 1:
  1740. i++;
  1741. default:
  1742. i++;
  1743. case 2:
  1744. i++;
  1745. break;
  1746. case 3:
  1747. i++;
  1748. }
  1749. return i;
  1750. }
  1751. F(0) + F(1) + F(2) + F(4);
  1752. `
  1753. testScript1(SCRIPT, intToValue(10), t)
  1754. }
  1755. func TestSwitchDefFirst(t *testing.T) {
  1756. const SCRIPT = `
  1757. function F(x) {
  1758. var i = 0;
  1759. switch (x) {
  1760. default:
  1761. i++;
  1762. case 0:
  1763. i++;
  1764. case 1:
  1765. i++;
  1766. case 2:
  1767. i++;
  1768. break;
  1769. case 3:
  1770. i++;
  1771. }
  1772. return i;
  1773. }
  1774. F(0) + F(1) + F(2) + F(4);
  1775. `
  1776. testScript1(SCRIPT, intToValue(10), t)
  1777. }
  1778. func TestSwitchResult(t *testing.T) {
  1779. const SCRIPT = `
  1780. var x = 2;
  1781. switch (x) {
  1782. case 0:
  1783. "zero";
  1784. case 1:
  1785. "one";
  1786. case 2:
  1787. "two";
  1788. break;
  1789. case 3:
  1790. "three";
  1791. default:
  1792. "default";
  1793. }
  1794. `
  1795. testScript1(SCRIPT, asciiString("two"), t)
  1796. }
  1797. func TestSwitchResult1(t *testing.T) {
  1798. const SCRIPT = `
  1799. var x = 0;
  1800. switch (x) { case 0: "two"; case 1: break}
  1801. `
  1802. testScript1(SCRIPT, asciiString("two"), t)
  1803. }
  1804. func TestSwitchResult2(t *testing.T) {
  1805. const SCRIPT = `
  1806. 6; switch ("a") { case "a": 7; case "b": }
  1807. `
  1808. testScript1(SCRIPT, valueInt(7), t)
  1809. }
  1810. func TestSwitchResultJumpIntoEmptyEval(t *testing.T) {
  1811. const SCRIPT = `
  1812. function t(x) {
  1813. return eval("switch(x) { case 1: 2; break; case 2: let x = 1; case 3: x+2; break; case 4: default: 9}");
  1814. }
  1815. ""+t(2)+t();
  1816. `
  1817. testScript1(SCRIPT, asciiString("39"), t)
  1818. }
  1819. func TestSwitchResultJumpIntoEmpty(t *testing.T) {
  1820. const SCRIPT = `
  1821. switch(2) { case 1: 2; break; case 2: let x = 1; case 3: x+2; case 4: {let y = 2}; break; default: 9};
  1822. `
  1823. testScript1(SCRIPT, valueInt(3), t)
  1824. }
  1825. func TestSwitchLexical(t *testing.T) {
  1826. const SCRIPT = `
  1827. switch (true) { case true: let x = 1; }
  1828. `
  1829. testScript1(SCRIPT, _undefined, t)
  1830. }
  1831. func TestSwitchBreakOuter(t *testing.T) {
  1832. const SCRIPT = `
  1833. LOOP:
  1834. for (let i = 0; i < 10; i++) {
  1835. switch (i) {
  1836. case 0:
  1837. continue;
  1838. case 1:
  1839. let x = 1;
  1840. continue;
  1841. case 2:
  1842. try {
  1843. x++;
  1844. } catch (e) {
  1845. if (e instanceof ReferenceError) {
  1846. break LOOP;
  1847. }
  1848. }
  1849. throw new Error("Exception was not thrown");
  1850. }
  1851. }
  1852. `
  1853. testScript1(SCRIPT, _undefined, t)
  1854. }
  1855. func TestIfBreakResult(t *testing.T) {
  1856. const SCRIPT = `
  1857. L: {if (true) {42;} break L;}
  1858. `
  1859. testScript1(SCRIPT, intToValue(42), t)
  1860. }
  1861. func TestSwitchNoMatch(t *testing.T) {
  1862. const SCRIPT = `
  1863. var result;
  1864. var x;
  1865. switch (x) {
  1866. case 0:
  1867. result = "2";
  1868. break;
  1869. }
  1870. result;
  1871. `
  1872. testScript1(SCRIPT, _undefined, t)
  1873. }
  1874. func TestSwitchNoMatchNoDefault(t *testing.T) {
  1875. const SCRIPT = `
  1876. switch (1) {
  1877. case 0:
  1878. }
  1879. `
  1880. testScript1(SCRIPT, _undefined, t)
  1881. }
  1882. func TestSwitchNoMatchNoDefaultNoResult(t *testing.T) {
  1883. const SCRIPT = `
  1884. switch (1) {
  1885. case 0:
  1886. }
  1887. 42;
  1888. `
  1889. testScript1(SCRIPT, intToValue(42), t)
  1890. }
  1891. func TestSwitchNoMatchNoDefaultNoResultMatch(t *testing.T) {
  1892. const SCRIPT = `
  1893. switch (1) {
  1894. case 1:
  1895. }
  1896. 42;
  1897. `
  1898. testScript1(SCRIPT, intToValue(42), t)
  1899. }
  1900. func TestEmptySwitchNoResult(t *testing.T) {
  1901. const SCRIPT = `
  1902. switch (1) {}
  1903. 42;
  1904. `
  1905. testScript1(SCRIPT, intToValue(42), t)
  1906. }
  1907. func TestGetOwnPropertyNames(t *testing.T) {
  1908. const SCRIPT = `
  1909. var o = {
  1910. prop1: 42,
  1911. prop2: "test"
  1912. }
  1913. var hasProp1 = false;
  1914. var hasProp2 = false;
  1915. var names = Object.getOwnPropertyNames(o);
  1916. for (var i in names) {
  1917. var p = names[i];
  1918. switch(p) {
  1919. case "prop1":
  1920. hasProp1 = true;
  1921. break;
  1922. case "prop2":
  1923. hasProp2 = true;
  1924. break;
  1925. }
  1926. }
  1927. hasProp1 && hasProp2;
  1928. `
  1929. testScript1(SCRIPT, valueTrue, t)
  1930. }
  1931. func TestArrayLiteral(t *testing.T) {
  1932. const SCRIPT = `
  1933. var f1Called = false;
  1934. var f2Called = false;
  1935. var f3Called = false;
  1936. var errorThrown = false;
  1937. function F1() {
  1938. f1Called = true;
  1939. }
  1940. function F2() {
  1941. f2Called = true;
  1942. }
  1943. function F3() {
  1944. f3Called = true;
  1945. }
  1946. try {
  1947. var a = [F1(), x(F3()), F2()];
  1948. } catch(e) {
  1949. if (e instanceof ReferenceError) {
  1950. errorThrown = true;
  1951. } else {
  1952. throw e;
  1953. }
  1954. }
  1955. f1Called && !f2Called && f3Called && errorThrown && a === undefined;
  1956. `
  1957. testScript1(SCRIPT, valueTrue, t)
  1958. }
  1959. func TestJumpOutOfReturn(t *testing.T) {
  1960. const SCRIPT = `
  1961. function f() {
  1962. var a;
  1963. if (a == 0) {
  1964. return true;
  1965. }
  1966. }
  1967. f();
  1968. `
  1969. testScript1(SCRIPT, _undefined, t)
  1970. }
  1971. func TestSwitchJumpOutOfReturn(t *testing.T) {
  1972. const SCRIPT = `
  1973. function f(x) {
  1974. switch(x) {
  1975. case 0:
  1976. break;
  1977. default:
  1978. return x;
  1979. }
  1980. }
  1981. f(0);
  1982. `
  1983. testScript1(SCRIPT, _undefined, t)
  1984. }
  1985. func TestSetToReadOnlyPropertyStrictBracket(t *testing.T) {
  1986. const SCRIPT = `
  1987. 'use strict';
  1988. var o = {};
  1989. var thrown = false;
  1990. Object.defineProperty(o, "test", {value: 42, configurable: true});
  1991. try {
  1992. o["test"] = 43;
  1993. } catch (e) {
  1994. thrown = e instanceof TypeError;
  1995. }
  1996. thrown;
  1997. `
  1998. testScript1(SCRIPT, valueTrue, t)
  1999. }
  2000. func TestSetToReadOnlyPropertyStrictDot(t *testing.T) {
  2001. const SCRIPT = `
  2002. 'use strict';
  2003. var o = {};
  2004. var thrown = false;
  2005. Object.defineProperty(o, "test", {value: 42, configurable: true});
  2006. try {
  2007. o.test = 43;
  2008. } catch (e) {
  2009. thrown = e instanceof TypeError;
  2010. }
  2011. thrown;
  2012. `
  2013. testScript1(SCRIPT, valueTrue, t)
  2014. }
  2015. func TestDeleteNonConfigurablePropertyStrictBracket(t *testing.T) {
  2016. const SCRIPT = `
  2017. 'use strict';
  2018. var o = {};
  2019. var thrown = false;
  2020. Object.defineProperty(o, "test", {value: 42});
  2021. try {
  2022. delete o["test"];
  2023. } catch (e) {
  2024. thrown = e instanceof TypeError;
  2025. }
  2026. thrown;
  2027. `
  2028. testScript1(SCRIPT, valueTrue, t)
  2029. }
  2030. func TestDeleteNonConfigurablePropertyStrictDot(t *testing.T) {
  2031. const SCRIPT = `
  2032. 'use strict';
  2033. var o = {};
  2034. var thrown = false;
  2035. Object.defineProperty(o, "test", {value: 42});
  2036. try {
  2037. delete o.test;
  2038. } catch (e) {
  2039. thrown = e instanceof TypeError;
  2040. }
  2041. thrown;
  2042. `
  2043. testScript1(SCRIPT, valueTrue, t)
  2044. }
  2045. func TestCompound1(t *testing.T) {
  2046. const SCRIPT = `
  2047. var x = 0;
  2048. var scope = {x: 1};
  2049. var f;
  2050. with (scope) {
  2051. f = function() {
  2052. x *= (delete scope.x, 2);
  2053. }
  2054. }
  2055. f();
  2056. scope.x === 2 && x === 0;
  2057. `
  2058. testScript1(SCRIPT, valueTrue, t)
  2059. }
  2060. func TestCompound2(t *testing.T) {
  2061. const SCRIPT = `
  2062. var x;
  2063. x = "x";
  2064. x ^= "1";
  2065. `
  2066. testScript1(SCRIPT, intToValue(1), t)
  2067. }
  2068. func TestDeleteArguments(t *testing.T) {
  2069. defer func() {
  2070. if _, ok := recover().(*CompilerSyntaxError); !ok {
  2071. t.Fatal("Expected syntax error")
  2072. }
  2073. }()
  2074. const SCRIPT = `
  2075. 'use strict';
  2076. function f() {
  2077. delete arguments;
  2078. }
  2079. `
  2080. testScript1(SCRIPT, _undefined, t)
  2081. }
  2082. func TestReturnUndefined(t *testing.T) {
  2083. const SCRIPT = `
  2084. function f() {
  2085. return x;
  2086. }
  2087. var thrown = false;
  2088. try {
  2089. f();
  2090. } catch (e) {
  2091. thrown = e instanceof ReferenceError;
  2092. }
  2093. thrown;
  2094. `
  2095. testScript1(SCRIPT, valueTrue, t)
  2096. }
  2097. func TestForBreak(t *testing.T) {
  2098. const SCRIPT = `
  2099. var supreme, count;
  2100. supreme = 5;
  2101. var __evaluated = eval("for(count=0;;) {if (count===supreme)break;else count++; }");
  2102. if (__evaluated !== void 0) {
  2103. throw new Error('#1: __evaluated === 4. Actual: __evaluated ==='+ __evaluated );
  2104. }
  2105. `
  2106. testScript1(SCRIPT, _undefined, t)
  2107. }
  2108. func TestLargeNumberLiteral(t *testing.T) {
  2109. const SCRIPT = `
  2110. var x = 0x800000000000000000000;
  2111. x.toString();
  2112. `
  2113. testScript1(SCRIPT, asciiString("9.671406556917033e+24"), t)
  2114. }
  2115. func TestIncDelete(t *testing.T) {
  2116. const SCRIPT = `
  2117. var o = {x: 1};
  2118. o.x += (delete o.x, 1);
  2119. o.x;
  2120. `
  2121. testScript1(SCRIPT, intToValue(2), t)
  2122. }
  2123. func TestCompoundAssignRefError(t *testing.T) {
  2124. const SCRIPT = `
  2125. var thrown = false;
  2126. try {
  2127. a *= 1;
  2128. } catch (e) {
  2129. if (e instanceof ReferenceError) {
  2130. thrown = true;
  2131. } else {
  2132. throw e;
  2133. }
  2134. }
  2135. thrown;
  2136. `
  2137. testScript1(SCRIPT, valueTrue, t)
  2138. }
  2139. func TestObjectLiteral__Proto__(t *testing.T) {
  2140. const SCRIPT = `
  2141. var o = {
  2142. __proto__: null,
  2143. test: 42
  2144. }
  2145. Object.getPrototypeOf(o);
  2146. `
  2147. testScript1(SCRIPT, _null, t)
  2148. }
  2149. func TestEmptyCodeError(t *testing.T) {
  2150. if _, err := New().RunString(`i`); err == nil {
  2151. t.Fatal("Expected an error")
  2152. } else {
  2153. if e := err.Error(); e != "ReferenceError: i is not defined at <eval>:1:1(0)" {
  2154. t.Fatalf("Unexpected error: '%s'", e)
  2155. }
  2156. }
  2157. }
  2158. func TestForOfArray(t *testing.T) {
  2159. const SCRIPT = `
  2160. var array = [0, 'a', true, false, null, /* hole */, undefined, NaN];
  2161. var i = 0;
  2162. for (var value of array) {
  2163. assert.sameValue(value, array[i], 'element at index ' + i);
  2164. i++;
  2165. }
  2166. assert.sameValue(i, 8, 'Visits all elements');
  2167. `
  2168. testScript1(TESTLIB+SCRIPT, _undefined, t)
  2169. }
  2170. func TestForOfReturn(t *testing.T) {
  2171. const SCRIPT = `
  2172. var callCount = 0;
  2173. var iterationCount = 0;
  2174. var iterable = {};
  2175. var x = {
  2176. set attr(_) {
  2177. throw new Test262Error();
  2178. }
  2179. };
  2180. iterable[Symbol.iterator] = function() {
  2181. return {
  2182. next: function() {
  2183. return { done: false, value: 0 };
  2184. },
  2185. return: function() {
  2186. callCount += 1;
  2187. }
  2188. }
  2189. };
  2190. assert.throws(Test262Error, function() {
  2191. for (x.attr of iterable) {
  2192. iterationCount += 1;
  2193. }
  2194. });
  2195. assert.sameValue(iterationCount, 0, 'The loop body is not evaluated');
  2196. assert.sameValue(callCount, 1, 'Iterator is closed');
  2197. `
  2198. testScript1(TESTLIB+SCRIPT, _undefined, t)
  2199. }
  2200. func TestForOfReturn1(t *testing.T) {
  2201. const SCRIPT = `
  2202. var iterable = {};
  2203. var iterationCount = 0;
  2204. iterable[Symbol.iterator] = function() {
  2205. return {
  2206. next: function() {
  2207. return { done: false, value: null };
  2208. },
  2209. get return() {
  2210. throw new Test262Error();
  2211. }
  2212. };
  2213. };
  2214. assert.throws(Test262Error, function() {
  2215. for (var x of iterable) {
  2216. iterationCount += 1;
  2217. break;
  2218. }
  2219. });
  2220. assert.sameValue(iterationCount, 1, 'The loop body is evaluated');
  2221. `
  2222. testScript1(TESTLIB+SCRIPT, _undefined, t)
  2223. }
  2224. func TestForOfLet(t *testing.T) {
  2225. const SCRIPT = `
  2226. var iterCount = 0;
  2227. function f() {}
  2228. for (var let of [23]) {
  2229. f(let);
  2230. if (let != 23) {
  2231. throw new Error("");
  2232. }
  2233. iterCount += 1;
  2234. }
  2235. iterCount;
  2236. `
  2237. testScript1(SCRIPT, valueInt(1), t)
  2238. }
  2239. func TestForOfLetLet(t *testing.T) {
  2240. const SCRIPT = `
  2241. for (let let of [23]) {
  2242. }
  2243. `
  2244. _, err := Compile("", SCRIPT, false)
  2245. if err == nil {
  2246. t.Fatal("Expected error")
  2247. }
  2248. }
  2249. func TestForHeadLet(t *testing.T) {
  2250. const SCRIPT = `
  2251. for (let = 0; let < 2; let++);
  2252. `
  2253. testScript1(SCRIPT, _undefined, t)
  2254. }
  2255. func TestLhsLet(t *testing.T) {
  2256. const SCRIPT = `
  2257. let = 1;
  2258. let;
  2259. `
  2260. testScript1(SCRIPT, valueInt(1), t)
  2261. }
  2262. func TestLetPostfixASI(t *testing.T) {
  2263. const SCRIPT = `
  2264. let
  2265. ++
  2266. `
  2267. _, err := Compile("", SCRIPT, false)
  2268. if err == nil {
  2269. t.Fatal("Expected error")
  2270. }
  2271. }
  2272. func TestIteratorReturnNormal(t *testing.T) {
  2273. const SCRIPT = `
  2274. var iterable = {};
  2275. var iterationCount = 0;
  2276. iterable[Symbol.iterator] = function() {
  2277. return {
  2278. next: function() {
  2279. return { done: ++iterationCount > 2, value: null };
  2280. },
  2281. get return() {
  2282. throw new Test262Error();
  2283. }
  2284. };
  2285. };
  2286. for (var x of iterable) {
  2287. }
  2288. `
  2289. testScript1(TESTLIB+SCRIPT, _undefined, t)
  2290. }
  2291. func TestIteratorReturnErrorNested(t *testing.T) {
  2292. const SCRIPT = `
  2293. var returnCalled = {};
  2294. function iter(id) {
  2295. return function() {
  2296. var count = 0;
  2297. return {
  2298. next: function () {
  2299. return {
  2300. value: null,
  2301. done: ++count > 2
  2302. };
  2303. },
  2304. return: function () {
  2305. returnCalled[id] = true;
  2306. throw new Error(id);
  2307. }
  2308. };
  2309. }
  2310. }
  2311. var iterable1 = {};
  2312. iterable1[Symbol.iterator] = iter("1");
  2313. var iterable2 = {};
  2314. iterable2[Symbol.iterator] = iter("2");
  2315. try {
  2316. for (var i of iterable1) {
  2317. for (var j of iterable2) {
  2318. break;
  2319. }
  2320. }
  2321. throw new Error("no exception was thrown");
  2322. } catch (e) {
  2323. if (e.message !== "2") {
  2324. throw e;
  2325. }
  2326. }
  2327. if (!returnCalled["1"]) {
  2328. throw new Error("no return 1");
  2329. }
  2330. if (!returnCalled["2"]) {
  2331. throw new Error("no return 2");
  2332. }
  2333. `
  2334. testScript1(SCRIPT, _undefined, t)
  2335. }
  2336. func TestReturnFromForInLoop(t *testing.T) {
  2337. const SCRIPT = `
  2338. (function f() {
  2339. for (var i in {a: 1}) {
  2340. return true;
  2341. }
  2342. })();
  2343. `
  2344. testScript1(SCRIPT, valueTrue, t)
  2345. }
  2346. func TestReturnFromForOfLoop(t *testing.T) {
  2347. const SCRIPT = `
  2348. (function f() {
  2349. for (var i of [1]) {
  2350. return true;
  2351. }
  2352. })();
  2353. `
  2354. testScript1(SCRIPT, valueTrue, t)
  2355. }
  2356. func TestIfStackLeaks(t *testing.T) {
  2357. const SCRIPT = `
  2358. var t = 0;
  2359. if (t === 0) {
  2360. t;
  2361. }
  2362. `
  2363. testScript1(SCRIPT, _positiveZero, t)
  2364. }
  2365. func TestWithCallee(t *testing.T) {
  2366. const SCRIPT = `
  2367. function O() {
  2368. var that = this;
  2369. this.m = function() {
  2370. return this === that;
  2371. }
  2372. }
  2373. with(new O()) {
  2374. m();
  2375. }
  2376. `
  2377. testScript1(SCRIPT, valueTrue, t)
  2378. }
  2379. func TestWithScope(t *testing.T) {
  2380. const SCRIPT = `
  2381. function f(o) {
  2382. var x = 42;
  2383. function innerf(o) {
  2384. with (o) {
  2385. return x;
  2386. }
  2387. }
  2388. return innerf(o);
  2389. }
  2390. f({});
  2391. `
  2392. testScript1(SCRIPT, valueInt(42), t)
  2393. }
  2394. func TestEvalCallee(t *testing.T) {
  2395. const SCRIPT = `
  2396. (function () {
  2397. 'use strict';
  2398. var v = function() {
  2399. return this === undefined;
  2400. };
  2401. return eval('v()');
  2402. })();
  2403. `
  2404. testScript1(SCRIPT, valueTrue, t)
  2405. }
  2406. func TestEvalBindingDeleteVar(t *testing.T) {
  2407. const SCRIPT = `
  2408. (function () {
  2409. eval("var x = 1");
  2410. return x === 1 && delete x;
  2411. })();
  2412. `
  2413. testScript1(SCRIPT, valueTrue, t)
  2414. }
  2415. func TestEvalBindingDeleteFunc(t *testing.T) {
  2416. const SCRIPT = `
  2417. (function () {
  2418. eval("function x(){}");
  2419. return typeof x === "function" && delete x;
  2420. })();
  2421. `
  2422. testScript1(SCRIPT, valueTrue, t)
  2423. }
  2424. func TestDeleteGlobalLexical(t *testing.T) {
  2425. const SCRIPT = `
  2426. let x;
  2427. delete x;
  2428. `
  2429. testScript1(SCRIPT, valueFalse, t)
  2430. }
  2431. func TestDeleteGlobalEval(t *testing.T) {
  2432. const SCRIPT = `
  2433. eval("var x");
  2434. delete x;
  2435. `
  2436. testScript1(SCRIPT, valueTrue, t)
  2437. }
  2438. func TestGlobalVarNames(t *testing.T) {
  2439. vm := New()
  2440. _, err := vm.RunString("(0,eval)('var x')")
  2441. if err != nil {
  2442. t.Fatal(err)
  2443. }
  2444. _, err = vm.RunString("let x")
  2445. if err == nil {
  2446. t.Fatal("Expected error")
  2447. }
  2448. }
  2449. func TestTryResultEmpty(t *testing.T) {
  2450. const SCRIPT = `
  2451. 1; try { } finally { }
  2452. `
  2453. testScript1(SCRIPT, _undefined, t)
  2454. }
  2455. func TestTryResultEmptyCatch(t *testing.T) {
  2456. const SCRIPT = `
  2457. 1; try { throw null } catch(e) { }
  2458. `
  2459. testScript1(SCRIPT, _undefined, t)
  2460. }
  2461. func TestTryResultEmptyContinueLoop(t *testing.T) {
  2462. const SCRIPT = `
  2463. for (var i = 0; i < 2; i++) { try {throw null;} catch(e) {continue;} 'bad'}
  2464. `
  2465. testScript1(SCRIPT, _undefined, t)
  2466. }
  2467. func TestTryEmptyCatchStackLeak(t *testing.T) {
  2468. const SCRIPT = `
  2469. (function() {
  2470. var f;
  2471. // Make sure the outer function is not stashless.
  2472. (function() {
  2473. f++;
  2474. })();
  2475. try {
  2476. throw new Error();
  2477. } catch(e) {}
  2478. })();
  2479. `
  2480. testScript1(SCRIPT, _undefined, t)
  2481. }
  2482. func TestTryThrowEmptyCatch(t *testing.T) {
  2483. const SCRIPT = `
  2484. try {
  2485. throw new Error();
  2486. }
  2487. catch (e) {}
  2488. `
  2489. testScript1(SCRIPT, _undefined, t)
  2490. }
  2491. func TestFalsyLoopBreak(t *testing.T) {
  2492. const SCRIPT = `
  2493. while(false) {
  2494. break;
  2495. }
  2496. for(;false;) {
  2497. break;
  2498. }
  2499. undefined;
  2500. `
  2501. MustCompile("", SCRIPT, false)
  2502. }
  2503. func TestFalsyLoopBreakWithResult(t *testing.T) {
  2504. const SCRIPT = `
  2505. while(false) {
  2506. break;
  2507. }
  2508. `
  2509. testScript1(SCRIPT, _undefined, t)
  2510. }
  2511. func TestDummyCompile(t *testing.T) {
  2512. const SCRIPT = `
  2513. 'use strict';
  2514. for (;false;) {
  2515. eval = 1;
  2516. }
  2517. `
  2518. _, err := Compile("", SCRIPT, false)
  2519. if err == nil {
  2520. t.Fatal("expected error")
  2521. }
  2522. }
  2523. func TestDummyCompileForUpdate(t *testing.T) {
  2524. const SCRIPT = `
  2525. 'use strict';
  2526. for (;false;eval=1) {
  2527. }
  2528. `
  2529. _, err := Compile("", SCRIPT, false)
  2530. if err == nil {
  2531. t.Fatal("expected error")
  2532. }
  2533. }
  2534. func TestObjectLiteralWithNumericKeys(t *testing.T) {
  2535. const SCRIPT = `
  2536. var o = {1e3: true};
  2537. var keys = Object.keys(o);
  2538. var o1 = {get 1e3() {return true;}};
  2539. var keys1 = Object.keys(o1);
  2540. var o2 = {1e21: true};
  2541. var keys2 = Object.keys(o2);
  2542. keys.length === 1 && keys[0] === "1000" &&
  2543. keys1.length === 1 && keys1[0] === "1000" && o1[1e3] === true &&
  2544. keys2.length === 1 && keys2[0] === "1e+21";
  2545. `
  2546. testScript1(SCRIPT, valueTrue, t)
  2547. }
  2548. func TestEscapedObjectPropertyKeys(t *testing.T) {
  2549. const SCRIPT = `
  2550. var obj = {
  2551. w\u0069th: 42
  2552. };
  2553. var obj = {
  2554. with() {42}
  2555. };
  2556. `
  2557. _, err := Compile("", SCRIPT, false)
  2558. if err != nil {
  2559. t.Fatal(err)
  2560. }
  2561. }
  2562. func TestEscapedKeywords(t *testing.T) {
  2563. const SCRIPT = `r\u0065turn;`
  2564. _, err := Compile("", SCRIPT, false)
  2565. if err == nil {
  2566. t.Fatal("Expected error")
  2567. }
  2568. }
  2569. func TestEscapedLet(t *testing.T) {
  2570. const SCRIPT = `
  2571. this.let = 0;
  2572. l\u0065t // ASI
  2573. a;
  2574. // If the parser treated the previous escaped "let" as a lexical declaration,
  2575. // this variable declaration will result an early syntax error.
  2576. var a;
  2577. `
  2578. _, err := Compile("", SCRIPT, false)
  2579. if err != nil {
  2580. t.Fatal(err)
  2581. }
  2582. }
  2583. func TestObjectLiteralFuncProps(t *testing.T) {
  2584. const SCRIPT = `
  2585. (function() {
  2586. 'use strict';
  2587. var o = {
  2588. eval: function() {return 1;},
  2589. arguments() {return 2;},
  2590. test: function test1() {}
  2591. }
  2592. assert.sameValue(o.eval.name, "eval");
  2593. assert.sameValue(o.arguments.name, "arguments");
  2594. assert.sameValue(o.eval(), 1);
  2595. assert.sameValue(o.arguments(), 2);
  2596. assert.sameValue(o.test.name, "test1");
  2597. })();
  2598. `
  2599. testScript1(TESTLIB+SCRIPT, _undefined, t)
  2600. }
  2601. func TestFuncName(t *testing.T) {
  2602. const SCRIPT = `
  2603. var method = 1;
  2604. var o = {
  2605. method: function() {
  2606. return method;
  2607. },
  2608. method1: function method() {
  2609. return method;
  2610. }
  2611. }
  2612. o.method() === 1 && o.method1() === o.method1;
  2613. `
  2614. testScript1(SCRIPT, valueTrue, t)
  2615. }
  2616. func TestFuncNameAssign(t *testing.T) {
  2617. const SCRIPT = `
  2618. var f = function() {};
  2619. var f1;
  2620. f1 = function() {};
  2621. let f2 = function() {};
  2622. f.name === "f" && f1.name === "f1" && f2.name === "f2";
  2623. `
  2624. testScript1(SCRIPT, valueTrue, t)
  2625. }
  2626. func TestLexicalDeclGlobal(t *testing.T) {
  2627. const SCRIPT = `
  2628. if (true) {
  2629. let it = "be";
  2630. if (it !== "be") {
  2631. throw new Error(it);
  2632. }
  2633. }
  2634. let thrown = false;
  2635. try {
  2636. it;
  2637. } catch(e) {
  2638. if (e instanceof ReferenceError) {
  2639. thrown = true;
  2640. }
  2641. }
  2642. thrown;
  2643. `
  2644. testScript1(SCRIPT, valueTrue, t)
  2645. }
  2646. func TestLexicalDeclFunction(t *testing.T) {
  2647. const SCRIPT = `
  2648. function f() {
  2649. if (true) {
  2650. let it = "be";
  2651. if (it !== "be") {
  2652. throw new Error(it);
  2653. }
  2654. }
  2655. let thrown = false;
  2656. try {
  2657. it;
  2658. } catch(e) {
  2659. if (e instanceof ReferenceError) {
  2660. thrown = true;
  2661. }
  2662. }
  2663. return thrown;
  2664. }
  2665. f();
  2666. `
  2667. testScript1(SCRIPT, valueTrue, t)
  2668. }
  2669. func TestLexicalDynamicScope(t *testing.T) {
  2670. const SCRIPT = `
  2671. const global = 1;
  2672. function f() {
  2673. const func = global + 1;
  2674. function inner() {
  2675. function assertThrows(fn) {
  2676. let thrown = false;
  2677. try {
  2678. fn();
  2679. } catch (e) {
  2680. if (e instanceof TypeError) {
  2681. thrown = true;
  2682. } else {
  2683. throw e;
  2684. }
  2685. }
  2686. if (!thrown) {
  2687. throw new Error("Did not throw");
  2688. }
  2689. }
  2690. assertThrows(function() {
  2691. func++;
  2692. });
  2693. assertThrows(function() {
  2694. global++;
  2695. });
  2696. assertThrows(function() {
  2697. eval("func++");
  2698. });
  2699. assertThrows(function() {
  2700. eval("global++");
  2701. });
  2702. return eval("func + 1");
  2703. }
  2704. return inner();
  2705. }
  2706. f();
  2707. `
  2708. testScript1(SCRIPT, valueInt(3), t)
  2709. }
  2710. func TestNonStrictLet(t *testing.T) {
  2711. const SCRIPT = `
  2712. var let = 1;
  2713. `
  2714. testScript1(SCRIPT, _undefined, t)
  2715. }
  2716. func TestStrictLet(t *testing.T) {
  2717. const SCRIPT = `
  2718. var let = 1;
  2719. `
  2720. _, err := Compile("", SCRIPT, true)
  2721. if err == nil {
  2722. t.Fatal("Expected an error")
  2723. }
  2724. }
  2725. func TestLetLet(t *testing.T) {
  2726. const SCRIPT = `
  2727. let let = 1;
  2728. `
  2729. _, err := Compile("", SCRIPT, false)
  2730. if err == nil {
  2731. t.Fatal("Expected an error")
  2732. }
  2733. }
  2734. func TestLetASI(t *testing.T) {
  2735. const SCRIPT = `
  2736. while (false) let // ASI
  2737. x = 1;
  2738. `
  2739. _, err := Compile("", SCRIPT, false)
  2740. if err != nil {
  2741. t.Fatal(err)
  2742. }
  2743. }
  2744. func TestLetASI1(t *testing.T) {
  2745. const SCRIPT = `
  2746. let
  2747. x = 1;
  2748. `
  2749. _, err := Compile("", SCRIPT, true)
  2750. if err != nil {
  2751. t.Fatal(err)
  2752. }
  2753. }
  2754. func TestLetNoASI(t *testing.T) {
  2755. const SCRIPT = `
  2756. function f() {}let
  2757. x = 1;
  2758. `
  2759. _, err := Compile("", SCRIPT, true)
  2760. if err != nil {
  2761. t.Fatal(err)
  2762. }
  2763. }
  2764. func TestLetNoASI1(t *testing.T) {
  2765. const SCRIPT = `
  2766. let
  2767. let = 1;
  2768. `
  2769. _, err := Compile("", SCRIPT, false)
  2770. if err == nil {
  2771. t.Fatal("Expected error")
  2772. }
  2773. }
  2774. func TestLetArrayWithNewline(t *testing.T) {
  2775. const SCRIPT = `
  2776. with ({}) let
  2777. [a] = 0;
  2778. `
  2779. _, err := Compile("", SCRIPT, false)
  2780. if err == nil {
  2781. t.Fatal("Expected error")
  2782. }
  2783. }
  2784. func TestDynamicUninitedVarAccess(t *testing.T) {
  2785. const SCRIPT = `
  2786. function f() {
  2787. var x;
  2788. return eval("x");
  2789. }
  2790. f();
  2791. `
  2792. testScript1(SCRIPT, _undefined, t)
  2793. }
  2794. func TestLexicalForLoopNoClosure(t *testing.T) {
  2795. const SCRIPT = `
  2796. let sum = 0;
  2797. for (let i = 0; i < 3; i++) {
  2798. sum += i;
  2799. }
  2800. sum;
  2801. `
  2802. testScript1(SCRIPT, valueInt(3), t)
  2803. }
  2804. func TestLexicalForLoopClosure(t *testing.T) {
  2805. const SCRIPT = `
  2806. var f = [];
  2807. for (let i = 0; i < 3; i++) {
  2808. f.push(function() {
  2809. return i;
  2810. });
  2811. }
  2812. f.length === 3 && f[0]() === 0 && f[1]() === 1 && f[2]() === 2;
  2813. `
  2814. testScript1(SCRIPT, valueTrue, t)
  2815. }
  2816. func TestLexicalForLoopClosureInNext(t *testing.T) {
  2817. const SCRIPT = `
  2818. const a = [];
  2819. for (let i = 0; i < 5; a.push(function () { return i; }), ++i) { }
  2820. let res = "";
  2821. for (let k = 0; k < 5; ++k) {
  2822. res += ""+a[k]();
  2823. }
  2824. res;
  2825. `
  2826. testScript1(SCRIPT, asciiString("12345"), t)
  2827. }
  2828. func TestVarForLoop(t *testing.T) {
  2829. const SCRIPT = `
  2830. var f = [];
  2831. for (var i = 0, j = 0; i < 3; i++) {
  2832. f.push(function() {
  2833. return i;
  2834. });
  2835. }
  2836. f.length === 3 && f[0]() === 3 && f[1]() === 3 && f[2]() === 3;
  2837. `
  2838. testScript1(SCRIPT, valueTrue, t)
  2839. }
  2840. func TestLexicalForOfLoop(t *testing.T) {
  2841. const SCRIPT = `
  2842. var f = [];
  2843. for (let i of [0, 1, 2]) {
  2844. f.push(function() {
  2845. return i;
  2846. });
  2847. }
  2848. f.length === 3 && f[0]() === 0 && f[1]() === 1 && f[2]() === 2;
  2849. `
  2850. testScript1(SCRIPT, valueTrue, t)
  2851. }
  2852. func TestLexicalForOfLoopContBreak(t *testing.T) {
  2853. const SCRIPT = `
  2854. const f = [];
  2855. for (let i of [0, 1, 2, 3, 4, 5]) {
  2856. if (i % 2) continue;
  2857. f.push(function() {
  2858. return i;
  2859. });
  2860. if (i > 2) break;
  2861. }
  2862. let res = "";
  2863. f.forEach(function(item) {res += item()});
  2864. f.length === 3 && res === "024";
  2865. `
  2866. testScript1(SCRIPT, valueTrue, t)
  2867. }
  2868. func TestVarBlockConflict(t *testing.T) {
  2869. const SCRIPT = `
  2870. let x;
  2871. {
  2872. if (false) {
  2873. var x;
  2874. }
  2875. }
  2876. `
  2877. _, err := Compile("", SCRIPT, false)
  2878. if err == nil {
  2879. t.Fatal("Expected an error")
  2880. }
  2881. }
  2882. func TestVarBlockConflictEval(t *testing.T) {
  2883. const SCRIPT = `
  2884. assert.throws(SyntaxError, function() {
  2885. let x;
  2886. {
  2887. if (true) {
  2888. eval("var x");
  2889. }
  2890. }
  2891. });
  2892. `
  2893. testScript1(TESTLIB+SCRIPT, _undefined, t)
  2894. }
  2895. func TestVarBlockNoConflict(t *testing.T) {
  2896. const SCRIPT = `
  2897. function f() {
  2898. let x;
  2899. function ff() {
  2900. {
  2901. var x = 3;
  2902. }
  2903. }
  2904. ff();
  2905. }
  2906. f();
  2907. `
  2908. testScript1(SCRIPT, _undefined, t)
  2909. }
  2910. func TestVarBlockNoConflictEval(t *testing.T) {
  2911. const SCRIPT = `
  2912. function f() {
  2913. let x;
  2914. function ff() {
  2915. {
  2916. eval("var x = 3");
  2917. }
  2918. }
  2919. ff();
  2920. }
  2921. f();
  2922. `
  2923. testScript1(SCRIPT, _undefined, t)
  2924. }
  2925. func TestVarDeclCorrectScope(t *testing.T) {
  2926. const SCRIPT = `
  2927. function f() {
  2928. {
  2929. let z;
  2930. eval("var x = 3");
  2931. }
  2932. return x;
  2933. }
  2934. f();
  2935. `
  2936. testScript1(SCRIPT, valueInt(3), t)
  2937. }
  2938. func TestLexicalCatch(t *testing.T) {
  2939. const SCRIPT = `
  2940. try {
  2941. throw null;
  2942. } catch (e) {
  2943. let x = 1;
  2944. function f() {}
  2945. e;
  2946. }
  2947. `
  2948. testScript1(SCRIPT, _null, t)
  2949. }
  2950. func TestArgumentsLexicalDecl(t *testing.T) {
  2951. const SCRIPT = `
  2952. function f1() {
  2953. let arguments;
  2954. return arguments;
  2955. }
  2956. f1(42);
  2957. `
  2958. testScript1(SCRIPT, _undefined, t)
  2959. }
  2960. func TestArgumentsLexicalDeclAssign(t *testing.T) {
  2961. const SCRIPT = `
  2962. function f1() {
  2963. let arguments = arguments;
  2964. return a;
  2965. }
  2966. assert.throws(ReferenceError, function() {
  2967. f1(42);
  2968. });
  2969. `
  2970. testScript1(TESTLIB+SCRIPT, _undefined, t)
  2971. }
  2972. func TestLexicalConstModifyFromEval(t *testing.T) {
  2973. const SCRIPT = `
  2974. const x = 1;
  2975. function f() {
  2976. eval("x = 2");
  2977. }
  2978. assert.throws(TypeError, function() {
  2979. f();
  2980. });
  2981. `
  2982. testScript1(TESTLIB+SCRIPT, _undefined, t)
  2983. }
  2984. func TestLexicalStrictNames(t *testing.T) {
  2985. const SCRIPT = `let eval = 1;`
  2986. _, err := Compile("", SCRIPT, true)
  2987. if err == nil {
  2988. t.Fatal("Expected an error")
  2989. }
  2990. }
  2991. func TestAssignAfterStackExpand(t *testing.T) {
  2992. // make sure the reference to the variable x does not remain stale after the stack is copied
  2993. const SCRIPT = `
  2994. function f() {
  2995. let sum = 0;
  2996. for (let i = 0; i < arguments.length; i++) {
  2997. sum += arguments[i];
  2998. }
  2999. return sum;
  3000. }
  3001. function testAssignment() {
  3002. var x = 0;
  3003. var scope = {};
  3004. with (scope) {
  3005. x = (scope.x = f(0, 0, 0, 0, 0, 0, 1, 1), 1);
  3006. }
  3007. if (scope.x !== 2) {
  3008. throw new Error('#1: scope.x === 2. Actual: ' + (scope.x));
  3009. }
  3010. if (x !== 1) {
  3011. throw new Error('#2: x === 1. Actual: ' + (x));
  3012. }
  3013. }
  3014. testAssignment();
  3015. `
  3016. testScript1(SCRIPT, _undefined, t)
  3017. }
  3018. func TestArgAccessFromDynamicStash(t *testing.T) {
  3019. const SCRIPT = `
  3020. function f(arg) {
  3021. function test() {
  3022. eval("");
  3023. return a;
  3024. }
  3025. return arg;
  3026. }
  3027. f(true);
  3028. `
  3029. testScript1(SCRIPT, valueTrue, t)
  3030. }
  3031. func TestLoadMixedLex(t *testing.T) {
  3032. const SCRIPT = `
  3033. function f() {
  3034. let a = 1;
  3035. {
  3036. function inner() {
  3037. eval("var a = true");
  3038. return a;
  3039. }
  3040. return inner();
  3041. }
  3042. }
  3043. f();
  3044. `
  3045. testScript1(SCRIPT, valueTrue, t)
  3046. }
  3047. func TestObjectLiteralSpread(t *testing.T) {
  3048. const SCRIPT = `
  3049. let src = {prop1: 1};
  3050. Object.defineProperty(src, "prop2", {value: 2, configurable: true});
  3051. Object.defineProperty(src, "prop3", {value: 3, enumerable: true, configurable: true});
  3052. let target = {prop4: 4, ...src};
  3053. assert(deepEqual(target, {prop1: 1, prop3: 3, prop4: 4}));
  3054. `
  3055. testScript1(TESTLIBX+SCRIPT, _undefined, t)
  3056. }
  3057. func TestArrayLiteralSpread(t *testing.T) {
  3058. const SCRIPT = `
  3059. let a1 = [1, 2];
  3060. let a2 = [3, 4];
  3061. let a = [...a1, 0, ...a2, 1];
  3062. assert(compareArray(a, [1, 2, 0, 3, 4, 1]));
  3063. `
  3064. testScript1(TESTLIB+SCRIPT, _undefined, t)
  3065. }
  3066. func TestObjectAssignmentPattern(t *testing.T) {
  3067. const SCRIPT = `
  3068. let a, b, c;
  3069. ({a, b, c=3} = {a: 1, b: 2});
  3070. assert.sameValue(a, 1, "a");
  3071. assert.sameValue(b, 2, "b");
  3072. assert.sameValue(c, 3, "c");
  3073. `
  3074. testScript1(TESTLIB+SCRIPT, _undefined, t)
  3075. }
  3076. func TestObjectAssignmentPatternNested(t *testing.T) {
  3077. const SCRIPT = `
  3078. let a, b, c, d;
  3079. ({a, b, c: {d} = 3} = {a: 1, b: 2, c: {d: 4}});
  3080. assert.sameValue(a, 1, "a");
  3081. assert.sameValue(b, 2, "b");
  3082. assert.sameValue(c, undefined, "c");
  3083. assert.sameValue(d, 4, "d");
  3084. `
  3085. testScript1(TESTLIB+SCRIPT, _undefined, t)
  3086. }
  3087. func TestObjectAssignmentPatternEvalOrder(t *testing.T) {
  3088. const SCRIPT = `
  3089. let trace = "";
  3090. let target_obj = {};
  3091. function src() {
  3092. trace += "src(),";
  3093. return {
  3094. get a() {
  3095. trace += "get a,";
  3096. return "a";
  3097. }
  3098. }
  3099. }
  3100. function prop1() {
  3101. trace += "prop1(),"
  3102. return {
  3103. toString: function() {
  3104. trace += "prop1-to-string(),";
  3105. return "a";
  3106. }
  3107. }
  3108. }
  3109. function prop2() {
  3110. trace += "prop2(),";
  3111. return {
  3112. toString: function() {
  3113. trace += "prop2-to-string(),";
  3114. return "b";
  3115. }
  3116. }
  3117. }
  3118. function target() {
  3119. trace += "target(),"
  3120. return target_obj;
  3121. }
  3122. let a, b;
  3123. ({[prop1()]: target().a, [prop2()]: b} = src());
  3124. if (target_obj.a !== "a") {
  3125. throw new Error("target_obj.a="+target_obj.a);
  3126. }
  3127. trace;
  3128. `
  3129. testScript1(SCRIPT, asciiString("src(),prop1(),prop1-to-string(),target(),get a,prop2(),prop2-to-string(),"), t)
  3130. }
  3131. func TestArrayAssignmentPatternEvalOrder(t *testing.T) {
  3132. const SCRIPT = `
  3133. let trace = "";
  3134. let src_arr = {
  3135. [Symbol.iterator]: function() {
  3136. let done = false;
  3137. return {
  3138. next: function() {
  3139. trace += "next,";
  3140. if (!done) {
  3141. done = true;
  3142. return {value: 0};
  3143. }
  3144. return {done: true};
  3145. },
  3146. return: function() {
  3147. trace += "return,";
  3148. }
  3149. }
  3150. }
  3151. }
  3152. function src() {
  3153. trace += "src(),";
  3154. return src_arr;
  3155. }
  3156. let tgt = {
  3157. get a() {
  3158. trace += "get a,";
  3159. return "a";
  3160. },
  3161. get b() {
  3162. trace += "get b,";
  3163. return "b";
  3164. }
  3165. }
  3166. function target() {
  3167. trace += "target(),";
  3168. return tgt;
  3169. }
  3170. function default_a() {
  3171. trace += "default a,";
  3172. return "def_a";
  3173. }
  3174. function default_b() {
  3175. trace += "default b,";
  3176. return "def_b";
  3177. }
  3178. ([target().a = default_a(), target().b = default_b()] = src());
  3179. trace;
  3180. `
  3181. testScript1(SCRIPT, asciiString("src(),target(),next,target(),next,default b,"), t)
  3182. }
  3183. func TestObjectAssignPatternRest(t *testing.T) {
  3184. const SCRIPT = `
  3185. let a, b, c, d;
  3186. ({a, b, c, ...d} = {a: 1, b: 2, d: 4});
  3187. assert.sameValue(a, 1, "a");
  3188. assert.sameValue(b, 2, "b");
  3189. assert.sameValue(c, undefined, "c");
  3190. assert(deepEqual(d, {d: 4}), "d");
  3191. `
  3192. testScript1(TESTLIBX+SCRIPT, _undefined, t)
  3193. }
  3194. func TestObjectBindPattern(t *testing.T) {
  3195. const SCRIPT = `
  3196. let {a, b, c, ...d} = {a: 1, b: 2, d: 4};
  3197. assert.sameValue(a, 1, "a");
  3198. assert.sameValue(b, 2, "b");
  3199. assert.sameValue(c, undefined, "c");
  3200. assert(deepEqual(d, {d: 4}), "d");
  3201. var { x: y, } = { x: 23 };
  3202. assert.sameValue(y, 23);
  3203. assert.throws(ReferenceError, function() {
  3204. x;
  3205. });
  3206. `
  3207. testScript1(TESTLIBX+SCRIPT, _undefined, t)
  3208. }
  3209. func TestObjLiteralShorthandWithInitializer(t *testing.T) {
  3210. const SCRIPT = `
  3211. o = {a=1};
  3212. `
  3213. _, err := Compile("", SCRIPT, false)
  3214. if err == nil {
  3215. t.Fatal("Expected an error")
  3216. }
  3217. }
  3218. func TestObjLiteralShorthandLetStringLit(t *testing.T) {
  3219. const SCRIPT = `
  3220. o = {"let"};
  3221. `
  3222. _, err := Compile("", SCRIPT, false)
  3223. if err == nil {
  3224. t.Fatal("Expected an error")
  3225. }
  3226. }
  3227. func TestObjLiteralComputedKeys(t *testing.T) {
  3228. const SCRIPT = `
  3229. let o = {
  3230. get [Symbol.toString]() {
  3231. }
  3232. }
  3233. `
  3234. testScript1(SCRIPT, _undefined, t)
  3235. }
  3236. func TestObjLiteralComputedKeysEvalOrder(t *testing.T) {
  3237. const SCRIPT = `
  3238. let trace = [];
  3239. function key() {
  3240. trace.push("key");
  3241. return {
  3242. toString: function() {
  3243. trace.push("key-toString");
  3244. return "key";
  3245. }
  3246. }
  3247. }
  3248. function val() {
  3249. trace.push("val");
  3250. return "val";
  3251. }
  3252. const _ = {
  3253. [key()]: val(),
  3254. }
  3255. trace.join(",");
  3256. `
  3257. testScript1(SCRIPT, asciiString("key,key-toString,val"), t)
  3258. }
  3259. func TestArrayAssignPattern(t *testing.T) {
  3260. const SCRIPT = `
  3261. let a, b;
  3262. ([a, b] = [1, 2]);
  3263. a === 1 && b === 2;
  3264. `
  3265. testScript1(SCRIPT, valueTrue, t)
  3266. }
  3267. func TestArrayAssignPattern1(t *testing.T) {
  3268. const SCRIPT = `
  3269. let a, b;
  3270. ([a = 3, b = 2] = [1]);
  3271. a === 1 && b === 2;
  3272. `
  3273. testScript1(SCRIPT, valueTrue, t)
  3274. }
  3275. func TestArrayAssignPatternLHS(t *testing.T) {
  3276. const SCRIPT = `
  3277. let a = {};
  3278. [ a.b, a['c'] = 2 ] = [1];
  3279. a.b === 1 && a.c === 2;
  3280. `
  3281. testScript1(SCRIPT, valueTrue, t)
  3282. }
  3283. func TestArrayAssignPatternElision(t *testing.T) {
  3284. const SCRIPT = `
  3285. let a, b;
  3286. ([a,, b] = [1, 4, 2]);
  3287. a === 1 && b === 2;
  3288. `
  3289. testScript1(SCRIPT, valueTrue, t)
  3290. }
  3291. func TestArrayAssignPatternRestPattern(t *testing.T) {
  3292. const SCRIPT = `
  3293. let a, b, z;
  3294. [ z, ...[a, b] ] = [0, 1, 2];
  3295. z === 0 && a === 1 && b === 2;
  3296. `
  3297. testScript1(SCRIPT, valueTrue, t)
  3298. }
  3299. func TestArrayBindingPattern(t *testing.T) {
  3300. const SCRIPT = `
  3301. let [a, b] = [1, 2];
  3302. a === 1 && b === 2;
  3303. `
  3304. testScript1(SCRIPT, valueTrue, t)
  3305. }
  3306. func TestObjectPatternShorthandInit(t *testing.T) {
  3307. const SCRIPT = `
  3308. [...{ x = 1 }] = [];
  3309. x;
  3310. `
  3311. testScript1(SCRIPT, valueInt(1), t)
  3312. }
  3313. func TestArrayBindingPatternRestPattern(t *testing.T) {
  3314. const SCRIPT = `
  3315. const [a, b, ...[c, d]] = [1, 2, 3, 4];
  3316. a === 1 && b === 2 && c === 3 && d === 4;
  3317. `
  3318. testScript1(SCRIPT, valueTrue, t)
  3319. }
  3320. func TestForVarPattern(t *testing.T) {
  3321. const SCRIPT = `
  3322. var o = {a: 1};
  3323. var trace = "";
  3324. for (var [key, value] of Object.entries(o)) {
  3325. trace += key+":"+value;
  3326. }
  3327. trace;
  3328. `
  3329. testScript1(SCRIPT, asciiString("a:1"), t)
  3330. }
  3331. func TestForLexPattern(t *testing.T) {
  3332. const SCRIPT = `
  3333. var o = {a: 1};
  3334. var trace = "";
  3335. for (const [key, value] of Object.entries(o)) {
  3336. trace += key+":"+value;
  3337. }
  3338. trace;
  3339. `
  3340. testScript1(SCRIPT, asciiString("a:1"), t)
  3341. }
  3342. func TestBindingPatternRestTrailingComma(t *testing.T) {
  3343. const SCRIPT = `
  3344. const [a, b, ...rest,] = [];
  3345. `
  3346. _, err := Compile("", SCRIPT, false)
  3347. if err == nil {
  3348. t.Fatal("Expected an error")
  3349. }
  3350. }
  3351. func TestAssignPatternRestTrailingComma(t *testing.T) {
  3352. const SCRIPT = `
  3353. ([a, b, ...rest,] = []);
  3354. `
  3355. _, err := Compile("", SCRIPT, false)
  3356. if err == nil {
  3357. t.Fatal("Expected an error")
  3358. }
  3359. }
  3360. func TestFuncParamInitializerSimple(t *testing.T) {
  3361. const SCRIPT = `
  3362. function f(a = 1) {
  3363. return a;
  3364. }
  3365. ""+f()+f(2);
  3366. `
  3367. testScript1(SCRIPT, asciiString("12"), t)
  3368. }
  3369. func TestFuncParamObjectPatternSimple(t *testing.T) {
  3370. const SCRIPT = `
  3371. function f({a, b} = {a: 1, b: 2}) {
  3372. return "" + a + b;
  3373. }
  3374. ""+f()+" "+f({a: 3, b: 4});
  3375. `
  3376. testScript1(SCRIPT, asciiString("12 34"), t)
  3377. }
  3378. func TestFuncParamRestStackSimple(t *testing.T) {
  3379. const SCRIPT = `
  3380. function f(arg1, ...rest) {
  3381. return rest;
  3382. }
  3383. let ar = f(1, 2, 3);
  3384. ar.join(",");
  3385. `
  3386. testScript1(SCRIPT, asciiString("2,3"), t)
  3387. }
  3388. func TestFuncParamRestStashSimple(t *testing.T) {
  3389. const SCRIPT = `
  3390. function f(arg1, ...rest) {
  3391. eval("true");
  3392. return rest;
  3393. }
  3394. let ar = f(1, 2, 3);
  3395. ar.join(",");
  3396. `
  3397. testScript1(SCRIPT, asciiString("2,3"), t)
  3398. }
  3399. func TestRestArgsNotInStash(t *testing.T) {
  3400. const SCRIPT = `
  3401. function f(...rest) {
  3402. () => rest;
  3403. return rest.length;
  3404. }
  3405. f(1,2);
  3406. `
  3407. testScript1(SCRIPT, valueInt(2), t)
  3408. }
  3409. func TestRestArgsInStash(t *testing.T) {
  3410. const SCRIPT = `
  3411. function f(first, ...rest) {
  3412. () => first;
  3413. () => rest;
  3414. return rest.length;
  3415. }
  3416. f(1,2);
  3417. `
  3418. testScript1(SCRIPT, valueInt(1), t)
  3419. }
  3420. func TestRestArgsInStashFwdRef(t *testing.T) {
  3421. const SCRIPT = `
  3422. function f(first = eval(), ...rest) {
  3423. () => first;
  3424. () => rest;
  3425. return rest.length === 1 && rest[0] === 2;
  3426. }
  3427. f(1,2);
  3428. `
  3429. testScript1(SCRIPT, valueTrue, t)
  3430. }
  3431. func TestFuncParamRestPattern(t *testing.T) {
  3432. const SCRIPT = `
  3433. function f(arg1, ...{0: rest1, 1: rest2}) {
  3434. return ""+arg1+" "+rest1+" "+rest2;
  3435. }
  3436. f(1, 2, 3);
  3437. `
  3438. testScript1(SCRIPT, asciiString("1 2 3"), t)
  3439. }
  3440. func TestFuncParamForwardRef(t *testing.T) {
  3441. const SCRIPT = `
  3442. function f(a = b + 1, b) {
  3443. return ""+a+" "+b;
  3444. }
  3445. f(1, 2);
  3446. `
  3447. testScript1(SCRIPT, asciiString("1 2"), t)
  3448. }
  3449. func TestFuncParamForwardRefMissing(t *testing.T) {
  3450. const SCRIPT = `
  3451. function f(a = b + 1, b) {
  3452. return ""+a+" "+b;
  3453. }
  3454. assert.throws(ReferenceError, function() {
  3455. f();
  3456. });
  3457. `
  3458. testScript1(TESTLIB+SCRIPT, _undefined, t)
  3459. }
  3460. func TestFuncParamInnerRef(t *testing.T) {
  3461. const SCRIPT = `
  3462. function f(a = inner) {
  3463. var inner = 42;
  3464. return a;
  3465. }
  3466. assert.throws(ReferenceError, function() {
  3467. f();
  3468. });
  3469. `
  3470. testScript1(TESTLIB+SCRIPT, _undefined, t)
  3471. }
  3472. func TestFuncParamInnerRefEval(t *testing.T) {
  3473. const SCRIPT = `
  3474. function f(a = eval("inner")) {
  3475. var inner = 42;
  3476. return a;
  3477. }
  3478. assert.throws(ReferenceError, function() {
  3479. f();
  3480. });
  3481. `
  3482. testScript1(TESTLIB+SCRIPT, _undefined, t)
  3483. }
  3484. func TestFuncParamCalleeName(t *testing.T) {
  3485. const SCRIPT = `
  3486. function f(a = f) {
  3487. var f;
  3488. return f;
  3489. }
  3490. typeof f();
  3491. `
  3492. testScript1(SCRIPT, asciiString("undefined"), t)
  3493. }
  3494. func TestFuncParamVarCopy(t *testing.T) {
  3495. const SCRIPT = `
  3496. function f(a = f) {
  3497. var a;
  3498. return a;
  3499. }
  3500. typeof f();
  3501. `
  3502. testScript1(SCRIPT, asciiString("function"), t)
  3503. }
  3504. func TestFuncParamScope(t *testing.T) {
  3505. const SCRIPT = `
  3506. var x = 'outside';
  3507. var probe1, probe2;
  3508. function f(
  3509. _ = probe1 = function() { return x; },
  3510. __ = (eval('var x = "inside";'), probe2 = function() { return x; })
  3511. ) {
  3512. }
  3513. f();
  3514. probe1()+" "+probe2();
  3515. `
  3516. testScript1(SCRIPT, asciiString("inside inside"), t)
  3517. }
  3518. func TestDefParamsStackPtr(t *testing.T) {
  3519. const SCRIPT = `
  3520. function A() {};
  3521. A.B = function () {};
  3522. function D(message = '') {
  3523. var C = A.B;
  3524. C([1,2,3]);
  3525. };
  3526. D();
  3527. `
  3528. testScript1(SCRIPT, _undefined, t)
  3529. }
  3530. func TestNestedVariadicCalls(t *testing.T) {
  3531. const SCRIPT = `
  3532. function f() {
  3533. return Array.prototype.join.call(arguments, ",");
  3534. }
  3535. f(...[1], "a", f(...[2]));
  3536. `
  3537. testScript1(SCRIPT, asciiString("1,a,2"), t)
  3538. }
  3539. func TestVariadicNew(t *testing.T) {
  3540. const SCRIPT = `
  3541. function C() {
  3542. this.res = Array.prototype.join.call(arguments, ",");
  3543. }
  3544. var c = new C(...[1], "a", new C(...[2]).res);
  3545. c.res;
  3546. `
  3547. testScript1(SCRIPT, asciiString("1,a,2"), t)
  3548. }
  3549. func TestVariadicUseStackVars(t *testing.T) {
  3550. const SCRIPT = `
  3551. function A(message) { return message; }
  3552. function B(...args){
  3553. return A(...args);
  3554. }
  3555. B("C");
  3556. `
  3557. testScript1(SCRIPT, asciiString("C"), t)
  3558. }
  3559. func TestCatchParamPattern(t *testing.T) {
  3560. const SCRIPT = `
  3561. function f() {
  3562. let x = 3;
  3563. try {
  3564. throw {a: 1, b: 2};
  3565. } catch ({a, b, c = x}) {
  3566. let x = 99;
  3567. return ""+a+" "+b+" "+c;
  3568. }
  3569. }
  3570. f();
  3571. `
  3572. testScript1(SCRIPT, asciiString("1 2 3"), t)
  3573. }
  3574. func TestArrowUseStrict(t *testing.T) {
  3575. // simple parameter list -- ok
  3576. _, err := Compile("", "(a) => {'use strict';}", false)
  3577. if err != nil {
  3578. t.Fatal(err)
  3579. }
  3580. // non-simple parameter list -- syntax error
  3581. _, err = Compile("", "(a=0) => {'use strict';}", false)
  3582. if err == nil {
  3583. t.Fatal("expected error")
  3584. }
  3585. }
  3586. func TestArrowBoxedThis(t *testing.T) {
  3587. const SCRIPT = `
  3588. var context;
  3589. fn = function() {
  3590. return (arg) => { var local; context = this; };
  3591. };
  3592. fn()();
  3593. context === this;
  3594. `
  3595. testScript1(SCRIPT, valueTrue, t)
  3596. }
  3597. func TestParameterOverride(t *testing.T) {
  3598. const SCRIPT = `
  3599. function f(arg) {
  3600. var arg = arg || "default"
  3601. return arg
  3602. }
  3603. f()
  3604. `
  3605. testScript1(SCRIPT, asciiString("default"), t)
  3606. }
  3607. func TestEvalInIterScope(t *testing.T) {
  3608. const SCRIPT = `
  3609. for (let a = 0; a < 1; a++) {
  3610. eval("a");
  3611. }
  3612. `
  3613. testScript1(SCRIPT, valueInt(0), t)
  3614. }
  3615. func TestTemplateLiterals(t *testing.T) {
  3616. vm := New()
  3617. _, err := vm.RunString("const a = 1, b = 'b';")
  3618. if err != nil {
  3619. t.Fatal(err)
  3620. }
  3621. f := func(t *testing.T, template, expected string) {
  3622. res, err := vm.RunString(template)
  3623. if err != nil {
  3624. t.Fatal(err)
  3625. }
  3626. if actual := res.Export(); actual != expected {
  3627. t.Fatalf("Expected: %q, actual: %q", expected, actual)
  3628. }
  3629. }
  3630. t.Run("empty", func(t *testing.T) {
  3631. f(t, "``", "")
  3632. })
  3633. t.Run("noSub", func(t *testing.T) {
  3634. f(t, "`test`", "test")
  3635. })
  3636. t.Run("emptyTail", func(t *testing.T) {
  3637. f(t, "`a=${a},b=${b}`", "a=1,b=b")
  3638. })
  3639. t.Run("emptyHead", func(t *testing.T) {
  3640. f(t, "`${a},b=${b}$`", "1,b=b$")
  3641. })
  3642. t.Run("headAndTail", func(t *testing.T) {
  3643. f(t, "`a=${a},b=${b}$`", "a=1,b=b$")
  3644. })
  3645. }
  3646. func TestTaggedTemplate(t *testing.T) {
  3647. const SCRIPT = `
  3648. let res;
  3649. const o = {
  3650. tmpl() {
  3651. res = this;
  3652. return () => {};
  3653. }
  3654. }
  3655. ` +
  3656. "o.tmpl()`test`;" + `
  3657. res === o;
  3658. `
  3659. testScript1(SCRIPT, valueTrue, t)
  3660. }
  3661. func TestDuplicateGlobalFunc(t *testing.T) {
  3662. const SCRIPT = `
  3663. function a(){}
  3664. function b(){ return "b" }
  3665. function c(){ return "c" }
  3666. function a(){}
  3667. b();
  3668. `
  3669. testScript1(SCRIPT, asciiString("b"), t)
  3670. }
  3671. func TestDuplicateFunc(t *testing.T) {
  3672. const SCRIPT = `
  3673. function f() {
  3674. function a(){}
  3675. function b(){ return "b" }
  3676. function c(){ return "c" }
  3677. function a(){}
  3678. return b();
  3679. }
  3680. f();
  3681. `
  3682. testScript1(SCRIPT, asciiString("b"), t)
  3683. }
  3684. /*
  3685. func TestBabel(t *testing.T) {
  3686. src, err := ioutil.ReadFile("babel7.js")
  3687. if err != nil {
  3688. t.Fatal(err)
  3689. }
  3690. vm := New()
  3691. _, err = vm.RunString(string(src))
  3692. if err != nil {
  3693. t.Fatal(err)
  3694. }
  3695. _, err = vm.RunString(`var result = Babel.transform("", {presets: ["es2015"]});`)
  3696. if err != nil {
  3697. t.Fatal(err)
  3698. }
  3699. }*/
  3700. func BenchmarkCompile(b *testing.B) {
  3701. data, err := ioutil.ReadFile("testdata/S15.10.2.12_A1_T1.js")
  3702. if err != nil {
  3703. b.Fatal(err)
  3704. }
  3705. src := string(data)
  3706. for i := 0; i < b.N; i++ {
  3707. _, err := Compile("test.js", src, false)
  3708. if err != nil {
  3709. b.Fatal(err)
  3710. }
  3711. }
  3712. }