blob: 7d838e4a7c912766f3ec9c5dd0961e2038b1ea3a [file] [log] [blame]
Tim van der Lippe2c891972021-07-29 15:22:501'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4
Nikolay Vitkovd76576c2024-12-02 14:10:155var $Object = require('es-object-atoms');
Tim van der Lippe2c891972021-07-29 15:22:506var $StringPrototype = GetIntrinsic('%String.prototype%');
Nikolay Vitkovd76576c2024-12-02 14:10:157var $SyntaxError = require('es-errors/syntax');
8var $TypeError = require('es-errors/type');
Tim van der Lippe2c891972021-07-29 15:22:509
10var DefinePropertyOrThrow = require('./DefinePropertyOrThrow');
Tim van der Lippe2c891972021-07-29 15:22:5011
12var setProto = require('../helpers/setProto');
13
14// https://262.ecma-international.org/6.0/#sec-stringcreate
15
16module.exports = function StringCreate(value, prototype) {
Nikolay Vitkovd76576c2024-12-02 14:10:1517 if (typeof value !== 'string') {
Tim van der Lippe2c891972021-07-29 15:22:5018 throw new $TypeError('Assertion failed: `S` must be a String');
19 }
20
21 var S = $Object(value);
Nikolay Vitkovd76576c2024-12-02 14:10:1522 if (prototype !== $StringPrototype) {
Tim van der Lippe2c891972021-07-29 15:22:5023 if (setProto) {
24 setProto(S, prototype);
25 } else {
26 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]]');
27 }
28 }
29
30 var length = value.length;
31 DefinePropertyOrThrow(S, 'length', {
32 '[[Configurable]]': false,
33 '[[Enumerable]]': false,
34 '[[Value]]': length,
35 '[[Writable]]': false
36 });
37
38 return S;
39};