es2015.js 24 KB

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