2
0

letconst_eval_redecl.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. function print(x) { WScript.Echo(x+""); }
  6. var inFunctionTests =
  7. [
  8. // eval gets scope for let/const variables => they do not leak out, no redeclaration error with vars
  9. function () {
  10. var x = 1;
  11. eval("let x = 2;");
  12. print(x);
  13. },
  14. function () {
  15. var x = 1;
  16. eval("const x = 2;");
  17. print(x);
  18. },
  19. function () {
  20. eval("let x = 2;");
  21. var x = 1;
  22. print(x);
  23. },
  24. function () {
  25. eval("const x = 2;");
  26. var x = 1;
  27. print(x);
  28. },
  29. // var leaks out of eval in non-strict mode => redeclaration error with let/consts
  30. function () {
  31. let x = 1;
  32. eval("var x = 2;");
  33. print(x);
  34. },
  35. function () {
  36. const x = 1;
  37. eval("var x = 2;");
  38. print(x);
  39. },
  40. function () {
  41. eval("var x = 2;");
  42. let x = 1;
  43. print(x);
  44. },
  45. function () {
  46. eval("var x = 2;");
  47. const x = 1;
  48. print(x);
  49. },
  50. ];
  51. for (let i = 0; i < inFunctionTests.length; i++)
  52. {
  53. try {
  54. inFunctionTests[i]();
  55. } catch (e) {
  56. print(e);
  57. }
  58. }
  59. // global tests
  60. // eval gets scope for let/const variables => they do not leak out, no redeclaration error with vars
  61. var a = 1;
  62. eval("let a = 2;");
  63. print(a);
  64. var b = 1;
  65. eval("const b = 2;");
  66. print(b);
  67. eval("let c = 2;");
  68. var c = 1;
  69. print(c);
  70. eval("const d = 2;");
  71. var d = 1;
  72. print(d);
  73. // var leaks out of eval in non-strict mode => redeclaration error with let/consts
  74. let e = 1;
  75. try { eval("var e = 2;"); print(e); } catch (e) { print(e); }
  76. const f = 1;
  77. try { eval("var f = 2;"); print(f); } catch (e) { print(e); }
  78. var hadException = false;
  79. try { eval("var g = 2;"); } catch (e) { print(e); hadException = true; }
  80. let g = 1;
  81. if (!hadException) print(g);
  82. hadException = false;
  83. try { eval("var h = 2;"); } catch (e) { print(e); hadException = true; }
  84. const h = 1;
  85. if (!hadException) print(h);