funcExpr.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 write(v) { WScript.Echo(v + ""); }
  6. var count = 0;
  7. function check(scen) {
  8. var str = "check" + ++count + ": " + scen;
  9. try {
  10. var result = eval(scen);
  11. str += " #" + result;
  12. } catch (e) {
  13. str += " @" + e.message;
  14. }
  15. write(str);
  16. }
  17. var Test0 = function F_Test0()
  18. {
  19. }
  20. var Test1 = function F_Test1()
  21. {
  22. write(F_Test1);
  23. }
  24. // funcexpression assigned a value
  25. var Test2 = function F_Test2()
  26. {
  27. write(F_Test2);
  28. F_Test2 = 100;
  29. write(F_Test2);
  30. }
  31. //variable with same name
  32. var Test3 = function F_Test3()
  33. {
  34. write("Test3: " + F_Test3);
  35. var F_Test3;
  36. write("Test3: " + F_Test3);
  37. }
  38. // variable with same name with init
  39. var Test4 = function F_Test4()
  40. {
  41. write("Test4: " + F_Test4);
  42. var F_Test4 = 100;
  43. write("Test4: " + F_Test4);
  44. }
  45. // variable with same name with init
  46. var Test5 = function F_Test5()
  47. {
  48. write("Test5: " + F_Test5);
  49. var F_Test5;
  50. F_Test5 = 100;
  51. write("Test5: " + F_Test5);
  52. }
  53. // argument with same name
  54. var Test6 = function F_Test6(F_Test6)
  55. {
  56. write("Test6: " + F_Test6);
  57. }
  58. // argument with same name and assignment
  59. var Test7 = function F_Test7(F_Test7)
  60. {
  61. write("Test7: " + F_Test7);
  62. F_Test7 = 100;
  63. write("Test7: " + F_Test7);
  64. }
  65. // argument with same name and assignment
  66. var Test8 = function F_Test8(F_Test8)
  67. {
  68. write("Test8: " + F_Test8);
  69. F_Test8 = 100;
  70. write("Test8: " + F_Test8);
  71. write("Test8: " + arguments[0]);
  72. }
  73. // argument with same name and assignment
  74. var Test9 = function F_Test9(F_Test9)
  75. {
  76. write("Test9: " + F_Test9);
  77. arguments[0] = 100;
  78. write("Test9: " + F_Test9);
  79. write("Test9: " + arguments[0]);
  80. }
  81. eval("var Test10 = function F_Test10(){}");
  82. var Test11 = function F_Test11()
  83. {
  84. eval('write("Test11: " + F_Test11)');
  85. }
  86. eval("var Test12 = function F_Test12(){eval('write(\"Test12: \" + F_Test12)');}");
  87. // funcexpression assigned a value
  88. var Test13 = function F_Test13()
  89. {
  90. write("Test13: " + F_Test13);
  91. eval("F_Test13 = 100");
  92. write("Test13: " + F_Test13);
  93. }
  94. // variable with same name with init
  95. var Test14 = function F_Test14()
  96. {
  97. write("Test14: " + F_Test14);
  98. eval("var F_Test14 = 100;");
  99. write("Test14: " + F_Test14);
  100. }
  101. // variable with same name with init
  102. var Test15 = function F_Test15()
  103. {
  104. write("Test15: " + F_Test15);
  105. eval("var F_Test15;");
  106. write("Test15: " + F_Test15);
  107. eval("F_Test15 = 100;");
  108. write("Test15: " + F_Test15);
  109. }
  110. // argument with same name
  111. var Test16 = function F_Test16(F_Test16)
  112. {
  113. eval("write(F_Test16)");
  114. }
  115. // argument with same name and assignment
  116. var Test17 = function F_Test17(F_Test17)
  117. {
  118. write("Test17: " + F_Test17);
  119. eval("F_Test17 = 100;");
  120. write("Test17: " + F_Test17);
  121. }
  122. // argument with same name and assignment
  123. var Test18 = function F_Test18(F_Test18)
  124. {
  125. write("Test18: " + F_Test18);
  126. eval("F_Test18 = 100;");
  127. write("Test18: " + F_Test18);
  128. write("Test18: " + arguments[0]);
  129. }
  130. // argument with same name and assignment
  131. var Test19 = function F_Test19(F_Test19)
  132. {
  133. write("Test19: " + F_Test19);
  134. eval("arguments[0] = 100;");
  135. write("Test19: " + F_Test19);
  136. write("Test19: " + arguments[0]);
  137. }
  138. var Test20 = function F_Test20()
  139. {
  140. function inner20()
  141. {
  142. eval("var F_Test20 = 10");
  143. write(F_Test20);
  144. }
  145. inner20();
  146. WScript.Echo(F_Test20);
  147. return 0;
  148. }
  149. var Test21 = function F_Test21()
  150. {
  151. try {
  152. var x = function y() {
  153. var a = function b() {
  154. eval("WScript.Echo(y)");
  155. eval("y = 'legacy only'");
  156. eval("WScript.Echo(y)");
  157. }
  158. a();
  159. }
  160. x();
  161. } catch (ex) {
  162. WScript.Echo(ex);
  163. }
  164. }
  165. var Test22 = function F_Test22()
  166. {
  167. eval('try {} catch(e) {} (function e(x){}); WScript.Echo(e);');
  168. return e;
  169. }
  170. var Test23 = function F_Test23() {
  171. var x = 1;
  172. new (function g() {
  173. with({}) {
  174. WScript.Echo('typeof g === ' + typeof g);
  175. WScript.Echo('typeof F_Test23 === ' + typeof F_Test23);
  176. WScript.Echo('typeof x === ' + typeof x);
  177. if (!x)
  178. g();
  179. }
  180. } );
  181. }
  182. var Test24 = function F_Test24() {
  183. with( "" ){
  184. (function() {
  185. foo(F_Test24);
  186. })();
  187. }
  188. function foo(f) { if (f.name !== 'F_Test24') WScript.Echo('fail Test24') };
  189. }
  190. var numTests = 25;
  191. for (var i=0;i<numTests;i++)
  192. {
  193. check("Test" + i + "();");
  194. check("Test" + i + "('hello');");
  195. check("F_Test" + i + "();");
  196. }