With.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 A(k)
  6. {
  7. this.k = k;
  8. }
  9. var x = new A(1);
  10. var y = new A(undefined);
  11. var i = 1;
  12. var j = 2;
  13. with (x)
  14. {
  15. i = 2;
  16. with (A.prototype)
  17. {
  18. i = 3;
  19. x.i = 77;
  20. i = 4;
  21. }
  22. with (y)
  23. {
  24. i = 4.0;
  25. j = "y.j";
  26. y.j = undefined;
  27. k = null;
  28. }
  29. A.prototype.i = undefined;
  30. with (A.prototype)
  31. {
  32. i = 99;
  33. }
  34. WScript.Echo(i);
  35. WScript.Echo(j);
  36. WScript.Echo(k);
  37. with (y)
  38. {
  39. WScript.Echo(i);
  40. if (j === undefined)
  41. WScript.Echo("undefined");
  42. WScript.Echo(k);
  43. }
  44. j = 0;
  45. k = "x.k";
  46. var foo = function f() {
  47. var i = 'local.i';
  48. WScript.Echo(i);
  49. WScript.Echo(foo.length);
  50. };
  51. x.foo = 'x.foo';
  52. }
  53. WScript.Echo(i);
  54. WScript.Echo(A.prototype.i);
  55. WScript.Echo(x.i);
  56. WScript.Echo(y.i);
  57. WScript.Echo(j);
  58. if (A.prototype.j === undefined)
  59. WScript.Echo("undefined");
  60. if (x.j === undefined)
  61. WScript.Echo("undefined");
  62. if (y.j === undefined)
  63. WScript.Echo("undefined");
  64. WScript.Echo(x.k);
  65. WScript.Echo(y.k);
  66. WScript.Echo(x.foo);
  67. foo();
  68. try
  69. {
  70. f(); // doesn't escape the blocks cope
  71. } catch (e) {
  72. WScript.Echo(e);
  73. }
  74. function foo2() {
  75. var a1 = new Object();
  76. a1.foo = 16;
  77. a1.bar = "abcd";
  78. with ( a1 ) {
  79. a1 = new Object();
  80. a1.foo = 16;
  81. a1.bar = "dcba";
  82. WScript.Echo(bar);
  83. }
  84. }
  85. foo2();
  86. var evalinwith = 'global evalinwith';
  87. (function (){
  88. var O = {evalinwith:'no'};
  89. with(O)
  90. {
  91. eval('var evalinwith = "O.evalinwith"');
  92. eval('WScript.Echo(evalinwith + "")');
  93. }
  94. evalinwith = 'local evalinwith';
  95. eval('WScript.Echo(evalinwith + "")');
  96. })();
  97. eval('WScript.Echo(evalinwith + "")');
  98. function verify(act, msg) { WScript.Echo(msg + ': ' + act); }
  99. var level1Func = function level1FuncIdent() {
  100. var level1 = "level1";
  101. with({level1: ""}) {
  102. try {
  103. throw "throw";
  104. } catch(level1) {}
  105. }
  106. verify(level1, "Value of level1 after assignment at level 1");
  107. }
  108. level1Func();
  109. (function () {
  110. function a()
  111. {
  112. WScript.Echo("a is called");
  113. }
  114. (function(){
  115. try {
  116. throw a;
  117. }
  118. catch(x) {
  119. with({}){
  120. x();
  121. }
  122. }
  123. })();
  124. })();
  125. // Guarantee that function-body-in-array script length heuristic is not broken
  126. // by compat mode named-function-expression-in-with.
  127. with ({}) {
  128. var arrwithfunc = [(function handlerFactory() { return; })];
  129. }