CanDeclareGlobalFunction.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. // #sec-candeclareglobalfunction states that redeclaration of global functions
  6. // that are not configurable, not writable, and not enumerable should fail.
  7. // #sec-globaldeclarationinstantiation states that if #sec-candeclareglobalfunction
  8. // fails, a TypeError should be thrown. The following properties predefined in
  9. // the global object are not configurable, not writable, and not enumerable are
  10. // undefined, Infinity, and NaN. Because these TypeErrors can only occur in the
  11. // global scope, eval statements are used. A non eval statement can be found in
  12. // CanDeclareGlobalFunctionNonEval.js
  13. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  14. var tests = [
  15. {
  16. name: "Redeclaring a global function that is not configurable, not writable, and not enumerable",
  17. body: function () {
  18. assert.throws(
  19. function () {
  20. eval("function undefined(){}")
  21. },
  22. TypeError,
  23. "undefined in the global scope is not configurable, not writable, and not \
  24. enumerable, thus #sec-globaldeclarationinstantiation states a TypeError should be thrown",
  25. "The global property undefined is not configurable, writable, \
  26. nor enumerable, therefore cannot be declared as a function"
  27. );
  28. assert.throws(
  29. function () {
  30. eval("function Infinity(){}")
  31. },
  32. TypeError,
  33. "Infinity in the global scope is not configurable, not writable, and not \
  34. enumerable, thus #sec-globaldeclarationinstantiation states a TypeError should be thrown",
  35. "The global property Infinity is not configurable, writable, \
  36. nor enumerable, therefore cannot be declared as a function"
  37. );
  38. assert.throws(
  39. function () {
  40. eval("function NaN(){}")
  41. },
  42. TypeError,
  43. "NaN in the global scope is not configurable, not writable, and not \
  44. enumerable, thus #sec-globaldeclarationinstantiation states a TypeError should be thrown",
  45. "The global property NaN is not configurable, writable, \
  46. nor enumerable, therefore cannot be declared as a function"
  47. );
  48. assert.throws(
  49. function () {
  50. eval(`
  51. Object.defineProperty(this, "a", {
  52. writable: false,
  53. enumerable: false,
  54. configurable: false
  55. });
  56. WScript.LoadScript("function a(){}", "self");
  57. `);
  58. },
  59. TypeError,
  60. "The property \"a\" is defined to be not configurable, not writable, and not \
  61. enumerable, thus #sec-globaldeclarationinstantiation states a TypeError should be thrown when \
  62. \"a\" is defined as a function",
  63. "The global property a is not configurable, writable, \
  64. nor enumerable, therefore cannot be declared as a function"
  65. );
  66. }
  67. },
  68. {
  69. name: "Redeclaring a global async function that is not configurable, not writable, and not enumerable",
  70. body: function () {
  71. assert.throws(
  72. function () {
  73. eval("async function undefined(){}")
  74. },
  75. TypeError,
  76. "undefined in the global scope is not configurable, not writable, and not \
  77. enumerable, thus #sec-globaldeclarationinstantiation states a TypeError should be thrown",
  78. "The global property undefined is not configurable, writable, \
  79. nor enumerable, therefore cannot be declared as a function"
  80. );
  81. assert.throws(
  82. function () {
  83. eval("async function Infinity(){}")
  84. },
  85. TypeError,
  86. "Infinity in the global scope is not configurable, not writable, and not \
  87. enumerable, thus #sec-globaldeclarationinstantiation states a TypeError should be thrown",
  88. "The global property Infinity is not configurable, writable, \
  89. nor enumerable, therefore cannot be declared as a function"
  90. );
  91. assert.throws(
  92. function () {
  93. eval("async function NaN(){}")
  94. },
  95. TypeError,
  96. "NaN in the global scope is not configurable, not writable, and not \
  97. enumerable, thus #sec-globaldeclarationinstantiation states a TypeError should be thrown",
  98. "The global property NaN is not configurable, writable, \
  99. nor enumerable, therefore cannot be declared as a function"
  100. );
  101. }
  102. },
  103. {
  104. name: "Redeclaring a global generator function that is not configurable, not writable, and not enumerable",
  105. body: function () {
  106. assert.throws(
  107. function () {
  108. eval("function* undefined(){}")
  109. },
  110. TypeError,
  111. "undefined in the global scope is not configurable, not writable, and not \
  112. enumerable, thus #sec-globaldeclarationinstantiation states a TypeError should be thrown",
  113. "The global property undefined is not configurable, writable, \
  114. nor enumerable, therefore cannot be declared as a function"
  115. );
  116. assert.throws(
  117. function () {
  118. eval("function* Infinity(){}")
  119. },
  120. TypeError,
  121. "Infinity in the global scope is not configurable, not writable, and not \
  122. enumerable, thus #sec-globaldeclarationinstantiation states a TypeError should be thrown",
  123. "The global property Infinity is not configurable, writable, \
  124. nor enumerable, therefore cannot be declared as a function"
  125. );
  126. assert.throws(
  127. function () {
  128. eval("function* NaN(){}")
  129. },
  130. TypeError,
  131. "NaN in the global scope is not configurable, not writable, and not \
  132. enumerable, thus #sec-globaldeclarationinstantiation states a TypeError should be thrown",
  133. "The global property NaN is not configurable, writable, \
  134. nor enumerable, therefore cannot be declared as a function"
  135. );
  136. }
  137. },
  138. {
  139. name: "Redeclaring a global function that is configurable, writable, or enumerable should not throw a TypeError",
  140. body: function () {
  141. eval("function Array(){}");
  142. eval("function ArrayBuffer(){}");
  143. eval("function Boolean(){}");
  144. eval("function DataView(){}");
  145. eval("function Date(){}");
  146. eval("function Function(){}");
  147. eval("function Map(){}");
  148. eval("function Math(){}");
  149. eval("function Number(){}");
  150. eval("function Object(){}");
  151. eval("function Promise(){}");
  152. eval("function RegExp(){}");
  153. eval("function Symbol(){}");
  154. eval("function TypeError(){}");
  155. eval("function Uin16Array(){}");
  156. eval("function WeakMap(){}");
  157. eval("function decodeURI(){}");
  158. eval("function escape(){}");
  159. eval("function print(){}");
  160. eval("function parseInt(){}");
  161. eval("function readbuffer(){}");
  162. eval("function readline(){}");
  163. eval("function unescape(){}");
  164. }
  165. },
  166. {
  167. name: "Redeclaring a global function that is not configurable, not writable, \
  168. and not enumerable while not in the global scope should not throw a TypeError",
  169. body: function () {
  170. function undefined() { }
  171. function Infinity() { }
  172. function NaN() { }
  173. }
  174. }
  175. ]
  176. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });