js/src/tests/ecma_5/misc/function-definition-eval.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Any copyright is dedicated to the Public Domain.
michael@0 3 * http://creativecommons.org/licenses/publicdomain/
michael@0 4 */
michael@0 5
michael@0 6 //-----------------------------------------------------------------------------
michael@0 7 var BUGNUMBER = 577325;
michael@0 8 var summary = 'Implement the ES5 algorithm for processing function statements';
michael@0 9
michael@0 10 print(BUGNUMBER + ": " + summary);
michael@0 11
michael@0 12 /**************
michael@0 13 * BEGIN TEST *
michael@0 14 **************/
michael@0 15
michael@0 16 var outer, desc;
michael@0 17
michael@0 18 ///////////////////////////////////////////////////
michael@0 19 // Function definitions over accessor properties //
michael@0 20 ///////////////////////////////////////////////////
michael@0 21
michael@0 22 var getCalled, setCalled;
michael@0 23
michael@0 24 // configurable properties get blown away
michael@0 25
michael@0 26 getCalled = false, setCalled = false;
michael@0 27 Object.defineProperty(this, "acc1",
michael@0 28 {
michael@0 29 get: function() { getCalled = true; throw "FAIL get 1"; },
michael@0 30 set: function(v) { setCalled = true; throw "FAIL set 1 " + v; },
michael@0 31 configurable: true,
michael@0 32 enumerable: false
michael@0 33 });
michael@0 34
michael@0 35 // does not throw
michael@0 36 outer = undefined;
michael@0 37 eval("function acc1() { throw 'FAIL redefined 1'; } outer = acc1;");
michael@0 38 assertEq(getCalled, false);
michael@0 39 assertEq(setCalled, false);
michael@0 40 assertEq(typeof acc1, "function");
michael@0 41 assertEq(acc1, outer);
michael@0 42 desc = Object.getOwnPropertyDescriptor(this, "acc1");
michael@0 43 assertEq(desc.value, acc1);
michael@0 44 assertEq(desc.writable, true);
michael@0 45 assertEq(desc.enumerable, true);
michael@0 46 assertEq(desc.configurable, true);
michael@0 47
michael@0 48
michael@0 49 getCalled = false, setCalled = false;
michael@0 50 Object.defineProperty(this, "acc2",
michael@0 51 {
michael@0 52 get: function() { getCalled = true; throw "FAIL get 2"; },
michael@0 53 set: function(v) { setCalled = true; throw "FAIL set 2 " + v; },
michael@0 54 configurable: true,
michael@0 55 enumerable: true
michael@0 56 });
michael@0 57
michael@0 58 // does not throw
michael@0 59 outer = undefined;
michael@0 60 eval("function acc2() { throw 'FAIL redefined 2'; } outer = acc2;");
michael@0 61 assertEq(getCalled, false);
michael@0 62 assertEq(setCalled, false);
michael@0 63 assertEq(typeof acc2, "function");
michael@0 64 assertEq(acc2, outer);
michael@0 65 desc = Object.getOwnPropertyDescriptor(this, "acc2");
michael@0 66 assertEq(desc.value, acc2);
michael@0 67 assertEq(desc.writable, true);
michael@0 68 assertEq(desc.enumerable, true);
michael@0 69 assertEq(desc.configurable, true);
michael@0 70
michael@0 71
michael@0 72 // non-configurable properties produce a TypeError
michael@0 73
michael@0 74 getCalled = false, setCalled = false;
michael@0 75 Object.defineProperty(this, "acc3",
michael@0 76 {
michael@0 77 get: function() { getCalled = true; throw "FAIL get 3"; },
michael@0 78 set: function(v) { setCalled = true; throw "FAIL set 3 " + v; },
michael@0 79 configurable: false,
michael@0 80 enumerable: true
michael@0 81 });
michael@0 82
michael@0 83 outer = undefined;
michael@0 84 try
michael@0 85 {
michael@0 86 eval("function acc3() { throw 'FAIL redefined 3'; }; outer = acc3");
michael@0 87 throw new Error("should have thrown trying to redefine global function " +
michael@0 88 "over a non-configurable, enumerable accessor");
michael@0 89 }
michael@0 90 catch (e)
michael@0 91 {
michael@0 92 assertEq(e instanceof TypeError, true,
michael@0 93 "global function definition, when that function would overwrite " +
michael@0 94 "a non-configurable, enumerable accessor, must throw a TypeError " +
michael@0 95 "per ES5+errata: " + e);
michael@0 96 desc = Object.getOwnPropertyDescriptor(this, "acc3");
michael@0 97 assertEq(typeof desc.get, "function");
michael@0 98 assertEq(typeof desc.set, "function");
michael@0 99 assertEq(desc.enumerable, true);
michael@0 100 assertEq(desc.configurable, false);
michael@0 101 assertEq(outer, undefined);
michael@0 102 assertEq(getCalled, false);
michael@0 103 assertEq(setCalled, false);
michael@0 104 }
michael@0 105
michael@0 106
michael@0 107 getCalled = false, setCalled = false;
michael@0 108 Object.defineProperty(this, "acc4",
michael@0 109 {
michael@0 110 get: function() { getCalled = true; throw "FAIL get 4"; },
michael@0 111 set: function(v) { setCalled = true; throw "FAIL set 4 " + v; },
michael@0 112 configurable: false,
michael@0 113 enumerable: false
michael@0 114 });
michael@0 115
michael@0 116 outer = undefined;
michael@0 117 try
michael@0 118 {
michael@0 119 eval("function acc4() { throw 'FAIL redefined 4'; }; outer = acc4");
michael@0 120 throw new Error("should have thrown trying to redefine global function " +
michael@0 121 "over a non-configurable, non-enumerable accessor");
michael@0 122 }
michael@0 123 catch (e)
michael@0 124 {
michael@0 125 assertEq(e instanceof TypeError, true,
michael@0 126 "global function definition, when that function would overwrite " +
michael@0 127 "a non-configurable, non-enumerable accessor, must throw a " +
michael@0 128 "TypeError per ES5+errata: " + e);
michael@0 129 desc = Object.getOwnPropertyDescriptor(this, "acc4");
michael@0 130 assertEq(typeof desc.get, "function");
michael@0 131 assertEq(typeof desc.set, "function");
michael@0 132 assertEq(desc.enumerable, false);
michael@0 133 assertEq(desc.configurable, false);
michael@0 134 assertEq(outer, undefined);
michael@0 135 assertEq(getCalled, false);
michael@0 136 assertEq(setCalled, false);
michael@0 137 }
michael@0 138
michael@0 139
michael@0 140 ///////////////////////////////////////////////
michael@0 141 // Function definitions over data properties //
michael@0 142 ///////////////////////////////////////////////
michael@0 143
michael@0 144
michael@0 145 // configurable properties, regardless of other attributes, get blown away
michael@0 146
michael@0 147 Object.defineProperty(this, "data1",
michael@0 148 {
michael@0 149 configurable: true,
michael@0 150 enumerable: true,
michael@0 151 writable: true,
michael@0 152 value: "data1"
michael@0 153 });
michael@0 154
michael@0 155 outer = undefined;
michael@0 156 eval("function data1() { return 'data1 function'; } outer = data1;");
michael@0 157 assertEq(typeof data1, "function");
michael@0 158 assertEq(data1, outer);
michael@0 159 desc = Object.getOwnPropertyDescriptor(this, "data1");
michael@0 160 assertEq(desc.configurable, true);
michael@0 161 assertEq(desc.enumerable, true);
michael@0 162 assertEq(desc.writable, true);
michael@0 163 assertEq(desc.value, data1);
michael@0 164
michael@0 165
michael@0 166 Object.defineProperty(this, "data2",
michael@0 167 {
michael@0 168 configurable: true,
michael@0 169 enumerable: true,
michael@0 170 writable: false,
michael@0 171 value: "data2"
michael@0 172 });
michael@0 173
michael@0 174 outer = undefined;
michael@0 175 eval("function data2() { return 'data2 function'; } outer = data2;");
michael@0 176 assertEq(typeof data2, "function");
michael@0 177 assertEq(data2, outer);
michael@0 178 desc = Object.getOwnPropertyDescriptor(this, "data2");
michael@0 179 assertEq(desc.configurable, true);
michael@0 180 assertEq(desc.enumerable, true);
michael@0 181 assertEq(desc.writable, true);
michael@0 182 assertEq(desc.value, data2);
michael@0 183
michael@0 184
michael@0 185 Object.defineProperty(this, "data3",
michael@0 186 {
michael@0 187 configurable: true,
michael@0 188 enumerable: false,
michael@0 189 writable: true,
michael@0 190 value: "data3"
michael@0 191 });
michael@0 192
michael@0 193 outer = undefined;
michael@0 194 eval("function data3() { return 'data3 function'; } outer = data3;");
michael@0 195 assertEq(typeof data3, "function");
michael@0 196 assertEq(data3, outer);
michael@0 197 desc = Object.getOwnPropertyDescriptor(this, "data3");
michael@0 198 assertEq(desc.configurable, true);
michael@0 199 assertEq(desc.enumerable, true);
michael@0 200 assertEq(desc.writable, true);
michael@0 201 assertEq(desc.value, data3);
michael@0 202
michael@0 203
michael@0 204 Object.defineProperty(this, "data4",
michael@0 205 {
michael@0 206 configurable: true,
michael@0 207 enumerable: false,
michael@0 208 writable: false,
michael@0 209 value: "data4"
michael@0 210 });
michael@0 211
michael@0 212 outer = undefined;
michael@0 213 eval("function data4() { return 'data4 function'; } outer = data4;");
michael@0 214 assertEq(typeof data4, "function");
michael@0 215 assertEq(data4, outer);
michael@0 216 desc = Object.getOwnPropertyDescriptor(this, "data4");
michael@0 217 assertEq(desc.value, data4);
michael@0 218 assertEq(desc.writable, true);
michael@0 219 assertEq(desc.enumerable, true);
michael@0 220 assertEq(desc.configurable, true);
michael@0 221
michael@0 222
michael@0 223 // non-configurable data properties are trickier
michael@0 224
michael@0 225 Object.defineProperty(this, "data5",
michael@0 226 {
michael@0 227 configurable: false,
michael@0 228 enumerable: true,
michael@0 229 writable: true,
michael@0 230 value: "data5"
michael@0 231 });
michael@0 232
michael@0 233 outer = undefined;
michael@0 234 eval("function data5() { return 'data5 function'; } outer = data5;");
michael@0 235 assertEq(typeof data5, "function");
michael@0 236 assertEq(data5, outer);
michael@0 237 desc = Object.getOwnPropertyDescriptor(this, "data5");
michael@0 238 assertEq(desc.configurable, false);
michael@0 239 assertEq(desc.enumerable, true);
michael@0 240 assertEq(desc.writable, true);
michael@0 241 assertEq(desc.value, data5);
michael@0 242
michael@0 243
michael@0 244 Object.defineProperty(this, "data6",
michael@0 245 {
michael@0 246 configurable: false,
michael@0 247 enumerable: true,
michael@0 248 writable: false,
michael@0 249 value: "data6"
michael@0 250 });
michael@0 251
michael@0 252 outer = undefined;
michael@0 253 try
michael@0 254 {
michael@0 255 eval("function data6() { return 'data6 function'; } outer = data6;");
michael@0 256 throw new Error("should have thrown trying to redefine global function " +
michael@0 257 "over a non-configurable, enumerable, non-writable accessor");
michael@0 258 }
michael@0 259 catch (e)
michael@0 260 {
michael@0 261 assertEq(e instanceof TypeError, true,
michael@0 262 "global function definition, when that function would overwrite " +
michael@0 263 "a non-configurable, enumerable, non-writable data property, must " +
michael@0 264 "throw a TypeError per ES5+errata: " + e);
michael@0 265 assertEq(data6, "data6");
michael@0 266 assertEq(outer, undefined);
michael@0 267 desc = Object.getOwnPropertyDescriptor(this, "data6");
michael@0 268 assertEq(desc.configurable, false);
michael@0 269 assertEq(desc.enumerable, true);
michael@0 270 assertEq(desc.writable, false);
michael@0 271 assertEq(desc.value, "data6");
michael@0 272 }
michael@0 273
michael@0 274
michael@0 275 Object.defineProperty(this, "data7",
michael@0 276 {
michael@0 277 configurable: false,
michael@0 278 enumerable: false,
michael@0 279 writable: true,
michael@0 280 value: "data7"
michael@0 281 });
michael@0 282
michael@0 283 outer = undefined;
michael@0 284 try
michael@0 285 {
michael@0 286 eval("function data7() { return 'data7 function'; } outer = data7;");
michael@0 287 throw new Error("should have thrown trying to redefine global function " +
michael@0 288 "over a non-configurable, non-enumerable, writable data" +
michael@0 289 "property");
michael@0 290 }
michael@0 291 catch (e)
michael@0 292 {
michael@0 293 assertEq(e instanceof TypeError, true,
michael@0 294 "global function definition, when that function would overwrite " +
michael@0 295 "a non-configurable, non-enumerable, writable data property, must " +
michael@0 296 "throw a TypeError per ES5+errata: " + e);
michael@0 297 assertEq(data7, "data7");
michael@0 298 assertEq(outer, undefined);
michael@0 299 desc = Object.getOwnPropertyDescriptor(this, "data7");
michael@0 300 assertEq(desc.configurable, false);
michael@0 301 assertEq(desc.enumerable, false);
michael@0 302 assertEq(desc.writable, true);
michael@0 303 assertEq(desc.value, "data7");
michael@0 304 }
michael@0 305
michael@0 306
michael@0 307 Object.defineProperty(this, "data8",
michael@0 308 {
michael@0 309 configurable: false,
michael@0 310 enumerable: false,
michael@0 311 writable: false,
michael@0 312 value: "data8"
michael@0 313 });
michael@0 314
michael@0 315 outer = undefined;
michael@0 316 try
michael@0 317 {
michael@0 318 eval("function data8() { return 'data8 function'; } outer = data8;");
michael@0 319 throw new Error("should have thrown trying to redefine global function " +
michael@0 320 "over a non-configurable, non-enumerable, non-writable data" +
michael@0 321 "property");
michael@0 322 }
michael@0 323 catch (e)
michael@0 324 {
michael@0 325 assertEq(e instanceof TypeError, true,
michael@0 326 "global function definition, when that function would overwrite " +
michael@0 327 "a non-configurable, non-enumerable, non-writable data property, " +
michael@0 328 "must throw a TypeError per ES5+errata: " + e);
michael@0 329 assertEq(data8, "data8");
michael@0 330 assertEq(outer, undefined);
michael@0 331 desc = Object.getOwnPropertyDescriptor(this, "data8");
michael@0 332 assertEq(desc.configurable, false);
michael@0 333 assertEq(desc.enumerable, false);
michael@0 334 assertEq(desc.writable, false);
michael@0 335 assertEq(desc.value, "data8");
michael@0 336 }
michael@0 337
michael@0 338 /******************************************************************************/
michael@0 339
michael@0 340 if (typeof reportCompare === "function")
michael@0 341 reportCompare(true, true);
michael@0 342
michael@0 343 print("All tests passed!");

mercurial