deno.land / std@0.166.0 / encoding / testdata / jsonc / test262 / propertyHelper.js

propertyHelper.js
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.// Ported from test262// https://github.com/tc39/test262/blob/276e79d62e8c45bc1e427fc680320c4899eace27/harness/propertyHelper.js// Copyright (C) 2017 Ecma International. All rights reserved.// This code is governed by the BSD license found in the LICENSE file./*---description: | Collection of functions used to safely verify the correctness of property descriptors.defines: - verifyProperty - verifyEqualTo - verifyWritable - verifyNotWritable - verifyEnumerable - verifyNotEnumerable - verifyConfigurable - verifyNotConfigurable---*/
// @ts-check
/** * @param {object} obj * @param {string|symbol} name * @param {PropertyDescriptor|undefined} desc * @param {object} [options] * @param {boolean} [options.restore] */function verifyProperty(obj, name, desc, options) { assert( arguments.length > 2, 'verifyProperty should receive at least 3 arguments: obj, name, and descriptor' );
var originalDesc = Object.getOwnPropertyDescriptor(obj, name); var nameStr = String(name);
// Allows checking for undefined descriptor if it's explicitly given. if (desc === undefined) { assert.sameValue( originalDesc, undefined, "obj['" + nameStr + "'] descriptor should be undefined" );
// desc and originalDesc are both undefined, problem solved; return true; }
assert( Object.prototype.hasOwnProperty.call(obj, name), "obj should have an own property " + nameStr );
assert.notSameValue( desc, null, "The desc argument should be an object or undefined, null" );
assert.sameValue( typeof desc, "object", "The desc argument should be an object or undefined, " + String(desc) );
var failures = [];
if (Object.prototype.hasOwnProperty.call(desc, 'value')) { if (!isSameValue(desc.value, originalDesc.value)) { failures.push("descriptor value should be " + desc.value); } }
if (Object.prototype.hasOwnProperty.call(desc, 'enumerable')) { if (desc.enumerable !== originalDesc.enumerable || desc.enumerable !== isEnumerable(obj, name)) { failures.push('descriptor should ' + (desc.enumerable ? '' : 'not ') + 'be enumerable'); } }
if (Object.prototype.hasOwnProperty.call(desc, 'writable')) { if (desc.writable !== originalDesc.writable || desc.writable !== isWritable(obj, name)) { failures.push('descriptor should ' + (desc.writable ? '' : 'not ') + 'be writable'); } }
if (Object.prototype.hasOwnProperty.call(desc, 'configurable')) { if (desc.configurable !== originalDesc.configurable || desc.configurable !== isConfigurable(obj, name)) { failures.push('descriptor should ' + (desc.configurable ? '' : 'not ') + 'be configurable'); } }
assert(!failures.length, failures.join('; '));
if (options && options.restore) { Object.defineProperty(obj, name, originalDesc); }
return true;}
function isConfigurable(obj, name) { var hasOwnProperty = Object.prototype.hasOwnProperty; try { delete obj[name]; } catch (e) { if (!(e instanceof TypeError)) { $ERROR("Expected TypeError, got " + e); } } return !hasOwnProperty.call(obj, name);}
function isEnumerable(obj, name) { var stringCheck = false;
if (typeof name === "string") { for (var x in obj) { if (x === name) { stringCheck = true; break; } } } else { // skip it if name is not string, works for Symbol names. stringCheck = true; }
return stringCheck && Object.prototype.hasOwnProperty.call(obj, name) && Object.prototype.propertyIsEnumerable.call(obj, name);}
function isSameValue(a, b) { if (a === 0 && b === 0) return 1 / a === 1 / b; if (a !== a && b !== b) return true;
return a === b;}
var __isArray = Array.isArray;function isWritable(obj, name, verifyProp, value) { var unlikelyValue = __isArray(obj) && name === "length" ? Math.pow(2, 32) - 1 : "unlikelyValue"; var newValue = value || unlikelyValue; var hadValue = Object.prototype.hasOwnProperty.call(obj, name); var oldValue = obj[name]; var writeSucceeded;
try { obj[name] = newValue; } catch (e) { if (!(e instanceof TypeError)) { $ERROR("Expected TypeError, got " + e); } }
writeSucceeded = isSameValue(obj[verifyProp || name], newValue);
// Revert the change only if it was successful (in other cases, reverting // is unnecessary and may trigger exceptions for certain property // configurations) if (writeSucceeded) { if (hadValue) { obj[name] = oldValue; } else { delete obj[name]; } }
return writeSucceeded;}
function verifyEqualTo(obj, name, value) { if (!isSameValue(obj[name], value)) { $ERROR("Expected obj[" + String(name) + "] to equal " + value + ", actually " + obj[name]); }}
function verifyWritable(obj, name, verifyProp, value) { if (!verifyProp) { assert(Object.getOwnPropertyDescriptor(obj, name).writable, "Expected obj[" + String(name) + "] to have writable:true."); } if (!isWritable(obj, name, verifyProp, value)) { $ERROR("Expected obj[" + String(name) + "] to be writable, but was not."); }}
function verifyNotWritable(obj, name, verifyProp, value) { if (!verifyProp) { assert(!Object.getOwnPropertyDescriptor(obj, name).writable, "Expected obj[" + String(name) + "] to have writable:false."); } if (isWritable(obj, name, verifyProp)) { $ERROR("Expected obj[" + String(name) + "] NOT to be writable, but was."); }}
function verifyEnumerable(obj, name) { assert(Object.getOwnPropertyDescriptor(obj, name).enumerable, "Expected obj[" + String(name) + "] to have enumerable:true."); if (!isEnumerable(obj, name)) { $ERROR("Expected obj[" + String(name) + "] to be enumerable, but was not."); }}
function verifyNotEnumerable(obj, name) { assert(!Object.getOwnPropertyDescriptor(obj, name).enumerable, "Expected obj[" + String(name) + "] to have enumerable:false."); if (isEnumerable(obj, name)) { $ERROR("Expected obj[" + String(name) + "] NOT to be enumerable, but was."); }}
function verifyConfigurable(obj, name) { assert(Object.getOwnPropertyDescriptor(obj, name).configurable, "Expected obj[" + String(name) + "] to have configurable:true."); if (!isConfigurable(obj, name)) { $ERROR("Expected obj[" + String(name) + "] to be configurable, but was not."); }}
function verifyNotConfigurable(obj, name) { assert(!Object.getOwnPropertyDescriptor(obj, name).configurable, "Expected obj[" + String(name) + "] to have configurable:false."); if (isConfigurable(obj, name)) { $ERROR("Expected obj[" + String(name) + "] NOT to be configurable, but was."); }}
std

Version Info

Tagged at
a year ago