StringCreate.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $Object = GetIntrinsic('%Object%');
  4. var $StringPrototype = GetIntrinsic('%String.prototype%');
  5. var $SyntaxError = GetIntrinsic('%SyntaxError%');
  6. var $TypeError = GetIntrinsic('%TypeError%');
  7. var DefinePropertyOrThrow = require('./DefinePropertyOrThrow');
  8. var Type = require('./Type');
  9. var setProto = require('../helpers/setProto');
  10. // https://262.ecma-international.org/6.0/#sec-stringcreate
  11. module.exports = function StringCreate(value, prototype) {
  12. if (Type(value) !== 'String') {
  13. throw new $TypeError('Assertion failed: `S` must be a String');
  14. }
  15. var S = $Object(value);
  16. if (prototype !== $StringPrototype) {
  17. if (setProto) {
  18. setProto(S, prototype);
  19. } else {
  20. throw new $SyntaxError('StringCreate: a `proto` argument that is not `String.prototype` is not supported in an environment that does not support setting the [[Prototype]]');
  21. }
  22. }
  23. var length = value.length;
  24. DefinePropertyOrThrow(S, 'length', {
  25. '[[Configurable]]': false,
  26. '[[Enumerable]]': false,
  27. '[[Value]]': length,
  28. '[[Writable]]': false
  29. });
  30. return S;
  31. };