123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // This is modified from Mozilla Reflect.parse test suite (the file is located
  2. // at js/src/tests/js1_8_5/extensions/reflect-parse.js in the source tree).
  3. //
  4. // Some notable changes:
  5. // * Removed unsupported features (destructuring, let, comprehensions...).
  6. // * Removed tests for E4X (ECMAScript for XML).
  7. // * Removed everything related to builder.
  8. // * Enclosed every 'Pattern' construct with a scope.
  9. // * Removed the test for bug 632030 and bug 632024.
  10. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  11. /*
  12. * Any copyright is dedicated to the Public Domain.
  13. * http://creativecommons.org/licenses/publicdomain/
  14. */
  15. (function (exports) {
  16. function testReflect(Reflect, Pattern) {
  17. function program(elts) { return Pattern({ type: "Program", body: elts }) }
  18. function exprStmt(expr) { return Pattern({ type: "ExpressionStatement", expression: expr }) }
  19. function throwStmt(expr) { return Pattern({ type: "ThrowStatement", argument: expr }) }
  20. function returnStmt(expr) { return Pattern({ type: "ReturnStatement", argument: expr }) }
  21. function yieldExpr(expr) { return Pattern({ type: "YieldExpression", argument: expr }) }
  22. function lit(val) { return Pattern({ type: "Literal", value: val }) }
  23. var thisExpr = Pattern({ type: "ThisExpression" });
  24. function funDecl(id, params, body) { return Pattern({ type: "FunctionDeclaration",
  25. id: id,
  26. params: params,
  27. defaults: [],
  28. body: body,
  29. rest: null,
  30. generator: false,
  31. expression: false
  32. }) }
  33. function genFunDecl(id, params, body) { return Pattern({ type: "FunctionDeclaration",
  34. id: id,
  35. params: params,
  36. defaults: [],
  37. body: body,
  38. rest: null,
  39. generator: true,
  40. expression: false
  41. }) }
  42. function declarator(id, init) { return Pattern({ type: "VariableDeclarator", id: id, init: init }) }
  43. function varDecl(decls) { return Pattern({ type: "VariableDeclaration", declarations: decls, kind: "var" }) }
  44. function letDecl(decls) { return Pattern({ type: "VariableDeclaration", declarations: decls, kind: "let" }) }
  45. function constDecl(decls) { return Pattern({ type: "VariableDeclaration", declarations: decls, kind: "const" }) }
  46. function ident(name) { return Pattern({ type: "Identifier", name: name }) }
  47. function dotExpr(obj, id) { return Pattern({ type: "MemberExpression", computed: false, object: obj, property: id }) }
  48. function memExpr(obj, id) { return Pattern({ type: "MemberExpression", computed: true, object: obj, property: id }) }
  49. function forStmt(init, test, update, body) { return Pattern({ type: "ForStatement", init: init, test: test, update: update, body: body }) }
  50. function forInStmt(lhs, rhs, body) { return Pattern({ type: "ForInStatement", left: lhs, right: rhs, body: body, each: false }) }
  51. function forEachInStmt(lhs, rhs, body) { return Pattern({ type: "ForInStatement", left: lhs, right: rhs, body: body, each: true }) }
  52. function breakStmt(lab) { return Pattern({ type: "BreakStatement", label: lab }) }
  53. function continueStmt(lab) { return Pattern({ type: "ContinueStatement", label: lab }) }
  54. function blockStmt(body) { return Pattern({ type: "BlockStatement", body: body }) }
  55. var emptyStmt = Pattern({ type: "EmptyStatement" });
  56. function ifStmt(test, cons, alt) { return Pattern({ type: "IfStatement", test: test, alternate: alt, consequent: cons }) }
  57. function labStmt(lab, stmt) { return Pattern({ type: "LabeledStatement", label: lab, body: stmt }) }
  58. function withStmt(obj, stmt) { return Pattern({ type: "WithStatement", object: obj, body: stmt }) }
  59. function whileStmt(test, stmt) { return Pattern({ type: "WhileStatement", test: test, body: stmt }) }
  60. function doStmt(stmt, test) { return Pattern({ type: "DoWhileStatement", test: test, body: stmt }) }
  61. function switchStmt(disc, cases) { return Pattern({ type: "SwitchStatement", discriminant: disc, cases: cases }) }
  62. function caseClause(test, stmts) { return Pattern({ type: "SwitchCase", test: test, consequent: stmts }) }
  63. function defaultClause(stmts) { return Pattern({ type: "SwitchCase", test: null, consequent: stmts }) }
  64. function catchClause(id, guard, body) { if (guard) { return Pattern({ type: "GuardedCatchClause", param: id, guard: guard, body: body }) } else { return Pattern({ type: "CatchClause", param: id, body: body }) } }
  65. function tryStmt(body, guarded, catches, fin) { return Pattern({ type: "TryStatement", block: body, guardedHandlers: guarded, handlers: catches, finalizer: fin }) }
  66. function letStmt(head, body) { return Pattern({ type: "LetStatement", head: head, body: body }) }
  67. function funExpr(id, args, body, gen) { return Pattern({ type: "FunctionExpression",
  68. id: id,
  69. params: args,
  70. defaults: [],
  71. body: body,
  72. rest: null,
  73. generator: false,
  74. expression: false
  75. }) }
  76. function genFunExpr(id, args, body) { return Pattern({ type: "FunctionExpression",
  77. id: id,
  78. params: args,
  79. defaults: [],
  80. body: body,
  81. rest: null,
  82. generator: true,
  83. expression: false
  84. }) }
  85. function unExpr(op, arg) { return Pattern({ type: "UnaryExpression", operator: op, argument: arg }) }
  86. function binExpr(op, left, right) { return Pattern({ type: "BinaryExpression", operator: op, left: left, right: right }) }
  87. function aExpr(op, left, right) { return Pattern({ type: "AssignmentExpression", operator: op, left: left, right: right }) }
  88. function updExpr(op, arg, prefix) { return Pattern({ type: "UpdateExpression", operator: op, argument: arg, prefix: prefix }) }
  89. function logExpr(op, left, right) { return Pattern({ type: "LogicalExpression", operator: op, left: left, right: right }) }
  90. function condExpr(test, cons, alt) { return Pattern({ type: "ConditionalExpression", test: test, consequent: cons, alternate: alt }) }
  91. function seqExpr(exprs) { return Pattern({ type: "SequenceExpression", expressions: exprs }) }
  92. function newExpr(callee, args) { return Pattern({ type: "NewExpression", callee: callee, arguments: args }) }
  93. function callExpr(callee, args) { return Pattern({ type: "CallExpression", callee: callee, arguments: args }) }
  94. function arrExpr(elts) { return Pattern({ type: "ArrayExpression", elements: elts }) }
  95. function objExpr(elts) { return Pattern({ type: "ObjectExpression", properties: elts }) }
  96. function objProp(key, value, kind) { return Pattern({ type: "Property", key: key, value: value, kind: kind, method: false, shorthand: false }) }
  97. function arrPatt(elts) { return Pattern({ type: "ArrayPattern", elements: elts }) }
  98. function objPatt(elts) { return Pattern({ type: "ObjectPattern", properties: elts }) }
  99. function localSrc(src) { return "(function(){ " + src + " })" }
  100. function localPatt(patt) { return program([exprStmt(funExpr(null, [], blockStmt([patt])))]) }
  101. function blockSrc(src) { return "(function(){ { " + src + " } })" }
  102. function blockPatt(patt) { return program([exprStmt(funExpr(null, [], blockStmt([blockStmt([patt])])))]) }
  103. function assertBlockStmt(src, patt) {
  104. blockPatt(patt).assert(Reflect.parse(blockSrc(src)));
  105. }
  106. function assertBlockExpr(src, patt) {
  107. assertBlockStmt(src, exprStmt(patt));
  108. }
  109. function assertBlockDecl(src, patt, builder) {
  110. blockPatt(patt).assert(Reflect.parse(blockSrc(src), {builder: builder}));
  111. }
  112. function assertLocalStmt(src, patt) {
  113. localPatt(patt).assert(Reflect.parse(localSrc(src)));
  114. }
  115. function assertLocalExpr(src, patt) {
  116. assertLocalStmt(src, exprStmt(patt));
  117. }
  118. function assertLocalDecl(src, patt) {
  119. localPatt(patt).assert(Reflect.parse(localSrc(src)));
  120. }
  121. function assertGlobalStmt(src, patt, builder) {
  122. program([patt]).assert(Reflect.parse(src, {builder: builder}));
  123. }
  124. function assertGlobalExpr(src, patt, builder) {
  125. program([exprStmt(patt)]).assert(Reflect.parse(src, {builder: builder}));
  126. //assertStmt(src, exprStmt(patt));
  127. }
  128. function assertGlobalDecl(src, patt) {
  129. program([patt]).assert(Reflect.parse(src));
  130. }
  131. function assertProg(src, patt) {
  132. program(patt).assert(Reflect.parse(src));
  133. }
  134. function assertStmt(src, patt) {
  135. assertLocalStmt(src, patt);
  136. assertGlobalStmt(src, patt);
  137. assertBlockStmt(src, patt);
  138. }
  139. function assertExpr(src, patt) {
  140. assertLocalExpr(src, patt);
  141. assertGlobalExpr(src, patt);
  142. assertBlockExpr(src, patt);
  143. }
  144. function assertDecl(src, patt) {
  145. assertLocalDecl(src, patt);
  146. assertGlobalDecl(src, patt);
  147. assertBlockDecl(src, patt);
  148. }
  149. function assertError(src, errorType) {
  150. try {
  151. Reflect.parse(src);
  152. } catch (e) {
  153. return;
  154. }
  155. throw new Error("expected " + errorType.name + " for " + uneval(src));
  156. }
  157. // general tests
  158. // NB: These are useful but for now jit-test doesn't do I/O reliably.
  159. //program(_).assert(Reflect.parse(snarf('data/flapjax.txt')));
  160. //program(_).assert(Reflect.parse(snarf('data/jquery-1.4.2.txt')));
  161. //program(_).assert(Reflect.parse(snarf('data/prototype.js')));
  162. //program(_).assert(Reflect.parse(snarf('data/dojo.js.uncompressed.js')));
  163. //program(_).assert(Reflect.parse(snarf('data/mootools-1.2.4-core-nc.js')));
  164. // declarations
  165. assertDecl("var x = 1, y = 2, z = 3",
  166. varDecl([declarator(ident("x"), lit(1)),
  167. declarator(ident("y"), lit(2)),
  168. declarator(ident("z"), lit(3))]));
  169. assertDecl("var x, y, z",
  170. varDecl([declarator(ident("x"), null),
  171. declarator(ident("y"), null),
  172. declarator(ident("z"), null)]));
  173. assertDecl("function foo() { }",
  174. funDecl(ident("foo"), [], blockStmt([])));
  175. assertDecl("function foo() { return 42 }",
  176. funDecl(ident("foo"), [], blockStmt([returnStmt(lit(42))])));
  177. // Bug 591437: rebound args have their defs turned into uses
  178. assertDecl("function f(a) { function a() { } }",
  179. funDecl(ident("f"), [ident("a")], blockStmt([funDecl(ident("a"), [], blockStmt([]))])));
  180. assertDecl("function f(a,b,c) { function b() { } }",
  181. funDecl(ident("f"), [ident("a"),ident("b"),ident("c")], blockStmt([funDecl(ident("b"), [], blockStmt([]))])));
  182. // expressions
  183. assertExpr("true", lit(true));
  184. assertExpr("false", lit(false));
  185. assertExpr("42", lit(42));
  186. assertExpr("(/asdf/)", lit(/asdf/));
  187. assertExpr("this", thisExpr);
  188. assertExpr("foo", ident("foo"));
  189. assertExpr("foo.bar", dotExpr(ident("foo"), ident("bar")));
  190. assertExpr("foo[bar]", memExpr(ident("foo"), ident("bar")));
  191. assertExpr("(function(){})", funExpr(null, [], blockStmt([])));
  192. assertExpr("(function f() {})", funExpr(ident("f"), [], blockStmt([])));
  193. assertExpr("(function f(x,y,z) {})", funExpr(ident("f"), [ident("x"),ident("y"),ident("z")], blockStmt([])));
  194. assertExpr("(++x)", updExpr("++", ident("x"), true));
  195. assertExpr("(x++)", updExpr("++", ident("x"), false));
  196. assertExpr("(+x)", unExpr("+", ident("x")));
  197. assertExpr("(-x)", unExpr("-", ident("x")));
  198. assertExpr("(!x)", unExpr("!", ident("x")));
  199. assertExpr("(~x)", unExpr("~", ident("x")));
  200. assertExpr("(delete x)", unExpr("delete", ident("x")));
  201. assertExpr("(typeof x)", unExpr("typeof", ident("x")));
  202. assertExpr("(void x)", unExpr("void", ident("x")));
  203. assertExpr("(x == y)", binExpr("==", ident("x"), ident("y")));
  204. assertExpr("(x != y)", binExpr("!=", ident("x"), ident("y")));
  205. assertExpr("(x === y)", binExpr("===", ident("x"), ident("y")));
  206. assertExpr("(x !== y)", binExpr("!==", ident("x"), ident("y")));
  207. assertExpr("(x < y)", binExpr("<", ident("x"), ident("y")));
  208. assertExpr("(x <= y)", binExpr("<=", ident("x"), ident("y")));
  209. assertExpr("(x > y)", binExpr(">", ident("x"), ident("y")));
  210. assertExpr("(x >= y)", binExpr(">=", ident("x"), ident("y")));
  211. assertExpr("(x << y)", binExpr("<<", ident("x"), ident("y")));
  212. assertExpr("(x >> y)", binExpr(">>", ident("x"), ident("y")));
  213. assertExpr("(x >>> y)", binExpr(">>>", ident("x"), ident("y")));
  214. assertExpr("(x + y)", binExpr("+", ident("x"), ident("y")));
  215. assertExpr("(w + x + y + z)", binExpr("+", binExpr("+", binExpr("+", ident("w"), ident("x")), ident("y")), ident("z")));
  216. assertExpr("(x - y)", binExpr("-", ident("x"), ident("y")));
  217. assertExpr("(w - x - y - z)", binExpr("-", binExpr("-", binExpr("-", ident("w"), ident("x")), ident("y")), ident("z")));
  218. assertExpr("(x * y)", binExpr("*", ident("x"), ident("y")));
  219. assertExpr("(x / y)", binExpr("/", ident("x"), ident("y")));
  220. assertExpr("(x % y)", binExpr("%", ident("x"), ident("y")));
  221. assertExpr("(x | y)", binExpr("|", ident("x"), ident("y")));
  222. assertExpr("(x ^ y)", binExpr("^", ident("x"), ident("y")));
  223. assertExpr("(x & y)", binExpr("&", ident("x"), ident("y")));
  224. assertExpr("(x in y)", binExpr("in", ident("x"), ident("y")));
  225. assertExpr("(x instanceof y)", binExpr("instanceof", ident("x"), ident("y")));
  226. assertExpr("(x = y)", aExpr("=", ident("x"), ident("y")));
  227. assertExpr("(x += y)", aExpr("+=", ident("x"), ident("y")));
  228. assertExpr("(x -= y)", aExpr("-=", ident("x"), ident("y")));
  229. assertExpr("(x *= y)", aExpr("*=", ident("x"), ident("y")));
  230. assertExpr("(x /= y)", aExpr("/=", ident("x"), ident("y")));
  231. assertExpr("(x %= y)", aExpr("%=", ident("x"), ident("y")));
  232. assertExpr("(x <<= y)", aExpr("<<=", ident("x"), ident("y")));
  233. assertExpr("(x >>= y)", aExpr(">>=", ident("x"), ident("y")));
  234. assertExpr("(x >>>= y)", aExpr(">>>=", ident("x"), ident("y")));
  235. assertExpr("(x |= y)", aExpr("|=", ident("x"), ident("y")));
  236. assertExpr("(x ^= y)", aExpr("^=", ident("x"), ident("y")));
  237. assertExpr("(x &= y)", aExpr("&=", ident("x"), ident("y")));
  238. assertExpr("(x || y)", logExpr("||", ident("x"), ident("y")));
  239. assertExpr("(x && y)", logExpr("&&", ident("x"), ident("y")));
  240. assertExpr("(w || x || y || z)", logExpr("||", logExpr("||", logExpr("||", ident("w"), ident("x")), ident("y")), ident("z")))
  241. assertExpr("(x ? y : z)", condExpr(ident("x"), ident("y"), ident("z")));
  242. assertExpr("(x,y)", seqExpr([ident("x"),ident("y")]))
  243. assertExpr("(x,y,z)", seqExpr([ident("x"),ident("y"),ident("z")]))
  244. assertExpr("(a,b,c,d,e,f,g)", seqExpr([ident("a"),ident("b"),ident("c"),ident("d"),ident("e"),ident("f"),ident("g")]));
  245. assertExpr("(new Object)", newExpr(ident("Object"), []));
  246. assertExpr("(new Object())", newExpr(ident("Object"), []));
  247. assertExpr("(new Object(42))", newExpr(ident("Object"), [lit(42)]));
  248. assertExpr("(new Object(1,2,3))", newExpr(ident("Object"), [lit(1),lit(2),lit(3)]));
  249. assertExpr("(String())", callExpr(ident("String"), []));
  250. assertExpr("(String(42))", callExpr(ident("String"), [lit(42)]));
  251. assertExpr("(String(1,2,3))", callExpr(ident("String"), [lit(1),lit(2),lit(3)]));
  252. assertExpr("[]", arrExpr([]));
  253. assertExpr("[1]", arrExpr([lit(1)]));
  254. assertExpr("[1,2]", arrExpr([lit(1),lit(2)]));
  255. assertExpr("[1,2,3]", arrExpr([lit(1),lit(2),lit(3)]));
  256. assertExpr("[1,,2,3]", arrExpr([lit(1),,lit(2),lit(3)]));
  257. assertExpr("[1,,,2,3]", arrExpr([lit(1),,,lit(2),lit(3)]));
  258. assertExpr("[1,,,2,,3]", arrExpr([lit(1),,,lit(2),,lit(3)]));
  259. assertExpr("[1,,,2,,,3]", arrExpr([lit(1),,,lit(2),,,lit(3)]));
  260. assertExpr("[,1,2,3]", arrExpr([,lit(1),lit(2),lit(3)]));
  261. assertExpr("[,,1,2,3]", arrExpr([,,lit(1),lit(2),lit(3)]));
  262. assertExpr("[,,,1,2,3]", arrExpr([,,,lit(1),lit(2),lit(3)]));
  263. assertExpr("[,,,1,2,3,]", arrExpr([,,,lit(1),lit(2),lit(3)]));
  264. assertExpr("[,,,1,2,3,,]", arrExpr([,,,lit(1),lit(2),lit(3),undefined]));
  265. assertExpr("[,,,1,2,3,,,]", arrExpr([,,,lit(1),lit(2),lit(3),undefined,undefined]));
  266. assertExpr("[,,,,,]", arrExpr([undefined,undefined,undefined,undefined,undefined]));
  267. assertExpr("({})", objExpr([]));
  268. assertExpr("({x:1})", objExpr([objProp(ident("x"), lit(1), "init")]));
  269. assertExpr("({x:1, y:2})", objExpr([objProp(ident("x"), lit(1), "init"),
  270. objProp(ident("y"), lit(2), "init")]));
  271. assertExpr("({x:1, y:2, z:3})", objExpr([objProp(ident("x"), lit(1), "init"),
  272. objProp(ident("y"), lit(2), "init"),
  273. objProp(ident("z"), lit(3), "init") ]));
  274. assertExpr("({x:1, 'y':2, z:3})", objExpr([objProp(ident("x"), lit(1), "init"),
  275. objProp(lit("y"), lit(2), "init"),
  276. objProp(ident("z"), lit(3), "init") ]));
  277. assertExpr("({'x':1, 'y':2, z:3})", objExpr([objProp(lit("x"), lit(1), "init"),
  278. objProp(lit("y"), lit(2), "init"),
  279. objProp(ident("z"), lit(3), "init") ]));
  280. assertExpr("({'x':1, 'y':2, 3:3})", objExpr([objProp(lit("x"), lit(1), "init"),
  281. objProp(lit("y"), lit(2), "init"),
  282. objProp(lit(3), lit(3), "init") ]));
  283. // Bug 571617: eliminate constant-folding
  284. assertExpr("2 + 3", binExpr("+", lit(2), lit(3)));
  285. // Bug 632026: constant-folding
  286. assertExpr("typeof(0?0:a)", unExpr("typeof", condExpr(lit(0), lit(0), ident("a"))));
  287. // Bug 632056: constant-folding
  288. program([exprStmt(ident("f")),
  289. ifStmt(lit(1),
  290. funDecl(ident("f"), [], blockStmt([])),
  291. null)]).assert(Reflect.parse("f; if (1) function f(){}"));
  292. // statements
  293. assertStmt("throw 42", throwStmt(lit(42)));
  294. assertStmt("for (;;) break", forStmt(null, null, null, breakStmt(null)));
  295. assertStmt("for (x; y; z) break", forStmt(ident("x"), ident("y"), ident("z"), breakStmt(null)));
  296. assertStmt("for (var x; y; z) break", forStmt(varDecl([declarator(ident("x"), null)]), ident("y"), ident("z"), breakStmt(null)));
  297. assertStmt("for (var x = 42; y; z) break", forStmt(varDecl([declarator(ident("x"), lit(42))]), ident("y"), ident("z"), breakStmt(null)));
  298. assertStmt("for (x; ; z) break", forStmt(ident("x"), null, ident("z"), breakStmt(null)));
  299. assertStmt("for (var x; ; z) break", forStmt(varDecl([declarator(ident("x"), null)]), null, ident("z"), breakStmt(null)));
  300. assertStmt("for (var x = 42; ; z) break", forStmt(varDecl([declarator(ident("x"), lit(42))]), null, ident("z"), breakStmt(null)));
  301. assertStmt("for (x; y; ) break", forStmt(ident("x"), ident("y"), null, breakStmt(null)));
  302. assertStmt("for (var x; y; ) break", forStmt(varDecl([declarator(ident("x"), null)]), ident("y"), null, breakStmt(null)));
  303. assertStmt("for (var x = 42; y; ) break", forStmt(varDecl([declarator(ident("x"),lit(42))]), ident("y"), null, breakStmt(null)));
  304. assertStmt("for (var x in y) break", forInStmt(varDecl([declarator(ident("x"),null)]), ident("y"), breakStmt(null)));
  305. assertStmt("for (x in y) break", forInStmt(ident("x"), ident("y"), breakStmt(null)));
  306. assertStmt("{ }", blockStmt([]));
  307. assertStmt("{ throw 1; throw 2; throw 3; }", blockStmt([ throwStmt(lit(1)), throwStmt(lit(2)), throwStmt(lit(3))]));
  308. assertStmt(";", emptyStmt);
  309. assertStmt("if (foo) throw 42;", ifStmt(ident("foo"), throwStmt(lit(42)), null));
  310. assertStmt("if (foo) throw 42; else true;", ifStmt(ident("foo"), throwStmt(lit(42)), exprStmt(lit(true))));
  311. assertStmt("if (foo) { throw 1; throw 2; throw 3; }",
  312. ifStmt(ident("foo"),
  313. blockStmt([throwStmt(lit(1)), throwStmt(lit(2)), throwStmt(lit(3))]),
  314. null));
  315. assertStmt("if (foo) { throw 1; throw 2; throw 3; } else true;",
  316. ifStmt(ident("foo"),
  317. blockStmt([throwStmt(lit(1)), throwStmt(lit(2)), throwStmt(lit(3))]),
  318. exprStmt(lit(true))));
  319. assertStmt("foo: for(;;) break foo;", labStmt(ident("foo"), forStmt(null, null, null, breakStmt(ident("foo")))));
  320. assertStmt("foo: for(;;) continue foo;", labStmt(ident("foo"), forStmt(null, null, null, continueStmt(ident("foo")))));
  321. assertStmt("with (obj) { }", withStmt(ident("obj"), blockStmt([])));
  322. assertStmt("with (obj) { obj; }", withStmt(ident("obj"), blockStmt([exprStmt(ident("obj"))])));
  323. assertStmt("while (foo) { }", whileStmt(ident("foo"), blockStmt([])));
  324. assertStmt("while (foo) { foo; }", whileStmt(ident("foo"), blockStmt([exprStmt(ident("foo"))])));
  325. assertStmt("do { } while (foo);", doStmt(blockStmt([]), ident("foo")));
  326. assertStmt("do { foo; } while (foo)", doStmt(blockStmt([exprStmt(ident("foo"))]), ident("foo")));
  327. assertStmt("switch (foo) { case 1: 1; break; case 2: 2; break; default: 3; }",
  328. switchStmt(ident("foo"),
  329. [ caseClause(lit(1), [ exprStmt(lit(1)), breakStmt(null) ]),
  330. caseClause(lit(2), [ exprStmt(lit(2)), breakStmt(null) ]),
  331. defaultClause([ exprStmt(lit(3)) ]) ]));
  332. assertStmt("switch (foo) { case 1: 1; break; case 2: 2; break; default: 3; case 42: 42; }",
  333. switchStmt(ident("foo"),
  334. [ caseClause(lit(1), [ exprStmt(lit(1)), breakStmt(null) ]),
  335. caseClause(lit(2), [ exprStmt(lit(2)), breakStmt(null) ]),
  336. defaultClause([ exprStmt(lit(3)) ]),
  337. caseClause(lit(42), [ exprStmt(lit(42)) ]) ]));
  338. assertStmt("try { } catch (e) { }",
  339. tryStmt(blockStmt([]),
  340. [],
  341. [ catchClause(ident("e"), null, blockStmt([])) ],
  342. null));
  343. assertStmt("try { } catch (e) { } finally { }",
  344. tryStmt(blockStmt([]),
  345. [],
  346. [ catchClause(ident("e"), null, blockStmt([])) ],
  347. blockStmt([])));
  348. assertStmt("try { } finally { }",
  349. tryStmt(blockStmt([]),
  350. [],
  351. [],
  352. blockStmt([])));
  353. // redeclarations (TOK_NAME nodes with lexdef)
  354. assertStmt("function f() { function g() { } function g() { } }",
  355. funDecl(ident("f"), [], blockStmt([funDecl(ident("g"), [], blockStmt([])),
  356. funDecl(ident("g"), [], blockStmt([]))])));
  357. assertStmt("function f() { function g() { } function g() { return 42 } }",
  358. funDecl(ident("f"), [], blockStmt([funDecl(ident("g"), [], blockStmt([])),
  359. funDecl(ident("g"), [], blockStmt([returnStmt(lit(42))]))])));
  360. assertStmt("function f() { var x = 42; var x = 43; }",
  361. funDecl(ident("f"), [], blockStmt([varDecl([declarator(ident("x"),lit(42))]),
  362. varDecl([declarator(ident("x"),lit(43))])])));
  363. // getters and setters
  364. assertExpr("({ get x() { return 42 } })",
  365. objExpr([ objProp(ident("x"),
  366. funExpr(null, [], blockStmt([returnStmt(lit(42))])),
  367. "get" ) ]));
  368. assertExpr("({ set x(v) { return 42 } })",
  369. objExpr([ objProp(ident("x"),
  370. funExpr(null, [ident("v")], blockStmt([returnStmt(lit(42))])),
  371. "set" ) ]));
  372. }
  373. exports.testReflect = testReflect;
  374. }(typeof exports === 'undefined' ? this : exports));