blob: 182b8849d528111b4ab1bc2856804f500e5ee87c [file] [log] [blame]
Tim van der Lippefdbd42e2020-04-07 14:14:361'use strict';
2
Tim van der Lippe2c891972021-07-29 15:22:503var GetIntrinsic = require('get-intrinsic');
Tim van der Lippefdbd42e2020-04-07 14:14:364
5var $ObjectCreate = GetIntrinsic('%Object.create%', true);
Nikolay Vitkovd76576c2024-12-02 14:10:156var $TypeError = require('es-errors/type');
7var $SyntaxError = require('es-errors/syntax');
Tim van der Lippefdbd42e2020-04-07 14:14:368
Nikolay Vitkovd76576c2024-12-02 14:10:159var IsArray = require('./IsArray');
Tim van der Lippefdbd42e2020-04-07 14:14:3610var Type = require('./Type');
11
Nikolay Vitkovd76576c2024-12-02 14:10:1512var forEach = require('../helpers/forEach');
Tim van der Lippefdbd42e2020-04-07 14:14:3613
Nikolay Vitkovd76576c2024-12-02 14:10:1514var SLOT = require('internal-slot');
15
16var hasProto = require('has-proto')();
17
18// https://262.ecma-international.org/6.0/#sec-objectcreate
Tim van der Lippefdbd42e2020-04-07 14:14:3619
20module.exports = function ObjectCreate(proto, internalSlotsList) {
21 if (proto !== null && Type(proto) !== 'Object') {
22 throw new $TypeError('Assertion failed: `proto` must be null or an object');
23 }
Nikolay Vitkovd76576c2024-12-02 14:10:1524 var slots = arguments.length < 2 ? [] : internalSlotsList; // step 1
25 if (arguments.length >= 2 && !IsArray(slots)) {
26 throw new $TypeError('Assertion failed: `internalSlotsList` must be an Array');
Tim van der Lippefdbd42e2020-04-07 14:14:3627 }
28
Nikolay Vitkovd76576c2024-12-02 14:10:1529 var O;
Tim van der Lippefdbd42e2020-04-07 14:14:3630 if ($ObjectCreate) {
Nikolay Vitkovd76576c2024-12-02 14:10:1531 O = $ObjectCreate(proto);
32 } else if (hasProto) {
33 O = { __proto__: proto };
34 } else {
35 if (proto === null) {
36 throw new $SyntaxError('native Object.create support is required to create null objects');
37 }
38 var T = function T() {};
39 T.prototype = proto;
40 O = new T();
Tim van der Lippefdbd42e2020-04-07 14:14:3641 }
42
Nikolay Vitkovd76576c2024-12-02 14:10:1543 if (slots.length > 0) {
44 forEach(slots, function (slot) {
45 SLOT.set(O, slot, void undefined);
46 });
Tim van der Lippefdbd42e2020-04-07 14:14:3647 }
Nikolay Vitkovd76576c2024-12-02 14:10:1548
49 return O; // step 6
Tim van der Lippefdbd42e2020-04-07 14:14:3650};