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