compiler_test.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248
  1. package goja
  2. import (
  3. "github.com/dop251/goja/parser"
  4. "io/ioutil"
  5. "os"
  6. "testing"
  7. )
  8. func testScript(script string, expectedResult Value, t *testing.T) {
  9. prg, err := parser.ParseFile(nil, "test.js", script, 0)
  10. if err != nil {
  11. t.Fatal(err)
  12. }
  13. c := newCompiler()
  14. c.compile(prg)
  15. r := &Runtime{}
  16. r.init()
  17. vm := r.vm
  18. vm.prg = c.p
  19. vm.prg.dumpCode(t.Logf)
  20. vm.run()
  21. vm.pop()
  22. t.Logf("stack size: %d", len(vm.stack))
  23. t.Logf("stashAllocs: %d", vm.stashAllocs)
  24. v := vm.r.globalObject.self.getStr("rv", nil)
  25. if v == nil {
  26. v = _undefined
  27. }
  28. if !v.SameAs(expectedResult) {
  29. t.Fatalf("Result: %+v, expected: %+v", v, expectedResult)
  30. }
  31. if vm.sp != 0 {
  32. t.Fatalf("sp: %d", vm.sp)
  33. }
  34. }
  35. func testScript1(script string, expectedResult Value, t *testing.T) {
  36. prg, err := parser.ParseFile(nil, "test.js", script, 0)
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. c := newCompiler()
  41. c.compile(prg)
  42. r := &Runtime{}
  43. r.init()
  44. vm := r.vm
  45. vm.prg = c.p
  46. vm.prg.dumpCode(t.Logf)
  47. vm.run()
  48. v := vm.pop()
  49. t.Logf("stack size: %d", len(vm.stack))
  50. t.Logf("stashAllocs: %d", vm.stashAllocs)
  51. if v == nil && expectedResult != nil || !v.SameAs(expectedResult) {
  52. t.Fatalf("Result: %+v, expected: %+v", v, expectedResult)
  53. }
  54. if vm.sp != 0 {
  55. t.Fatalf("sp: %d", vm.sp)
  56. }
  57. if l := len(vm.iterStack); l > 0 {
  58. t.Fatalf("iter stack is not empty: %d", l)
  59. }
  60. }
  61. func TestEmptyProgram(t *testing.T) {
  62. const SCRIPT = `
  63. `
  64. testScript1(SCRIPT, _undefined, t)
  65. }
  66. func TestErrorProto(t *testing.T) {
  67. const SCRIPT = `
  68. var e = new TypeError();
  69. e.name;
  70. `
  71. testScript1(SCRIPT, asciiString("TypeError"), t)
  72. }
  73. func TestThis1(t *testing.T) {
  74. const SCRIPT = `
  75. function independent() {
  76. return this.prop;
  77. }
  78. var o = {};
  79. o.b = {g: independent, prop: 42};
  80. var rv = o.b.g();
  81. `
  82. testScript(SCRIPT, intToValue(42), t)
  83. }
  84. func TestThis2(t *testing.T) {
  85. const SCRIPT = `
  86. var o = {
  87. prop: 37,
  88. f: function() {
  89. return this.prop;
  90. }
  91. };
  92. var rv = o.f();
  93. `
  94. testScript(SCRIPT, intToValue(37), t)
  95. }
  96. func TestThisStrict(t *testing.T) {
  97. const SCRIPT = `
  98. "use strict";
  99. Object.defineProperty(Object.prototype, "x", { get: function () { return this; } });
  100. (5).x === 5;
  101. `
  102. testScript1(SCRIPT, valueTrue, t)
  103. }
  104. func TestThisNoStrict(t *testing.T) {
  105. const SCRIPT = `
  106. Object.defineProperty(Object.prototype, "x", { get: function () { return this; } });
  107. (5).x == 5;
  108. `
  109. testScript1(SCRIPT, valueTrue, t)
  110. }
  111. func TestCallLessArgs(t *testing.T) {
  112. const SCRIPT = `
  113. function A(a, b, c) {
  114. return String(a) + " " + String(b) + " " + String(c);
  115. }
  116. var rv = A(1, 2);
  117. `
  118. testScript(SCRIPT, asciiString("1 2 undefined"), t)
  119. }
  120. func TestCallMoreArgs(t *testing.T) {
  121. const SCRIPT = `
  122. function A(a, b) {
  123. var c = 4;
  124. return a - b + c;
  125. }
  126. var rv = A(1, 2, 3);
  127. `
  128. testScript(SCRIPT, intToValue(3), t)
  129. }
  130. func TestCallMoreArgsDynamic(t *testing.T) {
  131. const SCRIPT = `
  132. function A(a, b) {
  133. var c = 4;
  134. if (false) {
  135. eval("");
  136. }
  137. return a - b + c;
  138. }
  139. var rv = A(1, 2, 3);
  140. `
  141. testScript(SCRIPT, intToValue(3), t)
  142. }
  143. func TestCallLessArgsDynamic(t *testing.T) {
  144. const SCRIPT = `
  145. function A(a, b, c) {
  146. // Make it stashful
  147. function B() {
  148. return a;
  149. }
  150. return String(a) + " " + String(b) + " " + String(c);
  151. }
  152. var rv = A(1, 2);
  153. `
  154. testScript(SCRIPT, asciiString("1 2 undefined"), t)
  155. }
  156. func TestCallLessArgsDynamicLocalVar(t *testing.T) {
  157. const SCRIPT = `
  158. function f(param) {
  159. var a = 42;
  160. if (false) {
  161. eval("");
  162. }
  163. return a;
  164. }
  165. f();
  166. `
  167. testScript1(SCRIPT, intToValue(42), t)
  168. }
  169. /*
  170. func TestFib(t *testing.T) {
  171. testScript(TEST_FIB, valueInt(9227465), t)
  172. }
  173. */
  174. func TestNativeCall(t *testing.T) {
  175. const SCRIPT = `
  176. var o = Object(1);
  177. Object.defineProperty(o, "test", {value: 42});
  178. var rv = o.test;
  179. `
  180. testScript(SCRIPT, intToValue(42), t)
  181. }
  182. func TestJSCall(t *testing.T) {
  183. const SCRIPT = `
  184. function getter() {
  185. return this.x;
  186. }
  187. var o = Object(1);
  188. o.x = 42;
  189. Object.defineProperty(o, "test", {get: getter});
  190. var rv = o.test;
  191. `
  192. testScript(SCRIPT, intToValue(42), t)
  193. }
  194. func TestLoop1(t *testing.T) {
  195. const SCRIPT = `
  196. function A() {
  197. var x = 1;
  198. for (var i = 0; i < 1; i++) {
  199. var x = 2;
  200. }
  201. return x;
  202. }
  203. var rv = A();
  204. `
  205. testScript(SCRIPT, intToValue(2), t)
  206. }
  207. func TestLoopBreak(t *testing.T) {
  208. const SCRIPT = `
  209. function A() {
  210. var x = 1;
  211. for (var i = 0; i < 1; i++) {
  212. break;
  213. var x = 2;
  214. }
  215. return x;
  216. }
  217. var rv = A();
  218. `
  219. testScript(SCRIPT, intToValue(1), t)
  220. }
  221. func TestForLoopOptionalExpr(t *testing.T) {
  222. const SCRIPT = `
  223. function A() {
  224. var x = 1;
  225. for (;;) {
  226. break;
  227. var x = 2;
  228. }
  229. return x;
  230. }
  231. var rv = A();
  232. `
  233. testScript(SCRIPT, intToValue(1), t)
  234. }
  235. func TestBlockBreak(t *testing.T) {
  236. const SCRIPT = `
  237. var rv = 0;
  238. B1: {
  239. rv = 1;
  240. B2: {
  241. rv = 2;
  242. break B1;
  243. }
  244. rv = 3;
  245. }
  246. `
  247. testScript(SCRIPT, intToValue(2), t)
  248. }
  249. func TestTry(t *testing.T) {
  250. const SCRIPT = `
  251. function A() {
  252. var x = 1;
  253. try {
  254. x = 2;
  255. } catch(e) {
  256. x = 3;
  257. } finally {
  258. x = 4;
  259. }
  260. return x;
  261. }
  262. var rv = A();
  263. `
  264. testScript(SCRIPT, intToValue(4), t)
  265. }
  266. func TestTryCatch(t *testing.T) {
  267. const SCRIPT = `
  268. function A() {
  269. var x;
  270. try {
  271. throw 4;
  272. } catch(e) {
  273. x = e;
  274. }
  275. return x;
  276. }
  277. var rv = A();
  278. `
  279. testScript(SCRIPT, intToValue(4), t)
  280. }
  281. func TestTryExceptionInCatch(t *testing.T) {
  282. const SCRIPT = `
  283. function A() {
  284. var x;
  285. try {
  286. throw 4;
  287. } catch(e) {
  288. throw 5;
  289. }
  290. return x;
  291. }
  292. var rv;
  293. try {
  294. A();
  295. } catch (e) {
  296. rv = e;
  297. }
  298. `
  299. testScript(SCRIPT, intToValue(5), t)
  300. }
  301. func TestTryContinueInFinally(t *testing.T) {
  302. const SCRIPT = `
  303. var c3 = 0, fin3 = 0;
  304. while (c3 < 2) {
  305. try {
  306. throw "ex1";
  307. } catch(er1) {
  308. c3 += 1;
  309. } finally {
  310. fin3 = 1;
  311. continue;
  312. }
  313. fin3 = 0;
  314. }
  315. fin3;
  316. `
  317. testScript1(SCRIPT, intToValue(1), t)
  318. }
  319. func TestCatchLexicalEnv(t *testing.T) {
  320. const SCRIPT = `
  321. function F() {
  322. try {
  323. throw 1;
  324. } catch (e) {
  325. var x = e;
  326. }
  327. return x;
  328. }
  329. F();
  330. `
  331. testScript1(SCRIPT, intToValue(1), t)
  332. }
  333. func TestThrowType(t *testing.T) {
  334. const SCRIPT = `
  335. function Exception(message) {
  336. this.message = message;
  337. }
  338. function A() {
  339. try {
  340. throw new Exception("boo!");
  341. } catch(e) {
  342. return e;
  343. }
  344. }
  345. var thrown = A();
  346. var rv = thrown !== null && typeof thrown === "object" && thrown.constructor === Exception;
  347. `
  348. testScript(SCRIPT, valueTrue, t)
  349. }
  350. func TestThrowConstructorName(t *testing.T) {
  351. const SCRIPT = `
  352. function Exception(message) {
  353. this.message = message;
  354. }
  355. function A() {
  356. try {
  357. throw new Exception("boo!");
  358. } catch(e) {
  359. return e;
  360. }
  361. }
  362. A().constructor.name;
  363. `
  364. testScript1(SCRIPT, asciiString("Exception"), t)
  365. }
  366. func TestThrowNativeConstructorName(t *testing.T) {
  367. const SCRIPT = `
  368. function A() {
  369. try {
  370. throw new TypeError();
  371. } catch(e) {
  372. return e;
  373. }
  374. }
  375. A().constructor.name;
  376. `
  377. testScript1(SCRIPT, asciiString("TypeError"), t)
  378. }
  379. func TestEmptyTryNoCatch(t *testing.T) {
  380. const SCRIPT = `
  381. var called = false;
  382. try {
  383. } finally {
  384. called = true;
  385. }
  386. called;
  387. `
  388. testScript1(SCRIPT, valueTrue, t)
  389. }
  390. func TestIfElse(t *testing.T) {
  391. const SCRIPT = `
  392. var rv;
  393. if (rv === undefined) {
  394. rv = "passed";
  395. } else {
  396. rv = "failed";
  397. }
  398. `
  399. testScript(SCRIPT, asciiString("passed"), t)
  400. }
  401. func TestIfElseRetVal(t *testing.T) {
  402. const SCRIPT = `
  403. var x;
  404. if (x === undefined) {
  405. "passed";
  406. } else {
  407. "failed";
  408. }
  409. `
  410. testScript1(SCRIPT, asciiString("passed"), t)
  411. }
  412. func TestWhileReturnValue(t *testing.T) {
  413. const SCRIPT = `
  414. var x = 0;
  415. while(true) {
  416. x = 1;
  417. break;
  418. }
  419. `
  420. testScript1(SCRIPT, intToValue(1), t)
  421. }
  422. func TestIfElseLabel(t *testing.T) {
  423. const SCRIPT = `
  424. var x = 0;
  425. abc: if (true) {
  426. x = 1;
  427. break abc;
  428. }
  429. `
  430. testScript1(SCRIPT, intToValue(1), t)
  431. }
  432. func TestIfMultipleLabels(t *testing.T) {
  433. const SCRIPT = `
  434. var x = 0;
  435. xyz:abc: if (true) {
  436. break xyz;
  437. }
  438. `
  439. testScript1(SCRIPT, _undefined, t)
  440. }
  441. func TestBreakOutOfTry(t *testing.T) {
  442. const SCRIPT = `
  443. function A() {
  444. var x = 1;
  445. B: {
  446. try {
  447. x = 2;
  448. } catch(e) {
  449. x = 3;
  450. } finally {
  451. break B;
  452. x = 4;
  453. }
  454. }
  455. return x;
  456. }
  457. A();
  458. `
  459. testScript1(SCRIPT, intToValue(2), t)
  460. }
  461. func TestReturnOutOfTryNested(t *testing.T) {
  462. const SCRIPT = `
  463. function A() {
  464. function nested() {
  465. try {
  466. return 1;
  467. } catch(e) {
  468. return 2;
  469. }
  470. }
  471. return nested();
  472. }
  473. A();
  474. `
  475. testScript1(SCRIPT, intToValue(1), t)
  476. }
  477. func TestContinueLoop(t *testing.T) {
  478. const SCRIPT = `
  479. function A() {
  480. var r = 0;
  481. for (var i = 0; i < 5; i++) {
  482. if (i > 1) {
  483. continue;
  484. }
  485. r++;
  486. }
  487. return r;
  488. }
  489. A();
  490. `
  491. testScript1(SCRIPT, intToValue(2), t)
  492. }
  493. func TestContinueOutOfTry(t *testing.T) {
  494. const SCRIPT = `
  495. function A() {
  496. var r = 0;
  497. for (var i = 0; i < 5; i++) {
  498. try {
  499. if (i > 1) {
  500. continue;
  501. }
  502. } catch(e) {
  503. return 99;
  504. }
  505. r++;
  506. }
  507. return r;
  508. }
  509. A();
  510. `
  511. testScript1(SCRIPT, intToValue(2), t)
  512. }
  513. func TestThisInCatch(t *testing.T) {
  514. const SCRIPT = `
  515. function O() {
  516. try {
  517. f();
  518. } catch (e) {
  519. this.value = e.toString();
  520. }
  521. }
  522. function f() {
  523. throw "ex";
  524. }
  525. var o = new O();
  526. o.value;
  527. `
  528. testScript1(SCRIPT, asciiString("ex"), t)
  529. }
  530. func TestNestedTry(t *testing.T) {
  531. const SCRIPT = `
  532. var ex;
  533. try {
  534. throw "ex1";
  535. } catch (er1) {
  536. try {
  537. throw "ex2";
  538. } catch (er1) {
  539. ex = er1;
  540. }
  541. }
  542. ex;
  543. `
  544. testScript1(SCRIPT, asciiString("ex2"), t)
  545. }
  546. func TestNestedTryInStashlessFunc(t *testing.T) {
  547. const SCRIPT = `
  548. function f() {
  549. var ex1, ex2;
  550. try {
  551. throw "ex1";
  552. } catch (er1) {
  553. try {
  554. throw "ex2";
  555. } catch (er1) {
  556. ex2 = er1;
  557. }
  558. ex1 = er1;
  559. }
  560. return ex1 == "ex1" && ex2 == "ex2";
  561. }
  562. f();
  563. `
  564. testScript1(SCRIPT, valueTrue, t)
  565. }
  566. func TestEvalInCatchInStashlessFunc(t *testing.T) {
  567. const SCRIPT = `
  568. function f() {
  569. var ex;
  570. try {
  571. throw "ex1";
  572. } catch (er1) {
  573. eval("ex = er1");
  574. }
  575. return ex;
  576. }
  577. f();
  578. `
  579. testScript1(SCRIPT, asciiString("ex1"), t)
  580. }
  581. func TestCatchClosureInStashlessFunc(t *testing.T) {
  582. const SCRIPT = `
  583. function f() {
  584. var ex;
  585. try {
  586. throw "ex1";
  587. } catch (er1) {
  588. return function() {
  589. return er1;
  590. }
  591. }
  592. }
  593. f()();
  594. `
  595. testScript1(SCRIPT, asciiString("ex1"), t)
  596. }
  597. func TestCatchVarNotUsedInStashlessFunc(t *testing.T) {
  598. const SCRIPT = `
  599. function f() {
  600. var ex;
  601. try {
  602. throw "ex1";
  603. } catch (er1) {
  604. ex = "ok";
  605. }
  606. return ex;
  607. }
  608. f();
  609. `
  610. testScript1(SCRIPT, asciiString("ok"), t)
  611. }
  612. func TestNew(t *testing.T) {
  613. const SCRIPT = `
  614. function O() {
  615. this.x = 42;
  616. }
  617. new O().x;
  618. `
  619. testScript1(SCRIPT, intToValue(42), t)
  620. }
  621. func TestStringConstructor(t *testing.T) {
  622. const SCRIPT = `
  623. function F() {
  624. return String(33) + " " + String("cows");
  625. }
  626. F();
  627. `
  628. testScript1(SCRIPT, asciiString("33 cows"), t)
  629. }
  630. func TestError(t *testing.T) {
  631. const SCRIPT = `
  632. function F() {
  633. return new Error("test");
  634. }
  635. var e = F();
  636. var rv = e.message == "test" && e.name == "Error";
  637. `
  638. testScript(SCRIPT, valueTrue, t)
  639. }
  640. func TestTypeError(t *testing.T) {
  641. const SCRIPT = `
  642. function F() {
  643. return new TypeError("test");
  644. }
  645. var e = F();
  646. e.message == "test" && e.name == "TypeError";
  647. `
  648. testScript1(SCRIPT, valueTrue, t)
  649. }
  650. func TestToString(t *testing.T) {
  651. const SCRIPT = `
  652. var o = {x: 42};
  653. o.toString = function() {
  654. return String(this.x);
  655. }
  656. var o1 = {};
  657. o.toString() + " ### " + o1.toString();
  658. `
  659. testScript1(SCRIPT, asciiString("42 ### [object Object]"), t)
  660. }
  661. func TestEvalOrder(t *testing.T) {
  662. const SCRIPT = `
  663. var o = {f: function() {return 42}, x: 0};
  664. var trace = "";
  665. function F1() {
  666. trace += "First!";
  667. return o;
  668. }
  669. function F2() {
  670. trace += "Second!";
  671. return "f";
  672. }
  673. function F3() {
  674. trace += "Third!";
  675. }
  676. var rv = F1()[F2()](F3());
  677. rv += trace;
  678. `
  679. testScript(SCRIPT, asciiString("42First!Second!Third!"), t)
  680. }
  681. func TestPostfixIncBracket(t *testing.T) {
  682. const SCRIPT = `
  683. var o = {x: 42};
  684. var trace = "";
  685. function F1() {
  686. trace += "First!";
  687. return o;
  688. }
  689. function F2() {
  690. trace += "Second!";
  691. return "x";
  692. }
  693. var rv = F1()[F2()]++;
  694. rv += trace + o.x;
  695. `
  696. testScript(SCRIPT, asciiString("42First!Second!43"), t)
  697. }
  698. func TestPostfixIncDot(t *testing.T) {
  699. const SCRIPT = `
  700. var o = {x: 42};
  701. var trace = "";
  702. function F1() {
  703. trace += "First!";
  704. return o;
  705. }
  706. var rv = F1().x++;
  707. rv += trace + o.x;
  708. `
  709. testScript(SCRIPT, asciiString("42First!43"), t)
  710. }
  711. func TestPrefixIncBracket(t *testing.T) {
  712. const SCRIPT = `
  713. var o = {x: 42};
  714. var trace = "";
  715. function F1() {
  716. trace += "First!";
  717. return o;
  718. }
  719. function F2() {
  720. trace += "Second!";
  721. return "x";
  722. }
  723. var rv = ++F1()[F2()];
  724. rv += trace + o.x;
  725. `
  726. testScript(SCRIPT, asciiString("43First!Second!43"), t)
  727. }
  728. func TestPrefixIncDot(t *testing.T) {
  729. const SCRIPT = `
  730. var o = {x: 42};
  731. var trace = "";
  732. function F1() {
  733. trace += "First!";
  734. return o;
  735. }
  736. var rv = ++F1().x;
  737. rv += trace + o.x;
  738. `
  739. testScript(SCRIPT, asciiString("43First!43"), t)
  740. }
  741. func TestPostDecObj(t *testing.T) {
  742. const SCRIPT = `
  743. var object = {valueOf: function() {return 1}};
  744. var y = object--;
  745. var ok = false;
  746. if (y === 1) {
  747. ok = true;
  748. }
  749. ok;
  750. `
  751. testScript1(SCRIPT, valueTrue, t)
  752. }
  753. func TestPropAcc1(t *testing.T) {
  754. const SCRIPT = `
  755. 1..toString()
  756. `
  757. testScript1(SCRIPT, asciiString("1"), t)
  758. }
  759. func TestEvalDirect(t *testing.T) {
  760. const SCRIPT = `
  761. var rv = false;
  762. function foo(){ rv = true; }
  763. var o = { };
  764. function f() {
  765. try {
  766. eval("o.bar( foo() );");
  767. } catch (e) {
  768. }
  769. }
  770. f();
  771. `
  772. testScript(SCRIPT, valueTrue, t)
  773. }
  774. func TestEvalRet(t *testing.T) {
  775. const SCRIPT = `
  776. eval("for (var i = 0; i < 3; i++) {i}")
  777. `
  778. testScript1(SCRIPT, valueInt(2), t)
  779. }
  780. func TestEvalFunctionDecl(t *testing.T) {
  781. const SCRIPT = `
  782. eval("function F() {}")
  783. `
  784. testScript1(SCRIPT, _undefined, t)
  785. }
  786. func TestEvalFunctionExpr(t *testing.T) {
  787. const SCRIPT = `
  788. eval("(function F() {return 42;})")()
  789. `
  790. testScript1(SCRIPT, intToValue(42), t)
  791. }
  792. func TestForLoopRet(t *testing.T) {
  793. const SCRIPT = `
  794. for (var i = 0; i < 20; i++) { if (i > 2) {break;} else { i }}
  795. `
  796. testScript1(SCRIPT, _undefined, t)
  797. }
  798. func TestForLoopRet1(t *testing.T) {
  799. const SCRIPT = `
  800. for (var i = 0; i < 20; i++) { if (i > 2) {42;; {L:{break;}}} else { i }}
  801. `
  802. testScript1(SCRIPT, intToValue(42), t)
  803. }
  804. func TestForInLoopRet(t *testing.T) {
  805. const SCRIPT = `
  806. var o = [1, 2, 3, 4];
  807. for (var i in o) { if (i > 2) {break;} else { i }}
  808. `
  809. testScript1(SCRIPT, _undefined, t)
  810. }
  811. func TestForInLoopRet1(t *testing.T) {
  812. const SCRIPT = `
  813. var o = {};
  814. o.x = 1;
  815. o.y = 2;
  816. for (var i in o) {
  817. true;
  818. }
  819. `
  820. testScript1(SCRIPT, valueTrue, t)
  821. }
  822. func TestDoWhileLoopRet(t *testing.T) {
  823. const SCRIPT = `
  824. var i = 0;
  825. do {
  826. if (i > 2) {
  827. break;
  828. } else {
  829. i;
  830. }
  831. } while (i++ < 20);
  832. `
  833. testScript1(SCRIPT, _undefined, t)
  834. }
  835. func TestDoWhileContinueRet(t *testing.T) {
  836. const SCRIPT = `
  837. var i = 0;
  838. do {
  839. if (i > 2) {
  840. true;
  841. continue;
  842. } else {
  843. i;
  844. }
  845. } while (i++ < 20);
  846. `
  847. testScript1(SCRIPT, valueTrue, t)
  848. }
  849. func TestWhileLoopRet(t *testing.T) {
  850. const SCRIPT = `
  851. var i; while (i < 20) { if (i > 2) {break;} else { i++ }}
  852. `
  853. testScript1(SCRIPT, _undefined, t)
  854. }
  855. func TestLoopRet1(t *testing.T) {
  856. const SCRIPT = `
  857. for (var i = 0; i < 20; i++) { }
  858. `
  859. testScript1(SCRIPT, _undefined, t)
  860. }
  861. func TestInstanceof(t *testing.T) {
  862. const SCRIPT = `
  863. var rv;
  864. try {
  865. true();
  866. } catch (e) {
  867. rv = e instanceof TypeError;
  868. }
  869. `
  870. testScript(SCRIPT, valueTrue, t)
  871. }
  872. func TestStrictAssign(t *testing.T) {
  873. const SCRIPT = `
  874. 'use strict';
  875. var rv;
  876. var called = false;
  877. function F() {
  878. called = true;
  879. return 1;
  880. }
  881. try {
  882. x = F();
  883. } catch (e) {
  884. rv = e instanceof ReferenceError;
  885. }
  886. rv += " " + called;
  887. `
  888. testScript(SCRIPT, asciiString("true true"), t)
  889. }
  890. func TestStrictScope(t *testing.T) {
  891. const SCRIPT = `
  892. var rv;
  893. var called = false;
  894. function F() {
  895. 'use strict';
  896. x = 1;
  897. }
  898. try {
  899. F();
  900. } catch (e) {
  901. rv = e instanceof ReferenceError;
  902. }
  903. x = 1;
  904. rv += " " + x;
  905. `
  906. testScript(SCRIPT, asciiString("true 1"), t)
  907. }
  908. func TestStringObj(t *testing.T) {
  909. const SCRIPT = `
  910. var s = new String("test");
  911. s[0] + s[2] + s[1];
  912. `
  913. testScript1(SCRIPT, asciiString("tse"), t)
  914. }
  915. func TestStringPrimitive(t *testing.T) {
  916. const SCRIPT = `
  917. var s = "test";
  918. s[0] + s[2] + s[1];
  919. `
  920. testScript1(SCRIPT, asciiString("tse"), t)
  921. }
  922. func TestCallGlobalObject(t *testing.T) {
  923. const SCRIPT = `
  924. var rv;
  925. try {
  926. this();
  927. } catch (e) {
  928. rv = e instanceof TypeError
  929. }
  930. `
  931. testScript(SCRIPT, valueTrue, t)
  932. }
  933. func TestFuncLength(t *testing.T) {
  934. const SCRIPT = `
  935. function F(x, y) {
  936. }
  937. F.length
  938. `
  939. testScript1(SCRIPT, intToValue(2), t)
  940. }
  941. func TestNativeFuncLength(t *testing.T) {
  942. const SCRIPT = `
  943. eval.length + Object.defineProperty.length + String.length
  944. `
  945. testScript1(SCRIPT, intToValue(5), t)
  946. }
  947. func TestArguments(t *testing.T) {
  948. const SCRIPT = `
  949. function F() {
  950. return arguments.length + " " + arguments[1];
  951. }
  952. F(1,2,3)
  953. `
  954. testScript1(SCRIPT, asciiString("3 2"), t)
  955. }
  956. func TestArgumentsPut(t *testing.T) {
  957. const SCRIPT = `
  958. function F(x, y) {
  959. arguments[0] -= arguments[1];
  960. return x;
  961. }
  962. F(5, 2)
  963. `
  964. testScript1(SCRIPT, intToValue(3), t)
  965. }
  966. func TestArgumentsPutStrict(t *testing.T) {
  967. const SCRIPT = `
  968. function F(x, y) {
  969. 'use strict';
  970. arguments[0] -= arguments[1];
  971. return x;
  972. }
  973. F(5, 2)
  974. `
  975. testScript1(SCRIPT, intToValue(5), t)
  976. }
  977. func TestArgumentsExtra(t *testing.T) {
  978. const SCRIPT = `
  979. function F(x, y) {
  980. return arguments[2];
  981. }
  982. F(1, 2, 42)
  983. `
  984. testScript1(SCRIPT, intToValue(42), t)
  985. }
  986. func TestArgumentsExist(t *testing.T) {
  987. const SCRIPT = `
  988. function F(x, arguments) {
  989. return arguments;
  990. }
  991. F(1, 42)
  992. `
  993. testScript1(SCRIPT, intToValue(42), t)
  994. }
  995. func TestArgumentsDelete(t *testing.T) {
  996. const SCRIPT = `
  997. function f(x) {
  998. delete arguments[0];
  999. arguments[0] = 42;
  1000. return x;
  1001. }
  1002. f(1)
  1003. `
  1004. testScript1(SCRIPT, intToValue(1), t)
  1005. }
  1006. func TestWith(t *testing.T) {
  1007. const SCRIPT = `
  1008. var b = 1;
  1009. var o = {a: 41};
  1010. with(o) {
  1011. a += b;
  1012. }
  1013. o.a;
  1014. `
  1015. testScript1(SCRIPT, intToValue(42), t)
  1016. }
  1017. func TestWithInFunc(t *testing.T) {
  1018. const SCRIPT = `
  1019. function F() {
  1020. var b = 1;
  1021. var c = 0;
  1022. var o = {a: 40, c: 1};
  1023. with(o) {
  1024. a += b + c;
  1025. }
  1026. return o.a;
  1027. }
  1028. F();
  1029. `
  1030. testScript1(SCRIPT, intToValue(42), t)
  1031. }
  1032. func TestAssignNonExtendable(t *testing.T) {
  1033. const SCRIPT = `
  1034. 'use strict';
  1035. function F() {
  1036. this.x = 1;
  1037. }
  1038. var o = new F();
  1039. Object.preventExtensions(o);
  1040. o.x = 42;
  1041. o.x;
  1042. `
  1043. testScript1(SCRIPT, intToValue(42), t)
  1044. }
  1045. func TestAssignNonExtendable1(t *testing.T) {
  1046. const SCRIPT = `
  1047. 'use strict';
  1048. function F() {
  1049. }
  1050. var o = new F();
  1051. var rv;
  1052. Object.preventExtensions(o);
  1053. try {
  1054. o.x = 42;
  1055. } catch (e) {
  1056. rv = e.constructor === TypeError;
  1057. }
  1058. rv += " " + o.x;
  1059. `
  1060. testScript(SCRIPT, asciiString("true undefined"), t)
  1061. }
  1062. func TestAssignStrict(t *testing.T) {
  1063. const SCRIPT = `
  1064. 'use strict';
  1065. try {
  1066. eval("eval = 42");
  1067. } catch(e) {
  1068. var rv = e instanceof SyntaxError
  1069. }
  1070. `
  1071. testScript(SCRIPT, valueTrue, t)
  1072. }
  1073. func TestIllegalArgmentName(t *testing.T) {
  1074. const SCRIPT = `
  1075. 'use strict';
  1076. try {
  1077. eval("function F(eval) {}");
  1078. } catch (e) {
  1079. var rv = e instanceof SyntaxError
  1080. }
  1081. `
  1082. testScript(SCRIPT, valueTrue, t)
  1083. }
  1084. func TestFunction(t *testing.T) {
  1085. const SCRIPT = `
  1086. var f0 = Function("");
  1087. var f1 = Function("return ' one'");
  1088. var f2 = Function("arg", "return ' ' + arg");
  1089. f0() + f1() + f2("two");
  1090. `
  1091. testScript1(SCRIPT, asciiString("undefined one two"), t)
  1092. }
  1093. func TestFunction1(t *testing.T) {
  1094. const SCRIPT = `
  1095. var f = function f1(count) {
  1096. if (count == 0) {
  1097. return true;
  1098. }
  1099. return f1(count-1);
  1100. }
  1101. f(1);
  1102. `
  1103. testScript1(SCRIPT, valueTrue, t)
  1104. }
  1105. func TestFunction2(t *testing.T) {
  1106. const SCRIPT = `
  1107. var trace = "";
  1108. function f(count) {
  1109. trace += "f("+count+")";
  1110. if (count == 0) {
  1111. return;
  1112. }
  1113. return f(count-1);
  1114. }
  1115. function f1() {
  1116. trace += "f1";
  1117. }
  1118. var f2 = f;
  1119. f = f1;
  1120. f2(1);
  1121. trace;
  1122. `
  1123. testScript1(SCRIPT, asciiString("f(1)f1"), t)
  1124. }
  1125. func TestFunctionToString(t *testing.T) {
  1126. const SCRIPT = `
  1127. Function("arg1", "arg2", "return 42").toString();
  1128. `
  1129. testScript1(SCRIPT, asciiString("function anonymous(arg1,arg2){return 42}"), t)
  1130. }
  1131. func TestObjectLiteral(t *testing.T) {
  1132. const SCRIPT = `
  1133. var getterCalled = false;
  1134. var setterCalled = false;
  1135. var o = {get x() {getterCalled = true}, set x() {setterCalled = true}};
  1136. o.x;
  1137. o.x = 42;
  1138. getterCalled && setterCalled;
  1139. `
  1140. testScript1(SCRIPT, valueTrue, t)
  1141. }
  1142. func TestConst(t *testing.T) {
  1143. const SCRIPT = `
  1144. var v1 = true && true;
  1145. var v2 = 1/(-1 * 0);
  1146. var v3 = 1 == 2 || v1;
  1147. var v4 = true && false
  1148. v1 === true && v2 === -Infinity && v3 === v1 && v4 === false;
  1149. `
  1150. testScript1(SCRIPT, valueTrue, t)
  1151. }
  1152. func TestConstWhile(t *testing.T) {
  1153. const SCRIPT = `
  1154. var c = 0;
  1155. while (2 + 2 === 4) {
  1156. if (++c > 9) {
  1157. break;
  1158. }
  1159. }
  1160. c === 10;
  1161. `
  1162. testScript1(SCRIPT, valueTrue, t)
  1163. }
  1164. func TestConstWhileThrow(t *testing.T) {
  1165. const SCRIPT = `
  1166. var thrown = false;
  1167. try {
  1168. while ('s' in true) {
  1169. break;
  1170. }
  1171. } catch (e) {
  1172. thrown = e instanceof TypeError
  1173. }
  1174. thrown;
  1175. `
  1176. testScript1(SCRIPT, valueTrue, t)
  1177. }
  1178. func TestDupParams(t *testing.T) {
  1179. const SCRIPT = `
  1180. function F(x, y, x) {
  1181. return x;
  1182. }
  1183. F(1, 2);
  1184. `
  1185. testScript1(SCRIPT, _undefined, t)
  1186. }
  1187. func TestUseUnsuppliedParam(t *testing.T) {
  1188. const SCRIPT = `
  1189. function getMessage(message) {
  1190. if (message === undefined) {
  1191. message = '';
  1192. }
  1193. message += " 123 456";
  1194. return message;
  1195. }
  1196. getMessage();
  1197. `
  1198. testScript1(SCRIPT, asciiString(" 123 456"), t)
  1199. }
  1200. func TestForInLoop(t *testing.T) {
  1201. const SCRIPT = `
  1202. function Proto() {}
  1203. Proto.prototype.x = 42;
  1204. var o = new Proto();
  1205. o.y = 44;
  1206. o.x = 45;
  1207. var hasX = false;
  1208. var hasY = false;
  1209. for (var i in o) {
  1210. switch(i) {
  1211. case "x":
  1212. if (hasX) {
  1213. throw new Error("Already has X");
  1214. }
  1215. hasX = true;
  1216. break;
  1217. case "y":
  1218. if (hasY) {
  1219. throw new Error("Already has Y");
  1220. }
  1221. hasY = true;
  1222. break;
  1223. }
  1224. }
  1225. hasX && hasY;
  1226. `
  1227. testScript1(SCRIPT, valueTrue, t)
  1228. }
  1229. func TestWhileLoopResult(t *testing.T) {
  1230. const SCRIPT = `
  1231. while(false);
  1232. `
  1233. testScript1(SCRIPT, _undefined, t)
  1234. }
  1235. func TestEmptySwitch(t *testing.T) {
  1236. const SCRIPT = `
  1237. switch(1){}
  1238. `
  1239. testScript1(SCRIPT, _undefined, t)
  1240. }
  1241. func TestEmptyDoWhile(t *testing.T) {
  1242. const SCRIPT = `
  1243. do {} while(false)
  1244. `
  1245. testScript1(SCRIPT, _undefined, t)
  1246. }
  1247. func TestSwitch(t *testing.T) {
  1248. const SCRIPT = `
  1249. function F(x) {
  1250. var i = 0;
  1251. switch (x) {
  1252. case 0:
  1253. i++;
  1254. case 1:
  1255. i++;
  1256. default:
  1257. i++;
  1258. case 2:
  1259. i++;
  1260. break;
  1261. case 3:
  1262. i++;
  1263. }
  1264. return i;
  1265. }
  1266. F(0) + F(1) + F(2) + F(4);
  1267. `
  1268. testScript1(SCRIPT, intToValue(10), t)
  1269. }
  1270. func TestSwitchDefFirst(t *testing.T) {
  1271. const SCRIPT = `
  1272. function F(x) {
  1273. var i = 0;
  1274. switch (x) {
  1275. default:
  1276. i++;
  1277. case 0:
  1278. i++;
  1279. case 1:
  1280. i++;
  1281. case 2:
  1282. i++;
  1283. break;
  1284. case 3:
  1285. i++;
  1286. }
  1287. return i;
  1288. }
  1289. F(0) + F(1) + F(2) + F(4);
  1290. `
  1291. testScript1(SCRIPT, intToValue(10), t)
  1292. }
  1293. func TestSwitchResult(t *testing.T) {
  1294. const SCRIPT = `
  1295. var x = 2;
  1296. switch (x) {
  1297. case 0:
  1298. "zero";
  1299. case 1:
  1300. "one";
  1301. case 2:
  1302. "two";
  1303. break;
  1304. case 3:
  1305. "three";
  1306. default:
  1307. "default";
  1308. }
  1309. `
  1310. testScript1(SCRIPT, asciiString("two"), t)
  1311. }
  1312. func TestSwitchResult1(t *testing.T) {
  1313. const SCRIPT = `
  1314. var x = 0;
  1315. switch (x) { case 0: "two"; case 1: break}
  1316. `
  1317. testScript1(SCRIPT, asciiString("two"), t)
  1318. }
  1319. func TestIfBreakResult(t *testing.T) {
  1320. const SCRIPT = `
  1321. L: {if (true) {42;} break L;}
  1322. `
  1323. testScript1(SCRIPT, intToValue(42), t)
  1324. }
  1325. func TestSwitchNoMatch(t *testing.T) {
  1326. const SCRIPT = `
  1327. var result;
  1328. var x;
  1329. switch (x) {
  1330. case 0:
  1331. result = "2";
  1332. break;
  1333. }
  1334. result;
  1335. `
  1336. testScript1(SCRIPT, _undefined, t)
  1337. }
  1338. func TestSwitchNoMatchNoDefault(t *testing.T) {
  1339. const SCRIPT = `
  1340. switch (1) {
  1341. case 0:
  1342. }
  1343. `
  1344. testScript1(SCRIPT, _undefined, t)
  1345. }
  1346. func TestSwitchNoMatchNoDefaultNoResult(t *testing.T) {
  1347. const SCRIPT = `
  1348. switch (1) {
  1349. case 0:
  1350. }
  1351. 42;
  1352. `
  1353. testScript1(SCRIPT, intToValue(42), t)
  1354. }
  1355. func TestSwitchNoMatchNoDefaultNoResultMatch(t *testing.T) {
  1356. const SCRIPT = `
  1357. switch (1) {
  1358. case 1:
  1359. }
  1360. 42;
  1361. `
  1362. testScript1(SCRIPT, intToValue(42), t)
  1363. }
  1364. func TestEmptySwitchNoResult(t *testing.T) {
  1365. const SCRIPT = `
  1366. switch (1) {}
  1367. 42;
  1368. `
  1369. testScript1(SCRIPT, intToValue(42), t)
  1370. }
  1371. func TestGetOwnPropertyNames(t *testing.T) {
  1372. const SCRIPT = `
  1373. var o = {
  1374. prop1: 42,
  1375. prop2: "test"
  1376. }
  1377. var hasProp1 = false;
  1378. var hasProp2 = false;
  1379. var names = Object.getOwnPropertyNames(o);
  1380. for (var i in names) {
  1381. var p = names[i];
  1382. switch(p) {
  1383. case "prop1":
  1384. hasProp1 = true;
  1385. break;
  1386. case "prop2":
  1387. hasProp2 = true;
  1388. break;
  1389. }
  1390. }
  1391. hasProp1 && hasProp2;
  1392. `
  1393. testScript1(SCRIPT, valueTrue, t)
  1394. }
  1395. func TestArrayLiteral(t *testing.T) {
  1396. const SCRIPT = `
  1397. var f1Called = false;
  1398. var f2Called = false;
  1399. var f3Called = false;
  1400. var errorThrown = false;
  1401. function F1() {
  1402. f1Called = true;
  1403. }
  1404. function F2() {
  1405. f2Called = true;
  1406. }
  1407. function F3() {
  1408. f3Called = true;
  1409. }
  1410. try {
  1411. var a = [F1(), x(F3()), F2()];
  1412. } catch(e) {
  1413. if (e instanceof ReferenceError) {
  1414. errorThrown = true;
  1415. } else {
  1416. throw e;
  1417. }
  1418. }
  1419. f1Called && !f2Called && f3Called && errorThrown && a === undefined;
  1420. `
  1421. testScript1(SCRIPT, valueTrue, t)
  1422. }
  1423. func TestJumpOutOfReturn(t *testing.T) {
  1424. const SCRIPT = `
  1425. function f() {
  1426. var a;
  1427. if (a == 0) {
  1428. return true;
  1429. }
  1430. }
  1431. f();
  1432. `
  1433. testScript1(SCRIPT, _undefined, t)
  1434. }
  1435. func TestSwitchJumpOutOfReturn(t *testing.T) {
  1436. const SCRIPT = `
  1437. function f(x) {
  1438. switch(x) {
  1439. case 0:
  1440. break;
  1441. default:
  1442. return x;
  1443. }
  1444. }
  1445. f(0);
  1446. `
  1447. testScript1(SCRIPT, _undefined, t)
  1448. }
  1449. func TestSetToReadOnlyPropertyStrictBracket(t *testing.T) {
  1450. const SCRIPT = `
  1451. 'use strict';
  1452. var o = {};
  1453. var thrown = false;
  1454. Object.defineProperty(o, "test", {value: 42, configurable: true});
  1455. try {
  1456. o["test"] = 43;
  1457. } catch (e) {
  1458. thrown = e instanceof TypeError;
  1459. }
  1460. thrown;
  1461. `
  1462. testScript1(SCRIPT, valueTrue, t)
  1463. }
  1464. func TestSetToReadOnlyPropertyStrictDot(t *testing.T) {
  1465. const SCRIPT = `
  1466. 'use strict';
  1467. var o = {};
  1468. var thrown = false;
  1469. Object.defineProperty(o, "test", {value: 42, configurable: true});
  1470. try {
  1471. o.test = 43;
  1472. } catch (e) {
  1473. thrown = e instanceof TypeError;
  1474. }
  1475. thrown;
  1476. `
  1477. testScript1(SCRIPT, valueTrue, t)
  1478. }
  1479. func TestDeleteNonConfigurablePropertyStrictBracket(t *testing.T) {
  1480. const SCRIPT = `
  1481. 'use strict';
  1482. var o = {};
  1483. var thrown = false;
  1484. Object.defineProperty(o, "test", {value: 42});
  1485. try {
  1486. delete o["test"];
  1487. } catch (e) {
  1488. thrown = e instanceof TypeError;
  1489. }
  1490. thrown;
  1491. `
  1492. testScript1(SCRIPT, valueTrue, t)
  1493. }
  1494. func TestDeleteNonConfigurablePropertyStrictDot(t *testing.T) {
  1495. const SCRIPT = `
  1496. 'use strict';
  1497. var o = {};
  1498. var thrown = false;
  1499. Object.defineProperty(o, "test", {value: 42});
  1500. try {
  1501. delete o.test;
  1502. } catch (e) {
  1503. thrown = e instanceof TypeError;
  1504. }
  1505. thrown;
  1506. `
  1507. testScript1(SCRIPT, valueTrue, t)
  1508. }
  1509. func TestCompound1(t *testing.T) {
  1510. const SCRIPT = `
  1511. var x = 0;
  1512. var scope = {x: 1};
  1513. var f;
  1514. with (scope) {
  1515. f = function() {
  1516. x *= (delete scope.x, 2);
  1517. }
  1518. }
  1519. f();
  1520. scope.x === 2 && x === 0;
  1521. `
  1522. testScript1(SCRIPT, valueTrue, t)
  1523. }
  1524. func TestCompound2(t *testing.T) {
  1525. const SCRIPT = `
  1526. var x;
  1527. x = "x";
  1528. x ^= "1";
  1529. `
  1530. testScript1(SCRIPT, intToValue(1), t)
  1531. }
  1532. func TestDeleteArguments(t *testing.T) {
  1533. defer func() {
  1534. if _, ok := recover().(*CompilerSyntaxError); !ok {
  1535. t.Fatal("Expected syntax error")
  1536. }
  1537. }()
  1538. const SCRIPT = `
  1539. 'use strict';
  1540. function f() {
  1541. delete arguments;
  1542. }
  1543. `
  1544. testScript1(SCRIPT, _undefined, t)
  1545. }
  1546. func TestReturnUndefined(t *testing.T) {
  1547. const SCRIPT = `
  1548. function f() {
  1549. return x;
  1550. }
  1551. var thrown = false;
  1552. try {
  1553. f();
  1554. } catch (e) {
  1555. thrown = e instanceof ReferenceError;
  1556. }
  1557. thrown;
  1558. `
  1559. testScript1(SCRIPT, valueTrue, t)
  1560. }
  1561. func TestForBreak(t *testing.T) {
  1562. const SCRIPT = `
  1563. var supreme, count;
  1564. supreme = 5;
  1565. var __evaluated = eval("for(count=0;;) {if (count===supreme)break;else count++; }");
  1566. if (__evaluated !== void 0) {
  1567. throw new Error('#1: __evaluated === 4. Actual: __evaluated ==='+ __evaluated );
  1568. }
  1569. `
  1570. testScript1(SCRIPT, _undefined, t)
  1571. }
  1572. func TestLargeNumberLiteral(t *testing.T) {
  1573. const SCRIPT = `
  1574. var x = 0x800000000000000000000;
  1575. x.toString();
  1576. `
  1577. testScript1(SCRIPT, asciiString("9.671406556917033e+24"), t)
  1578. }
  1579. func TestIncDelete(t *testing.T) {
  1580. const SCRIPT = `
  1581. var o = {x: 1};
  1582. o.x += (delete o.x, 1);
  1583. o.x;
  1584. `
  1585. testScript1(SCRIPT, intToValue(2), t)
  1586. }
  1587. func TestCompoundAssignRefError(t *testing.T) {
  1588. const SCRIPT = `
  1589. var thrown = false;
  1590. try {
  1591. a *= 1;
  1592. } catch (e) {
  1593. if (e instanceof ReferenceError) {
  1594. thrown = true;
  1595. } else {
  1596. throw e;
  1597. }
  1598. }
  1599. thrown;
  1600. `
  1601. testScript1(SCRIPT, valueTrue, t)
  1602. }
  1603. func TestObjectLiteral__Proto__(t *testing.T) {
  1604. const SCRIPT = `
  1605. var o = {
  1606. __proto__: null,
  1607. test: 42
  1608. }
  1609. Object.getPrototypeOf(o);
  1610. `
  1611. testScript1(SCRIPT, _null, t)
  1612. }
  1613. func TestEmptyCodeError(t *testing.T) {
  1614. if _, err := New().RunString(`i`); err == nil {
  1615. t.Fatal("Expected an error")
  1616. } else {
  1617. if e := err.Error(); e != "ReferenceError: i is not defined at <eval>:1:1(0)" {
  1618. t.Fatalf("Unexpected error: '%s'", e)
  1619. }
  1620. }
  1621. }
  1622. func TestForOfArray(t *testing.T) {
  1623. const SCRIPT = `
  1624. var array = [0, 'a', true, false, null, /* hole */, undefined, NaN];
  1625. var i = 0;
  1626. for (var value of array) {
  1627. assert.sameValue(value, array[i], 'element at index ' + i);
  1628. i++;
  1629. }
  1630. assert.sameValue(i, 8, 'Visits all elements');
  1631. `
  1632. testScript1(TESTLIB+SCRIPT, _undefined, t)
  1633. }
  1634. func TestForOfReturn(t *testing.T) {
  1635. const SCRIPT = `
  1636. var callCount = 0;
  1637. var iterationCount = 0;
  1638. var iterable = {};
  1639. var x = {
  1640. set attr(_) {
  1641. throw new Test262Error();
  1642. }
  1643. };
  1644. iterable[Symbol.iterator] = function() {
  1645. return {
  1646. next: function() {
  1647. return { done: false, value: 0 };
  1648. },
  1649. return: function() {
  1650. callCount += 1;
  1651. }
  1652. }
  1653. };
  1654. assert.throws(Test262Error, function() {
  1655. for (x.attr of iterable) {
  1656. iterationCount += 1;
  1657. }
  1658. });
  1659. assert.sameValue(iterationCount, 0, 'The loop body is not evaluated');
  1660. assert.sameValue(callCount, 1, 'Iterator is closed');
  1661. `
  1662. testScript1(TESTLIB+SCRIPT, _undefined, t)
  1663. }
  1664. func TestReturnFromForInLoop(t *testing.T) {
  1665. const SCRIPT = `
  1666. (function f() {
  1667. for (var i in {a: 1}) {
  1668. return true;
  1669. }
  1670. })();
  1671. `
  1672. testScript1(SCRIPT, valueTrue, t)
  1673. }
  1674. func TestReturnFromForOfLoop(t *testing.T) {
  1675. const SCRIPT = `
  1676. (function f() {
  1677. for (var i of [1]) {
  1678. return true;
  1679. }
  1680. })();
  1681. `
  1682. testScript1(SCRIPT, valueTrue, t)
  1683. }
  1684. func TestIfStackLeaks(t *testing.T) {
  1685. const SCRIPT = `
  1686. var t = 0;
  1687. if (t === 0) {
  1688. t;
  1689. }
  1690. `
  1691. testScript1(SCRIPT, _positiveZero, t)
  1692. }
  1693. func TestWithCallee(t *testing.T) {
  1694. const SCRIPT = `
  1695. function O() {
  1696. var that = this;
  1697. this.m = function() {
  1698. return this === that;
  1699. }
  1700. }
  1701. with(new O()) {
  1702. m();
  1703. }
  1704. `
  1705. testScript1(SCRIPT, valueTrue, t)
  1706. }
  1707. func TestEvalCallee(t *testing.T) {
  1708. const SCRIPT = `
  1709. (function () {
  1710. 'use strict';
  1711. var v = function() {
  1712. return this === undefined;
  1713. };
  1714. return eval('v()');
  1715. })();
  1716. `
  1717. testScript1(SCRIPT, valueTrue, t)
  1718. }
  1719. func TestTryEmptyCatchStackLeak(t *testing.T) {
  1720. const SCRIPT = `
  1721. (function() {
  1722. var f;
  1723. // Make sure the outer function is not stashless as retStashless masks all stack leaks.
  1724. (function() {
  1725. f++;
  1726. })();
  1727. try {
  1728. throw new Error();
  1729. } catch(e) {}
  1730. })();
  1731. `
  1732. testScript1(SCRIPT, _undefined, t)
  1733. }
  1734. func TestFalsyLoopBreak(t *testing.T) {
  1735. const SCRIPT = `
  1736. while(false) {
  1737. break;
  1738. }
  1739. for(;false;) {
  1740. break;
  1741. }
  1742. undefined;
  1743. `
  1744. MustCompile("", SCRIPT, false)
  1745. }
  1746. func TestFalsyLoopBreakWithResult(t *testing.T) {
  1747. const SCRIPT = `
  1748. while(false) {
  1749. break;
  1750. }
  1751. `
  1752. testScript1(SCRIPT, _undefined, t)
  1753. }
  1754. func TestDummyCompile(t *testing.T) {
  1755. const SCRIPT = `
  1756. 'use strict';
  1757. for (;false;) {
  1758. eval = 1;
  1759. }
  1760. `
  1761. _, err := Compile("", SCRIPT, false)
  1762. if err == nil {
  1763. t.Fatal("expected error")
  1764. }
  1765. }
  1766. func TestDummyCompileForUpdate(t *testing.T) {
  1767. const SCRIPT = `
  1768. 'use strict';
  1769. for (;false;eval=1) {
  1770. }
  1771. `
  1772. _, err := Compile("", SCRIPT, false)
  1773. if err == nil {
  1774. t.Fatal("expected error")
  1775. }
  1776. }
  1777. func TestObjectLiteralWithNumericKeys(t *testing.T) {
  1778. const SCRIPT = `
  1779. var o = {1e3: true};
  1780. var keys = Object.keys(o);
  1781. var o1 = {get 1e3() {return true;}};
  1782. var keys1 = Object.keys(o1);
  1783. var o2 = {1e21: true};
  1784. var keys2 = Object.keys(o2);
  1785. keys.length === 1 && keys[0] === "1000" &&
  1786. keys1.length === 1 && keys1[0] === "1000" && o1[1e3] === true &&
  1787. keys2.length === 1 && keys2[0] === "1e+21";
  1788. `
  1789. testScript1(SCRIPT, valueTrue, t)
  1790. }
  1791. func TestEscapedObjectPropertyKeys(t *testing.T) {
  1792. const SCRIPT = `
  1793. var obj = {
  1794. w\u0069th: 42
  1795. };
  1796. var obj = {
  1797. with() {42}
  1798. };
  1799. `
  1800. _, err := Compile("", SCRIPT, false)
  1801. if err != nil {
  1802. t.Fatal(err)
  1803. }
  1804. }
  1805. func TestObjectLiteralFuncProps(t *testing.T) {
  1806. const SCRIPT = `
  1807. (function() {
  1808. 'use strict';
  1809. var o = {
  1810. eval: function() {return 1;},
  1811. arguments() {return 2;},
  1812. test: function test1() {}
  1813. }
  1814. assert.sameValue(o.eval.name, "eval");
  1815. assert.sameValue(o.arguments.name, "arguments");
  1816. assert.sameValue(o.eval(), 1);
  1817. assert.sameValue(o.arguments(), 2);
  1818. assert.sameValue(o.test.name, "test1");
  1819. })();
  1820. `
  1821. testScript1(TESTLIB+SCRIPT, _undefined, t)
  1822. }
  1823. func TestFuncName(t *testing.T) {
  1824. const SCRIPT = `
  1825. var method = 1;
  1826. var o = {
  1827. method: function() {
  1828. return method;
  1829. },
  1830. method1: function method() {
  1831. return method;
  1832. }
  1833. }
  1834. o.method() === 1 && o.method1() === o.method1;
  1835. `
  1836. testScript1(SCRIPT, valueTrue, t)
  1837. }
  1838. func TestFuncNameAssign(t *testing.T) {
  1839. const SCRIPT = `
  1840. var f = function() {};
  1841. var f1;
  1842. f1 = function() {};
  1843. f.name === "f" && f1.name === "f1";
  1844. `
  1845. testScript1(SCRIPT, valueTrue, t)
  1846. }
  1847. func BenchmarkCompile(b *testing.B) {
  1848. f, err := os.Open("testdata/S15.10.2.12_A1_T1.js")
  1849. data, err := ioutil.ReadAll(f)
  1850. if err != nil {
  1851. b.Fatal(err)
  1852. }
  1853. f.Close()
  1854. src := string(data)
  1855. for i := 0; i < b.N; i++ {
  1856. _, err := Compile("test.js", src, false)
  1857. if err != nil {
  1858. b.Fatal(err)
  1859. }
  1860. }
  1861. }