[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var GetIntrinsic = require('get-intrinsic');
|
---|
| 4 |
|
---|
| 5 | var $ObjectCreate = GetIntrinsic('%Object.create%', true);
|
---|
| 6 | var $TypeError = require('es-errors/type');
|
---|
| 7 | var $SyntaxError = require('es-errors/syntax');
|
---|
| 8 |
|
---|
| 9 | var IsArray = require('./IsArray');
|
---|
| 10 | var Type = require('./Type');
|
---|
| 11 |
|
---|
| 12 | var forEach = require('../helpers/forEach');
|
---|
| 13 |
|
---|
| 14 | var SLOT = require('internal-slot');
|
---|
| 15 |
|
---|
| 16 | var hasProto = require('has-proto')();
|
---|
| 17 |
|
---|
| 18 | // https://262.ecma-international.org/11.0/#sec-objectcreate
|
---|
| 19 |
|
---|
| 20 | module.exports = function OrdinaryObjectCreate(proto) {
|
---|
| 21 | if (proto !== null && Type(proto) !== 'Object') {
|
---|
| 22 | throw new $TypeError('Assertion failed: `proto` must be null or an object');
|
---|
| 23 | }
|
---|
| 24 | var additionalInternalSlotsList = arguments.length < 2 ? [] : arguments[1];
|
---|
| 25 | if (!IsArray(additionalInternalSlotsList)) {
|
---|
| 26 | throw new $TypeError('Assertion failed: `additionalInternalSlotsList` must be an Array');
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | // var internalSlotsList = ['[[Prototype]]', '[[Extensible]]']; // step 1
|
---|
| 30 | // internalSlotsList.push(...additionalInternalSlotsList); // step 2
|
---|
| 31 | // var O = MakeBasicObject(internalSlotsList); // step 3
|
---|
| 32 | // setProto(O, proto); // step 4
|
---|
| 33 | // return O; // step 5
|
---|
| 34 |
|
---|
| 35 | var O;
|
---|
| 36 | if ($ObjectCreate) {
|
---|
| 37 | O = $ObjectCreate(proto);
|
---|
| 38 | } else if (hasProto) {
|
---|
| 39 | O = { __proto__: proto };
|
---|
| 40 | } else {
|
---|
| 41 | if (proto === null) {
|
---|
| 42 | throw new $SyntaxError('native Object.create support is required to create null objects');
|
---|
| 43 | }
|
---|
| 44 | var T = function T() {};
|
---|
| 45 | T.prototype = proto;
|
---|
| 46 | O = new T();
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | if (additionalInternalSlotsList.length > 0) {
|
---|
| 50 | forEach(additionalInternalSlotsList, function (slot) {
|
---|
| 51 | SLOT.set(O, slot, void undefined);
|
---|
| 52 | });
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | return O;
|
---|
| 56 | };
|
---|