eval1.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. // 1. Use eval to initialize an object.
  6. var x = eval("({" + '"' + "3d-cube" + '"' + ":[1]})");
  7. WScript.Echo(x["3d-cube"][0]);
  8. WScript.Echo(x);
  9. // 2. Use eval to assign a global variable
  10. eval("var i = " + '"' + "3d-cube" + '"' + ";");
  11. var str1 = "var x;"
  12. var str2 = "x = 9; g(); x = 8;"
  13. // 3. Use eval to define nested functions. Use nested eval to
  14. // manipulate local variables. Access a global variable
  15. // with the same name as a local.
  16. eval("function f()" +
  17. "{" +
  18. " eval(str1);" +
  19. " function g() { WScript.Echo(x); };" +
  20. " eval(str2);" +
  21. " return g;" +
  22. "}; " +
  23. "WScript.Echo(x[i][0]);" );
  24. // 4. Use eval to call the function defined within the previous eval
  25. // and get back the nested function.
  26. var z = eval("f()");
  27. eval("z()");
  28. // 5. Call function containing eval taking multiple params; pass it too few.
  29. // Test of ArgIn_A handling when params are homed to a heap location.
  30. function foo(a, b, c) {
  31. eval("WScript.Echo(a);");
  32. eval("WScript.Echo(b);");
  33. eval("WScript.Echo(c);");
  34. }
  35. foo("foo.a", "foo.b");
  36. (function () {
  37. function foo(a) {
  38. WScript.Echo(foo);
  39. eval("bar(false)");
  40. function bar(x) {
  41. if (x)
  42. foo(x);
  43. }
  44. };
  45. foo(true);
  46. })();
  47. // 6. Function declarations inside eval should go to the enclosing scope
  48. // (but not to a "with" object).
  49. var O = {xxx:'O.xxx'};
  50. with (O)
  51. {
  52. eval('function xxx(){}');
  53. }
  54. WScript.Echo(O.xxx);
  55. WScript.Echo(xxx);
  56. (function () { eval("function foobaz() {}") })();
  57. try {
  58. foobaz();
  59. WScript.Echo("fail");
  60. } catch(e) {
  61. WScript.Echo("pass");
  62. }
  63. // 7. Check 'this' inside eval. See WOOB 1127689.
  64. function F(obj)
  65. {
  66. this.name = "F";
  67. WScript.Echo("inside eval: this.name = " + obj.eval('this.name'));
  68. }
  69. this.name = "global object";
  70. var f = new F(this);
  71. var test11glob=42;
  72. function test11() {
  73. var result;
  74. function test()
  75. {
  76. return this.test11glob
  77. }
  78. WScript.Echo(test());
  79. WScript.Echo(eval("test()"));
  80. }
  81. test11();
  82. // Make sure that deeply aliased eval call does the right thing.
  83. var G = this;
  84. G.NSEval = G["eval"];
  85. function alias() {
  86. var x = 'hello';
  87. // In compat mode, print hello. In standards mode, print the global x.
  88. // And in compat mode, run with deferred parsing to make sure the aliasing
  89. // of "this" persists across parser instances.
  90. G.NSEval('WScript.Echo(x)');
  91. }
  92. alias();
  93. // bug 1147044
  94. eval("with ({}) (function fibonacci() {})();");
  95. // Test recursive evals to make sure closure environments remain intact
  96. var flg = 0;
  97. function TestDirect() {
  98. var func = "if(flg == 0) { flg = 1; eval(func); (function(a){(function(){if (a !== undefined) throw 0;})()})(); WScript.Echo('pass direct')}";
  99. eval(func);
  100. }
  101. TestDirect();
  102. var func = "if(flg == 1) { flg = 2; this.eval(func); (function(a){(function(){if (a !== undefined) throw 0;})()})(); WScript.Echo('pass indirect');}";
  103. function TestIndirect() {
  104. this.eval(func);
  105. }
  106. TestIndirect();
  107. // 8. Set up a custom eval that indirectly calls built-in eval, evoke it, and verify the effect.
  108. var q = eval;
  109. var eval = function(s) {
  110. // Do some extra stuff.
  111. WScript.Echo("Custom eval:");
  112. for (var index = 0; index < arguments.length; index++)
  113. {
  114. WScript.Echo("arg " + index + " = \'" + arguments[index] + "\'");
  115. }
  116. q(s);
  117. }
  118. eval("x[i][0] = 2;");
  119. WScript.Echo(x[i][0]);
  120. // 9. Test that the extra scope chain parameter is hidden.
  121. eval = (function (x, y) { WScript.Echo(y + ''); });
  122. eval('hello');
  123. // 10. Test jitting of a store to a closure-captured block-scoped variable.
  124. function test5810363() {
  125. (function () {
  126. if (false) {
  127. (function () {
  128. eval('');
  129. }());
  130. function func13() {
  131. }
  132. while (func13 = func13) {
  133. }
  134. }
  135. }());
  136. }
  137. test5810363();
  138. test5810363();
  139. test5810363();