UNPKG

4.65 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _index = require("./index.js");
9
10var _is = require("../methods/is.js");
11
12var _singletons = require("../singletons.js");
13
14var _invariant = _interopRequireDefault(require("../invariant.js"));
15
16function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
18/**
19 * Copyright (c) 2017-present, Facebook, Inc.
20 * All rights reserved.
21 *
22 * This source code is licensed under the BSD-style license found in the
23 * LICENSE file in the root directory of this source tree. An additional grant
24 * of patent rights can be found in the PATENTS file in the same directory.
25 */
26
27/* strict-local */
28class StringExotic extends _index.ObjectValue {
29 constructor(realm, intrinsicName) {
30 super(realm, realm.intrinsics.StringPrototype, intrinsicName);
31 } // ECMA262 9.4.3.1
32
33
34 $GetOwnProperty(P) {
35 // 1. Assert: IsPropertyKey(P) is true.
36 // 2. Let desc be OrdinaryGetOwnProperty(S, P).
37 let desc = _singletons.Properties.OrdinaryGetOwnProperty(this.$Realm, this, P); // 3. If desc is not undefined, return desc.
38
39
40 if (desc !== undefined) {
41 _singletons.Properties.ThrowIfMightHaveBeenDeleted(desc.value);
42
43 return desc;
44 } // 4. If Type(P) is not String, return undefined.
45
46
47 if (typeof P !== "string" && !(P instanceof _index.StringValue)) return undefined; // 5. Let index be ! CanonicalNumericIndexString(P).
48
49 let index = _singletons.To.CanonicalNumericIndexString(this.$Realm, typeof P === "string" ? new _index.StringValue(this.$Realm, P) : P); // 6. If index is undefined, return undefined.
50
51
52 if (index === undefined || index === null) return undefined; // 7. If IsInteger(index) is false, return undefined.
53
54 if ((0, _is.IsInteger)(this.$Realm, index) === false) return undefined; // 8. If index = -0, return undefined.
55
56 if (1.0 / index === -Infinity) return undefined; // 9. Let str be the String value of S.[[StringData]].
57
58 let str = this.$StringData;
59 (0, _invariant.default)(str);
60 str = str.throwIfNotConcreteString(); // 10. Let len be the number of elements in str.
61
62 let len = str.value.length; // 11. If index < 0 or len ≤ index, return undefined.
63
64 if (index < 0 || len <= index) return undefined; // 12. Let resultStr be a String value of length 1, containing one code unit from str, specifically the code unit at index index.
65
66 let resultStr = new _index.StringValue(this.$Realm, str.value.charAt(index)); // 13. Return a PropertyDescriptor{[[Value]]: resultStr, [[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: false}.
67
68 return {
69 value: resultStr,
70 writable: false,
71 enumerable: true,
72 configurable: false
73 };
74 } // ECMA262 9.4.3.2
75
76
77 $OwnPropertyKeys() {
78 // 1. Let keys be a new empty List.
79 let keys = []; // 2. Let str be the String value of O.[[StringData]].
80
81 let str = this.$StringData;
82 (0, _invariant.default)(str);
83 str = str.throwIfNotConcreteString(); // 3. Let len be the number of elements in str.
84
85 let len = str.value.length; // 4. For each integer i starting with 0 such that i < len, in ascending order,
86
87 for (let i = 0; i < len; ++i) {
88 // a. Add ! ToString(i) as the last element of keys.
89 keys.push(new _index.StringValue(this.$Realm, _singletons.To.ToString(this.$Realm, new _index.NumberValue(this.$Realm, i))));
90 } // 5. For each own property key P of O such that P is an integer index and ToInteger(P) ≥ len, in ascending numeric index order,
91
92
93 let properties = this.getOwnPropertyKeysArray();
94
95 for (let key of properties.filter(x => (0, _is.IsArrayIndex)(this.$Realm, x)).map(x => parseInt(x, 10)).filter(x => _singletons.To.ToInteger(this.$Realm, x) >= len).sort((x, y) => x - y)) {
96 // i. Add P as the last element of keys.
97 keys.push(new _index.StringValue(this.$Realm, key + ""));
98 } // 6. For each own property key P of O such that Type(P) is String and P is not an integer index, in ascending chronological order of property creation,
99
100
101 for (let key of properties.filter(x => !(0, _is.IsArrayIndex)(this.$Realm, x))) {
102 // i. Add P as the last element of keys.
103 keys.push(new _index.StringValue(this.$Realm, key));
104 } // 7. For each own property key P of O such that Type(P) is Symbol, in ascending chronological order of property creation,
105
106
107 for (let key of this.symbols.keys()) {
108 // i. Add P as the last element of keys.
109 keys.push(key);
110 } // 12. Return keys.
111
112
113 return keys;
114 }
115
116}
117
118exports.default = StringExotic;
119//# sourceMappingURL=StringExotic.js.map
\No newline at end of file