| 1 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
|
| 5 |
js> load('testsrc/doctests/util.js'); |
| 6 |
|
| 7 |
js> Object.defineProperty |
| 8 |
function defineProperty() { [native code for Object.defineProperty, arity=3] } |
| 9 |
|
| 10 |
js> expectTypeError(function() { Object.defineProperty() }); |
| 11 |
js> expectTypeError(function() { Object.defineProperty({}) }); |
| 12 |
js> expectTypeError(function() { Object.defineProperty({}, 'p') }); |
| 13 |
js> [undefined, null, true, 1, 'hello'].forEach(function(value) { |
| 14 |
> expectTypeError(function() { Object.defineProperty(value, 'p', {}) }) |
| 15 |
> }) |
| 16 |
|
| 17 |
js> Object.defineProperty({}, 'p', {value:1}).p |
| 18 |
1 |
| 19 |
js> var obj = {} |
| 20 |
js> Object.defineProperty(obj, 'a', { |
| 21 |
> value: 1, |
| 22 |
> enumerable: false, |
| 23 |
> writable: false, |
| 24 |
> configurable: false |
| 25 |
> }) |
| 26 |
[object Object] |
| 27 |
js> for (var p in obj) print(p); // check it has no enumerable properties |
| 28 |
js> obj.a = 2; obj.a; // check that the property is not writable |
| 29 |
1 |
| 30 |
js> delete obj.a; obj.a // check that the property is not deletable |
| 31 |
1 |
| 32 |
|
| 33 |
js> var define = Object.defineProperty; |
| 34 |
js> var describe = Object.getOwnPropertyDescriptor; |
| 35 |
|
| 36 |
js> // when define new property with empty descriptor then default values are used for the descriptor |
| 37 |
js> var obj = define({}, 'a', {}); |
| 38 |
js> describe(obj, 'a').toSource() |
| 39 |
({value:undefined, writable:false, enumerable:false, configurable:false}) |
| 40 |
|
| 41 |
js> // when define new property with data descriptor then those values are used for the descriptor |
| 42 |
js> var obj = define({}, 'a', { value: 2, writable: true }); |
| 43 |
js> var {value:v, writable:w} = describe(obj, 'a'); [v, w].toSource(); |
| 44 |
[2, true] |
| 45 |
js> obj.a |
| 46 |
2 |
| 47 |
|
| 48 |
js> // when define new property with accessor descriptor then those values are used for the descriptor |
| 49 |
js> var obj = define({}, 'a', { get: function() { return 3; }, set: function(value) {} }); |
| 50 |
js> var {get:g, set:s} = describe(obj, 'a'); [g, s].toSource(); |
| 51 |
[(function () {return 3;}), (function (value) {})] |
| 52 |
js> obj.a |
| 53 |
3 |
| 54 |
|
| 55 |
js> // when define existing property with empty descriptor then descriptor is left unchanged |
| 56 |
js> var descriptor = {value:1, writable:true, enumerable:true, configurable:true}; |
| 57 |
js> var obj = define({}, 'a', descriptor); |
| 58 |
js> var obj = define(obj, 'a', {}); |
| 59 |
js> describe(obj, 'a').toSource() |
| 60 |
({value:1, writable:true, enumerable:true, configurable:true}) |
| 61 |
|
| 62 |
js> // when define existing property with same descriptor then descriptor is left unchanged |
| 63 |
js> var descriptor = {value:1, writable:true, enumerable:true, configurable:true}; |
| 64 |
js> var obj = define({}, 'a', descriptor); |
| 65 |
js> var obj = define(obj, 'a', descriptor); |
| 66 |
js> describe(obj, 'a').toSource() |
| 67 |
({value:1, writable:true, enumerable:true, configurable:true}) |
| 68 |
|
| 69 |
js> // may not change configurable from false to true |
| 70 |
js> expectTypeError(function() { |
| 71 |
> var obj = define({}, 'a', {configurable : false}); |
| 72 |
> define(obj, 'a', {configurable : true}); |
| 73 |
> }); |
| 74 |
|
| 75 |
js> // may not change enumerable when configurable is false |
| 76 |
js> expectTypeError(function() { |
| 77 |
> var obj = define({}, 'a', {enumerable : true, configurable:false}); |
| 78 |
> define(obj, 'a', {enumerable : false}); |
| 79 |
> }); |
| 80 |
|
| 81 |
js> // may not change writable from false to true when configurable is false |
| 82 |
js> expectTypeError(function() { |
| 83 |
> var obj = define({}, 'a', {writable : false, configurable: false}); |
| 84 |
> define(obj, 'a', {writable : true}); |
| 85 |
> }); |
| 86 |
|
| 87 |
js> // may not change value when writable is false |
| 88 |
js> expectTypeError(function() { |
| 89 |
> var obj = define({}, 'a', {value : 1, writable:false}); |
| 90 |
> define(obj, 'a', {value : 2}); |
| 91 |
> }); |
| 92 |
|
| 93 |
js> // may not change getter when configurable is false |
| 94 |
js> expectTypeError(function() { |
| 95 |
> var obj = define({}, 'a', {get: function() { return 1 }, configurable:false}); |
| 96 |
> define(obj, 'a', {get: function() { return 1 }}); |
| 97 |
> }); |
| 98 |
|
| 99 |
js> // may not change setter when configurable is false |
| 100 |
js> expectTypeError(function() { |
| 101 |
> var obj = define({}, 'a', {set: function(val) {}, configurable:false}); |
| 102 |
> define(obj, 'a', {set: function(val) {}}); |
| 103 |
> }); |
| 104 |
|
| 105 |
js> // may not change from data property to accessor property when configurable is false |
| 106 |
js> expectTypeError(function() { |
| 107 |
> var obj = define({}, 'a', {value : 1, configurable:false}); |
| 108 |
> define(obj, 'a', {get : function() { return 1 }}); |
| 109 |
> }); |
| 110 |
|
| 111 |
js> // may not change from accessor property to data property when configurable is false |
| 112 |
js> expectTypeError(function() { |
| 113 |
> var obj = define({}, 'a', {get : function() { return 1 }, configurable:false}); |
| 114 |
> define(obj, 'a', {value : 1}); |
| 115 |
> }); |
| 116 |
|
| 117 |
js> // can change writable from true to false when configurable is false |
| 118 |
js> var obj = define({}, 'a', {writable:true, configurable:false}); |
| 119 |
js> var obj = define(obj, 'a', {writable:false}); |
| 120 |
|
| 121 |
js> // can set enumerable to the same value when configurable is false |
| 122 |
js> var obj = define({}, 'a', {enumerable:true, configurable:false}); |
| 123 |
js> var obj = define(obj, 'a', {enumerable:true}); |
| 124 |
|
| 125 |
js> // can change from data property to accessor property when configurable is true |
| 126 |
js> var obj = define({}, 'a', {value : 1, configurable: true}); |
| 127 |
js> var obj = define(obj, 'a', {get : function() { return 4 }}); |
| 128 |
js> obj.a |
| 129 |
4 |
| 130 |
js> describe(obj, 'a').toSource() |
| 131 |
({enumerable:false, configurable:true, get:(function () {return 4;})}) |
| 132 |
|
| 133 |
js> // can change from accessor property to data property when configurable is true |
| 134 |
js> var obj = define({}, 'a', {get : function() { return 2 }, configurable:true}); |
| 135 |
js> var obj = define(obj, 'a', {value : 5}); |
| 136 |
js> obj.a |
| 137 |
5 |
| 138 |
js> describe(obj, 'a').toSource() |
| 139 |
({value:5, writable:false, enumerable:false, configurable:true}) |
| 140 |
|
| 141 |
js> // can change enumerable and writable to true when configurable is true |
| 142 |
js> var obj = define({}, 'a', {writable : false, enumerable : false, configurable:true}); |
| 143 |
js> var obj = define(obj, 'a', {writable : true, enumerable : true, configurable:true}); |
| 144 |
|
| 145 |
js> // can change the value if writable is true |
| 146 |
js> var obj = define({}, 'a', {value:6, writable:true}) |
| 147 |
js> obj.a |
| 148 |
6 |
| 149 |
js> var obj = define(obj, 'a', {value:7}) |
| 150 |
js> obj.a |
| 151 |
7 |
| 152 |
|
| 153 |
js> // defining a new property should fail loudly when object is not extensible |
| 154 |
js> var obj = Object.preventExtensions({}); |
| 155 |
js> expectTypeError(function() { define(obj, 'a', {value:1}) }) |
| 156 |
|
| 157 |
js> // defining new property should succeed when object is extensible |
| 158 |
js> var obj = {} |
| 159 |
js> Object.isExtensible(obj); |
| 160 |
true |
| 161 |
js> obj.a = 8; obj.a |
| 162 |
8 |
| 163 |
|
| 164 |
js> // changing existing property should succeed when object is not extensible |
| 165 |
js> var obj = define({}, 'a', {value:1, writable:true}); |
| 166 |
js> var obj = Object.preventExtensions(obj); |
| 167 |
js> obj.a = 9; obj.a |
| 168 |
9 |
| 169 |
|
| 170 |
js> // defined getters and setters must be functions |
| 171 |
js> expectTypeError(function() { define({}, 'a', {get:1}); }) |
| 172 |
js> expectTypeError(function() { define({}, 'a', {set:1}); }) |
| 173 |
|
| 174 |
js> // make sure defineProperty works properly with numbers as ids |
| 175 |
js> Object.defineProperty({}, 0, {value:1, enumerable:true})['0'] |
| 176 |
1 |
| 177 |
|
| 178 |
js> // make sure defineProperty works properly with arrays |
| 179 |
js> Object.defineProperty([], 0, {value:1, enumerable:true})[0] |
| 180 |
1 |
| 181 |
|
| 182 |
js> // make sure defineProperty works properly with arrays |
| 183 |
js> Object.defineProperty([], 'a', {value:1, enumerable:true})['a'] |
| 184 |
1 |
| 185 |
|
| 186 |
js> // make sure defineProperty updates length properly for arrays |
| 187 |
js> Object.defineProperty([], 0, {value:1}).length |
| 188 |
1 |
| 189 |
|
| 190 |
js> // make sure that getters and setters are actually used to get and set property |
| 191 |
js> Object.defineProperty({}, 'a', {get:function() { return "get called"; }}).a |
| 192 |
get called |
| 193 |
js> Object.defineProperty({}, 'a', {set:function(val) { print("set called with "+val); }}).a = 1; undefined; |
| 194 |
set called with 1 |
| 195 |
|
| 196 |
js> // make sure defineProperty works for builtin properties |
| 197 |
js> Object.defineProperty(JSON, 'stringify', {value:1}).stringify |
| 198 |
1 |
| 199 |
js> Object.defineProperty(JSON, 'parse', {get:function() {print('do get'); return undefined}}).parse |
| 200 |
do get |
| 201 |
|
| 202 |
js> // an accessor property without a setter behaves as if the setter were undefined |
| 203 |
js> // and thus the setter can be set to undefined even when configurable is false |
| 204 |
js> var obj = Object.defineProperty({}, 'a', {get:function(){return 1}}) |
| 205 |
js> var _ = Object.defineProperty(obj, 'a', {set:undefined}) |
| 206 |
js> |
| 207 |
|