UNPKG

prepack

Version:

Execute a JS bundle, serialize global state and side effects to a snapshot that can be quickly restored.

173 lines (108 loc) 7.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = _default; var _index = require("../../values/index.js"); var _index2 = require("../../methods/index.js"); var _singletons = require("../../singletons.js"); var _invariant = _interopRequireDefault(require("../../invariant.js")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * 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. */ /* strict-local */ function _default(realm) { // ECMA262 21.1.1 let func = new _index.NativeFunctionValue(realm, "String", "String", 1, (context, [value], argCount, NewTarget) => { let s; // 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 = _singletons.To.ToStringValue(realm, value); } // 3. If NewTarget is undefined, return s. if (!NewTarget) return s; // 4. Return ? StringCreate(s, ? GetPrototypeFromConstructor(NewTarget, "%StringPrototype%")). s = s.throwIfNotConcreteString(); return _singletons.Create.StringCreate(realm, s, (0, _index2.GetPrototypeFromConstructor)(realm, NewTarget, "StringPrototype")); }); // ECMA262 21.1.2.1 ( ..._codeUnits_ ) func.defineNativeMethod("fromCharCode", 1, (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. let length = argCount; // 3. Let elements be a new empty List. let elements = []; // 4. Let nextIndex be 0. let nextIndex = 0; // 5. Repeat while nextIndex < length while (nextIndex < length) { // a. Let next be codeUnits[nextIndex]. let next = codeUnits[nextIndex]; // b. Let nextCU be ? ToUint16(next). let 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) && !realm.isCompatibleWith("mobile")) func.defineNativeMethod("fromCodePoint", 1, (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. let length = argCount; // 3. Let elements be a new empty List. let elements = []; // 4. Let nextIndex be 0. let nextIndex = 0; // 5. Repeat while nextIndex < length while (nextIndex < length) { // a. Let next be codePoints[nextIndex]. let next = codePoints[nextIndex]; // b. Let nextCP be ? ToNumber(next). let 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) && !realm.isCompatibleWith("mobile")) func.defineNativeMethod("raw", 1, (context, [template, ..._substitutions], argCount) => { let substitutions = _substitutions; // 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. let numberOfSubstitutions = substitutions.length; // 3. Let cooked be ? ToObject(template). let cooked = _singletons.To.ToObject(realm, template); // 4. Let raw be ? ToObject(? Get(cooked, "raw")). let raw = _singletons.To.ToObject(realm, (0, _index2.Get)(realm, cooked, "raw")); // 5. Let literalSegments be ? ToLength(? Get(raw, "length")). let 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. let stringElements = ""; // 8. Let nextIndex be 0. let nextIndex = 0; // 9. Repeat while (true) { // a. Let nextKey be ! ToString(nextIndex). let nextKey = _singletons.To.ToString(realm, new _index.NumberValue(realm, nextIndex)); // b. Let nextSeg be ? ToString(? Get(raw, nextKey)). let 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); } let next; // 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). let 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, _invariant.default)(false); }); return func; } //# sourceMappingURL=String.js.map