reflect.js 23 KB

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