CallNonFunction.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. var obj = new Object();
  6. obj.n = null;
  7. obj.blah = 1;
  8. obj[0] = null;
  9. obj[1] = 1;
  10. var n = null;
  11. var blah = 1;
  12. // Call integer global var
  13. function CallErrorTest0()
  14. {
  15. blah();
  16. }
  17. // new integer global var
  18. function CallErrorTest1()
  19. {
  20. new blah();
  21. }
  22. // Call integer property
  23. function CallErrorTest2()
  24. {
  25. obj.blah();
  26. }
  27. // new integer property
  28. function CallErrorTest3()
  29. {
  30. new obj.blah();
  31. }
  32. // Call primitive null
  33. function CallErrorTest4()
  34. {
  35. null();
  36. }
  37. // New primitive null
  38. function CallErrorTest5()
  39. {
  40. new null();
  41. }
  42. // Call integer
  43. function CallErrorTest6()
  44. {
  45. 1();
  46. }
  47. // new integer
  48. function CallErrorTest7()
  49. {
  50. new 1();
  51. }
  52. // Call parameter
  53. function CallErrorTest8()
  54. {
  55. function func(f)
  56. {
  57. f();
  58. }
  59. func(1);
  60. }
  61. // Call index - null
  62. function CallErrorTest9()
  63. {
  64. obj[0]();
  65. }
  66. // new index - null
  67. function CallErrorTest10()
  68. {
  69. new obj[0]();
  70. }
  71. // Call index - number
  72. function CallErrorTest11()
  73. {
  74. obj[1]();
  75. }
  76. // new index - number
  77. function CallErrorTest12()
  78. {
  79. new obj[1]();
  80. }
  81. // Call index on null
  82. function CallErrorTest13()
  83. {
  84. n[1]();
  85. }
  86. // new index on null
  87. function CallErrorTest14()
  88. {
  89. new n[1]();
  90. }
  91. // Call property on null
  92. function CallErrorTest15()
  93. {
  94. n.prop();
  95. }
  96. // new property on null
  97. function CallErrorTest16()
  98. {
  99. new n.prop();
  100. }
  101. // Call null property
  102. function CallErrorTest17()
  103. {
  104. obj.n();
  105. }
  106. // new null property
  107. function CallErrorTest18()
  108. {
  109. new obj.n();
  110. }
  111. // Call null global var
  112. function CallErrorTest17()
  113. {
  114. n();
  115. }
  116. // new null global var
  117. function CallErrorTest18()
  118. {
  119. new n();
  120. }
  121. var i = 0;
  122. while (this["CallErrorTest" + i] != undefined)
  123. {
  124. safeCall(this["CallErrorTest" + i]);
  125. i++;
  126. }
  127. writeLine("");
  128. writeLine("");
  129. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  130. // The following tests invalid function calls on unresolvable vars, nonexisting and existing globals accessed using different
  131. // methods, local vars, objects, arrays, and deleted properties. Test functions are generated for each case and evaluated.
  132. var testCase_implicitGlobal = "implicit global";
  133. var testCase_globalUsingWindow = "global using window";
  134. var testCase_globalUsingThis = "global using this";
  135. var testCase_local = "local";
  136. var testCase_object = "object";
  137. var testCase_arrayInitialized = "array initialized";
  138. var testCase_arrayAssigned = "array assigned";
  139. var testCases =
  140. [
  141. testCase_implicitGlobal,
  142. testCase_globalUsingWindow,
  143. testCase_globalUsingThis,
  144. testCase_local,
  145. testCase_object,
  146. testCase_arrayInitialized,
  147. testCase_arrayAssigned
  148. ];
  149. var testValue_uninitialized = null; // don't initialize before calling as a function
  150. var testValue_undefined = "undefined";
  151. var testValue_null = "null";
  152. var testValue_number = "1";
  153. var testValue_object = "{}";
  154. var testValues =
  155. [
  156. testValue_uninitialized,
  157. testValue_undefined,
  158. testValue_null,
  159. testValue_number,
  160. testValue_object
  161. ];
  162. var globalIndex = 0;
  163. function generateAndRunTests(testCase, doDelete)
  164. {
  165. if (testCase === testCase_local && doDelete)
  166. return; // deleting is not supported for this test case
  167. writeLine("--- Test case: " + testCase + ", do delete: " + doDelete + " ---");
  168. writeLine("");
  169. for (var testValueIndex in testValues)
  170. {
  171. var testValue = testValues[testValueIndex];
  172. // A function that looks like the following is generated, and varies based on the test case and test value. The function
  173. // below is generated for the following parameters: {testCase_arrayAssigned, testValue_object, doDelete = true}
  174. // safeCall(function ()
  175. // {
  176. // var a = [];
  177. // a[0] = {};
  178. // delete a[0];
  179. // a[0]();
  180. // });
  181. var globalIdentifier;
  182. switch (testCase)
  183. {
  184. case testCase_implicitGlobal:
  185. globalIdentifier = "g" + globalIndex++;
  186. break;
  187. case testCase_globalUsingWindow:
  188. globalIdentifier = "window.g" + globalIndex++;
  189. break;
  190. case testCase_globalUsingThis:
  191. globalIdentifier = "this.g" + globalIndex++;
  192. break;
  193. }
  194. var f = "safeCall(function(){";
  195. switch (testCase)
  196. {
  197. case testCase_implicitGlobal:
  198. case testCase_globalUsingWindow:
  199. case testCase_globalUsingThis:
  200. if (!testValue && doDelete)
  201. continue; // no need to delete an uninitialized property
  202. if (testCase === testCase_globalUsingWindow)
  203. writeLine("Only valid in IE:"); // the result of this test is only valid when run in IE since 'window' is undefined otherwise
  204. if (testCase === testCase_globalUsingThis && (!testValue || doDelete))
  205. writeLine("INCORRECT in JC all versions:"); // BUG: these cases produce incorrect results in JC (all versions) but work in IE
  206. if (testValue)
  207. f += globalIdentifier + "=" + testValue + ";";
  208. if (doDelete)
  209. f += "delete " + globalIdentifier + ";";
  210. f += globalIdentifier + "();";
  211. break;
  212. case testCase_local:
  213. f += "var v";
  214. if (testValue)
  215. f += "=" + testValue;
  216. f += ";v();";
  217. break;
  218. case testCase_object:
  219. if (!testValue && doDelete)
  220. continue; // no need to delete an uninitialized property
  221. f += "var o={";
  222. if (testValue)
  223. f += "p:" + testValue;
  224. f += "};"
  225. if (doDelete)
  226. f += "delete o.p;";
  227. f += "o.p();";
  228. break;
  229. case testCase_arrayInitialized:
  230. if (!testValue && doDelete)
  231. continue; // no need to delete an uninitialized property
  232. if (testValue === testValue_undefined && !doDelete)
  233. writeLine("INCORRECT in compat modes:");
  234. f += "var a=[";
  235. if (testValue)
  236. f += testValue;
  237. f += "];"
  238. if (doDelete)
  239. f += "delete a[0];";
  240. f += "a[0]();";
  241. break;
  242. case testCase_arrayAssigned:
  243. if (!testValue && doDelete)
  244. continue; // no need to delete an uninitialized property
  245. f += "var a=[];";
  246. if (testValue)
  247. f += "a[0]=" + testValue + ";";
  248. if (doDelete)
  249. f += "delete a[0];";
  250. f += "a[0]();";
  251. break;
  252. default:
  253. writeLine("Unknown test case '" + testCase + "'.");
  254. return;
  255. }
  256. f += "});";
  257. writeLine(f);
  258. eval(f);
  259. writeLine("");
  260. }
  261. writeLine("");
  262. }
  263. var booleans = [false, true];
  264. for (var testCaseIndex in testCases)
  265. {
  266. var testCase = testCases[testCaseIndex];
  267. for (var doDeleteIndex in booleans)
  268. {
  269. var doDelete = booleans[doDeleteIndex];
  270. generateAndRunTests(testCase, doDelete);
  271. }
  272. }
  273. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  274. // Helpers
  275. function writeLine(str)
  276. {
  277. WScript.Echo("" + str);
  278. }
  279. function safeCall(func)
  280. {
  281. try
  282. {
  283. return func();
  284. }
  285. catch (ex)
  286. {
  287. writeLine(ex.name + " (" + ex.number + "): " + ex.message);
  288. }
  289. }