"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); /** * Copyright (c) 2017-present, Facebook, Inc. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. */ exports.default = function (realm) { // ECMA262 21.1.1 var func = new _index.NativeFunctionValue(realm, "String", "String", 1, function (context, _ref, argCount, NewTarget) { var _ref2 = _slicedToArray(_ref, 1), value = _ref2[0]; var s = void 0; // 1. If no arguments were passed to this function invocation, let s be "". if (argCount === 0) { s = realm.intrinsics.emptyString; } else { // 2. Else, // a. If NewTarget is undefined and Type(value) is Symbol, return SymbolDescriptiveString(value). if (!NewTarget && value instanceof _index.SymbolValue) { return new _index.StringValue(realm, (0, _index2.SymbolDescriptiveString)(realm, value)); } // b. Let s be ? ToString(value). s = new _index.StringValue(realm, _singletons.To.ToStringPartial(realm, value)); } // 3. If NewTarget is undefined, return s. if (!NewTarget) return s; // 4. Return ? StringCreate(s, ? GetPrototypeFromConstructor(NewTarget, "%StringPrototype%")). return _singletons.Create.StringCreate(realm, s, (0, _index2.GetPrototypeFromConstructor)(realm, NewTarget, "StringPrototype")); }); // ECMA262 21.1.2.1 ( ..._codeUnits_ ) func.defineNativeMethod("fromCharCode", 1, function (context, codeUnits, argCount) { // 1. Let codeUnits be a List containing the arguments passed to this function. codeUnits; // 2. Let length be the number of elements in codeUnits. var length = argCount; // 3. Let elements be a new empty List. var elements = []; // 4. Let nextIndex be 0. var nextIndex = 0; // 5. Repeat while nextIndex < length while (nextIndex < length) { // a. Let next be codeUnits[nextIndex]. var next = codeUnits[nextIndex]; // b. Let nextCU be ? ToUint16(next). var nextCU = _singletons.To.ToUint16(realm, next); // c. Append nextCU to the end of elements. elements.push(nextCU); // d. Let nextIndex be nextIndex + 1. nextIndex++; } // 6. Return the String value whose elements are, in order, the elements in the List elements. If length // is 0, the empty string is returned. return new _index.StringValue(realm, String.fromCharCode.apply(null, elements)); }); // ECMA262 21.1.2.2 ( ..._codePoints_ ) if (!realm.isCompatibleWith(realm.MOBILE_JSC_VERSION)) func.defineNativeMethod("fromCodePoint", 1, function (context, codePoints, argCount) { // 1. Let codePoints be a List containing the arguments passed to this function. codePoints; // 2. Let length be the number of elements in codePoints. var length = argCount; // 3. Let elements be a new empty List. var elements = []; // 4. Let nextIndex be 0. var nextIndex = 0; // 5. Repeat while nextIndex < length while (nextIndex < length) { // a. Let next be codePoints[nextIndex]. var next = codePoints[nextIndex]; // b. Let nextCP be ? ToNumber(next). var nextCP = _singletons.To.ToNumber(realm, next); // c. If SameValue(nextCP, ToInteger(nextCP)) is false, throw a RangeError exception. if (nextCP !== _singletons.To.ToInteger(realm, nextCP)) { throw realm.createErrorThrowCompletion(realm.intrinsics.RangeError, "SameValue(nextCP, To.ToInteger(nextCP)) is false"); } // d. If nextCP < 0 or nextCP > 0x10FFFF, throw a RangeError exception. if (nextCP < 0 || nextCP > 0x10ffff) { throw realm.createErrorThrowCompletion(realm.intrinsics.RangeError, "SameValue(nextCP, To.ToInteger(nextCP)) is false"); } // e. Append the elements of the UTF16Encoding of nextCP to the end of elements. elements.push(String.fromCodePoint(nextCP)); // f. Let nextIndex be nextIndex + 1. nextIndex++; } // 6. Return the String value whose elements are, in order, the elements in the List elements. If length // is 0, the empty string is returned. return new _index.StringValue(realm, elements.join("")); }); // ECMA262 21.1.2.4 if (!realm.isCompatibleWith(realm.MOBILE_JSC_VERSION)) func.defineNativeMethod("raw", 1, function (context, _ref3, argCount) { var _ref4 = _toArray(_ref3), template = _ref4[0], substitutions = _ref4.slice(1); // 1. Let substitutions be a List consisting of all of the arguments passed to this function, starting with the second argument. If fewer than two arguments were passed, the List is empty. substitutions = argCount < 2 ? [] : substitutions; // 2. Let numberOfSubstitutions be the number of elements in substitutions. var numberOfSubstitutions = substitutions.length; // 3. Let cooked be ? ToObject(template). var cooked = _singletons.To.ToObjectPartial(realm, template); // 4. Let raw be ? ToObject(? Get(cooked, "raw")). var raw = _singletons.To.ToObjectPartial(realm, (0, _index2.Get)(realm, cooked, "raw")); // 5. Let literalSegments be ? ToLength(? Get(raw, "length")). var literalSegments = _singletons.To.ToLength(realm, (0, _index2.Get)(realm, raw, "length")); // 6. If literalSegments ≤ 0, return the empty string. if (literalSegments <= 0) return realm.intrinsics.emptyString; // 7. Let stringElements be a new empty List. var stringElements = ""; // 8. Let nextIndex be 0. var nextIndex = 0; // 9. Repeat while (true) { // a. Let nextKey be ! ToString(nextIndex). var nextKey = _singletons.To.ToString(realm, new _index.NumberValue(realm, nextIndex)); // b. Let nextSeg be ? ToString(? Get(raw, nextKey)). var nextSeg = _singletons.To.ToStringPartial(realm, (0, _index2.Get)(realm, raw, nextKey)); // c. Append in order the code unit elements of nextSeg to the end of stringElements. stringElements = stringElements + nextSeg; // d. If nextIndex + 1 = literalSegments, then if (nextIndex + 1 === literalSegments) { // i. Return the String value whose code units are, in order, the elements in the List stringElements. If stringElements has no elements, the empty string is returned. return new _index.StringValue(realm, stringElements); } var next = void 0; // e. If nextIndex < numberOfSubstitutions, let next be substitutions[nextIndex]. if (nextIndex < numberOfSubstitutions) next = substitutions[nextIndex];else // f. Else, let next be the empty String. next = realm.intrinsics.emptyString; // g. Let nextSub be ? ToString(next). var nextSub = _singletons.To.ToStringPartial(realm, next); // h. Append in order the code unit elements of nextSub to the end of stringElements. stringElements = stringElements + nextSub; // i. Let nextIndex be nextIndex + 1. nextIndex = nextIndex + 1; } (0, _invariant2.default)(false); }); return func; }; var _index = require("../../values/index.js"); var _index2 = require("../../methods/index.js"); var _singletons = require("../../singletons.js"); var _invariant = require("../../invariant.js"); var _invariant2 = _interopRequireDefault(_invariant); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _toArray(arr) { return Array.isArray(arr) ? arr : Array.from(arr); } //# sourceMappingURL=String.js.map