es2015.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. 'use strict';
  2. var has = require('has');
  3. var toPrimitive = require('es-to-primitive/es6');
  4. var GetIntrinsic = require('./GetIntrinsic');
  5. var $TypeError = GetIntrinsic('%TypeError%');
  6. var $SyntaxError = GetIntrinsic('%SyntaxError%');
  7. var $Array = GetIntrinsic('%Array%');
  8. var $String = GetIntrinsic('%String%');
  9. var $Object = GetIntrinsic('%Object%');
  10. var $Number = GetIntrinsic('%Number%');
  11. var $Symbol = GetIntrinsic('%Symbol%', true);
  12. var $RegExp = GetIntrinsic('%RegExp%');
  13. var hasSymbols = !!$Symbol;
  14. var $isNaN = require('./helpers/isNaN');
  15. var $isFinite = require('./helpers/isFinite');
  16. var MAX_SAFE_INTEGER = $Number.MAX_SAFE_INTEGER || Math.pow(2, 53) - 1;
  17. var assign = require('./helpers/assign');
  18. var sign = require('./helpers/sign');
  19. var mod = require('./helpers/mod');
  20. var isPrimitive = require('./helpers/isPrimitive');
  21. var parseInteger = parseInt;
  22. var bind = require('function-bind');
  23. var arraySlice = bind.call(Function.call, $Array.prototype.slice);
  24. var strSlice = bind.call(Function.call, $String.prototype.slice);
  25. var isBinary = bind.call(Function.call, $RegExp.prototype.test, /^0b[01]+$/i);
  26. var isOctal = bind.call(Function.call, $RegExp.prototype.test, /^0o[0-7]+$/i);
  27. var regexExec = bind.call(Function.call, $RegExp.prototype.exec);
  28. var nonWS = ['\u0085', '\u200b', '\ufffe'].join('');
  29. var nonWSregex = new $RegExp('[' + nonWS + ']', 'g');
  30. var hasNonWS = bind.call(Function.call, $RegExp.prototype.test, nonWSregex);
  31. var invalidHexLiteral = /^[-+]0x[0-9a-f]+$/i;
  32. var isInvalidHexLiteral = bind.call(Function.call, $RegExp.prototype.test, invalidHexLiteral);
  33. var $charCodeAt = bind.call(Function.call, $String.prototype.charCodeAt);
  34. var toStr = bind.call(Function.call, Object.prototype.toString);
  35. var $floor = Math.floor;
  36. var $abs = Math.abs;
  37. var $ObjectCreate = Object.create;
  38. var $gOPD = $Object.getOwnPropertyDescriptor;
  39. var $isExtensible = $Object.isExtensible;
  40. // whitespace from: http://es5.github.io/#x15.5.4.20
  41. // implementation from https://github.com/es-shims/es5-shim/blob/v3.4.0/es5-shim.js#L1304-L1324
  42. var ws = [
  43. '\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003',
  44. '\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028',
  45. '\u2029\uFEFF'
  46. ].join('');
  47. var trimRegex = new RegExp('(^[' + ws + ']+)|([' + ws + ']+$)', 'g');
  48. var replace = bind.call(Function.call, $String.prototype.replace);
  49. var trim = function (value) {
  50. return replace(value, trimRegex, '');
  51. };
  52. var ES5 = require('./es5');
  53. var hasRegExpMatcher = require('is-regex');
  54. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-abstract-operations
  55. var ES6 = assign(assign({}, ES5), {
  56. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-call-f-v-args
  57. Call: function Call(F, V) {
  58. var args = arguments.length > 2 ? arguments[2] : [];
  59. if (!this.IsCallable(F)) {
  60. throw new $TypeError(F + ' is not a function');
  61. }
  62. return F.apply(V, args);
  63. },
  64. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-toprimitive
  65. ToPrimitive: toPrimitive,
  66. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-toboolean
  67. // ToBoolean: ES5.ToBoolean,
  68. // https://ecma-international.org/ecma-262/6.0/#sec-tonumber
  69. ToNumber: function ToNumber(argument) {
  70. var value = isPrimitive(argument) ? argument : toPrimitive(argument, $Number);
  71. if (typeof value === 'symbol') {
  72. throw new $TypeError('Cannot convert a Symbol value to a number');
  73. }
  74. if (typeof value === 'string') {
  75. if (isBinary(value)) {
  76. return this.ToNumber(parseInteger(strSlice(value, 2), 2));
  77. } else if (isOctal(value)) {
  78. return this.ToNumber(parseInteger(strSlice(value, 2), 8));
  79. } else if (hasNonWS(value) || isInvalidHexLiteral(value)) {
  80. return NaN;
  81. } else {
  82. var trimmed = trim(value);
  83. if (trimmed !== value) {
  84. return this.ToNumber(trimmed);
  85. }
  86. }
  87. }
  88. return $Number(value);
  89. },
  90. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-tointeger
  91. // ToInteger: ES5.ToNumber,
  92. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-toint32
  93. // ToInt32: ES5.ToInt32,
  94. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-touint32
  95. // ToUint32: ES5.ToUint32,
  96. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-toint16
  97. ToInt16: function ToInt16(argument) {
  98. var int16bit = this.ToUint16(argument);
  99. return int16bit >= 0x8000 ? int16bit - 0x10000 : int16bit;
  100. },
  101. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-touint16
  102. // ToUint16: ES5.ToUint16,
  103. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-toint8
  104. ToInt8: function ToInt8(argument) {
  105. var int8bit = this.ToUint8(argument);
  106. return int8bit >= 0x80 ? int8bit - 0x100 : int8bit;
  107. },
  108. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-touint8
  109. ToUint8: function ToUint8(argument) {
  110. var number = this.ToNumber(argument);
  111. if ($isNaN(number) || number === 0 || !$isFinite(number)) { return 0; }
  112. var posInt = sign(number) * $floor($abs(number));
  113. return mod(posInt, 0x100);
  114. },
  115. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-touint8clamp
  116. ToUint8Clamp: function ToUint8Clamp(argument) {
  117. var number = this.ToNumber(argument);
  118. if ($isNaN(number) || number <= 0) { return 0; }
  119. if (number >= 0xFF) { return 0xFF; }
  120. var f = $floor(argument);
  121. if (f + 0.5 < number) { return f + 1; }
  122. if (number < f + 0.5) { return f; }
  123. if (f % 2 !== 0) { return f + 1; }
  124. return f;
  125. },
  126. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-tostring
  127. ToString: function ToString(argument) {
  128. if (typeof argument === 'symbol') {
  129. throw new $TypeError('Cannot convert a Symbol value to a string');
  130. }
  131. return $String(argument);
  132. },
  133. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-toobject
  134. ToObject: function ToObject(value) {
  135. this.RequireObjectCoercible(value);
  136. return $Object(value);
  137. },
  138. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-topropertykey
  139. ToPropertyKey: function ToPropertyKey(argument) {
  140. var key = this.ToPrimitive(argument, $String);
  141. return typeof key === 'symbol' ? key : this.ToString(key);
  142. },
  143. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-tolength
  144. ToLength: function ToLength(argument) {
  145. var len = this.ToInteger(argument);
  146. if (len <= 0) { return 0; } // includes converting -0 to +0
  147. if (len > MAX_SAFE_INTEGER) { return MAX_SAFE_INTEGER; }
  148. return len;
  149. },
  150. // https://ecma-international.org/ecma-262/6.0/#sec-canonicalnumericindexstring
  151. CanonicalNumericIndexString: function CanonicalNumericIndexString(argument) {
  152. if (toStr(argument) !== '[object String]') {
  153. throw new $TypeError('must be a string');
  154. }
  155. if (argument === '-0') { return -0; }
  156. var n = this.ToNumber(argument);
  157. if (this.SameValue(this.ToString(n), argument)) { return n; }
  158. return void 0;
  159. },
  160. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-requireobjectcoercible
  161. RequireObjectCoercible: ES5.CheckObjectCoercible,
  162. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-isarray
  163. IsArray: $Array.isArray || function IsArray(argument) {
  164. return toStr(argument) === '[object Array]';
  165. },
  166. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-iscallable
  167. // IsCallable: ES5.IsCallable,
  168. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-isconstructor
  169. IsConstructor: function IsConstructor(argument) {
  170. return typeof argument === 'function' && !!argument.prototype; // unfortunately there's no way to truly check this without try/catch `new argument`
  171. },
  172. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-isextensible-o
  173. IsExtensible: Object.preventExtensions
  174. ? function IsExtensible(obj) {
  175. if (isPrimitive(obj)) {
  176. return false;
  177. }
  178. return $isExtensible(obj);
  179. }
  180. : function isExtensible(obj) { return true; }, // eslint-disable-line no-unused-vars
  181. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-isinteger
  182. IsInteger: function IsInteger(argument) {
  183. if (typeof argument !== 'number' || $isNaN(argument) || !$isFinite(argument)) {
  184. return false;
  185. }
  186. var abs = $abs(argument);
  187. return $floor(abs) === abs;
  188. },
  189. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-ispropertykey
  190. IsPropertyKey: function IsPropertyKey(argument) {
  191. return typeof argument === 'string' || typeof argument === 'symbol';
  192. },
  193. // https://ecma-international.org/ecma-262/6.0/#sec-isregexp
  194. IsRegExp: function IsRegExp(argument) {
  195. if (!argument || typeof argument !== 'object') {
  196. return false;
  197. }
  198. if (hasSymbols) {
  199. var isRegExp = argument[$Symbol.match];
  200. if (typeof isRegExp !== 'undefined') {
  201. return ES5.ToBoolean(isRegExp);
  202. }
  203. }
  204. return hasRegExpMatcher(argument);
  205. },
  206. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevalue
  207. // SameValue: ES5.SameValue,
  208. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero
  209. SameValueZero: function SameValueZero(x, y) {
  210. return (x === y) || ($isNaN(x) && $isNaN(y));
  211. },
  212. /**
  213. * 7.3.2 GetV (V, P)
  214. * 1. Assert: IsPropertyKey(P) is true.
  215. * 2. Let O be ToObject(V).
  216. * 3. ReturnIfAbrupt(O).
  217. * 4. Return O.[[Get]](P, V).
  218. */
  219. GetV: function GetV(V, P) {
  220. // 7.3.2.1
  221. if (!this.IsPropertyKey(P)) {
  222. throw new $TypeError('Assertion failed: IsPropertyKey(P) is not true');
  223. }
  224. // 7.3.2.2-3
  225. var O = this.ToObject(V);
  226. // 7.3.2.4
  227. return O[P];
  228. },
  229. /**
  230. * 7.3.9 - https://ecma-international.org/ecma-262/6.0/#sec-getmethod
  231. * 1. Assert: IsPropertyKey(P) is true.
  232. * 2. Let func be GetV(O, P).
  233. * 3. ReturnIfAbrupt(func).
  234. * 4. If func is either undefined or null, return undefined.
  235. * 5. If IsCallable(func) is false, throw a TypeError exception.
  236. * 6. Return func.
  237. */
  238. GetMethod: function GetMethod(O, P) {
  239. // 7.3.9.1
  240. if (!this.IsPropertyKey(P)) {
  241. throw new $TypeError('Assertion failed: IsPropertyKey(P) is not true');
  242. }
  243. // 7.3.9.2
  244. var func = this.GetV(O, P);
  245. // 7.3.9.4
  246. if (func == null) {
  247. return void 0;
  248. }
  249. // 7.3.9.5
  250. if (!this.IsCallable(func)) {
  251. throw new $TypeError(P + 'is not a function');
  252. }
  253. // 7.3.9.6
  254. return func;
  255. },
  256. /**
  257. * 7.3.1 Get (O, P) - https://ecma-international.org/ecma-262/6.0/#sec-get-o-p
  258. * 1. Assert: Type(O) is Object.
  259. * 2. Assert: IsPropertyKey(P) is true.
  260. * 3. Return O.[[Get]](P, O).
  261. */
  262. Get: function Get(O, P) {
  263. // 7.3.1.1
  264. if (this.Type(O) !== 'Object') {
  265. throw new $TypeError('Assertion failed: Type(O) is not Object');
  266. }
  267. // 7.3.1.2
  268. if (!this.IsPropertyKey(P)) {
  269. throw new $TypeError('Assertion failed: IsPropertyKey(P) is not true');
  270. }
  271. // 7.3.1.3
  272. return O[P];
  273. },
  274. Type: function Type(x) {
  275. if (typeof x === 'symbol') {
  276. return 'Symbol';
  277. }
  278. return ES5.Type(x);
  279. },
  280. // https://ecma-international.org/ecma-262/6.0/#sec-speciesconstructor
  281. SpeciesConstructor: function SpeciesConstructor(O, defaultConstructor) {
  282. if (this.Type(O) !== 'Object') {
  283. throw new $TypeError('Assertion failed: Type(O) is not Object');
  284. }
  285. var C = O.constructor;
  286. if (typeof C === 'undefined') {
  287. return defaultConstructor;
  288. }
  289. if (this.Type(C) !== 'Object') {
  290. throw new $TypeError('O.constructor is not an Object');
  291. }
  292. var S = hasSymbols && $Symbol.species ? C[$Symbol.species] : void 0;
  293. if (S == null) {
  294. return defaultConstructor;
  295. }
  296. if (this.IsConstructor(S)) {
  297. return S;
  298. }
  299. throw new $TypeError('no constructor found');
  300. },
  301. // https://ecma-international.org/ecma-262/6.0/#sec-completepropertydescriptor
  302. CompletePropertyDescriptor: function CompletePropertyDescriptor(Desc) {
  303. if (!this.IsPropertyDescriptor(Desc)) {
  304. throw new $TypeError('Desc must be a Property Descriptor');
  305. }
  306. if (this.IsGenericDescriptor(Desc) || this.IsDataDescriptor(Desc)) {
  307. if (!has(Desc, '[[Value]]')) {
  308. Desc['[[Value]]'] = void 0;
  309. }
  310. if (!has(Desc, '[[Writable]]')) {
  311. Desc['[[Writable]]'] = false;
  312. }
  313. } else {
  314. if (!has(Desc, '[[Get]]')) {
  315. Desc['[[Get]]'] = void 0;
  316. }
  317. if (!has(Desc, '[[Set]]')) {
  318. Desc['[[Set]]'] = void 0;
  319. }
  320. }
  321. if (!has(Desc, '[[Enumerable]]')) {
  322. Desc['[[Enumerable]]'] = false;
  323. }
  324. if (!has(Desc, '[[Configurable]]')) {
  325. Desc['[[Configurable]]'] = false;
  326. }
  327. return Desc;
  328. },
  329. // https://ecma-international.org/ecma-262/6.0/#sec-set-o-p-v-throw
  330. Set: function Set(O, P, V, Throw) {
  331. if (this.Type(O) !== 'Object') {
  332. throw new $TypeError('O must be an Object');
  333. }
  334. if (!this.IsPropertyKey(P)) {
  335. throw new $TypeError('P must be a Property Key');
  336. }
  337. if (this.Type(Throw) !== 'Boolean') {
  338. throw new $TypeError('Throw must be a Boolean');
  339. }
  340. if (Throw) {
  341. O[P] = V;
  342. return true;
  343. } else {
  344. try {
  345. O[P] = V;
  346. } catch (e) {
  347. return false;
  348. }
  349. }
  350. },
  351. // https://ecma-international.org/ecma-262/6.0/#sec-hasownproperty
  352. HasOwnProperty: function HasOwnProperty(O, P) {
  353. if (this.Type(O) !== 'Object') {
  354. throw new $TypeError('O must be an Object');
  355. }
  356. if (!this.IsPropertyKey(P)) {
  357. throw new $TypeError('P must be a Property Key');
  358. }
  359. return has(O, P);
  360. },
  361. // https://ecma-international.org/ecma-262/6.0/#sec-hasproperty
  362. HasProperty: function HasProperty(O, P) {
  363. if (this.Type(O) !== 'Object') {
  364. throw new $TypeError('O must be an Object');
  365. }
  366. if (!this.IsPropertyKey(P)) {
  367. throw new $TypeError('P must be a Property Key');
  368. }
  369. return P in O;
  370. },
  371. // https://ecma-international.org/ecma-262/6.0/#sec-isconcatspreadable
  372. IsConcatSpreadable: function IsConcatSpreadable(O) {
  373. if (this.Type(O) !== 'Object') {
  374. return false;
  375. }
  376. if (hasSymbols && typeof $Symbol.isConcatSpreadable === 'symbol') {
  377. var spreadable = this.Get(O, Symbol.isConcatSpreadable);
  378. if (typeof spreadable !== 'undefined') {
  379. return this.ToBoolean(spreadable);
  380. }
  381. }
  382. return this.IsArray(O);
  383. },
  384. // https://ecma-international.org/ecma-262/6.0/#sec-invoke
  385. Invoke: function Invoke(O, P) {
  386. if (!this.IsPropertyKey(P)) {
  387. throw new $TypeError('P must be a Property Key');
  388. }
  389. var argumentsList = arraySlice(arguments, 2);
  390. var func = this.GetV(O, P);
  391. return this.Call(func, O, argumentsList);
  392. },
  393. // https://ecma-international.org/ecma-262/6.0/#sec-getiterator
  394. GetIterator: function GetIterator(obj, method) {
  395. if (!hasSymbols) {
  396. throw new SyntaxError('ES.GetIterator depends on native iterator support.');
  397. }
  398. var actualMethod = method;
  399. if (arguments.length < 2) {
  400. actualMethod = this.GetMethod(obj, $Symbol.iterator);
  401. }
  402. var iterator = this.Call(actualMethod, obj);
  403. if (this.Type(iterator) !== 'Object') {
  404. throw new $TypeError('iterator must return an object');
  405. }
  406. return iterator;
  407. },
  408. // https://ecma-international.org/ecma-262/6.0/#sec-iteratornext
  409. IteratorNext: function IteratorNext(iterator, value) {
  410. var result = this.Invoke(iterator, 'next', arguments.length < 2 ? [] : [value]);
  411. if (this.Type(result) !== 'Object') {
  412. throw new $TypeError('iterator next must return an object');
  413. }
  414. return result;
  415. },
  416. // https://ecma-international.org/ecma-262/6.0/#sec-iteratorcomplete
  417. IteratorComplete: function IteratorComplete(iterResult) {
  418. if (this.Type(iterResult) !== 'Object') {
  419. throw new $TypeError('Assertion failed: Type(iterResult) is not Object');
  420. }
  421. return this.ToBoolean(this.Get(iterResult, 'done'));
  422. },
  423. // https://ecma-international.org/ecma-262/6.0/#sec-iteratorvalue
  424. IteratorValue: function IteratorValue(iterResult) {
  425. if (this.Type(iterResult) !== 'Object') {
  426. throw new $TypeError('Assertion failed: Type(iterResult) is not Object');
  427. }
  428. return this.Get(iterResult, 'value');
  429. },
  430. // https://ecma-international.org/ecma-262/6.0/#sec-iteratorstep
  431. IteratorStep: function IteratorStep(iterator) {
  432. var result = this.IteratorNext(iterator);
  433. var done = this.IteratorComplete(result);
  434. return done === true ? false : result;
  435. },
  436. // https://ecma-international.org/ecma-262/6.0/#sec-iteratorclose
  437. IteratorClose: function IteratorClose(iterator, completion) {
  438. if (this.Type(iterator) !== 'Object') {
  439. throw new $TypeError('Assertion failed: Type(iterator) is not Object');
  440. }
  441. if (!this.IsCallable(completion)) {
  442. throw new $TypeError('Assertion failed: completion is not a thunk for a Completion Record');
  443. }
  444. var completionThunk = completion;
  445. var iteratorReturn = this.GetMethod(iterator, 'return');
  446. if (typeof iteratorReturn === 'undefined') {
  447. return completionThunk();
  448. }
  449. var completionRecord;
  450. try {
  451. var innerResult = this.Call(iteratorReturn, iterator, []);
  452. } catch (e) {
  453. // if we hit here, then "e" is the innerResult completion that needs re-throwing
  454. // if the completion is of type "throw", this will throw.
  455. completionRecord = completionThunk();
  456. completionThunk = null; // ensure it's not called twice.
  457. // if not, then return the innerResult completion
  458. throw e;
  459. }
  460. completionRecord = completionThunk(); // if innerResult worked, then throw if the completion does
  461. completionThunk = null; // ensure it's not called twice.
  462. if (this.Type(innerResult) !== 'Object') {
  463. throw new $TypeError('iterator .return must return an object');
  464. }
  465. return completionRecord;
  466. },
  467. // https://ecma-international.org/ecma-262/6.0/#sec-createiterresultobject
  468. CreateIterResultObject: function CreateIterResultObject(value, done) {
  469. if (this.Type(done) !== 'Boolean') {
  470. throw new $TypeError('Assertion failed: Type(done) is not Boolean');
  471. }
  472. return {
  473. value: value,
  474. done: done
  475. };
  476. },
  477. // https://ecma-international.org/ecma-262/6.0/#sec-regexpexec
  478. RegExpExec: function RegExpExec(R, S) {
  479. if (this.Type(R) !== 'Object') {
  480. throw new $TypeError('R must be an Object');
  481. }
  482. if (this.Type(S) !== 'String') {
  483. throw new $TypeError('S must be a String');
  484. }
  485. var exec = this.Get(R, 'exec');
  486. if (this.IsCallable(exec)) {
  487. var result = this.Call(exec, R, [S]);
  488. if (result === null || this.Type(result) === 'Object') {
  489. return result;
  490. }
  491. throw new $TypeError('"exec" method must return `null` or an Object');
  492. }
  493. return regexExec(R, S);
  494. },
  495. // https://ecma-international.org/ecma-262/6.0/#sec-arrayspeciescreate
  496. ArraySpeciesCreate: function ArraySpeciesCreate(originalArray, length) {
  497. if (!this.IsInteger(length) || length < 0) {
  498. throw new $TypeError('Assertion failed: length must be an integer >= 0');
  499. }
  500. var len = length === 0 ? 0 : length;
  501. var C;
  502. var isArray = this.IsArray(originalArray);
  503. if (isArray) {
  504. C = this.Get(originalArray, 'constructor');
  505. // TODO: figure out how to make a cross-realm normal Array, a same-realm Array
  506. // if (this.IsConstructor(C)) {
  507. // if C is another realm's Array, C = undefined
  508. // Object.getPrototypeOf(Object.getPrototypeOf(Object.getPrototypeOf(Array))) === null ?
  509. // }
  510. if (this.Type(C) === 'Object' && hasSymbols && $Symbol.species) {
  511. C = this.Get(C, $Symbol.species);
  512. if (C === null) {
  513. C = void 0;
  514. }
  515. }
  516. }
  517. if (typeof C === 'undefined') {
  518. return $Array(len);
  519. }
  520. if (!this.IsConstructor(C)) {
  521. throw new $TypeError('C must be a constructor');
  522. }
  523. return new C(len); // this.Construct(C, len);
  524. },
  525. CreateDataProperty: function CreateDataProperty(O, P, V) {
  526. if (this.Type(O) !== 'Object') {
  527. throw new $TypeError('Assertion failed: Type(O) is not Object');
  528. }
  529. if (!this.IsPropertyKey(P)) {
  530. throw new $TypeError('Assertion failed: IsPropertyKey(P) is not true');
  531. }
  532. var oldDesc = $gOPD(O, P);
  533. var extensible = oldDesc || (typeof $isExtensible !== 'function' || $isExtensible(O));
  534. var immutable = oldDesc && (!oldDesc.writable || !oldDesc.configurable);
  535. if (immutable || !extensible) {
  536. return false;
  537. }
  538. var newDesc = {
  539. configurable: true,
  540. enumerable: true,
  541. value: V,
  542. writable: true
  543. };
  544. Object.defineProperty(O, P, newDesc);
  545. return true;
  546. },
  547. // https://ecma-international.org/ecma-262/6.0/#sec-createdatapropertyorthrow
  548. CreateDataPropertyOrThrow: function CreateDataPropertyOrThrow(O, P, V) {
  549. if (this.Type(O) !== 'Object') {
  550. throw new $TypeError('Assertion failed: Type(O) is not Object');
  551. }
  552. if (!this.IsPropertyKey(P)) {
  553. throw new $TypeError('Assertion failed: IsPropertyKey(P) is not true');
  554. }
  555. var success = this.CreateDataProperty(O, P, V);
  556. if (!success) {
  557. throw new $TypeError('unable to create data property');
  558. }
  559. return success;
  560. },
  561. // https://www.ecma-international.org/ecma-262/6.0/#sec-objectcreate
  562. ObjectCreate: function ObjectCreate(proto, internalSlotsList) {
  563. if (proto !== null && this.Type(proto) !== 'Object') {
  564. throw new $TypeError('Assertion failed: proto must be null or an object');
  565. }
  566. var slots = arguments.length < 2 ? [] : internalSlotsList;
  567. if (slots.length > 0) {
  568. throw new $SyntaxError('es-abstract does not yet support internal slots');
  569. }
  570. if (proto === null && !$ObjectCreate) {
  571. throw new $SyntaxError('native Object.create support is required to create null objects');
  572. }
  573. return $ObjectCreate(proto);
  574. },
  575. // https://ecma-international.org/ecma-262/6.0/#sec-advancestringindex
  576. AdvanceStringIndex: function AdvanceStringIndex(S, index, unicode) {
  577. if (this.Type(S) !== 'String') {
  578. throw new $TypeError('S must be a String');
  579. }
  580. if (!this.IsInteger(index) || index < 0 || index > MAX_SAFE_INTEGER) {
  581. throw new $TypeError('Assertion failed: length must be an integer >= 0 and <= 2**53');
  582. }
  583. if (this.Type(unicode) !== 'Boolean') {
  584. throw new $TypeError('Assertion failed: unicode must be a Boolean');
  585. }
  586. if (!unicode) {
  587. return index + 1;
  588. }
  589. var length = S.length;
  590. if ((index + 1) >= length) {
  591. return index + 1;
  592. }
  593. var first = $charCodeAt(S, index);
  594. if (first < 0xD800 || first > 0xDBFF) {
  595. return index + 1;
  596. }
  597. var second = $charCodeAt(S, index + 1);
  598. if (second < 0xDC00 || second > 0xDFFF) {
  599. return index + 1;
  600. }
  601. return index + 2;
  602. }
  603. });
  604. delete ES6.CheckObjectCoercible; // renamed in ES6 to RequireObjectCoercible
  605. module.exports = ES6;