2
0

tdz1.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. // ES6 block-scoping with closures, switches
  6. var x = 'global x';
  7. function write(x) { WScript.Echo(x); }
  8. (function() {
  9. try {
  10. inner();
  11. }
  12. catch(e) {
  13. write(e);
  14. }
  15. let x = 'local x';
  16. try {
  17. inner();
  18. }
  19. catch(e) {
  20. write(e);
  21. }
  22. function inner() {
  23. write(x);
  24. }
  25. })();
  26. (function() {
  27. while(1) {
  28. try {
  29. LABEL0:
  30. switch(x) {
  31. case 0:
  32. let x = 'local x';
  33. // fall through
  34. default:
  35. function inner() {
  36. write(x);
  37. }
  38. inner();
  39. // Should only get here after executing case 0.
  40. return;
  41. case 'seriously?':
  42. break LABEL0;
  43. }
  44. }
  45. catch(e) {
  46. write(e);
  47. // Should assign to global x.
  48. x = 0;
  49. }
  50. }
  51. })();
  52. try {
  53. (function() {
  54. if (typeof a === 'string') {
  55. write('a is a string');
  56. }
  57. let a = 'let a';
  58. })();
  59. } catch (e) {
  60. write(e);
  61. }
  62. try {
  63. (function() {
  64. let a = 'let a';
  65. if (typeof a === 'string') {
  66. write('a is a string');
  67. }
  68. })();
  69. } catch (e) {
  70. write(e);
  71. }
  72. try {
  73. (function () {
  74. if (delete a) {
  75. write("deleted a");
  76. } else {
  77. write("did not delete a");
  78. }
  79. let a = 'let a';
  80. })();
  81. } catch (e) {
  82. write(e);
  83. }
  84. try {
  85. (function () {
  86. let a = 'let a';
  87. if (delete a) {
  88. write("deleted a");
  89. } else {
  90. write("did not delete a");
  91. }
  92. })();
  93. } catch (e) {
  94. write(e);
  95. }
  96. try {
  97. try {
  98. write(eval('typeof t'));
  99. }
  100. catch(e) {
  101. write(e);
  102. }
  103. (function() {
  104. write(eval('typeof t'));
  105. })();
  106. }
  107. catch(e) {
  108. write(e);
  109. }
  110. let t;
  111. write(eval('typeof t'));
  112. (function () {
  113. try {
  114. let foo = eval('foo();');
  115. } catch (e) {
  116. write(e);
  117. }
  118. try {
  119. const foo = eval('foo();');
  120. } catch (e) {
  121. write(e);
  122. }
  123. try {
  124. eval('foo();');
  125. let foo = 123;
  126. } catch (e) {
  127. write(e);
  128. }
  129. try {
  130. eval('foo();');
  131. const foo = 123;
  132. } catch (e) {
  133. write(e);
  134. }
  135. })();
  136. (function () {
  137. try {
  138. eval('let a = a;');
  139. } catch (e) {
  140. write(e);
  141. }
  142. try {
  143. eval('const a = a;');
  144. } catch (e) {
  145. write(e);
  146. }
  147. try {
  148. // this works in a try/catch because Use Before Declaration is always a runtime error by the spec
  149. let a = a;
  150. } catch (e) {
  151. write(e);
  152. }
  153. try {
  154. // this works in a try/catch because Use Before Declaration is always a runtime error by the spec
  155. const a = a;
  156. } catch (e) {
  157. write(e);
  158. }
  159. })();
  160. (function () {
  161. try {
  162. eval('a() = 123; let a;');
  163. } catch (e) {
  164. write(e);
  165. }
  166. try {
  167. eval('a() = 123; const a = undefined;');
  168. } catch (e) {
  169. write(e);
  170. }
  171. })();
  172. try {
  173. write(delete glo_a);
  174. } catch (e) {
  175. write(e);
  176. }
  177. try {
  178. write(typeof glo_a);
  179. } catch (e) {
  180. write(e);
  181. }
  182. let glo_a = 'glo_a';
  183. try {
  184. write(delete glo_a);
  185. } catch (e) {
  186. write(e);
  187. }
  188. try {
  189. write(typeof glo_a);
  190. } catch (e) {
  191. write(e);
  192. }
  193. try {
  194. write(delete glo_b);
  195. } catch (e) {
  196. write(e);
  197. }
  198. try {
  199. write(typeof glo_b);
  200. } catch (e) {
  201. write(e);
  202. }
  203. const glo_b = 'glo_b';
  204. try {
  205. write(delete glo_b);
  206. } catch (e) {
  207. write(e);
  208. }
  209. try {
  210. write(typeof glo_b);
  211. } catch (e) {
  212. write(e);
  213. }
  214. //BLUE 404930
  215. try {
  216. switch (Math.round(.2)) {
  217. case 3:
  218. let x = eval("");
  219. default:
  220. x; // should emit a use-before-decl runtime error
  221. }
  222. } catch (e) {
  223. write(e);
  224. }
  225. // BLUE 404930 (bug reused, different from above)
  226. function testStSlotNoThrow() { let y = function () { write(y = 123); }; write(y); y(); write(y); }
  227. function testStSlotThrow() { let y = (function () { write(y = 123); })(); write(y); }
  228. function testStObjSlotNoThrow() { let y = function () { write(y = 123); }; write(y); y(); write(y); eval('y'); }
  229. function testStObjSlotThrow() { let y = (function () { write(y = 123); })(); write(y); eval('y'); }
  230. function testTypeOfNoThrow() { let y = function () { write(typeof y); }; y(); }
  231. function testTypeOfThrow() { let y = (function () { write(typeof y); })(); }
  232. function testTypeOfObjNoThrow() { let y = function () { write(typeof y); }; y(); eval('y'); }
  233. function testTypeOfObjThrow() { let y = (function () { write(typeof y); })(); eval('y'); }
  234. function testLdSlotNoThrow() { let y = function () { write(y); }; y(); }
  235. function testLdSlotThrow() { let y = (function () { write(y); })(); }
  236. function testLdObjSlotNoThrow() { let y = function () { write(y); }; y(); eval('y'); }
  237. function testLdObjSlotThrow() { let y = (function () { write(y); })(); eval('y'); }
  238. try { testStSlotNoThrow(); } catch (e) { write("shouldn't throw! " + e); }
  239. try { testStSlotThrow(); } catch (e) { write(e); }
  240. try { testStObjSlotNoThrow(); } catch (e) { write("shouldn't throw! " + e); }
  241. try { testStObjSlotThrow(); } catch (e) { write(e); }
  242. try { testTypeOfNoThrow(); } catch (e) { write("shouldn't throw! " + e); }
  243. try { testTypeOfThrow(); } catch (e) { write(e); }
  244. try { testTypeOfObjNoThrow(); } catch (e) { write("shouldn't throw! " + e); }
  245. try { testTypeOfObjThrow(); } catch (e) { write(e); }
  246. try { testLdSlotNoThrow(); } catch (e) { write("shouldn't throw! " + e); }
  247. try { testLdSlotThrow(); } catch (e) { write(e); }
  248. try { testLdObjSlotNoThrow(); } catch (e) { write("shouldn't throw! " + e); }
  249. try { testLdObjSlotThrow(); } catch (e) { write(e); }
  250. function testBugVsoOs849056() {
  251. let x; // this x should get a scope slot
  252. function inner() {
  253. y = x; // should throw use before declaration
  254. let y;
  255. }
  256. inner();
  257. }
  258. try { testBugVsoOs849056(); } catch (e) { write(e); }
  259. function testBugVsoOs1141661() {
  260. y = 5;
  261. let y;
  262. eval("write('This should be unreachable');");
  263. }
  264. try { testBugVsoOs1141661(); } catch (e) { write(e); }