blob: da0c0ea0665bb9b8f149d6926a72268aa074c916 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4
5var $Object = GetIntrinsic('%Object%');
6var $StringPrototype = GetIntrinsic('%String.prototype%');
7var $SyntaxError = GetIntrinsic('%SyntaxError%');
8var $TypeError = GetIntrinsic('%TypeError%');
9
10var DefinePropertyOrThrow = require('./DefinePropertyOrThrow');
11var Type = require('./Type');
12
13var setProto = require('../helpers/setProto');
14
15// https://262.ecma-international.org/6.0/#sec-stringcreate
16
17module.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};