es2018.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. 'use strict';
  2. var GetIntrinsic = require('./GetIntrinsic');
  3. var keys = require('object-keys');
  4. var inspect = require('object-inspect');
  5. var ES2017 = require('./es2017');
  6. var assign = require('./helpers/assign');
  7. var forEach = require('./helpers/forEach');
  8. var callBind = require('./helpers/callBind');
  9. var every = require('./helpers/every');
  10. var $String = GetIntrinsic('%String%');
  11. var $Object = GetIntrinsic('%Object%');
  12. var $TypeError = GetIntrinsic('%TypeError%');
  13. var $RegExp = GetIntrinsic('%RegExp%');
  14. var $SymbolProto = GetIntrinsic('%SymbolPrototype%', true);
  15. var $SymbolValueOf = $SymbolProto ? callBind($SymbolProto.valueOf) : null;
  16. var $StringProto = GetIntrinsic('%StringPrototype%');
  17. var $charAt = callBind($StringProto.charAt);
  18. var strSlice = callBind($StringProto.slice);
  19. var $indexOf = callBind($StringProto.indexOf);
  20. var $parseInt = parseInt;
  21. var isDigit = callBind($RegExp.prototype.test, /^[0-9]$/);
  22. var $PromiseResolveOrig = GetIntrinsic('%Promise_resolve%', true);
  23. var $PromiseResolve = $PromiseResolveOrig ? callBind($PromiseResolveOrig) : null;
  24. var $isEnumerable = callBind(GetIntrinsic('%ObjectPrototype%').propertyIsEnumerable);
  25. var $pushApply = callBind.apply(GetIntrinsic('%ArrayPrototype%').push);
  26. var $gOPS = $SymbolValueOf ? $Object.getOwnPropertySymbols : null;
  27. var OwnPropertyKeys = function OwnPropertyKeys(ES, source) {
  28. var ownKeys = keys(source);
  29. if ($gOPS) {
  30. $pushApply(ownKeys, $gOPS(source));
  31. }
  32. return ownKeys;
  33. };
  34. var ES2018 = assign(assign({}, ES2017), {
  35. EnumerableOwnPropertyNames: ES2017.EnumerableOwnProperties,
  36. // https://ecma-international.org/ecma-262/9.0/#sec-thissymbolvalue
  37. thisSymbolValue: function thisSymbolValue(value) {
  38. if (!$SymbolValueOf) {
  39. throw new SyntaxError('Symbols are not supported; thisSymbolValue requires that `value` be a Symbol or a Symbol object');
  40. }
  41. if (this.Type(value) === 'Symbol') {
  42. return value;
  43. }
  44. return $SymbolValueOf(value);
  45. },
  46. // https://www.ecma-international.org/ecma-262/9.0/#sec-isstringprefix
  47. IsStringPrefix: function IsStringPrefix(p, q) {
  48. if (this.Type(p) !== 'String') {
  49. throw new TypeError('Assertion failed: "p" must be a String');
  50. }
  51. if (this.Type(q) !== 'String') {
  52. throw new TypeError('Assertion failed: "q" must be a String');
  53. }
  54. if (p === q || p === '') {
  55. return true;
  56. }
  57. var pLength = p.length;
  58. var qLength = q.length;
  59. if (pLength >= qLength) {
  60. return false;
  61. }
  62. // assert: pLength < qLength
  63. for (var i = 0; i < pLength; i += 1) {
  64. if ($charAt(p, i) !== $charAt(q, i)) {
  65. return false;
  66. }
  67. }
  68. return true;
  69. },
  70. // https://www.ecma-international.org/ecma-262/9.0/#sec-tostring-applied-to-the-number-type
  71. NumberToString: function NumberToString(m) {
  72. if (this.Type(m) !== 'Number') {
  73. throw new TypeError('Assertion failed: "m" must be a String');
  74. }
  75. return $String(m);
  76. },
  77. // https://www.ecma-international.org/ecma-262/9.0/#sec-copydataproperties
  78. CopyDataProperties: function CopyDataProperties(target, source, excludedItems) {
  79. if (this.Type(target) !== 'Object') {
  80. throw new TypeError('Assertion failed: "target" must be an Object');
  81. }
  82. if (!this.IsArray(excludedItems)) {
  83. throw new TypeError('Assertion failed: "excludedItems" must be a List of Property Keys');
  84. }
  85. for (var i = 0; i < excludedItems.length; i += 1) {
  86. if (!this.IsPropertyKey(excludedItems[i])) {
  87. throw new TypeError('Assertion failed: "excludedItems" must be a List of Property Keys');
  88. }
  89. }
  90. if (typeof source === 'undefined' || source === null) {
  91. return target;
  92. }
  93. var ES = this;
  94. var fromObj = ES.ToObject(source);
  95. var sourceKeys = OwnPropertyKeys(ES, fromObj);
  96. forEach(sourceKeys, function (nextKey) {
  97. var excluded = false;
  98. forEach(excludedItems, function (e) {
  99. if (ES.SameValue(e, nextKey) === true) {
  100. excluded = true;
  101. }
  102. });
  103. var enumerable = $isEnumerable(fromObj, nextKey) || (
  104. // this is to handle string keys being non-enumerable in older engines
  105. typeof source === 'string'
  106. && nextKey >= 0
  107. && ES.IsInteger(ES.ToNumber(nextKey))
  108. );
  109. if (excluded === false && enumerable) {
  110. var propValue = ES.Get(fromObj, nextKey);
  111. ES.CreateDataProperty(target, nextKey, propValue);
  112. }
  113. });
  114. return target;
  115. },
  116. // https://ecma-international.org/ecma-262/9.0/#sec-promise-resolve
  117. PromiseResolve: function PromiseResolve(C, x) {
  118. if (!$PromiseResolve) {
  119. throw new SyntaxError('This environment does not support Promises.');
  120. }
  121. return $PromiseResolve(C, x);
  122. },
  123. // http://www.ecma-international.org/ecma-262/9.0/#sec-getsubstitution
  124. // eslint-disable-next-line max-statements, max-params, max-lines-per-function
  125. GetSubstitution: function GetSubstitution(matched, str, position, captures, namedCaptures, replacement) {
  126. if (this.Type(matched) !== 'String') {
  127. throw new $TypeError('Assertion failed: `matched` must be a String');
  128. }
  129. var matchLength = matched.length;
  130. if (this.Type(str) !== 'String') {
  131. throw new $TypeError('Assertion failed: `str` must be a String');
  132. }
  133. var stringLength = str.length;
  134. if (!this.IsInteger(position) || position < 0 || position > stringLength) {
  135. throw new $TypeError('Assertion failed: `position` must be a nonnegative integer, and less than or equal to the length of `string`, got ' + inspect(position));
  136. }
  137. var ES = this;
  138. var isStringOrHole = function (capture, index, arr) { return ES.Type(capture) === 'String' || !(index in arr); };
  139. if (!this.IsArray(captures) || !every(captures, isStringOrHole)) {
  140. throw new $TypeError('Assertion failed: `captures` must be a List of Strings, got ' + inspect(captures));
  141. }
  142. if (this.Type(replacement) !== 'String') {
  143. throw new $TypeError('Assertion failed: `replacement` must be a String');
  144. }
  145. var tailPos = position + matchLength;
  146. var m = captures.length;
  147. if (this.Type(namedCaptures) !== 'Undefined') {
  148. namedCaptures = this.ToObject(namedCaptures); // eslint-disable-line no-param-reassign
  149. }
  150. var result = '';
  151. for (var i = 0; i < replacement.length; i += 1) {
  152. // if this is a $, and it's not the end of the replacement
  153. var current = replacement[i];
  154. var isLast = (i + 1) >= replacement.length;
  155. var nextIsLast = (i + 2) >= replacement.length;
  156. if (current === '$' && !isLast) {
  157. var next = replacement[i + 1];
  158. if (next === '$') {
  159. result += '$';
  160. i += 1;
  161. } else if (next === '&') {
  162. result += matched;
  163. i += 1;
  164. } else if (next === '`') {
  165. result += position === 0 ? '' : strSlice(str, 0, position - 1);
  166. i += 1;
  167. } else if (next === "'") {
  168. result += tailPos >= stringLength ? '' : strSlice(str, tailPos);
  169. i += 1;
  170. } else {
  171. var nextNext = nextIsLast ? null : replacement[i + 2];
  172. if (isDigit(next) && next !== '0' && (nextIsLast || !isDigit(nextNext))) {
  173. // $1 through $9, and not followed by a digit
  174. var n = $parseInt(next, 10);
  175. // if (n > m, impl-defined)
  176. result += (n <= m && this.Type(captures[n - 1]) === 'Undefined') ? '' : captures[n - 1];
  177. i += 1;
  178. } else if (isDigit(next) && (nextIsLast || isDigit(nextNext))) {
  179. // $00 through $99
  180. var nn = next + nextNext;
  181. var nnI = $parseInt(nn, 10) - 1;
  182. // if nn === '00' or nn > m, impl-defined
  183. result += (nn <= m && this.Type(captures[nnI]) === 'Undefined') ? '' : captures[nnI];
  184. i += 2;
  185. } else if (next === '<') {
  186. // eslint-disable-next-line max-depth
  187. if (this.Type(namedCaptures) === 'Undefined') {
  188. result += '$<';
  189. i += 2;
  190. } else {
  191. var endIndex = $indexOf(replacement, '>', i);
  192. // eslint-disable-next-line max-depth
  193. if (endIndex > -1) {
  194. var groupName = strSlice(replacement, i, endIndex);
  195. var capture = this.Get(namedCaptures, groupName);
  196. // eslint-disable-next-line max-depth
  197. if (this.Type(capture) !== 'Undefined') {
  198. result += this.ToString(capture);
  199. }
  200. i += '$<' + groupName + '>'.length;
  201. }
  202. }
  203. } else {
  204. result += '$';
  205. }
  206. }
  207. } else {
  208. // the final $, or else not a $
  209. result += replacement[i];
  210. }
  211. }
  212. return result;
  213. }
  214. });
  215. delete ES2018.EnumerableOwnProperties; // replaced with EnumerableOwnPropertyNames
  216. delete ES2018.IsPropertyDescriptor; // not an actual abstract operation
  217. module.exports = ES2018;