eval1.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. // Let/const redecl/reassign cases in presence of eval.
  6. // Eval creates its own block scope, preventing let and const variables from leaking out.
  7. function write(x) { WScript.Echo(x) }
  8. // Global scope.
  9. const z = 'global z';
  10. let w = 'global w';
  11. eval('let x = "global x"; const y = "global y"; write(z);');
  12. try { write(x); } catch (e) { write(e); }
  13. try { write(y); } catch (e) { write(e); }
  14. // Try redeclaration at global scope.
  15. try {
  16. eval('var z = "global var z";');
  17. }
  18. catch(e) {
  19. write(e);
  20. }
  21. try {
  22. eval('var w = "global var w";');
  23. }
  24. catch(e) {
  25. write(e);
  26. }
  27. // Block scope in global function.
  28. try {
  29. const z = 'global block z';
  30. eval('let x = "global block x"; const y = "global block y"; write(z);');
  31. try { write(x); } catch (e) { write(e); }
  32. try { write(y); } catch (e) { write(e); }
  33. // function declared in global block.
  34. outer();
  35. function outer() {
  36. let w = 'outer w';
  37. // Try redeclaration at function scope.
  38. try {
  39. eval('var w = "outer var w";');
  40. }
  41. catch(e) {
  42. write(e);
  43. }
  44. write(w);
  45. try {
  46. const z = 'outer z';
  47. eval('let x = "outer x"; const y = "outer y"; write(z);');
  48. try { write(x); } catch (e) { write(e); }
  49. try { write(y); } catch (e) { write(e); }
  50. // Try assigning const y; shouldn't see const y and instead create function var y
  51. eval('y = "outer var y";');
  52. write(y);
  53. // function nested within function body.
  54. inner();
  55. write(y);
  56. function inner() {
  57. let w = 'inner w';
  58. // Try redeclaration at function scope.
  59. try {
  60. eval('var w = "inner var w";');
  61. }
  62. catch(e) {
  63. write(e);
  64. }
  65. write(w);
  66. try {
  67. const z = 'inner z';
  68. // const y shouldn't affect outer y
  69. eval('let x = "inner x"; const y = "inner y"; write(z);');
  70. try { write(x); } catch (e) { write(e); }
  71. write(y); // outer var y
  72. }
  73. catch(e) {
  74. write(e);
  75. }
  76. function foo() {
  77. let yy = "b";
  78. const yx = "a";
  79. yy += "a";
  80. eval("WScript.Echo(yy);")
  81. WScript.Echo(yy);
  82. }
  83. foo();
  84. }
  85. }
  86. catch(e) {
  87. write(e);
  88. }
  89. }
  90. }
  91. catch(e) {
  92. write(e);
  93. }
  94. // BLUE Bug 454963 (shouldn't crash)
  95. {
  96. with ({})
  97. eval("");
  98. function f() { x; }
  99. let x;
  100. }
  101. this.eval('let x = 0; function f() { return x; }; WScript.Echo(f());');