OrdinaryObjectCreate.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 hasProto = !({ __proto__: null } instanceof Object);
  9. // https://262.ecma-international.org/6.0/#sec-objectcreate
  10. module.exports = function OrdinaryObjectCreate(proto) {
  11. if (proto !== null && Type(proto) !== 'Object') {
  12. throw new $TypeError('Assertion failed: `proto` must be null or an object');
  13. }
  14. var additionalInternalSlotsList = arguments.length < 2 ? [] : arguments[1];
  15. if (!IsArray(additionalInternalSlotsList)) {
  16. throw new $TypeError('Assertion failed: `additionalInternalSlotsList` must be an Array');
  17. }
  18. // var internalSlotsList = ['[[Prototype]]', '[[Extensible]]'];
  19. if (additionalInternalSlotsList.length > 0) {
  20. throw new $SyntaxError('es-abstract does not yet support internal slots');
  21. // internalSlotsList.push(...additionalInternalSlotsList);
  22. }
  23. // var O = MakeBasicObject(internalSlotsList);
  24. // setProto(O, proto);
  25. // return O;
  26. if ($ObjectCreate) {
  27. return $ObjectCreate(proto);
  28. }
  29. if (hasProto) {
  30. return { __proto__: proto };
  31. }
  32. if (proto === null) {
  33. throw new $SyntaxError('native Object.create support is required to create null objects');
  34. }
  35. var T = function T() {};
  36. T.prototype = proto;
  37. return new T();
  38. };