StringCreate.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $Object = require('es-object-atoms');
  4. var $StringPrototype = GetIntrinsic('%String.prototype%');
  5. var $SyntaxError = require('es-errors/syntax');
  6. var $TypeError = require('es-errors/type');
  7. var DefinePropertyOrThrow = require('./DefinePropertyOrThrow');
  8. var setProto = require('../helpers/setProto');
  9. // https://262.ecma-international.org/6.0/#sec-stringcreate
  10. module.exports = function StringCreate(value, prototype) {
  11. if (typeof value !== 'string') {
  12. throw new $TypeError('Assertion failed: `S` must be a String');
  13. }
  14. var S = $Object(value);
  15. if (prototype !== $StringPrototype) {
  16. if (setProto) {
  17. setProto(S, prototype);
  18. } else {
  19. 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]]');
  20. }
  21. }
  22. var length = value.length;
  23. DefinePropertyOrThrow(S, 'length', {
  24. '[[Configurable]]': false,
  25. '[[Enumerable]]': false,
  26. '[[Value]]': length,
  27. '[[Writable]]': false
  28. });
  29. return S;
  30. };