compiler_test.go 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138
  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. // FIXME
  1735. /*
  1736. func TestDummyCompile(t *testing.T) {
  1737. const SCRIPT = `
  1738. 'use strict';
  1739. for (;false;) {
  1740. eval = 1;
  1741. }
  1742. `
  1743. defer func() {
  1744. if recover() == nil {
  1745. t.Fatal("Expected panic")
  1746. }
  1747. }()
  1748. testScript1(SCRIPT, _undefined, t)
  1749. }*/
  1750. func BenchmarkCompile(b *testing.B) {
  1751. f, err := os.Open("testdata/S15.10.2.12_A1_T1.js")
  1752. data, err := ioutil.ReadAll(f)
  1753. if err != nil {
  1754. b.Fatal(err)
  1755. }
  1756. f.Close()
  1757. src := string(data)
  1758. for i := 0; i < b.N; i++ {
  1759. _, err := Compile("test.js", src, false)
  1760. if err != nil {
  1761. b.Fatal(err)
  1762. }
  1763. }
  1764. }