OrdinaryObjectCreate.js 1.6 KB

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