compiler_test.go 29 KB

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