summaryrefslogtreecommitdiffstats
path: root/js/src/builtin/Object.js
blob: 6193f1125c62d72a7c307ffaea05554e77aad40c (plain)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// ES stage 4 proposal
function ObjectGetOwnPropertyDescriptors(O) {
  // Step 1.
  var obj = ToObject(O);

  // Step 2.
  var keys = std_Reflect_ownKeys(obj);

  // Step 3.
  var descriptors = {};

  // Step 4.
  for (var index = 0, len = keys.length; index < len; index++) {
    var key = keys[index];

    // Steps 4.a-b.
    var desc = ObjectGetOwnPropertyDescriptor(obj, key);

    // Step 4.c.
    if (typeof desc !== "undefined") {
      DefineDataProperty(descriptors, key, desc);
    }
  }

  // Step 5.
  return descriptors;
}

/* ES6 draft rev 32 (2015 Feb 2) 19.1.2.9. */
function ObjectGetPrototypeOf(obj) {
  return std_Reflect_getPrototypeOf(ToObject(obj));
}

/* ES6 draft rev 32 (2015 Feb 2) 19.1.2.11. */
function ObjectIsExtensible(obj) {
  return IsObject(obj) && std_Reflect_isExtensible(obj);
}

/* ES2015 19.1.3.5 Object.prototype.toLocaleString */
function Object_toLocaleString() {
  // Step 1.
  var O = this;

  // Step 2.
  return callContentFunction(O.toString, O);
}

// ES 2017 draft bb96899bb0d9ef9be08164a26efae2ee5f25e875 19.1.3.7
function Object_valueOf() {
  // Step 1.
  return ToObject(this);
}

// ES 2018 draft 19.1.3.2
function Object_hasOwnProperty(V) {
  // Implement hasOwnProperty as a pseudo function that becomes a JSOp
  // to easier add an inline cache for this.
  return hasOwn(V, this);
}

// ES 2021 draft rev 0b988b7700de675331ac360d164c978d6ea452ec
// B.2.2.1.1 get Object.prototype.__proto__
function $ObjectProtoGetter() {
  return std_Reflect_getPrototypeOf(ToObject(this));
}
SetCanonicalName($ObjectProtoGetter, "get __proto__");

// ES 2021 draft rev 0b988b7700de675331ac360d164c978d6ea452ec
// B.2.2.1.2 set Object.prototype.__proto__
function $ObjectProtoSetter(proto) {
  return callFunction(std_Object_setProto, this, proto);
}
SetCanonicalName($ObjectProtoSetter, "set __proto__");

// ES7 draft (2016 March 8) B.2.2.3
function ObjectDefineSetter(name, setter) {
  // Step 1.
  var object = ToObject(this);

  // Step 2.
  if (!IsCallable(setter)) {
    ThrowTypeError(JSMSG_BAD_GETTER_OR_SETTER, "setter");
  }

  // Step 4.
  var key = TO_PROPERTY_KEY(name);

  // Steps 3, 5.
  DefineProperty(
    object,
    key,
    ACCESSOR_DESCRIPTOR_KIND | ATTR_ENUMERABLE | ATTR_CONFIGURABLE,
    null,
    setter,
    true
  );

  // Step 6. (implicit)
}

// ES7 draft (2016 March 8) B.2.2.2
function ObjectDefineGetter(name, getter) {
  // Step 1.
  var object = ToObject(this);

  // Step 2.
  if (!IsCallable(getter)) {
    ThrowTypeError(JSMSG_BAD_GETTER_OR_SETTER, "getter");
  }

  // Step 4.
  var key = TO_PROPERTY_KEY(name);

  // Steps 3, 5.
  DefineProperty(
    object,
    key,
    ACCESSOR_DESCRIPTOR_KIND | ATTR_ENUMERABLE | ATTR_CONFIGURABLE,
    getter,
    null,
    true
  );

  // Step 6. (implicit)
}

// ES7 draft (2016 March 8) B.2.2.5
function ObjectLookupSetter(name) {
  // Step 1.
  var object = ToObject(this);

  // Step 2.
  var key = TO_PROPERTY_KEY(name);

  do {
    // Step 3.a.
    var desc = GetOwnPropertyDescriptorToArray(object, key);

    // Step 3.b.
    if (desc) {
      // Step.b.i.
      if (desc[PROP_DESC_ATTRS_AND_KIND_INDEX] & ACCESSOR_DESCRIPTOR_KIND) {
        return desc[PROP_DESC_SETTER_INDEX];
      }

      // Step.b.i.
      return undefined;
    }

    // Step 3.c.
    object = std_Reflect_getPrototypeOf(object);
  } while (object !== null);

  // Step 3.d. (implicit)
}

// ES7 draft (2016 March 8) B.2.2.4
function ObjectLookupGetter(name) {
  // Step 1.
  var object = ToObject(this);

  // Step 2.
  var key = TO_PROPERTY_KEY(name);

  do {
    // Step 3.a.
    var desc = GetOwnPropertyDescriptorToArray(object, key);

    // Step 3.b.
    if (desc) {
      // Step.b.i.
      if (desc[PROP_DESC_ATTRS_AND_KIND_INDEX] & ACCESSOR_DESCRIPTOR_KIND) {
        return desc[PROP_DESC_GETTER_INDEX];
      }

      // Step.b.ii.
      return undefined;
    }

    // Step 3.c.
    object = std_Reflect_getPrototypeOf(object);
  } while (object !== null);

  // Step 3.d. (implicit)
}

// ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
// 19.1.2.6 Object.getOwnPropertyDescriptor ( O, P )
function ObjectGetOwnPropertyDescriptor(obj, propertyKey) {
  // Steps 1-3.
  var desc = GetOwnPropertyDescriptorToArray(obj, propertyKey);

  // Step 4 (Call to 6.2.4.4 FromPropertyDescriptor).

  // 6.2.4.4 FromPropertyDescriptor, step 1.
  if (!desc) {
    return undefined;
  }

  // 6.2.4.4 FromPropertyDescriptor, steps 2-5, 8-11.
  var attrsAndKind = desc[PROP_DESC_ATTRS_AND_KIND_INDEX];
  if (attrsAndKind & DATA_DESCRIPTOR_KIND) {
    return {
      value: desc[PROP_DESC_VALUE_INDEX],
      writable: !!(attrsAndKind & ATTR_WRITABLE),
      enumerable: !!(attrsAndKind & ATTR_ENUMERABLE),
      configurable: !!(attrsAndKind & ATTR_CONFIGURABLE),
    };
  }

  // 6.2.4.4 FromPropertyDescriptor, steps 2-3, 6-11.
  assert(
    attrsAndKind & ACCESSOR_DESCRIPTOR_KIND,
    "expected accessor property descriptor"
  );
  return {
    get: desc[PROP_DESC_GETTER_INDEX],
    set: desc[PROP_DESC_SETTER_INDEX],
    enumerable: !!(attrsAndKind & ATTR_ENUMERABLE),
    configurable: !!(attrsAndKind & ATTR_CONFIGURABLE),
  };
}

// ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
// 19.1.2.4 Object.defineProperty ( O, P, Attributes )
// 26.1.3 Reflect.defineProperty ( target, propertyKey, attributes )
function ObjectOrReflectDefineProperty(obj, propertyKey, attributes, strict) {
  // Step 1.
  if (!IsObject(obj)) {
    ThrowTypeError(JSMSG_OBJECT_REQUIRED, DecompileArg(0, obj));
  }

  // Step 2.
  propertyKey = TO_PROPERTY_KEY(propertyKey);

  // Step 3 (Call to 6.2.4.5 ToPropertyDescriptor).

  // 6.2.4.5 ToPropertyDescriptor, step 1.
  if (!IsObject(attributes)) {
    ThrowTypeError(
      JSMSG_OBJECT_REQUIRED_PROP_DESC,
      DecompileArg(2, attributes)
    );
  }

  // 6.2.4.5 ToPropertyDescriptor, step 2.
  var attrs = 0;
  var hasValue = false;
  var value;
  var getter = null;
  var setter = null;

  // 6.2.4.5 ToPropertyDescriptor, steps 3-4.
  if ("enumerable" in attributes) {
    attrs |= attributes.enumerable ? ATTR_ENUMERABLE : ATTR_NONENUMERABLE;
  }

  // 6.2.4.5 ToPropertyDescriptor, steps 5-6.
  if ("configurable" in attributes) {
    attrs |= attributes.configurable ? ATTR_CONFIGURABLE : ATTR_NONCONFIGURABLE;
  }

  // 6.2.4.5 ToPropertyDescriptor, steps 7-8.
  if ("value" in attributes) {
    attrs |= DATA_DESCRIPTOR_KIND;
    value = attributes.value;
    hasValue = true;
  }

  // 6.2.4.5 ToPropertyDescriptor, steps 9-10.
  if ("writable" in attributes) {
    attrs |= DATA_DESCRIPTOR_KIND;
    attrs |= attributes.writable ? ATTR_WRITABLE : ATTR_NONWRITABLE;
  }

  // 6.2.4.5 ToPropertyDescriptor, steps 11-12.
  if ("get" in attributes) {
    attrs |= ACCESSOR_DESCRIPTOR_KIND;
    getter = attributes.get;
    if (!IsCallable(getter) && getter !== undefined) {
      ThrowTypeError(JSMSG_BAD_GET_SET_FIELD, "get");
    }
  }

  // 6.2.4.5 ToPropertyDescriptor, steps 13-14.
  if ("set" in attributes) {
    attrs |= ACCESSOR_DESCRIPTOR_KIND;
    setter = attributes.set;
    if (!IsCallable(setter) && setter !== undefined) {
      ThrowTypeError(JSMSG_BAD_GET_SET_FIELD, "set");
    }
  }

  if (attrs & ACCESSOR_DESCRIPTOR_KIND) {
    // 6.2.4.5 ToPropertyDescriptor, step 15.
    if (attrs & DATA_DESCRIPTOR_KIND) {
      ThrowTypeError(JSMSG_INVALID_DESCRIPTOR);
    }

    // Step 4 (accessor descriptor property).
    return DefineProperty(obj, propertyKey, attrs, getter, setter, strict);
  }

  // Step 4 (data property descriptor with value).
  if (hasValue) {
    // Use the inlinable DefineDataProperty function when possible.
    if (strict) {
      if (
        (attrs & (ATTR_ENUMERABLE | ATTR_CONFIGURABLE | ATTR_WRITABLE)) ===
        (ATTR_ENUMERABLE | ATTR_CONFIGURABLE | ATTR_WRITABLE)
      ) {
        DefineDataProperty(obj, propertyKey, value);
        return true;
      }
    }

    // The fifth argument is set to |null| to mark that |value| is present.
    return DefineProperty(obj, propertyKey, attrs, value, null, strict);
  }

  // Step 4 (generic property descriptor or data property without value).
  return DefineProperty(obj, propertyKey, attrs, undefined, undefined, strict);
}

// ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
// 19.1.2.4 Object.defineProperty ( O, P, Attributes )
function ObjectDefineProperty(obj, propertyKey, attributes) {
  // Steps 1-4.
  if (!ObjectOrReflectDefineProperty(obj, propertyKey, attributes, true)) {
    // Not standardized yet: https://github.com/tc39/ecma262/pull/688
    return null;
  }

  // Step 5.
  return obj;
}

// Proposal https://tc39.github.io/proposal-object-from-entries/
// 1. Object.fromEntries ( iterable )
function ObjectFromEntries(iter) {
  // We omit the usual step number comments here because they don't help.
  // This implementation inlines AddEntriesFromIterator and
  // CreateDataPropertyOnObject, so it looks more like the polyfill
  // <https://github.com/tc39/proposal-object-from-entries/blob/master/polyfill.js>
  // than the spec algorithm.
  const obj = {};

  for (const pair of allowContentIter(iter)) {
    if (!IsObject(pair)) {
      ThrowTypeError(JSMSG_INVALID_MAP_ITERABLE, "Object.fromEntries");
    }
    DefineDataProperty(obj, pair[0], pair[1]);
  }

  return obj;
}

// Proposal https://github.com/tc39/proposal-accessible-object-hasownproperty
// 1. Object.hasOwn ( O, P )
function ObjectHasOwn(O, P) {
  // Step 1.
  var obj = ToObject(O);
  // Step 2-3.
  return hasOwn(P, obj);
}