| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | |
| 3 | var GetIntrinsic = require('get-intrinsic'); |
| 4 | |
| 5 | var $Object = GetIntrinsic('%Object%'); |
| 6 | var $StringPrototype = GetIntrinsic('%String.prototype%'); |
| 7 | var $SyntaxError = GetIntrinsic('%SyntaxError%'); |
| 8 | var $TypeError = GetIntrinsic('%TypeError%'); |
| 9 | |
| 10 | var DefinePropertyOrThrow = require('./DefinePropertyOrThrow'); |
| 11 | var Type = require('./Type'); |
| 12 | |
| 13 | var setProto = require('../helpers/setProto'); |
| 14 | |
| 15 | // https://262.ecma-international.org/6.0/#sec-stringcreate |
| 16 | |
| 17 | module.exports = function StringCreate(value, prototype) { |
| 18 | if (Type(value) !== 'String') { |
| 19 | throw new $TypeError('Assertion failed: `S` must be a String'); |
| 20 | } |
| 21 | |
| 22 | var S = $Object(value); |
| 23 | if (S !== $StringPrototype) { |
| 24 | if (setProto) { |
| 25 | setProto(S, prototype); |
| 26 | } else { |
| 27 | 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]]'); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | var length = value.length; |
| 32 | DefinePropertyOrThrow(S, 'length', { |
| 33 | '[[Configurable]]': false, |
| 34 | '[[Enumerable]]': false, |
| 35 | '[[Value]]': length, |
| 36 | '[[Writable]]': false |
| 37 | }); |
| 38 | |
| 39 | return S; |
| 40 | }; |