compiler_test.go 54 KB

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