2 // https://raw.githubusercontent.com/inexorabletash/polyfill/a6bc6ced78160c994f76a909b6ff6bbbab3d43de/es6.js
3 // Required for ethereumjs-utils.js when run in phantomjs-2.1.1
4 // but is not required in any modern browsers.
5 // For more information, see
6 // https://www.bountysource.com/issues/38485709-error-rendering-plot-with-phantomjs
8 //----------------------------------------------------------------------
10 // ECMAScript 2015 Polyfills
12 //----------------------------------------------------------------------
17 // Set this to always override native implementations, for testing
18 // the polyfill in browsers with partial/full ES2015 support.
19 var OVERRIDE_NATIVE_FOR_TESTING
= false;
21 var undefined = (void 0); // Paranoia
26 return o
=== global
? undefined : o
;
29 function hook(o
, p
, f
) {
31 console
.assert(typeof op
=== 'function', 'Hooking a non-function');
34 var r
= f
.apply(o
, arguments
);
35 return r
!== undefined ? r : op
.apply(o
, arguments
);
39 function isSymbol(s
) {
40 return (typeof s
=== 'symbol') || ('Symbol' in global
&& s
instanceof global
.Symbol
);
43 function getPropertyDescriptor(target
, name
) {
44 var desc
= Object
.getOwnPropertyDescriptor(target
, name
);
45 var proto
= Object
.getPrototypeOf(target
);
46 while (!desc
&& proto
) {
47 desc
= Object
.getOwnPropertyDescriptor(proto
, name
);
48 proto
= Object
.getPrototypeOf(proto
);
53 var enqueue
= (function(nativePromise
, nativeSetImmediate
) {
55 return function(job
) { nativePromise
.resolve().then(function() { job(); }); };
56 if (nativeSetImmediate
)
57 return function(job
) { nativeSetImmediate(job
); };
58 return function(job
) { setTimeout(job
, 0); };
59 }(global
['Promise'], global
['setImmediate']));
61 function define(o
, p
, v
, override
) {
62 if (p
in o
&& !override
&& !OVERRIDE_NATIVE_FOR_TESTING
)
65 if (typeof v
=== 'function') {
66 // Sanity check that functions are appropriately named (where possible)
67 console
.assert(isSymbol(p
) || !('name' in v
) || v
.name
=== p
|| v
.name
=== p
+ '_', 'Expected function name "' + p
.toString() + '", was "' + v
.name
+ '"');
68 Object
.defineProperty(o
, p
, {
75 Object
.defineProperty(o
, p
, {
84 function set_internal(o
, p
, v
) {
85 Object
.defineProperty(o
, p
, {
93 // Snapshot intrinsic functions
94 var $isNaN
= global
.isNaN
,
95 $parseInt
= global
.parseInt
,
96 $parseFloat
= global
.parseFloat
;
109 random
= Math
.random
,
112 var orig_match
= String
.prototype.match
,
113 orig_replace
= String
.prototype.replace
,
114 orig_search
= String
.prototype.search
,
115 orig_split
= String
.prototype.split
;
117 // These are used for implementing the polyfills, but not exported.
119 // Inspired by https://gist.github.com/1638059
121 function EphemeronTable() {
122 var secretKey
= ObjectCreate(null);
124 function conceal(o
) {
125 var oValueOf
= o
.valueOf
, secrets
= ObjectCreate(null);
126 Object
.defineProperty(o
, 'valueOf', {
127 value: (function(secretKey
) {
128 return function (k
) {
129 return (k
=== secretKey
) ? secrets : oValueOf
.apply(o
, arguments
);
140 var v
= typeof o
.valueOf
=== 'function' && o
.valueOf(secretKey
);
141 return v
=== o
? null : v
;
146 secretKey
= ObjectCreate(null);
148 remove: function(key
) {
149 var secrets
= reveal(key
);
150 if (secrets
&& HasOwnProperty(secrets
, 'value')) {
151 delete secrets
.value
;
156 get: function(key
, defaultValue
) {
157 var secrets
= reveal(key
);
158 return (secrets
&& HasOwnProperty(secrets
, 'value')) ? secrets
.value : defaultValue
;
161 var secrets
= reveal(key
);
162 return Boolean(secrets
&& HasOwnProperty(secrets
, 'value'));
164 set: function(key
, value
) {
165 var secrets
= reveal(key
) || conceal(key
);
166 secrets
.value
= value
;
171 var empty
= Object
.create(null);
173 //----------------------------------------------------------------------
176 // http://www.ecma-international.org/ecma-262/6.0/
178 //----------------------------------------------------------------------
180 // ---------------------------------------
181 // 19.4 Symbol Objects
182 // ---------------------------------------
184 // NOTE: Symbols are defined here - out of spec order - since we need the
185 // properties and prototype to be populated for other polyfills.
187 // NOTE: Not secure, nor is obj[$$symbol] hidden from Object.keys()
191 var secret
= Object
.create(null);
193 symbolForKey = function(k
) {
197 var GlobalSymbolRegistry
= [];
199 function unique(bits
) {
200 return Array(bits
+ 1).join('x').replace(/x
/g
, function() {
201 return random() < 0.5 ? '\u200C' : '\u200D'; // JWNJ / ZWJ
205 // 19.4.1 The Symbol Constructor
206 // 19.4.1.1 Symbol ( description=undefined )
207 function Symbol(description
) {
208 if (!(this instanceof Symbol
)) return new Symbol(description
, secret
);
209 if (this instanceof Symbol
&& arguments
[1] !== secret
) throw TypeError();
211 var descString
= description
=== undefined ? undefined : String(description
);
213 set_internal(this, '[[SymbolData]]', unique(128));
214 set_internal(this, '[[Description]]', descString
);
216 symbolMap
[this] = this;
220 if (!('Symbol' in global
) || OVERRIDE_NATIVE_FOR_TESTING
)
221 global
.Symbol
= Symbol
;
223 // 19.4.2 Properties of the Symbol Constructor
225 // 19.4.2.1 Symbol.for (key)
226 define(Symbol
, 'for', function for_(key
) {
227 var stringKey
= String(key
);
228 for (var i
= 0; i
< GlobalSymbolRegistry
.length
; ++i
) {
229 var e
= GlobalSymbolRegistry
[i
];
230 if (SameValue(e
['[[key]]'], stringKey
)) return e
['[[symbol]]'];
232 var newSymbol
= Symbol(key
);
233 GlobalSymbolRegistry
.push({'[[key]]': stringKey
, '[[symbol]]': newSymbol
});
237 // 19.4.2.2 Symbol.hasInstance
238 // 19.4.2.3 Symbol.isConcatSpreadable
240 // 19.4.2.4 Symbol.iterator
241 define(global
.Symbol
, 'iterator', global
.Symbol('Symbol.iterator'));
243 // 19.4.2.5 Symbol.keyFor (sym)
244 define(Symbol
, 'keyFor', function keyFor(sym
) {
245 if (!(sym
instanceof Symbol
)) throw TypeError();
246 for (var i
= 0; i
< GlobalSymbolRegistry
.length
; ++i
) {
247 var e
= GlobalSymbolRegistry
[i
];
248 if (SameValue(e
['[[symbol]]'], sym
)) return e
['[[key]]'];
253 // 19.4.2.6 Symbol.match
254 define(global
.Symbol
, 'match', global
.Symbol('Symbol.match'));
256 // 19.4.2.7 Symbol.prototype
258 // 19.4.2.8 Symbol.replace
259 define(global
.Symbol
, 'replace', global
.Symbol('Symbol.replace'));
261 // 19.4.2.9 Symbol.search
262 define(global
.Symbol
, 'search', global
.Symbol('Symbol.search'));
264 // 19.4.2.10 Symbol.species
266 // 19.4.2.11 Symbol.search
267 define(global
.Symbol
, 'split', global
.Symbol('Symbol.split'));
269 // 19.4.2.12 Symbol.toPrimitive
271 // 19.4.2.13 Symbol.toStringTag
272 define(global
.Symbol
, 'toStringTag', global
.Symbol('Symbol.toStringTag'));
274 // 19.4.2.14 Symbol.unscopables
276 // 19.4.3 Properties of the Symbol Prototype Object
277 // 19.4.3.1 Symbol.prototype.constructor
279 // 19.4.3.2 Symbol.prototype.toString ( )
280 Object
.defineProperty(Symbol
.prototype, 'toString', {
281 value: function toString() {
282 var s
= strict(this);
283 var desc
= s
['[[Description]]'];
284 return 'Symbol(' + (desc
=== undefined ? '' : desc
) + s
['[[SymbolData]]'] + ')';
286 configurable: true, writeable: true, enumerable: false });
288 // 19.4.3.3 Symbol.prototype.valueOf ( )
289 Object
.defineProperty(Symbol
.prototype, 'valueOf', {
290 value: function valueOf() {
291 // To prevent automatic string conversion:
294 // Spec has approximately the following:
295 //var s = strict(this);
296 //if (Type(s) === 'symbol') return s;
297 //if (Type(s) !== 'object') throw TypeError();
298 //if (!('[[SymbolData]]' in s)) throw TypeError();
299 //return s['[[SymbolData]]'];
301 configurable: true, writeable: true, enumerable: false });
303 // 19.4.3.4 Symbol.prototype [ @@toStringTag ]
304 // (Done later to polyfill partial implementations)
306 // 19.4.4 Properties of Symbol Instances
309 console
.assert(typeof global
.Symbol() === 'symbol' || symbolForKey(String(global
.Symbol('x'))));
311 // Defined here so that other prototypes can reference it
312 // 25.1.2 The %IteratorPrototype% Object
313 var $IteratorPrototype
$ = {};
315 //----------------------------------------
316 // 6 ECMAScript Data Types and Values
317 //----------------------------------------
319 // 6.1 ECMAScript Language Types
321 // "Type(x)" is used as shorthand for "the type of x"...
324 case 'undefined': return 'undefined';
325 case 'boolean': return 'boolean';
326 case 'number': return 'number';
327 case 'string': return 'string';
328 case 'symbol': return 'symbol';
330 if (v
=== null) return 'null';
331 if (v
instanceof global
.Symbol
) return 'symbol';
336 // 6.1.5.1 Well-Known Symbols
337 var $$iterator
= global
.Symbol
.iterator
,
338 $$match
= global
.Symbol
.match
,
339 $$replace
= global
.Symbol
.replace
,
340 $$search
= global
.Symbol
.search
,
341 $$split
= global
.Symbol
.split
,
342 $$toStringTag
= global
.Symbol
.toStringTag
;
344 //----------------------------------------
345 // 7 Abstract Operations
346 //----------------------------------------
348 //----------------------------------------
349 // 7.1 Type Conversion
350 //----------------------------------------
352 // 7.1.1 ToPrimitive ( input [, PreferredType] )
353 // just use valueOf()
355 // 7.1.2 ToBoolean ( argument )
356 // just use Boolean()
358 // 7.1.3 ToNumber ( argument )
361 // 7.1.4 ToInteger ( argument )
362 function ToInteger(n
) {
364 if ($isNaN(n
)) return 0;
365 if (n
=== 0 || n
=== Infinity
|| n
=== -Infinity
) return n
;
366 return ((n
< 0) ? -1 : 1) * floor(abs(n
));
369 // 7.1.5 ToInt32 ( argument )
370 function ToInt32(v
) { return v
>> 0; }
372 // 7.1.6 ToUint32 ( argument )
373 function ToUint32(v
) { return v
>>> 0; }
375 // 7.1.7 ToInt16 ( argument )
376 function ToInt16(v
) { return (v
<< 16) >> 16; }
378 // 7.1.8 ToUint16 ( argument )
379 function ToUint16(v
) { return v
& 0xFFFF; }
381 // 7.1.9 ToInt8 ( argument )
382 function ToInt8(v
) { return (v
<< 24) >> 24; }
384 // 7.1.10 ToUint8 ( argument )
385 function ToUint8(v
) { return v
& 0xFF; }
387 // 7.1.11 ToUint8Clamp ( argument )
388 function ToUint8Clamp(argument
) {
389 var number
= Number(argument
);
390 if ($isNaN(number
)) return 0;
391 if (number
<= 0) return 0;
392 if (number
>= 255) return 255;
393 var f
= floor(number
);
394 if ((f
+ 0.5) < number
) return f
+ 1;
395 if (number
< (f
+ 0.5)) return f
;
396 if (f
% 2) return f
+ 1;
400 // 7.1.12 ToString ( argument )
403 // 7.1.13 ToObject ( argument )
404 function ToObject(v
) {
405 if (v
=== null || v
=== undefined) throw TypeError();
409 // 7.1.14 ToPropertyKey ( argument )
410 function ToPropertyKey(v
) {
414 // 7.1.15 ToLength ( argument )
415 function ToLength(v
) {
416 var len
= ToInteger(v
);
417 if (len
<= 0) return 0;
418 if (len
=== Infinity
) return 0x20000000000000 - 1; // 2^53-1
419 return min(len
, 0x20000000000000 - 1); // 2^53-1
422 // 7.1.16 CanonicalNumericIndexString ( argument )
424 //----------------------------------------
425 // 7.2 Testing and Comparison Operations
426 //----------------------------------------
428 // 7.2.1 RequireObjectCoercible ( argument )
429 // 7.2.2 IsArray ( argument )
431 // 7.2.3 IsCallable ( argument )
432 function IsCallable(o
) { return typeof o
=== 'function'; }
434 // 7.2.4 IsConstructor ( argument )
435 function IsConstructor(o
) {
436 // Hacks for Safari 7 TypedArray XXXConstructor objects
437 if (/Constructor/.test(Object
.prototype.toString
.call(o
))) return true;
438 if (/Function/.test(Object
.prototype.toString
.call(o
))) return true;
439 // TODO: Can this be improved on?
440 return typeof o
=== 'function';
443 // 7.2.5 IsExtensible (O)
444 // 7.2.6 IsInteger ( argument )
446 // 7.2.7 IsPropertyKey ( argument )
447 function IsPropertyKey(argument
) {
448 if (Type(argument
) === 'string') return true;
449 if (Type(argument
) === 'symbol') return true;
453 // 7.2.8 IsRegExp ( argument )
454 // 7.2.5 IsConstructor ( argument )
456 // 7.2.9 SameValue(x, y)
457 function SameValue(x
, y
) {
458 if (typeof x
!== typeof y
) return false;
463 if (x
!== x
&& y
!== y
) return true;
464 if (x
=== 0 && y
=== 0) return 1/x
=== 1/y
;
474 // 7.2.10 SameValueZero(x, y)
475 function SameValueZero(x
, y
) {
476 if (typeof x
!== typeof y
) return false;
481 if (x
!== x
&& y
!== y
) return true;
491 //----------------------------------------
492 // 7.3 Operations on Objects
493 //----------------------------------------
496 // - just use o.p or o[p]
499 function GetV(v
, p
) {
504 // 7.3.3 Set (O, P, V, Throw)
505 // - just use o.p = v or o[p] = v
510 // 7.3.9 GetMethod (O, P)
511 function GetMethod(o
, p
) {
512 var func
= GetV(o
, p
);
513 if (func
=== undefined || func
=== null) return undefined;
514 if (!IsCallable(func
)) throw TypeError();
518 // 7.3.10 HasProperty (O, P)
519 function HasProperty(o
, p
) {
521 if (Object
.prototype.hasOwnProperty
.call(o
, p
)) return true;
522 if (Type(o
) !== 'object') return false;
523 o
= Object
.getPrototypeOf(o
);
528 // 7.3.11 HasOwnProperty (O, P)
529 function HasOwnProperty(o
, p
) {
530 return Object
.prototype.hasOwnProperty
.call(o
, p
);
533 //----------------------------------------
534 // 7.4 Operations on Iterator Objects
535 //----------------------------------------
537 // 7.4.1 GetIterator ( obj, method )
538 function GetIterator(obj
, method
) {
539 if (arguments
.length
< 2)
540 method
= GetMethod(obj
, $$iterator
);
541 var iterator
= method
.call(obj
);
542 if (Type(iterator
) !== 'object') throw TypeError();
546 // 7.4.2 IteratorNext ( iterator, value )
547 function IteratorNext(iterator
, value
) {
548 if (arguments
.length
< 2)
549 var result
= iterator
.next();
551 result
= iterator
.next(value
);
552 if (Type(result
) !== 'object') throw TypeError();
556 // 7.4.3 IteratorComplete ( iterResult )
557 function IteratorComplete(iterResult
) {
558 console
.assert(Type(iterResult
) === 'object');
559 return Boolean(iterResult
.done
);
562 // 7.4.4 IteratorValue ( iterResult )
563 function IteratorValue(iterResult
) {
564 console
.assert(Type(iterResult
) === 'object');
565 return iterResult
.value
;
568 // 7.4.5 IteratorStep ( iterator )
569 function IteratorStep( iterator
, value
) {
570 var result
= IteratorNext(iterator
, value
);
571 var done
= result
['done'];
572 if (Boolean(done
) === true) return false;
576 // 7.4.6 IteratorClose( iterator, completion )
577 function IteratorClose( iterator
, completion
) {
578 console
.assert(Type(iterator
) === 'object');
579 var _return
= GetMethod(iterator
, 'return');
580 if (_return
=== undefined) return completion
;
582 var innerResult
= _return
[iterator
]();
584 // TODO: If completion.[[type]] is throw, return completion
587 if (Type(innerResult
) !== 'object') throw TypeError();
591 // 7.4.7 CreateIterResultObject (value, done)
592 function CreateIterResultObject(value
, done
) {
593 console
.assert(Type(done
) === 'boolean');
595 obj
["value"] = value
;
600 // 7.4.8 CreateListIterator (list)
601 // 7.4.8.1 ListIterator next( )
602 // 7.4.9 CreateCompoundIterator ( iterator1, iterator2 )
603 // 7.4.9.1 CompoundIterator next( )
605 //----------------------------------------
606 // 8 Executable Code and Execution Contexts
607 //----------------------------------------
609 //----------------------------------------
610 // 8.4 Jobs and Job Queues
611 //----------------------------------------
613 // 8.4.1 EnqueueJob ( queueName, job, arguments)
614 function EnqueueJob(queueName
, job
, args
) {
615 var fn = function() { job
.apply(undefined, args
); };
619 // 8.4.2 NextJob result
620 function NextJob(result
) {
624 //----------------------------------------
625 // 9 Ordinary and Exotic Objects Behaviors
626 //----------------------------------------
628 // 9.1.11 [[Enumerate]] ()
629 function Enumerate(obj
) {
631 if (Object(obj
) !== obj
) return e
;
632 var visited
= new Set
;
633 while (obj
!== null) {
634 Object
.getOwnPropertyNames(obj
).forEach(function(name
) {
635 if (!visited
.has(name
)) {
636 var desc
= Object
.getOwnPropertyDescriptor(obj
, name
);
639 if (desc
.enumerable
) e
.push(name
);
643 obj
= Object
.getPrototypeOf(obj
);
645 return e
[$$iterator
]();
648 // 9.1.12 [[OwnPropertyKeys]] ( )
649 function OwnPropertyKeys(o
) {
650 return Object
.getOwnPropertyNames(o
);
653 // 9.1.13 ObjectCreate(proto, internalSlotsList)
654 function ObjectCreate(proto
, internalSlotsList
) {
655 return Object
.create(proto
, internalSlotsList
);
658 // ---------------------------------------
659 // 19 Fundamental Objects
660 // ---------------------------------------
662 // ---------------------------------------
663 // 19.1 Object Objects
664 // ---------------------------------------
666 // 19.1.1 The Object Constructor
667 // 19.1.1.1 Object ( [ value ] )
668 // 19.1.2 Properties of the Object Constructor
669 // 19.1.2.1 Object.assign ( target, ...sources )
672 function assign(target
, /*...*/sources
) {
673 var to
= ToObject(target
);
674 if (arguments
.length
< 2) return to
;
676 var sourcesIndex
= 1;
677 while (sourcesIndex
< arguments
.length
) {
678 var nextSource
= arguments
[sourcesIndex
++];
679 if (nextSource
=== undefined || nextSource
=== null) {
682 var from = ToObject(nextSource
);
683 keys
= OwnPropertyKeys(from);
685 for (var keysIndex
= 0; keysIndex
< keys
.length
; ++keysIndex
) {
686 var nextKey
= keys
[keysIndex
];
687 var desc
= Object
.getOwnPropertyDescriptor(from, nextKey
);
688 if (desc
!== undefined && desc
.enumerable
) {
689 var propValue
= from[nextKey
];
690 to
[nextKey
] = propValue
;
697 // 19.1.2.2 Object.create ( O [ , Properties ] )
698 // 19.1.2.3 Object.defineProperties ( O, Properties )
699 // 19.1.2.4 Object.defineProperty ( O, P, Attributes )
700 // 19.1.2.5 Object.freeze ( O )
701 // 19.1.2.6 Object.getOwnPropertyDescriptor ( O, P )
704 var nativeSymbols
= (typeof global
.Symbol() === 'symbol'),
705 $getOwnPropertyNames
= Object
.getOwnPropertyNames
,
707 $window_names
= (typeof window
=== 'object' ? $getOwnPropertyNames(window
) : []);
709 function isStringKey(k
) { return !symbolForKey(k
); }
711 // 19.1.2.7 Object.getOwnPropertyNames ( O )
713 Object
, 'getOwnPropertyNames',
714 function getOwnPropertyNames(o
) {
715 if (Object
.prototype.toString
.call(o
) === '[object Window]') {
716 // Workaround for cross-realm calling by IE itself.
717 // https://github.com/inexorabletash/polyfill/issues/96
719 return $getOwnPropertyNames(o
).filter(isStringKey
);
721 return $window_names
.slice();
724 return $getOwnPropertyNames(o
).filter(isStringKey
);
727 // 19.1.2.8 Object.getOwnPropertySymbols ( O )
729 Object
, 'getOwnPropertySymbols',
730 function getOwnPropertySymbols(o
) {
731 return $getOwnPropertyNames(o
).filter(symbolForKey
).map(symbolForKey
);
734 // 19.1.2.14 Object.keys ( O )
738 return $keys(o
).filter(isStringKey
);
742 // 19.1.2.9 Object.getPrototypeOf ( O )
743 // 19.1.2.10 Object.is ( value1, value2 )
746 function is(value1
, value2
) {
747 return SameValue(value1
, value2
);
750 // 19.1.2.11 Object.isExtensible ( O )
751 // 19.1.2.12 Object.isFrozen ( O )
752 // 19.1.2.13 Object.isSealed ( O )
754 // 19.1.2.14 Object.keys ( O )
757 // 19.1.2.15 Object.preventExtensions ( O )
758 // 19.1.2.16 Object.prototype
759 // 19.1.2.17 Object.seal ( O )
761 // 19.1.2.18 Object.setPrototypeOf ( O, proto )
763 Object
, 'setPrototypeOf',
764 function setPrototypeOf(o
, proto
) {
765 if (Type(o
) !== 'object') throw TypeError();
766 if (Type(proto
) !== 'object' && Type(proto
) !== 'null') throw TypeError();
772 // 19.1.3 Properties of the Object Prototype Object
773 // 19.1.3.1 Object.prototype.constructor
774 // 19.1.3.2 Object.prototype.hasOwnProperty ( V )
775 // 19.1.3.3 Object.prototype.isPrototypeOf ( V )
776 // 19.1.3.4 Object.prototype.propertyIsEnumerable ( V )
777 // 19.1.3.5 Object.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )
778 // 19.1.3.6 Object.prototype.toString ( )
779 hook(Object
.prototype, 'toString',
781 var o
= strict(this);
782 if (o
=== Object(o
) && $$toStringTag
in o
) {
783 return '[object ' + o
[$$toStringTag
] + ']';
788 // 19.1.3.7 Object.prototype.valueOf ( )
789 // 19.1.4 Properties of Object Instances
791 // ---------------------------------------
792 // 19.2 Function Objects
793 // ---------------------------------------
795 // 19.2.1 The Function Constructor
796 // 19.2.1.1 Function ( p1, p2, … , pn, body )
797 // 19.2.2 Properties of the Function Constructor
798 // 19.2.2.1 Function.length
799 // 19.2.2.2 Function.prototype
800 // 19.2.3 Properties of the Function Prototype Object
801 // 19.2.3.1 Function.prototype.apply ( thisArg, argArray )
802 // 19.2.3.2 Function.prototype.bind ( thisArg , ...args)
803 // 19.2.3.3 Function.prototype.call (thisArg , ...args)
804 // 19.2.3.4 Function.prototype.constructor
805 // 19.2.3.5 Function.prototype.toString ( )
806 // 19.2.3.6 Function.prototype[@@hasInstance] ( V )
807 // 19.2.4 Function Instances
810 // 19.2.4.3 prototype
812 // (No polyfillable changes from ES5)
814 // ---------------------------------------
815 // 19.3 Boolean Objects
816 // ---------------------------------------
818 // 19.3.1 The Boolean Constructor
819 // 19.3.1.1 Boolean ( value )
820 // 19.3.2 Properties of the Boolean Constructor
821 // 19.3.2.1 Boolean.prototype
822 // 19.3.3 Properties of the Boolean Prototype Object
823 // 19.3.3.1 Boolean.prototype.constructor
824 // 19.3.3.2 Boolean.prototype.toString ( )
825 // 19.3.3.3 Boolean.prototype.valueOf ( )
826 // 19.3.4 Properties of Boolean Instances
828 // (No polyfillable changes from ES5)
830 // ---------------------------------------
831 // 19.4 Symbol Objects
832 // ---------------------------------------
834 // Moved earlier in this script, so that other polyfills can depend on them.
836 // 19.4.3.4 Symbol.prototype [ @@toStringTag ]
837 define(global
.Symbol
.prototype, global
.Symbol
.toStringTag
, 'Symbol');
839 // ---------------------------------------
840 // 19.5 Error Objects
841 // ---------------------------------------
843 // 19.5.1 The Error Constructor
844 // 19.5.1.1 Error ( message )
845 // 19.5.1.2 new Error( ...argumentsList )
846 // 19.5.2 Properties of the Error Constructor
847 // 19.5.2.1 Error.prototype
848 // 19.5.3 Properties of the Error Prototype Object
849 // 19.5.3.1 Error.prototype.constructor
850 // 19.5.3.2 Error.prototype.message
851 // 19.5.3.3 Error.prototype.name
852 // 19.5.3.4 Error.prototype.toString ( )
853 // 19.5.4 Properties of Error Instances
854 // 19.5.5 Native Error Types Used in This Standard
855 // 19.5.5.1 EvalError
856 // 19.5.5.2 RangeError
857 // 19.5.5.3 ReferenceError
858 // 19.5.5.4 SyntaxError
859 // 19.5.5.5 TypeError
861 // 19.5.6 NativeError Object Structure
862 // 19.5.6.1 NativeError Constructors
863 // 19.5.6.1.1 NativeError ( message )
864 // 19.5.6.1.2 new NativeError ( ...argumentsList )
865 // 19.5.6.2 Properties of the NativeError Constructors
866 // 19.5.6.2.1 NativeError.prototype
867 // 19.5.6.3 Properties of the NativeError Prototype Objects
868 // 19.5.6.4 Properties of NativeError Instances
870 // (No polyfillable changes from ES5)
872 // ---------------------------------------
873 // 20 Numbers and Dates
874 // ---------------------------------------
876 // ---------------------------------------
877 // 20.1 Number Objects
878 // ---------------------------------------
880 // 20.1.1 The Number Constructor
881 // 20.1.1.1 Number ( [ value ] )
882 // 20.1.1.2 new Number ( ...argumentsList )
883 // 20.1.2 Properties of the Number Constructor
885 // 20.1.2.1 Number.EPSILON
890 for (next
= 1; 1 + next
!== 1; next
= next
/ 2)
895 // 20.1.2.2 Number.isFinite ( number )
898 function isFinite(number
) {
899 if (Type(number
) !== 'number') return false;
900 if (number
!== number
|| number
=== +Infinity
|| number
=== -Infinity
) return false;
904 // 20.1.2.3 Number.isInteger ( number )
907 function isInteger(number
) {
908 if (Type(number
) !== 'number') return false;
909 if (number
!== number
|| number
=== +Infinity
|| number
=== -Infinity
) return false;
910 var integer
= ToInteger(number
);
911 if (integer
!== number
) return false;
915 // 20.1.2.4 Number.isNaN ( number )
918 function isNaN(number
) {
919 if (Type(number
) !== 'number') return false;
920 if (number
!== number
) return true;
924 // 20.1.2.5 Number.isSafeInteger ( number )
926 Number
, 'isSafeInteger',
927 function isSafeInteger(number
) {
928 if (Type(number
) !== 'number') return false;
929 if (number
!== number
|| number
=== +Infinity
|| number
=== -Infinity
) return false;
930 var integer
= ToInteger(number
);
931 if (integer
!== number
) return false;
932 if (abs(integer
) <= (0x20000000000000 - 1)) // 2^53-1
937 // 20.1.2.6 Number.MAX_SAFE_INTEGER
939 Number
, 'MAX_SAFE_INTEGER',
940 9007199254740991); // 2^53-1
942 // 20.1.2.7 Number.MAX_VALUE
944 // 20.1.2.8 Number.MIN_SAFE_INTEGER
946 Number
, 'MIN_SAFE_INTEGER',
947 -9007199254740991); // -2^53+1
949 // 20.1.2.9 Number.MIN_VALUE
950 // 20.1.2.10 Number.NaN
951 // 20.1.2.11 Number.NEGATIVE_INFINITY
953 // 20.1.2.12 Number.parseFloat ( string )
954 define(Number
, 'parseFloat', $parseFloat
);
956 // 20.1.2.13 Number.parseInt ( string, radix )
957 define(Number
, 'parseInt', $parseInt
);
959 // 20.1.2.14 Number.POSITIVE_INFINITY
960 // 20.1.2.15 Number.prototype
962 // 20.1.3 Properties of the Number Prototype Object
963 // 20.1.3.1 Number.prototype.constructor
964 // 20.1.3.2 Number.prototype.toExponential ( fractionDigits )
965 // 20.1.3.3 Number.prototype.toFixed ( fractionDigits )
966 // 20.1.3.4 Number.prototype.toLocaleString( [ reserved1 [ , reserved2 ] ])
967 // 20.1.3.5 Number.prototype.toPrecision ( precision )
968 // 20.1.3.6 Number.prototype.toString ( [ radix ] )
969 // 20.1.3.7 Number.prototype.valueOf ( )
970 // 20.1.4 Properties of Number Instances
972 // ---------------------------------------
973 // 20.2 The Math Object
974 // ---------------------------------------
976 // 20.2.1 Value Properties of the Math Object
978 // 20.2.1.2 Math.LN10
980 // 20.2.1.4 Math.LOG10E
981 // 20.2.1.5 Math.LOG2E
983 // 20.2.1.7 Math.SQRT1_2
984 // 20.2.1.8 Math.SQRT2
986 // 20.2.1.9 Math [ @@toStringTag ]
987 define(Math
, $$toStringTag
, 'Math');
989 // 20.2.2 Function Properties of the Math Object
990 // 20.2.2.1 Math.abs ( x )
991 // 20.2.2.2 Math.acos ( x )
993 // 20.2.2.3 Math.acosh(x)
998 return log(x
+ sqrt(x
* x
- 1));
1001 // 20.2.2.4 Math.asin ( x )
1003 // 20.2.2.5 Math.asinh( x )
1008 if (SameValue(x
, -0)) {
1011 var s
= sqrt(x
* x
+ 1);
1012 return (s
=== -x
) ? log(0) : log(x
+ s
);
1015 // 20.2.2.6 Math.atan ( x )
1017 // 20.2.2.7 Math.atanh( x )
1022 return (x
=== 0) ? x : log((1 + x
) / (1 - x
)) / 2;
1025 // 20.2.2.8 Math.atan2 ( y, x )
1027 // 20.2.2.9 Math.cbrt ( x )
1035 var r
= pow(abs(x
), 1/3);
1037 return r
+ (r
* (t
-r
) / (2*r
+ t
));
1040 // 20.2.2.10 Math.ceil ( x )
1042 // 20.2.2.11 Math.clz32 ( x )
1047 return (x
& 0xf0) ? (x
& 0x80 ? 0 : x
& 0x40 ? 1 : x
& 0x20 ? 2 : 3) :
1048 (x
& 0x08 ? 4 : x
& 0x04 ? 5 : x
& 0x02 ? 6 : x
& 0x01 ? 7 : 8);
1051 return x
& 0xff000000 ? clz8(x
>> 24) :
1052 x
& 0xff0000 ? clz8(x
>> 16) + 8 :
1053 x
& 0xff00 ? clz8(x
>> 8) + 16 : clz8(x
) + 24;
1058 // 20.2.2.12 Math.cos ( x )
1060 // 20.2.2.13 Math.cosh ( x )
1065 return (pow(E
, x
) + pow(E
, -x
)) / 2;
1068 // 20.2.2.14 Math.exp ( x )
1070 // 20.2.2.15 Math.expm1 ( x )
1075 // from: http://www.johndcook.com/cpp_log1p.html
1076 if (SameValue(x
, -0)) {
1078 } else if (abs(x
) < 1e-5) {
1079 return x
+ 0.5 * x
* x
; // two terms of Taylor expansion
1085 // 20.2.2.16 Math.floor ( x )
1087 // 20.2.2.17 Math.fround ( x )
1090 function fround(x
) {
1094 if (1/x
=== +Infinity
|| 1/x
=== -Infinity
|| x
=== +Infinity
|| x
=== -Infinity
) {
1097 return (new Float32Array([x
]))[0];
1100 // 20.2.2.18 Math.hypot ( value1 [, value2 [ ... ] ] )
1105 var m
= 0, sawNaN
= false;
1106 for (var i
= 0; i
< arguments
.length
; ++i
) {
1107 var n
= abs(Number(arguments
[i
]));
1108 if (n
=== Infinity
) return n
;
1109 if (n
!== n
) sawNaN
= true;
1113 if (sawNaN
) return NaN
;
1114 if (m
=== 0) return +0;
1116 for (i
= 0; i
< values
.length
; ++i
) {
1117 var r
= values
[i
] / m
;
1120 return m
* sqrt(sum
);
1123 // 20.2.2.19 Math.imul ( x, y )
1126 function imul(x
, y
) {
1127 var a
= ToUint32(x
);
1128 var b
= ToUint32(y
);
1129 // (slow but accurate)
1130 var ah
= (a
>>> 16) & 0xffff;
1131 var al
= a
& 0xffff;
1132 var bh
= (b
>>> 16) & 0xffff;
1133 var bl
= b
& 0xffff;
1134 return ((al
* bl
) + (((ah
* bl
+ al
* bh
) << 16) >>> 0)|0);
1135 }, ('imul' in Math
&& Math
.imul(1, 0x80000000) === 0) // Safari 7 bug
1138 // 20.2.2.20 Math.log ( x )
1140 // 20.2.2.21 Math.log1p ( x )
1145 // from: http://www.johndcook.com/cpp_expm1.html
1148 } else if (SameValue(x
, -0)) {
1150 } else if (abs(x
) > 1e-4) {
1153 return (-0.5 * x
+ 1) * x
;
1157 // 20.2.2.22 Math.log10 ( x )
1162 return log(x
) * LOG10E
;
1165 // 20.2.2.23 Math.log2 ( x )
1170 return log(x
) * LOG2E
;
1173 // 20.2.2.24 Math.max ( value1, value2 , ...values )
1174 // 20.2.2.25 Math.min ( value1, value2 , ...values )
1175 // 20.2.2.26 Math.pow ( x, y )
1176 // 20.2.2.27 Math.random ( )
1177 // 20.2.2.28 Math.round ( x )
1179 // 20.2.2.29 Math.sign(x)
1184 return x
< 0 ? -1 : x
> 0 ? 1 : x
;
1187 // 20.2.2.30 Math.sin ( x )
1189 // 20.2.2.31 Math.sinh( x )
1194 return SameValue(x
, -0) ? x : (pow(E
, x
) - pow(E
, -x
)) / 2;
1197 // 20.2.2.32 Math.sqrt ( x )
1198 // 20.2.2.33 Math.tan ( x )
1200 // 20.2.2.34 Math.tanh ( x )
1205 var n
= pow(E
, 2 * x
) - 1,
1206 d
= pow(E
, 2 * x
) + 1;
1207 if (SameValue(x
, -0))
1209 return (n
=== d
) ? 1 : n
/ d
; // Handle Infinity/Infinity
1212 // 20.2.2.35 Math.trunc ( x )
1217 return $isNaN(x
) ? NaN :
1218 x
< 0 ? ceil(x
) : floor(x
);
1221 // ---------------------------------------
1222 // 20.3 Date Objects
1223 // ---------------------------------------
1225 // 20.3.1 Overview of Date Objects and Definitions of Abstract Operations
1226 // 20.3.1.1 Time Values and Time Range
1227 // 20.3.1.2 Day Number and Time within Day
1228 // 20.3.1.3 Year Number
1229 // 20.3.1.4 Month Number
1230 // 20.3.1.5 Date Number
1231 // 20.3.1.6 Week Day
1232 // 20.3.1.7 Local Time Zone Adjustment
1233 // 20.3.1.8 Daylight Saving Time Adjustment
1234 // 20.3.1.9 Local Time
1235 // 20.3.1.10 Hours, Minutes, Second, and Milliseconds
1236 // 20.3.1.11 MakeTime (hour, min, sec, ms)
1237 // 20.3.1.12 MakeDay (year, month, date)
1238 // 20.3.1.13 MakeDate (day, time)
1239 // 20.3.1.14 TimeClip (time)
1240 // 20.3.1.15 Date Time String Format
1241 // 20.3.1.15.1 Extended years
1242 // 20.3.2 The Date Constructor
1243 // 20.3.2.1 Date ( year, month [, date [ , hours [ , minutes [ , seconds [ , ms ] ] ] ] ] )
1244 // 20.3.2.2 Date ( value )
1245 // 20.3.2.3 Date ( )
1246 // 20.3.3 Properties of the Date Constructor
1247 // 20.3.3.1 Date.now ( )
1248 // 20.3.3.2 Date.parse (string)
1249 // 20.3.3.3 Date.prototype
1250 // 20.3.3.4 Date.UTC ( year, month [ , date [ , hours [ , minutes [ , seconds [ , ms ] ] ] ] ] )
1251 // 20.3.4 Properties of the Date Prototype Object
1252 // 20.3.4.1 Date.prototype.constructor
1253 // 20.3.4.2 Date.prototype.getDate ( )
1254 // 20.3.4.3 Date.prototype.getDay ( )
1255 // 20.3.4.4 Date.prototype.getFullYear ( )
1256 // 20.3.4.5 Date.prototype.getHours ( )
1257 // 20.3.4.6 Date.prototype.getMilliseconds ( )
1258 // 20.3.4.7 Date.prototype.getMinutes ( )
1259 // 20.3.4.8 Date.prototype.getMonth ( )
1260 // 20.3.4.9 Date.prototype.getSeconds ( )
1261 // 20.3.4.10 Date.prototype.getTime ( )
1262 // 20.3.4.11 Date.prototype.getTimezoneOffset ( )
1263 // 20.3.4.12 Date.prototype.getUTCDate ( )
1264 // 20.3.4.13 Date.prototype.getUTCDay ( )
1265 // 20.3.4.14 Date.prototype.getUTCFullYear ( )
1266 // 20.3.4.15 Date.prototype.getUTCHours ( )
1267 // 20.3.4.16 Date.prototype.getUTCMilliseconds ( )
1268 // 20.3.4.17 Date.prototype.getUTCMinutes ( )
1269 // 20.3.4.18 Date.prototype.getUTCMonth ( )
1270 // 20.3.4.19 Date.prototype.getUTCSeconds ( )
1271 // 20.3.4.20 Date.prototype.setDate ( date )
1272 // 20.3.4.21 Date.prototype.setFullYear ( year [ , month [ , date ] ] )
1273 // 20.3.4.22 Date.prototype.setHours ( hour [ , min [ , sec [ , ms ] ] ] )
1274 // 20.3.4.23 Date.prototype.setMilliseconds ( ms )
1275 // 20.3.4.24 Date.prototype.setMinutes ( min [ , sec [ , ms ] ] )
1276 // 20.3.4.25 Date.prototype.setMonth ( month [ , date ] )
1277 // 20.3.4.26 Date.prototype.setSeconds ( sec [ , ms ] )
1278 // 20.3.4.27 Date.prototype.setTime ( time )
1279 // 20.3.4.28 Date.prototype.setUTCDate ( date )
1280 // 20.3.4.29 Date.prototype.setUTCFullYear ( year [ , month [ , date ] ] )
1281 // 20.3.4.30 Date.prototype.setUTCHours ( hour [ , min [ , sec [ , ms ] ] ] )
1282 // 20.3.4.31 Date.prototype.setUTCMilliseconds ( ms )
1283 // 20.3.4.32 Date.prototype.setUTCMinutes ( min [ , sec [, ms ] ] )
1284 // 20.3.4.33 Date.prototype.setUTCMonth ( month [ , date ] )
1285 // 20.3.4.34 Date.prototype.setUTCSeconds ( sec [ , ms ] )
1286 // 20.3.4.35 Date.prototype.toDateString ( )
1287 // 20.3.4.36 Date.prototype.toISOString ( )
1288 // 20.3.4.37 Date.prototype.toJSON ( key )
1289 // 20.3.4.38 Date.prototype.toLocaleDateString ( [ reserved1 [ , reserved2 ] ] )
1290 // 20.3.4.39 Date.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )
1291 // 20.3.4.40 Date.prototype.toLocaleTimeString ( [ reserved1 [ , reserved2 ] ] )
1292 // 20.3.4.41 Date.prototype.toString ( )
1293 // 20.3.4.42 Date.prototype.toTimeString ( )
1294 // 20.3.4.43 Date.prototype.toUTCString ( )
1295 // 20.3.4.44 Date.prototype.valueOf ( )
1296 // 20.3.4.45 Date.prototype [ @@toPrimitive ] ( hint )
1297 // 20.3.5 Properties of Date Instances
1299 // (No polyfillable changes from ES5)
1301 // ---------------------------------------
1302 // 21 Text Processing
1303 // ---------------------------------------
1305 var string_regexp_dispatch
= (function() {
1306 var faux
= {}, secret
= Symbol();
1307 faux
[Symbol
.match
] = function() { return secret
; };
1308 return ("").match(faux
) === secret
;
1311 // 21.1 String Objects
1312 // 21.1.1 The String Constructor
1313 // 21.1.1.1 String ( value )
1314 // 21.1.2 Properties of the String Constructor
1315 // 21.1.2.1 String.fromCharCode ( ...codeUnits )
1317 // 21.1.2.2 String.fromCodePoint ( ...codePoints )
1319 String
, 'fromCodePoint',
1320 function fromCodePoint(/*...codePoints*/) {
1321 var codePoints
= arguments
,
1322 length
= codePoints
.length
,
1325 while (nextIndex
< length
) {
1326 var next
= codePoints
[nextIndex
];
1327 var nextCP
= Number(next
);
1328 if (!SameValue(nextCP
, ToInteger(nextCP
)) ||
1329 nextCP
< 0 || nextCP
> 0x10FFFF) {
1330 throw RangeError('Invalid code point ' + nextCP
);
1332 if (nextCP
< 0x10000) {
1333 elements
.push(String
.fromCharCode(nextCP
));
1336 elements
.push(String
.fromCharCode((nextCP
>> 10) + 0xD800));
1337 elements
.push(String
.fromCharCode((nextCP
% 0x400) + 0xDC00));
1341 return elements
.join('');
1344 // 21.1.2.3 String.prototype
1346 // 21.1.2.4 String.raw ( template , ...substitutions )
1349 function raw(template
/*, ...substitutions*/) {
1350 var substitutions
= [].slice
.call(arguments
, 1);
1352 var cooked
= Object(template
);
1353 var rawValue
= cooked
['raw'];
1354 var raw
= Object(rawValue
);
1355 var len
= raw
['length'];
1356 var literalSegments
= ToLength(len
);
1357 if (literalSegments
<= 0) return '';
1358 var stringElements
= [];
1361 var next
= raw
[nextIndex
];
1362 var nextSeg
= String(next
);
1363 stringElements
.push(nextSeg
);
1364 if (nextIndex
+ 1 === literalSegments
)
1365 return stringElements
.join('');
1366 next
= substitutions
[nextIndex
];
1367 var nextSub
= String(next
);
1368 stringElements
.push(nextSub
);
1369 nextIndex
= nextIndex
+ 1;
1373 // See https://githib.com/inexorabletash/uate for a more useful version.
1375 // 21.1.3 Properties of the String Prototype Object
1376 // 21.1.3.1 String.prototype.charAt ( pos )
1377 // 21.1.3.2 String.prototype.charCodeAt ( pos )
1379 // 21.1.3.3 String.prototype.codePointAt ( pos )
1381 String
.prototype, 'codePointAt',
1382 function codePointAt(pos
) {
1383 var o
= strict(this);
1385 var position
= ToInteger(pos
);
1386 var size
= s
.length
;
1387 if (position
< 0 || position
>= size
) return undefined;
1388 var first
= s
.charCodeAt(position
);
1389 if (first
< 0xD800 || first
> 0xDBFF || position
+ 1 === size
) return first
;
1390 var second
= s
.charCodeAt(position
+ 1);
1391 if (second
< 0xDC00 || second
> 0xDFFF) return first
;
1392 return ((first
- 0xD800) * 1024) + (second
- 0xDC00) + 0x10000;
1395 // 21.1.3.4 String.prototype.concat ( ...args )
1396 // 21.1.3.5 String.prototype.constructor
1398 // 21.1.3.6 String.prototype.endsWith ( searchString [ , endPosition] )
1400 String
.prototype, 'endsWith',
1401 function endsWith(searchString
) {
1402 var endPosition
= arguments
[1];
1404 var o
= strict(this);
1406 var searchStr
= String(searchString
);
1408 var pos
= (endPosition
=== undefined) ? len : ToInteger(endPosition
);
1409 var end
= min(max(pos
, 0), len
);
1410 var searchLength
= searchStr
.length
;
1411 var start
= end
- searchLength
;
1412 if (start
< 0) return false;
1413 if (s
.substring(start
, start
+ searchLength
) === searchStr
) return true;
1417 // 21.1.3.7 String.prototype.includes ( searchString [ , position ] )
1419 String
.prototype, 'includes',
1420 function includes(searchString
) {
1421 var position
= arguments
[1];
1423 var o
= strict(this);
1425 var searchStr
= String(searchString
);
1426 var pos
= ToInteger(position
);
1428 var start
= min(max(pos
, 0), len
);
1429 return s
.indexOf(searchStr
, start
) !== -1;
1432 // 21.1.3.8 String.prototype.indexOf ( searchString [ , position ] )
1433 // 21.1.3.9 String.prototype.lastIndexOf ( searchString [ , position ] )
1434 // 21.1.3.10 String.prototype.localeCompare ( that [, reserved1 [ , reserved2 ] ] )
1435 // 21.1.3.11 String.prototype.match ( regexp )
1437 String
.prototype, 'match',
1438 function match(regexp
) {
1439 var o
= strict(this);
1441 if (HasProperty(regexp
, $$match
)) var rx
= regexp
;
1442 else rx
= new RegExp(regexp
);
1443 return rx
[$$match
](s
);
1444 }, !string_regexp_dispatch
);
1446 // 21.1.3.12 String.prototype.normalize ( [ form ] )
1448 // Not practical due to table sizes; if needed, pull in:
1449 // https://github.com/walling/unorm/
1451 // 21.1.3.13 String.prototype.repeat ( count )
1453 String
.prototype, 'repeat',
1454 function repeat(count
) {
1455 var o
= strict(this);
1457 var n
= ToInteger(count
);
1458 if (n
< 0) throw RangeError();
1459 if (n
=== Infinity
) throw RangeError();
1460 var t
= new Array(n
+ 1).join(s
);
1464 // 21.1.3.14 String.prototype.replace (searchValue, replaceValue )
1466 String
.prototype, 'replace',
1467 function replace(searchValue
, replaceValue
) {
1468 var o
= strict(this);
1469 if (HasProperty(searchValue
, $$replace
))
1470 return searchValue
[$$replace
](o
, replaceValue
);
1471 return orig_replace
.call(o
, searchValue
, replaceValue
);
1472 }, !string_regexp_dispatch
);
1474 // 21.1.3.15 String.prototype.search ( regexp )
1476 String
.prototype, 'search',
1477 function search(regexp
) {
1478 var o
= strict(this);
1479 var string
= String(o
);
1480 if (HasProperty(regexp
, $$search
)) var rx
= regexp
;
1481 else rx
= new RegExp(regexp
);
1482 return rx
[$$search
](string
);
1483 }, !string_regexp_dispatch
);
1485 // 21.1.3.16 String.prototype.slice ( start, end )
1486 // 21.1.3.17 String.prototype.split ( separator, limit )
1488 String
.prototype, 'split',
1489 function split(separator
, limit
) {
1490 var o
= strict(this);
1491 if (HasProperty(separator
, $$split
))
1492 return separator
[$$split
](o
, limit
);
1493 return orig_split
.call(o
, separator
, limit
);
1494 }, !string_regexp_dispatch
);
1496 // 21.1.3.18 String.prototype.startsWith ( searchString [, position ] )
1498 String
.prototype, 'startsWith',
1499 function startsWith(searchString
) {
1500 var position
= arguments
[1];
1502 var o
= strict(this);
1504 var searchStr
= String(searchString
);
1505 var pos
= ToInteger(position
);
1507 var start
= min(max(pos
, 0), len
);
1508 var searchLength
= searchStr
.length
;
1509 if (searchLength
+ start
> len
) return false;
1510 if (s
.substring(start
, start
+ searchLength
) === searchStr
) return true;
1514 // 21.1.3.19 String.prototype.substring ( start, end )
1515 // 21.1.3.20 String.prototype.toLocaleLowerCase ( [ reserved1 [ , reserved2 ] ] )
1516 // 21.1.3.21 String.prototype.toLocaleUpperCase ([ reserved1 [ , reserved2 ] ] )
1517 // 21.1.3.22 String.prototype.toLowerCase ( )
1518 // 21.1.3.23 String.prototype.toString ( )
1519 // 21.1.3.24 String.prototype.toUpperCase ( )
1520 // 21.1.3.25 String.prototype.trim ( )
1521 // 21.1.3.26 String.prototype.valueOf ( )
1523 // 21.1.3.27 String.prototype [ @@iterator ]( )
1525 String
.prototype, $$iterator
,
1526 function entries() {
1527 return CreateStringIterator(this, 'value');
1530 // 21.1.4 Properties of String Instances
1533 // 21.1.5 String Iterator Objects
1535 function StringIterator() {}
1537 // 21.1.5.1 CreateStringIterator Abstract Operation
1538 function CreateStringIterator(string
, kind
) {
1539 var s
= String(string
);
1540 var iterator
= new StringIterator
;
1541 set_internal(iterator
, '[[IteratedString]]', s
);
1542 set_internal(iterator
, '[[StringIteratorNextIndex]]', 0);
1543 set_internal(iterator
, '[[StringIterationKind]]', kind
);
1547 // 21.1.5.2 The %StringIteratorPrototype% Object
1548 var $StringIteratorPrototype
$ = Object
.create($IteratorPrototype
$);
1549 StringIterator
.prototype = $StringIteratorPrototype
$;
1551 // 21.1.5.2.1 %StringIteratorPrototype%.next ( )
1553 $StringIteratorPrototype
$, 'next',
1555 var o
= ToObject(this);
1556 var s
= String(o
['[[IteratedString]]']),
1557 index
= o
['[[StringIteratorNextIndex]]'],
1560 set_internal(o
, '[[StringIteratorNextIndex]]', Infinity
);
1561 return CreateIterResultObject(undefined, true);
1563 var cp
= s
.codePointAt(index
);
1564 set_internal(o
, '[[StringIteratorNextIndex]]', index
+ (cp
> 0xFFFF ? 2 : 1));
1565 return CreateIterResultObject(String
.fromCodePoint(cp
), false);
1568 // 21.1.5.2.2 %StringIteratorPrototype% [ @@toStringTag ]
1569 define($StringIteratorPrototype
$, $$toStringTag
, 'String Iterator');
1571 // 21.1.5.3 Properties of String Iterator Instances
1573 // ---------------------------------------
1574 // 21.2 RegExp (Regular Expression) Objects
1575 // ---------------------------------------
1578 // 21.2.2 Pattern Semantics
1579 // 21.2.2.1 Notation
1581 // 21.2.2.3 Disjunction
1582 // 21.2.2.4 Alternative
1584 // 21.2.2.6 Assertion
1585 // 21.2.2.7 Quantifier
1587 // 21.2.2.9 AtomEscape
1588 // 21.2.2.10 CharacterEscape
1589 // 21.2.2.11 DecimalEscape
1590 // 21.2.2.12 CharacterClassEscape
1591 // 21.2.2.13 CharacterClass
1592 // 21.2.2.14 ClassRanges
1593 // 21.2.2.15 NonemptyClassRanges
1594 // 21.2.2.16 NonemptyClassRangesNoDash
1595 // 21.2.2.17 ClassAtom
1596 // 21.2.2.18 ClassAtomNoDash
1597 // 21.2.2.19 ClassEscape
1598 // 21.2.3 The RegExp Constructor
1599 // 21.2.3.1 RegExp ( pattern, flags )
1600 // 21.2.3.2 new RegExp( ...argumentsList )
1601 // 21.2.3.3 Abstract Operations for the RegExp Constructor
1602 // 21.2.4 Properties of the RegExp Constructor
1603 // 21.2.4.1 RegExp.prototype
1604 // 21.2.5 Properties of the RegExp Prototype Object
1605 // 21.2.5.1 RegExp.prototype.constructor
1606 // 21.2.5.2 RegExp.prototype.exec ( string )
1608 // 21.2.5.3 get RegExp.prototype.flags
1609 if (!('flags' in RegExp
.prototype)) {
1610 Object
.defineProperty(
1611 RegExp
.prototype, 'flags', {
1613 var s
= String(this);
1614 return s
.substring(s
.lastIndexOf('/') + 1);
1619 // 21.2.5.4 get RegExp.prototype.global
1620 // 21.2.5.5 get RegExp.prototype.ignoreCase
1622 // 21.2.5.6 RegExp.prototype [ @@match ] ( string )
1623 define(RegExp
.prototype, $$match
, function(string
) {
1624 var o
= strict(this);
1625 return orig_match
.call(string
, o
);
1628 // 21.2.5.7 get RegExp.prototype.multiline
1630 // 21.2.5.8 RegExp.prototype [ @@replace ] ( string, replaceValue )
1631 define(RegExp
.prototype, $$replace
, function(string
, replaceValue
) {
1632 var o
= strict(this);
1633 return orig_replace
.call(string
, o
, replaceValue
);
1636 // 21.2.5.9 RegExp.prototype [ @@search ] ( string )
1637 define(RegExp
.prototype, $$search
, function(string
) {
1638 var o
= strict(this);
1639 return orig_search
.call(string
, o
);
1642 // 21.2.5.10 get RegExp.prototype.source
1644 // 21.2.5.11 RegExp.prototype [ @@split ] ( string, limit )
1645 define(RegExp
.prototype, $$split
, function(string
, limit
) {
1646 var o
= strict(this);
1647 return orig_split
.call(string
, o
, limit
);
1650 // 21.2.5.12 get RegExp.prototype.sticky
1651 // 21.2.5.13 RegExp.prototype.test( S )
1652 // 21.2.5.14 RegExp.prototype.toString ( )
1653 // 21.2.5.15 get RegExp.prototype.unicode
1655 // 21.2.6 Properties of RegExp Instances
1656 // 21.2.6.1 lastIndex
1658 // (No polyfillable changes from ES5)
1660 // ---------------------------------------
1661 // 22 Indexed Collections
1662 // ---------------------------------------
1664 // ---------------------------------------
1665 // 22.1 Array Objects
1666 // ---------------------------------------
1668 // 22.1.1 The Array Constructor
1669 // 22.1.1.1 Array ( )
1670 // 22.1.1.2 Array (len)
1671 // 22.1.1.3 Array (...items )
1673 // 22.1.2 Properties of the Array Constructor
1675 // 22.1.2.1 Array.from ( items [ , mapfn [ , thisArg ] ] )
1678 function from(items
) {
1679 var mapfn
= arguments
[1];
1680 var thisArg
= arguments
[2];
1682 var c
= strict(this);
1683 if (mapfn
=== undefined) {
1684 var mapping
= false;
1686 if (!IsCallable(mapfn
)) throw TypeError();
1690 var usingIterator
= GetMethod(items
, $$iterator
);
1691 if (usingIterator
!== undefined) {
1692 if (IsConstructor(c
)) {
1697 var iterator
= GetIterator(items
, usingIterator
);
1700 var next
= IteratorStep(iterator
);
1701 if (next
=== false) {
1705 var nextValue
= IteratorValue(next
);
1707 var mappedValue
= mapfn
.call(t
, nextValue
);
1709 mappedValue
= nextValue
;
1714 var arrayLike
= ToObject(items
);
1715 var lenValue
= arrayLike
.length
;
1716 var len
= ToLength(lenValue
);
1717 if (IsConstructor(c
)) {
1724 var kValue
= arrayLike
[k
];
1726 mappedValue
= mapfn
.call(t
, kValue
, k
);
1728 mappedValue
= kValue
;
1736 // 22.1.2.2 Array.isArray ( arg )
1738 // 22.1.2.3 Array.of ( ...items )
1742 var items
= arguments
;
1744 var lenValue
= items
.length
;
1745 var len
= ToUint32(lenValue
);
1746 var c
= strict(this), a
;
1747 if (IsConstructor(c
)) {
1762 // 22.1.2.4 Array.prototype
1763 // 22.1.2.5 get Array [ @@species ]
1764 // 22.1.3 Properties of the Array Prototype Object
1765 // 22.1.3.1 Array.prototype.concat ( ...arguments )
1766 // 22.1.3.1.1 Runtime Semantics: IsConcatSpreadable ( O )
1767 // 22.1.3.2 Array.prototype.constructor
1768 // 22.1.3.3 Array.prototype.copyWithin (target, start [ , end ] )
1770 Array
.prototype, 'copyWithin',
1771 function copyWithin(target
, start
/*, end*/) {
1772 var end
= arguments
[2];
1774 var o
= ToObject(this);
1775 var lenVal
= o
.length
;
1776 var len
= ToLength(lenVal
);
1778 var relativeTarget
= ToInteger(target
);
1780 if (relativeTarget
< 0)
1781 to
= max(len
+ relativeTarget
, 0);
1783 to
= min(relativeTarget
, len
);
1784 var relativeStart
= ToInteger(start
);
1786 if (relativeStart
< 0)
1787 from = max(len
+ relativeStart
, 0);
1789 from = min(relativeStart
, len
);
1791 if (end
=== undefined)
1794 relativeEnd
= ToInteger(end
);
1796 if (relativeEnd
< 0)
1797 final
= max(len
+ relativeEnd
, 0);
1799 final
= min(relativeEnd
, len
);
1800 var count
= min(final
- from, len
- to
);
1802 if (from < to
&& to
< from + count
) {
1804 from = from + count
- 1;
1805 to
= to
+ count
- 1;
1810 var fromKey
= String(from);
1811 var toKey
= String(to
);
1812 var fromPresent
= HasProperty(o
, fromKey
);
1814 var fromVal
= o
[fromKey
];
1819 from = from + direction
;
1820 to
= to
+ direction
;
1826 // 22.1.3.4 Array.prototype.entries ( )
1827 var nativeArrayIteratorMethods
=
1828 ('entries' in Array
.prototype && 'next' in [].entries());
1831 Array
.prototype, 'entries',
1832 function entries() {
1833 return CreateArrayIterator(this, 'key+value');
1834 }, !nativeArrayIteratorMethods
);
1836 // 22.1.3.5 Array.prototype.every ( callbackfn [ , thisArg] )
1838 // 22.1.3.6 Array.prototype.fill (value [ , start [ , end ] ] )
1840 Array
.prototype, 'fill',
1841 function fill(value
/*, start, end*/) {
1842 var start
= arguments
[1],
1845 var o
= ToObject(this);
1846 var lenVal
= o
.length
;
1847 var len
= ToLength(lenVal
);
1849 var relativeStart
= ToInteger(start
);
1851 if (relativeStart
< 0)
1852 k
= max((len
+ relativeStart
), 0);
1854 k
= min(relativeStart
, len
);
1856 if (end
=== undefined)
1859 relativeEnd
= ToInteger(end
);
1861 if (relativeEnd
< 0)
1862 final
= max((len
+ relativeEnd
), 0);
1864 final
= min(relativeEnd
, len
);
1873 // 22.1.3.7 Array.prototype.filter ( callbackfn [ , thisArg ] )
1875 // 22.1.3.8 Array.prototype.find ( predicate [ , thisArg ] )
1877 Array
.prototype, 'find',
1878 function find(predicate
) {
1879 var o
= ToObject(this);
1880 var lenValue
= o
.length
;
1881 var len
= ToInteger(lenValue
);
1882 if (!IsCallable(predicate
)) throw TypeError();
1883 var t
= arguments
.length
> 1 ? arguments
[1] : undefined;
1887 var kPresent
= HasProperty(o
, pk
);
1890 var testResult
= predicate
.call(t
, kValue
, k
, o
);
1891 if (Boolean(testResult
)) {
1900 // 22.1.3.9 Array.prototype.findIndex ( predicate [ , thisArg ] )
1902 Array
.prototype, 'findIndex',
1903 function findIndex(predicate
) {
1904 var o
= ToObject(this);
1905 var lenValue
= o
.length
;
1906 var len
= ToLength(lenValue
);
1907 if (!IsCallable(predicate
)) throw TypeError();
1908 var t
= arguments
.length
> 1 ? arguments
[1] : undefined;
1912 var kPresent
= HasProperty(o
, pk
);
1915 var testResult
= predicate
.call(t
, kValue
, k
, o
);
1916 if (Boolean(testResult
)) {
1925 // 22.1.3.10 Array.prototype.forEach ( callbackfn [ , thisArg ] )
1926 // 22.1.3.11 Array.prototype.indexOf ( searchElement [ , fromIndex ] )
1927 // 22.1.3.12 Array.prototype.join (separator)
1929 // 22.1.3.13 Array.prototype.keys ( )
1931 Array
.prototype, 'keys',
1933 return CreateArrayIterator(this, 'key');
1934 }, !nativeArrayIteratorMethods
);
1936 // 22.1.3.14 Array.prototype.lastIndexOf ( searchElement [ , fromIndex ] )
1937 // 22.1.3.15 Array.prototype.map ( callbackfn [ , thisArg ] )
1938 // 22.1.3.16 Array.prototype.pop ( )
1939 // 22.1.3.17 Array.prototype.push ( ...items )
1940 // 22.1.3.18 Array.prototype.reduce ( callbackfn [ , initialValue ] )
1941 // 22.1.3.19 Array.prototype.reduceRight ( callbackfn [ , initialValue ] )
1942 // 22.1.3.20 Array.prototype.reverse ( )
1943 // 22.1.3.21 Array.prototype.shift ( )
1944 // 22.1.3.22 Array.prototype.slice (start, end)
1945 // 22.1.3.23 Array.prototype.some ( callbackfn [ , thisArg ] )
1946 // 22.1.3.24 Array.prototype.sort (comparefn)
1947 // 22.1.3.25 Array.prototype.splice (start, deleteCount , ...items )
1948 // 22.1.3.26 Array.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )
1949 // 22.1.3.27 Array.prototype.toString ( )
1950 // 22.1.3.28 Array.prototype.unshift ( ...items )
1952 // 22.1.3.29 Array.prototype.values ( )
1954 Array
.prototype, 'values',
1956 return CreateArrayIterator(this, 'value');
1957 }, !nativeArrayIteratorMethods
);
1959 // 22.1.3.30 Array.prototype [ @@iterator ] ( )
1961 Array
.prototype, $$iterator
,
1962 Array
.prototype.values
1965 // 22.1.3.31 Array.prototype [ @@unscopables ]
1966 // 22.1.4 Properties of Array Instances
1969 // 22.1.5 Array Iterator Objects
1970 function ArrayIterator() {}
1972 // 22.1.5.1 CreateArrayIterator Abstract Operation
1973 function CreateArrayIterator(array
, kind
) {
1974 var o
= ToObject(array
);
1975 var iterator
= new ArrayIterator
;
1976 set_internal(iterator
, '[[IteratedObject]]', o
);
1977 set_internal(iterator
, '[[ArrayIteratorNextIndex]]', 0);
1978 set_internal(iterator
, '[[ArrayIterationKind]]', kind
);
1982 // 22.1.5.2 The %ArrayIteratorPrototype% Object
1983 var $ArrayIteratorPrototype
$ = Object
.create($IteratorPrototype
$);
1984 ArrayIterator
.prototype = $ArrayIteratorPrototype
$;
1986 // 22.1.5.2.1 %ArrayIteratorPrototype%. next( )
1988 $ArrayIteratorPrototype
$, 'next',
1990 var o
= strict(this);
1991 if (Type(o
) !== 'object') throw TypeError();
1992 var a
= o
['[[IteratedObject]]'],
1993 index
= o
['[[ArrayIteratorNextIndex]]'],
1994 itemKind
= o
['[[ArrayIterationKind]]'],
1995 lenValue
= a
.length
,
1996 len
= ToUint32(lenValue
),
1999 if (itemKind
.indexOf('sparse') !== -1) {
2001 while (!found
&& index
< len
) {
2002 elementKey
= String(index
);
2003 found
= HasProperty(a
, elementKey
);
2010 set_internal(o
, '[[ArrayIteratorNextIndex]]', Infinity
);
2011 return CreateIterResultObject(undefined, true);
2014 set_internal(o
, '[[ArrayIteratorNextIndex]]', index
+ 1);
2015 if (itemKind
.indexOf('value') !== -1)
2016 elementValue
= a
[elementKey
];
2017 if (itemKind
.indexOf('key+value') !== -1)
2018 return CreateIterResultObject([elementKey
, elementValue
], false);
2019 if (itemKind
.indexOf('key') !== -1)
2020 return CreateIterResultObject(elementKey
, false);
2021 if (itemKind
=== 'value')
2022 return CreateIterResultObject(elementValue
, false);
2023 throw Error('Internal error');
2026 // 22.1.5.2.2 %ArrayIteratorPrototype% [ @@toStringTag ]
2027 define($ArrayIteratorPrototype
$, $$toStringTag
, 'Array Iterator');
2029 // 22.1.5.3 Properties of Array Iterator Instances
2032 // ---------------------------------------
2033 // 22.2 TypedArray Objects
2034 // ---------------------------------------
2036 // See typedarray.js for TypedArray polyfill
2038 ['Int8Array', 'Uint8Array', 'Uint8ClampedArray',
2039 'Int16Array', 'Uint16Array',
2040 'Int32Array', 'Uint32Array',
2041 'Float32Array', 'Float64Array'].forEach(function ($TypedArrayName
$) {
2042 if (!($TypedArrayName
$ in global
))
2044 var $TypedArray
$ = global
[$TypedArrayName
$];
2046 // 22.2.1 The %TypedArray% Intrinsic Object
2047 // 22.2.1.1 %TypedArray% ( length )
2048 // 22.2.1.2 %TypedArray% ( typedArray )
2049 // 22.2.1.3 %TypedArray% ( object )
2050 // 22.2.1.4 %TypedArray% ( buffer [ , byteOffset [ , length ] ] )
2051 // 22.2.1.5 %TypedArray% ( all other argument combinations )
2052 // 22.2.2 Properties of the %TypedArray% Intrinsic Object
2054 // 22.2.2.1 %TypedArray%.from ( source [ , mapfn [ , thisArg ] ] )
2056 $TypedArray
$, 'from',
2057 function from(source
) {
2058 var mapfn
= arguments
[1];
2059 var thisArg
= arguments
[2];
2061 var c
= strict(this);
2062 if (!IsConstructor(c
)) throw TypeError();
2063 if (mapfn
=== undefined) {
2064 var mapping
= false;
2066 if (IsCallable(mapfn
)) throw TypeError();
2070 var usingIterator
= GetMethod(source
, $$iterator
);
2071 if (usingIterator
!== undefined) {
2072 var iterator
= GetIterator(source
, usingIterator
);
2075 while (next
!== false) {
2076 next
= IteratorStep(iterator
);
2077 if (next
!== false) {
2078 var nextValue
= IteratorValue(next
);
2079 values
.push(nextValue
);
2082 var len
= values
.length
;
2083 var newObj
= new c(len
);
2086 var kValue
= values
.shift();
2088 var mappedValue
= mapfn
.call(t
, kValue
);
2090 mappedValue
= kValue
;
2092 newObj
[k
] = mappedValue
;
2095 console
.assert(values
.length
=== 0);
2098 var arrayLike
= ToObject(source
);
2099 var lenValue
= arrayLike
.length
;
2100 len
= ToLength(lenValue
);
2101 newObj
= new c(len
);
2104 kValue
= arrayLike
[k
];
2106 mappedValue
= mapfn
.call(t
, kValue
, k
);
2108 mappedValue
= kValue
;
2110 newObj
[k
] = mappedValue
;
2116 // 22.2.2.2 %TypedArray%.of ( ...items )
2120 var items
= arguments
;
2122 var len
= items
.length
;
2123 var c
= strict(this);
2124 var newObj
= new c(len
);
2127 newObj
[k
] = items
[k
];
2133 // 22.2.2.3 %TypedArray%.prototype
2134 // 22.2.2.4 get %TypedArray% [ @@species ]
2135 // 22.2.3 Properties of the %TypedArrayPrototype% Object
2136 // 22.2.3.1 get %TypedArray%.prototype.buffer
2137 // 22.2.3.2 get %TypedArray%.prototype.byteLength
2138 // 22.2.3.3 get %TypedArray%.prototype.byteOffset
2139 // 22.2.3.4 %TypedArray%.prototype.constructor
2141 // 22.2.3.5 %TypedArray%.prototype.copyWithin (target, start [, end ] )
2142 define($TypedArray
$.prototype, 'copyWithin', Array
.prototype.copyWithin
);
2144 // 22.2.3.6 %TypedArray%.prototype.entries ( )
2145 define($TypedArray
$.prototype, 'entries', Array
.prototype.entries
);
2147 // 22.2.3.7 %TypedArray%.prototype.every ( callbackfn [ , thisArg ] )
2148 define($TypedArray
$.prototype, 'every', Array
.prototype.every
);
2150 // 22.2.3.8 %TypedArray%.prototype.fill (value [ , start [ , end ] ] )
2152 $TypedArray
$.prototype, 'fill',
2153 //Array.prototype.fill // Doesn't work in Safari 7
2154 function fill(value
/*, start, end*/) {
2155 var start
= arguments
[1],
2158 var o
= ToObject(this);
2159 var lenVal
= o
.length
;
2160 var len
= ToLength(lenVal
);
2162 var relativeStart
= ToInteger(start
);
2164 if (relativeStart
< 0) k
= max((len
+ relativeStart
), 0);
2165 else k
= min(relativeStart
, len
);
2167 if (end
=== undefined) relativeEnd
= len
;
2168 else relativeEnd
= ToInteger(end
);
2170 if (relativeEnd
< 0) final
= max((len
+ relativeEnd
), 0);
2171 else final
= min(relativeEnd
, len
);
2180 // 22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
2182 $TypedArray
$.prototype, 'filter',
2183 function filter(callbackfn
) {
2184 var thisArg
= arguments
[1];
2186 var o
= ToObject(this);
2187 var lenVal
= o
.length
;
2188 var len
= ToLength(lenVal
);
2189 if (!IsCallable(callbackfn
)) throw TypeError();
2191 var c
= o
.constructor;
2197 var selected
= callbackfn
.call(t
, kValue
, k
, o
);
2204 var a
= new c(captured
);
2206 for (var i
= 0; i
< kept
.length
; ++i
) {
2214 // 22.2.3.10 %TypedArray%.prototype.find (predicate [ , thisArg ] )
2215 define($TypedArray
$.prototype, 'find', Array
.prototype.find
);
2217 // 22.2.3.11 %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] )
2218 define($TypedArray
$.prototype, 'findIndex', Array
.prototype.findIndex
);
2220 // 22.2.3.12 %TypedArray%.prototype.forEach ( callbackfn [ , thisArg ] )
2221 define($TypedArray
$.prototype, 'forEach', Array
.prototype.forEach
);
2223 // 22.2.3.13 %TypedArray%.prototype.indexOf (searchElement [ , fromIndex ] )
2224 define($TypedArray
$.prototype, 'indexOf', Array
.prototype.indexOf
);
2226 // 22.2.3.14 %TypedArray%.prototype.join ( separator )
2227 define($TypedArray
$.prototype, 'join', Array
.prototype.join
);
2229 // 22.2.3.15 %TypedArray%.prototype.keys ( )
2230 define($TypedArray
$.prototype, 'keys', Array
.prototype.keys
);
2232 // 22.2.3.16 %TypedArray%.prototype.lastIndexOf ( searchElement [ , fromIndex ] )
2233 define($TypedArray
$.prototype, 'lastIndexOf', Array
.prototype.lastIndexOf
);
2235 // 22.2.3.17 get %TypedArray%.prototype.length
2237 // 22.2.3.18 %TypedArray%.prototype.map ( callbackfn [ , thisArg ] )
2239 $TypedArray
$.prototype, 'map',
2240 function map(callbackfn
) {
2241 var thisArg
= arguments
[1];
2243 var o
= ToObject(this);
2244 var lenValue
= o
.length
;
2245 var len
= ToLength(lenValue
);
2246 if (!IsCallable(callbackfn
)) throw TypeError();
2249 var c
= o
.constructor;
2250 if (IsConstructor(c
))
2252 if (a
=== undefined)
2256 var kPresent
= HasProperty(o
, k
);
2259 var mappedValue
= callbackfn
.call(t
, kValue
, k
, o
);
2267 // 22.2.3.19 %TypedArray%.prototype.reduce ( callbackfn [, initialValue] )
2268 define($TypedArray
$.prototype, 'reduce', Array
.prototype.reduce
);
2270 // 22.2.3.20 %TypedArray%.prototype.reduceRight ( callbackfn [, initialValue] )
2271 define($TypedArray
$.prototype, 'reduceRight', Array
.prototype.reduceRight
);
2273 // 22.2.3.21 %TypedArray%.prototype.reverse ( )
2274 define($TypedArray
$.prototype, 'reverse', Array
.prototype.reverse
);
2276 // 22.2.3.22 %TypedArray%.prototype.set ( overloaded [ , offset ])
2277 // 22.2.3.22.1 %TypedArray%.prototype.set (array [ , offset ] )
2278 // 22.2.3.22.2 %TypedArray%.prototype.set(typedArray [, offset ] )
2280 // 22.2.3.23 %TypedArray%.prototype.slice ( start, end )
2282 $TypedArray
$.prototype, 'slice',
2283 function slice(start
, end
) {
2284 var o
= ToObject(this);
2285 var lenVal
= o
.length
;
2286 var len
= ToLength(lenVal
);
2287 var relativeStart
= ToInteger(start
);
2288 var k
= (relativeStart
< 0) ? max(len
+ relativeStart
, 0) : min(relativeStart
, len
);
2289 var relativeEnd
= (end
=== undefined) ? len : ToInteger(end
);
2290 var final
= (relativeEnd
< 0) ? max(len
+ relativeEnd
, 0) : min(relativeEnd
, len
);
2291 var count
= final
- k
;
2292 var c
= o
.constructor;
2293 if (IsConstructor(c
)) {
2294 var a
= new c(count
);
2308 // 22.2.3.24 %TypedArray%.prototype.some ( callbackfn [ , thisArg ] )
2309 define($TypedArray
$.prototype, 'some', Array
.prototype.some
);
2311 // 22.2.3.25 %TypedArray%.prototype.sort ( comparefn )
2313 $TypedArray
$.prototype, 'sort',
2315 var comparefn
= arguments
[0];
2317 function sortCompare(x
, y
) {
2318 console
.assert(Type(x
) === 'number' && Type(y
) === 'number');
2319 if (x
!== x
&& y
!== y
) return +0;
2320 if (x
!== x
) return 1;
2321 if (y
!== y
) return -1;
2322 if (comparefn
!== undefined) {
2323 return comparefn(x
, y
);
2325 if (x
< y
) return -1;
2326 if (x
> y
) return 1;
2329 return Array
.prototype.sort
.call(this, sortCompare
);
2332 // 22.2.3.26 %TypedArray%.prototype.subarray( [ begin [ , end ] ] )
2333 // 22.2.3.27 %TypedArray%.prototype.toLocaleString ([ reserved1 [ , reserved2 ] ])
2334 // 22.2.3.28 %TypedArray%.prototype.toString ( )
2336 // 22.2.3.29 %TypedArray%.prototype.values ( )
2337 define($TypedArray
$.prototype, 'values', Array
.prototype.values
);
2339 // 22.2.3.30 %TypedArray%.prototype [ @@iterator ] ( )
2341 $TypedArray
$.prototype, $$iterator
,
2342 $TypedArray
$.prototype.values
2345 // 22.2.3.31 get %TypedArray%.prototype [ @@toStringTag ]
2346 define($TypedArray
$.prototype, $$toStringTag
, $TypedArrayName
$);
2348 // 22.2.4 The TypedArray Constructors
2349 // 22.2.4.1TypedArray( ... argumentsList)
2350 // 22.2.5 Properties of the TypedArray Constructors
2351 // 22.2.5.1 TypedArray.BYTES_PER_ELEMENT
2352 // 22.2.5.2 TypedArray.prototype
2353 // 22.2.6 Properties of TypedArray Prototype Objects
2354 // 22.2.6.1 TypedArray.prototype.BYTES_PER_ELEMENT
2355 // 22.2.6.2 TypedArray.prototype.constructor
2356 // 22.2.7 Properties of TypedArray Instances
2359 // ---------------------------------------
2360 // 23 Keyed Collection
2361 // ---------------------------------------
2363 // ---------------------------------------
2365 // ---------------------------------------
2368 // 23.1.1 The Map Constructor
2370 // 23.1.1.1 Map ( [ iterable ] )
2372 function Map(/*iterable*/) {
2373 var map
= strict(this);
2374 var iterable
= arguments
[0];
2376 if (Type(map
) !== 'object') throw TypeError();
2377 if ('[[MapData]]' in map
) throw TypeError();
2379 if (iterable
!== undefined) {
2380 var adder
= map
['set'];
2381 if (!IsCallable(adder
)) throw TypeError();
2382 var iter
= GetIterator(ToObject(iterable
));
2384 set_internal(map
, '[[MapData]]', { keys: [], values: [] });
2385 if (iter
=== undefined) return map
;
2387 var next
= IteratorStep(iter
);
2390 var nextItem
= IteratorValue(next
);
2391 if (Type(nextItem
) !== 'object') throw TypeError();
2392 var k
= nextItem
[0];
2393 var v
= nextItem
[1];
2394 adder
.call(map
, k
, v
);
2400 if (!('Map' in global
) || OVERRIDE_NATIVE_FOR_TESTING
||
2401 (function() { try { new global
.Map([]); return false; } catch (_
) { return true; } }()) ||
2402 (function() { try { return !new global
.Map().entries().next
; } catch (_
) { return true; } }()) ||
2403 (new global
.Map([['a', 1]]).size
!== 1))
2407 function MapDataIndexOf(mapData
, key
) {
2409 if (key
=== key
) return mapData
.keys
.indexOf(key
);
2410 // Slow case for NaN
2411 for (i
= 0; i
< mapData
.keys
.length
; i
+= 1)
2412 if (SameValueZero(mapData
.keys
[i
], key
)) return i
;
2416 // 23.1.1.2 new Map ( ... argumentsList )
2417 // 23.1.2 Properties of the Map Constructor
2418 // 23.1.2.1 Map.prototype
2419 var $MapPrototype
$ = {};
2420 Map
.prototype = $MapPrototype
$;
2422 // 23.1.2.2 get Map [ @@species ]
2424 // 23.1.3 Properties of the Map Prototype Object
2425 // 23.1.3.1 Map.prototype.clear ()
2427 Map
.prototype, 'clear',
2429 var m
= strict(this);
2430 if (Type(m
) !== 'object') throw TypeError();
2431 if (!('[[MapData]]' in m
)) throw TypeError();
2432 if (m
['[[MapData]]'] === undefined) throw TypeError();
2433 var entries
= m
['[[MapData]]'];
2434 entries
.keys
.length
= 0;
2435 entries
.values
.length
= 0;
2439 // 23.1.3.2 Map.prototype.constructor
2441 // 23.1.3.3 Map.prototype.delete ( key )
2443 Map
.prototype, 'delete',
2444 function delete_(key
) {
2445 var m
= strict(this);
2446 if (Type(m
) !== 'object') throw TypeError();
2447 if (!('[[MapData]]' in m
)) throw TypeError();
2448 if (m
['[[MapData]]'] === undefined) throw TypeError();
2449 var entries
= m
['[[MapData]]'];
2450 var i
= MapDataIndexOf(entries
, key
);
2451 if (i
< 0) return false;
2452 entries
.keys
[i
] = empty
;
2453 entries
.values
[i
] = empty
;
2457 // 23.1.3.4 Map.prototype.entries ( )
2459 Map
.prototype, 'entries',
2460 function entries() {
2461 var m
= strict(this);
2462 if (Type(m
) !== 'object') throw TypeError();
2463 return CreateMapIterator(m
, 'key+value');
2466 // 23.1.3.5 Map.prototype.forEach ( callbackfn [ , thisArg ] )
2468 Map
.prototype, 'forEach',
2469 function forEach(callbackfn
/*, thisArg*/) {
2470 var thisArg
= arguments
[1];
2472 var m
= strict(this);
2473 if (Type(m
) !== 'object') throw TypeError();
2474 if (!('[[MapData]]' in m
)) throw TypeError();
2475 if (m
['[[MapData]]'] === undefined) throw TypeError();
2476 var entries
= m
['[[MapData]]'];
2478 if (!IsCallable(callbackfn
)) {
2479 throw TypeError('First argument to forEach is not callable.');
2481 for (var i
= 0; i
< entries
.keys
.length
; ++i
) {
2482 if (entries
.keys
[i
] !== empty
) {
2483 callbackfn
.call(thisArg
, entries
.values
[i
], entries
.keys
[i
], m
);
2489 // 23.1.3.6 Map.prototype.get ( key )
2491 Map
.prototype, 'get',
2493 var m
= strict(this);
2494 if (Type(m
) !== 'object') throw TypeError();
2495 if (!('[[MapData]]' in m
)) throw TypeError();
2496 if (m
['[[MapData]]'] === undefined) throw TypeError();
2497 var entries
= m
['[[MapData]]'];
2498 var i
= MapDataIndexOf(entries
, key
);
2499 if (i
>= 0) return entries
.values
[i
];
2503 // 23.1.3.7 Map.prototype.has ( key )
2505 Map
.prototype, 'has',
2507 var m
= strict(this);
2508 if (Type(m
) !== 'object') throw TypeError();
2509 if (!('[[MapData]]' in m
)) throw TypeError();
2510 if (m
['[[MapData]]'] === undefined) throw TypeError();
2511 var entries
= m
['[[MapData]]'];
2512 if (MapDataIndexOf(entries
, key
) >= 0) return true;
2516 // 23.1.3.8 Map.prototype.keys ( )
2518 Map
.prototype, 'keys',
2520 var m
= strict(this);
2521 if (Type(m
) !== 'object') throw TypeError();
2522 return CreateMapIterator(m
, 'key');
2525 // 23.1.3.9 Map.prototype.set ( key , value )
2527 Map
.prototype, 'set',
2528 function set(key
, value
) {
2529 var m
= strict(this);
2530 if (Type(m
) !== 'object') throw TypeError();
2531 if (!('[[MapData]]' in m
)) throw TypeError();
2532 if (m
['[[MapData]]'] === undefined) throw TypeError();
2533 var entries
= m
['[[MapData]]'];
2534 var i
= MapDataIndexOf(entries
, key
);
2535 if (i
< 0) i
= entries
.keys
.length
;
2536 if (SameValue(key
, -0)) key
= 0;
2537 entries
.keys
[i
] = key
;
2538 entries
.values
[i
] = value
;
2542 // 23.1.3.10 get Map.prototype.size
2543 Object
.defineProperty(
2544 Map
.prototype, 'size', {
2546 var m
= strict(this);
2547 if (Type(m
) !== 'object') throw TypeError();
2548 if (!('[[MapData]]' in m
)) throw TypeError();
2549 if (m
['[[MapData]]'] === undefined) throw TypeError();
2550 var entries
= m
['[[MapData]]'];
2552 for (var i
= 0; i
< entries
.keys
.length
; ++i
) {
2553 if (entries
.keys
[i
] !== empty
)
2560 // 23.1.3.11 Map.prototype.values ( )
2562 Map
.prototype, 'values',
2564 var m
= strict(this);
2565 if (Type(m
) !== 'object') throw TypeError();
2566 return CreateMapIterator(m
, 'value');
2569 // 23.1.3.12 Map.prototype [ @@iterator ]( )
2571 Map
.prototype, $$iterator
,
2573 var m
= strict(this);
2574 if (Type(m
) !== 'object') throw TypeError();
2575 return CreateMapIterator(m
, 'key+value');
2578 // 23.1.3.13 Map.prototype [ @@toStringTag ]
2579 define(global
.Map
.prototype, $$toStringTag
, 'Map');
2581 // 23.1.4 Properties of Map Instances
2582 // 23.1.5 Map Iterator Objects
2585 function MapIterator() {}
2587 // 23.1.5.1 CreateMapIterator Abstract Operation
2588 function CreateMapIterator(map
, kind
) {
2589 if (Type(map
) !== 'object') throw TypeError();
2590 if (!('[[MapData]]' in map
)) throw TypeError();
2591 if (map
['[[MapData]]'] === undefined) throw TypeError();
2592 var iterator
= new MapIterator
;
2593 set_internal(iterator
, '[[Map]]', map
);
2594 set_internal(iterator
, '[[MapNextIndex]]', 0);
2595 set_internal(iterator
, '[[MapIterationKind]]', kind
);
2599 // 23.1.5.2 The %MapIteratorPrototype% Object
2600 var $MapIteratorPrototype
$ = Object
.create($IteratorPrototype
$);
2601 MapIterator
.prototype = $MapIteratorPrototype
$;
2603 // 23.1.5.2.1 %MapIteratorPrototype%.next ( )
2605 $MapIteratorPrototype
$, 'next',
2607 var o
= strict(this);
2608 if (Type(o
) !== 'object') throw TypeError();
2609 var m
= o
['[[Map]]'],
2610 index
= o
['[[MapNextIndex]]'],
2611 itemKind
= o
['[[MapIterationKind]]'],
2612 entries
= m
['[[MapData]]'];
2613 while (index
< entries
.keys
.length
) {
2614 var e
= {key: entries
.keys
[index
], value: entries
.values
[index
]};
2616 set_internal(o
, '[[MapNextIndex]]', index
);
2617 if (e
.key
!== empty
) {
2618 if (itemKind
=== 'key') {
2619 return CreateIterResultObject(e
.key
, false);
2620 } else if (itemKind
=== 'value') {
2621 return CreateIterResultObject(e
.value
, false);
2623 return CreateIterResultObject([e
.key
, e
.value
], false);
2627 return CreateIterResultObject(undefined, true);
2630 // 23.1.5.2.2 %MapIteratorPrototype% [ @@toStringTag ]
2631 define($MapIteratorPrototype
$, $$toStringTag
, 'Map Iterator');
2633 // 23.1.5.3 Properties of Map Iterator Instances
2636 // ---------------------------------------
2638 // ---------------------------------------
2641 // 23.2.1 The Set Constructor
2642 // 23.2.1.1 Set ( [ iterable ] )
2645 function Set(/*iterable*/) {
2646 var set = strict(this);
2647 var iterable
= arguments
[0];
2649 if (Type(set) !== 'object') throw TypeError();
2650 if ('[[SetData]]' in set) throw TypeError();
2652 if (iterable
!== undefined) {
2653 var adder
= set['add'];
2654 if (!IsCallable(adder
)) throw TypeError();
2655 var iter
= GetIterator(ToObject(iterable
));
2657 set_internal(set, '[[SetData]]', []);
2658 if (iter
=== undefined) return set;
2660 var next
= IteratorStep(iter
);
2663 var nextValue
= IteratorValue(next
);
2664 adder
.call(set, nextValue
);
2670 if (!('Set' in global
) || OVERRIDE_NATIVE_FOR_TESTING
||
2671 (function() { try { return !new global
.Set().entries().next
; } catch (_
) { return true; } }()) ||
2672 (new global
.Set([1]).size
!== 1))
2675 function SetDataIndexOf(setData
, key
) {
2678 return setData
.indexOf(key
);
2679 // Slow case for NaN
2680 for (i
= 0; i
< setData
.length
; i
+= 1)
2681 if (SameValueZero(setData
[i
], key
)) return i
;
2685 // 23.2.1.2 new Set ( ...argumentsList )
2686 // 23.2.2 Properties of the Set Constructor
2688 // 23.2.2.1 Set.prototype
2689 var $SetPrototype
$ = {};
2690 Set
.prototype = $SetPrototype
$;
2692 // 23.2.2.2 get Set [ @@species ]
2693 // 23.2.3 Properties of the Set Prototype Object
2695 // 23.2.3.1 Set.prototype.add (value )
2697 Set
.prototype, 'add',
2698 function add(value
) {
2699 var s
= strict(this);
2700 if (Type(s
) !== 'object') throw TypeError();
2701 if (!('[[SetData]]' in s
)) throw TypeError();
2702 if (s
['[[SetData]]'] === undefined) throw TypeError();
2703 if (SameValue(value
, -0)) value
= 0;
2704 var entries
= s
['[[SetData]]'];
2705 var i
= SetDataIndexOf(entries
, value
);
2706 if (i
< 0) i
= s
['[[SetData]]'].length
;
2707 s
['[[SetData]]'][i
] = value
;
2712 // 23.2.3.2 Set.prototype.clear ()
2714 Set
.prototype, 'clear',
2716 var s
= strict(this);
2717 if (Type(s
) !== 'object') throw TypeError();
2718 if (!('[[SetData]]' in s
)) throw TypeError();
2719 if (s
['[[SetData]]'] === undefined) throw TypeError();
2720 var entries
= s
['[[SetData]]'];
2725 // 23.2.3.3 Set.prototype.constructor
2726 // 23.2.3.4 Set.prototype.delete ( value )
2728 Set
.prototype, 'delete',
2729 function delete_(value
) {
2730 var s
= strict(this);
2731 if (Type(s
) !== 'object') throw TypeError();
2732 if (!('[[SetData]]' in s
)) throw TypeError();
2733 if (s
['[[SetData]]'] === undefined) throw TypeError();
2734 var entries
= s
['[[SetData]]'];
2735 var i
= SetDataIndexOf(entries
, value
);
2736 if (i
< 0) return false;
2741 // 23.2.3.5 Set.prototype.entries ( )
2743 Set
.prototype, 'entries',
2744 function entries() {
2745 var s
= strict(this);
2746 if (Type(s
) !== 'object') throw TypeError();
2747 return CreateSetIterator(s
, 'key+value');
2750 // 23.2.3.6 Set.prototype.forEach ( callbackfn [ , thisArg ] )
2752 Set
.prototype, 'forEach',
2753 function forEach(callbackfn
/*, thisArg*/) {
2754 var thisArg
= arguments
[1];
2756 var s
= strict(this);
2757 if (Type(s
) !== 'object') throw TypeError();
2758 if (!('[[SetData]]' in s
)) throw TypeError();
2759 if (s
['[[SetData]]'] === undefined) throw TypeError();
2760 var entries
= s
['[[SetData]]'];
2762 if (!IsCallable(callbackfn
)) {
2763 throw TypeError('First argument to forEach is not callable.');
2765 for (var i
= 0; i
< entries
.length
; ++i
) {
2766 if (entries
[i
] !== empty
) {
2767 callbackfn
.call(thisArg
, entries
[i
], entries
[i
], s
);
2772 // 23.2.3.7 Set.prototype.has ( value )
2774 Set
.prototype, 'has',
2776 var s
= strict(this);
2777 if (Type(s
) !== 'object') throw TypeError();
2778 if (!('[[SetData]]' in s
)) throw TypeError();
2779 if (s
['[[SetData]]'] === undefined) throw TypeError();
2780 var entries
= s
['[[SetData]]'];
2781 return SetDataIndexOf(entries
, key
) !== -1;
2784 // 23.2.3.8 Set.prototype.keys ( )
2785 // See Set.prototype.values
2787 // 23.2.3.9 get Set.prototype.size
2788 Object
.defineProperty(
2789 Set
.prototype, 'size', {
2791 var s
= strict(this);
2792 if (Type(s
) !== 'object') throw TypeError();
2793 if (!('[[SetData]]' in s
)) throw TypeError();
2794 if (s
['[[SetData]]'] === undefined) throw TypeError();
2795 var entries
= s
['[[SetData]]'];
2797 for (var i
= 0; i
< entries
.length
; ++i
) {
2798 if (entries
[i
] !== empty
)
2805 // 23.2.3.10 Set.prototype.values ( )
2807 Set
.prototype, 'values',
2809 var s
= strict(this);
2810 if (Type(s
) !== 'object') throw TypeError();
2811 return CreateSetIterator(s
, 'value');
2813 // NOTE: function name is still 'values':
2814 Set
.prototype.keys
= Set
.prototype.values
;
2816 // 23.2.3.11 Set.prototype [@@iterator ] ( )
2818 Set
.prototype, $$iterator
,
2820 var s
= strict(this);
2821 if (Type(s
) !== 'object') throw TypeError();
2822 return CreateSetIterator(s
);
2825 // 23.2.3.12 Set.prototype [ @@toStringTag ]
2826 define(global
.Set
.prototype, $$toStringTag
, 'Set');
2828 // 23.2.4 Properties of Set Instances
2829 // 23.2.5 Set Iterator Objects
2831 function SetIterator() {}
2833 // 23.2.5.1 CreateSetIterator Abstract Operation
2834 function CreateSetIterator(set, kind
) {
2835 if (Type(set) !== 'object') throw TypeError();
2836 if (!('[[SetData]]' in set)) throw TypeError();
2837 if (set['[[SetData]]'] === undefined) throw TypeError();
2838 var iterator
= new SetIterator
;
2839 set_internal(iterator
, '[[IteratedSet]]', set);
2840 set_internal(iterator
, '[[SetNextIndex]]', 0);
2841 set_internal(iterator
, '[[SetIterationKind]]', kind
);
2845 // 23.2.5.2 The %SetIteratorPrototype% Object
2846 var $SetIteratorPrototype
$ = Object
.create($IteratorPrototype
$);
2847 SetIterator
.prototype = $SetIteratorPrototype
$;
2849 // 23.2.5.2.1 %SetIteratorPrototype%.next( )
2851 $SetIteratorPrototype
$, 'next',
2853 var o
= strict(this);
2854 if (Type(o
) !== 'object') throw TypeError();
2855 var s
= o
['[[IteratedSet]]'],
2856 index
= o
['[[SetNextIndex]]'],
2857 itemKind
= o
['[[SetIterationKind]]'],
2858 entries
= s
['[[SetData]]'];
2859 while (index
< entries
.length
) {
2860 var e
= entries
[index
];
2862 set_internal(o
, '[[SetNextIndex]]', index
);
2864 if (itemKind
=== 'key+value')
2865 return CreateIterResultObject([e
, e
], false);
2866 return CreateIterResultObject(e
, false);
2869 return CreateIterResultObject(undefined, true);
2872 // 23.2.5.2.2 %SetIteratorPrototype% [ @@toStringTag ]
2873 define($SetIteratorPrototype
$, $$toStringTag
, 'Set Iterator');
2875 // 23.2.5.3 Properties of Set Iterator Instances
2879 // ---------------------------------------
2880 // 23.3 WeakMap Objects
2881 // ---------------------------------------
2884 // 23.3.1 The WeakMap Constructor
2885 // 23.3.1.1 WeakMap ( [ iterable ] )
2887 function WeakMap(/*iterable*/) {
2888 var map
= strict(this);
2889 var iterable
= arguments
[0];
2891 if (Type(map
) !== 'object') throw TypeError();
2892 if ('[[WeakMapData]]' in map
) throw TypeError();
2894 if (iterable
!== undefined) {
2895 var adder
= map
['set'];
2896 if (!IsCallable(adder
)) throw TypeError();
2897 var iter
= GetIterator(ToObject(iterable
));
2899 set_internal(map
, '[[WeakMapData]]', new EphemeronTable
);
2900 if (iter
=== undefined) return map
;
2902 var next
= IteratorStep(iter
);
2905 var nextValue
= IteratorValue(next
);
2906 if (Type(nextValue
) !== 'object') throw TypeError();
2907 var k
= nextValue
[0];
2908 var v
= nextValue
[1];
2909 adder
.call(map
, k
, v
);
2915 if (!('WeakMap' in global
) || OVERRIDE_NATIVE_FOR_TESTING
)
2916 global
.WeakMap
= WeakMap
;
2918 // 23.3.2 Properties of the WeakMap Constructor
2919 // 23.3.2.1 WeakMap.prototype
2920 var $WeakMapPrototype
$ = {};
2921 WeakMap
.prototype = $WeakMapPrototype
$;
2925 // 23.3.2.2 WeakMap[ @@create ] ( )
2926 // 23.3.3 Properties of the WeakMap Prototype Object
2928 // 23.3.3.1 WeakMap.prototype.constructor
2930 // 23.3.3.2 WeakMap.prototype.delete ( key )
2932 WeakMap
.prototype, 'delete',
2933 function delete_(key
) {
2934 var M
= strict(this);
2935 if (Type(M
) !== 'object') throw TypeError();
2936 if (M
['[[WeakMapData]]'] === undefined) throw TypeError();
2937 if (Type(key
) !== 'object') throw TypeError('Expected object');
2938 return M
['[[WeakMapData]]'].remove(key
);
2941 // 23.3.3.3 WeakMap.prototype.get ( key )
2943 WeakMap
.prototype, 'get',
2944 function get(key
, defaultValue
) {
2945 var M
= strict(this);
2946 if (Type(M
) !== 'object') throw TypeError();
2947 if (M
['[[WeakMapData]]'] === undefined) throw TypeError();
2948 if (Type(key
) !== 'object') throw TypeError('Expected object');
2949 return M
['[[WeakMapData]]'].get(key
, defaultValue
);
2952 // 23.3.3.4 WeakMap.prototype.has ( key )
2954 WeakMap
.prototype, 'has',
2956 var M
= strict(this);
2957 if (Type(M
) !== 'object') throw TypeError();
2958 if (M
['[[WeakMapData]]'] === undefined) throw TypeError();
2959 if (Type(key
) !== 'object') throw TypeError('Expected object');
2960 return M
['[[WeakMapData]]'].has(key
);
2963 // 23.3.3.5 WeakMap.prototype.set ( key , value )
2965 WeakMap
.prototype, 'set',
2966 function set(key
, value
) {
2967 var M
= strict(this);
2968 if (Type(M
) !== 'object') throw TypeError();
2969 if (M
['[[WeakMapData]]'] === undefined) throw TypeError();
2970 if (Type(key
) !== 'object') throw TypeError('Expected object');
2971 M
['[[WeakMapData]]'].set(key
, value
);
2975 // 23.3.3.6 WeakMap.prototype [ @@toStringTag ]
2976 define(global
.WeakMap
.prototype, $$toStringTag
, 'WeakMap');
2978 // 23.3.4 Properties of WeakMap Instances
2980 // Polyfills for incomplete native implementations:
2982 var wm
= new global
.WeakMap();
2983 var orig
= global
.WeakMap
.prototype.set;
2984 define(global
.WeakMap
.prototype, 'set', function set() {
2985 orig
.apply(this, arguments
);
2987 }, wm
.set({}, 0) !== wm
);
2991 // ---------------------------------------
2992 // 23.4 WeakSet Objects
2993 // ---------------------------------------
2996 // 23.4.1 The WeakSet Constructor
2997 // 23.4.1.1 WeakSet ( [ iterable ] )
2999 function WeakSet(/*iterable*/) {
3000 var set = strict(this);
3001 var iterable
= arguments
[0];
3003 if (Type(set) !== 'object') throw TypeError();
3004 if ('[[WeakSetData]]' in set) throw TypeError();
3006 if (iterable
!== undefined) {
3007 var adder
= set['add'];
3008 if (!IsCallable(adder
)) throw TypeError();
3009 var iter
= GetIterator(ToObject(iterable
));
3011 set_internal(set, '[[WeakSetData]]', new EphemeronTable
);
3012 if (iter
=== undefined) return set;
3014 var next
= IteratorStep(iter
);
3017 var nextValue
= IteratorValue(next
);
3018 adder
.call(set, nextValue
);
3024 if (!('WeakSet' in global
) || OVERRIDE_NATIVE_FOR_TESTING
)
3025 global
.WeakSet
= WeakSet
;
3027 // 23.4.2 Properties of the WeakSet Constructor
3028 // 23.4.2.1 WeakSet.prototype
3029 var $WeakSetPrototype
$ = {};
3030 WeakSet
.prototype = $WeakSetPrototype
$;
3032 // 23.4.3 Properties of the WeakSet Prototype Object
3033 // 23.4.3.1 WeakSet.prototype.add (value )
3035 WeakSet
.prototype, 'add',
3036 function add(value
) {
3037 var S
= strict(this);
3038 if (Type(S
) !== 'object') throw TypeError();
3039 if (S
['[[WeakSetData]]'] === undefined) throw TypeError();
3040 if (Type(value
) !== 'object') throw TypeError('Expected object');
3041 S
['[[WeakSetData]]'].set(value
, true);
3045 // 23.4.3.2 WeakSet.prototype.constructor
3046 // 23.4.3.3 WeakSet.prototype.delete ( value )
3048 WeakSet
.prototype, 'delete',
3049 function delete_(value
) {
3050 var S
= strict(this);
3051 if (Type(S
) !== 'object') throw TypeError();
3052 if (S
['[[WeakSetData]]'] === undefined) throw TypeError();
3053 if (Type(value
) !== 'object') throw TypeError('Expected object');
3054 return S
['[[WeakSetData]]'].remove(value
);
3057 // 23.4.3.4 WeakSet.prototype.has ( value )
3059 WeakSet
.prototype, 'has',
3061 var S
= strict(this);
3062 if (Type(S
) !== 'object') throw TypeError();
3063 if (S
['[[WeakSetData]]'] === undefined) throw TypeError();
3064 if (Type(key
) !== 'object') throw TypeError('Expected object');
3065 return S
['[[WeakSetData]]'].has(key
);
3068 // 23.4.3.5 WeakSet.prototype [ @@toStringTag ]
3069 define(global
.WeakSet
.prototype, $$toStringTag
, 'WeakSet');
3071 // 23.4.4 Properties of WeakSet Instances
3073 // Polyfills for incomplete native implementations:
3075 var ws
= new global
.WeakSet();
3076 var orig
= global
.WeakSet
.prototype.add
;
3077 define(global
.WeakSet
.prototype, 'add', function add() {
3078 orig
.apply(this, arguments
);
3080 }, ws
.add({}) !== ws
);
3084 // ---------------------------------------
3085 // 24 Structured Data
3086 // ---------------------------------------
3088 // ---------------------------------------
3089 // 24.1 ArrayBuffer Objects
3090 // ---------------------------------------
3092 // See typedarray.js for TypedArray polyfill
3095 if (!('ArrayBuffer' in global
))
3098 // 24.1.1 Abstract Operations For ArrayBuffer Objects
3099 // 24.1.1.1 AllocateArrayBuffer( constructor, byteLength )
3100 // 24.1.1.2 IsDetachedBuffer( arrayBuffer )
3101 // 24.1.1.3 DetachArrayBuffer( arrayBuffer )
3102 // 24.1.1.4 CloneArrayBuffer( srcBuffer, srcByteOffset [, cloneConstructor] )
3103 // 24.1.1.5 GetValueFromBuffer ( arrayBuffer, byteIndex, type, isLittleEndian )
3104 // 24.1.1.6 SetValueInBuffer ( arrayBuffer, byteIndex, type, value, isLittleEndian )
3105 // 24.1.2 The ArrayBuffer Constructor
3106 // 24.1.2.1 ArrayBuffer( length )
3107 // 24.1.3 Properties of the ArrayBuffer Constructor
3109 // 24.1.3.1 ArrayBuffer.isView ( arg )
3111 ArrayBuffer
, 'isView',
3112 function isView(arg
) {
3113 if (Type(arg
) !== 'object') return false;
3114 if ('buffer' in arg
&& arg
.buffer
instanceof ArrayBuffer
) return true;
3118 // 24.1.3.2 ArrayBuffer.prototype
3119 // 24.1.3.3 get ArrayBuffer [ @@species ]
3120 // 24.1.4 Properties of the ArrayBuffer Prototype Object
3121 // 24.1.4.1 get ArrayBuffer.prototype.byteLength
3122 // 24.1.4.2 ArrayBuffer.prototype.constructor
3123 // 24.1.4.3 ArrayBuffer.prototype.slice ( start , end)
3125 // 24.1.4.4 ArrayBuffer.prototype [ @@toStringTag ]
3126 define(ArrayBuffer
.prototype, $$toStringTag
, 'ArrayBuffer');
3128 // 24.1.5 Properties of the ArrayBuffer Instances
3131 // ---------------------------------------
3132 // 24.2 DataView Objects
3133 // ---------------------------------------
3135 // See typedarray.js for TypedArray polyfill
3138 if (!('DataView' in global
))
3141 // 24.2.1 Abstract Operations For DataView Objects
3142 // 24.2.1.1 GetViewValue(view, requestIndex, isLittleEndian, type)
3143 // 24.2.1.2 SetViewValue(view, requestIndex, isLittleEndian, type, value)
3144 // 24.2.2 The DataView Constructor
3145 // 24.2.2.1 DataView (buffer [ , byteOffset [ , byteLength ] ] )
3146 // 24.2.3 Properties of the DataView Constructor
3147 // 24.2.3.1 DataView.prototype
3148 // 24.2.4 Properties of the DataView Prototype Object
3149 // 24.2.4.1 get DataView.prototype.buffer
3150 // 24.2.4.2 get DataView.prototype.byteLength
3151 // 24.2.4.3 get DataView.prototype.byteOffset
3152 // 24.2.4.4 DataView.prototype.constructor
3153 // 24.2.4.5 DataView.prototype.getFloat32 ( byteOffset [ , littleEndian ] )
3154 // 24.2.4.6 DataView.prototype.getFloat64 ( byteOffset [ , littleEndian ] )
3155 // 24.2.4.7 DataView.prototype.getInt8 ( byteOffset )
3156 // 24.2.4.8 DataView.prototype.getInt16 ( byteOffset [ , littleEndian ] )
3157 // 24.2.4.9 DataView.prototype.getInt32 ( byteOffset [ , littleEndian ] )
3158 // 24.2.4.10 DataView.prototype.getUint8 ( byteOffset )
3159 // 24.2.4.11 DataView.prototype.getUint16 ( byteOffset [ , littleEndian ] )
3160 // 24.2.4.12 DataView.prototype.getUint32 ( byteOffset [ , littleEndian ] )
3161 // 24.2.4.13 DataView.prototype.setFloat32 ( byteOffset, value [ , littleEndian ] )
3162 // 24.2.4.14 DataView.prototype.setFloat64 ( byteOffset, value [ , littleEndian ] )
3163 // 24.2.4.15 DataView.prototype.setInt8 ( byteOffset, value )
3164 // 24.2.4.16 DataView.prototype.setInt16 ( byteOffset, value [ , littleEndian ] )
3165 // 24.2.4.17 DataView.prototype.setInt32 ( byteOffset, value [ , littleEndian ] )
3166 // 24.2.4.18 DataView.prototype.setUint8 ( byteOffset, value )
3167 // 24.2.4.19 DataView.prototype.setUint16 ( byteOffset, value [ , littleEndian ] )
3168 // 24.2.4.20 DataView.prototype.setUint32 ( byteOffset, value [ , littleEndian ] )
3170 // 24.2.4.21 DataView.prototype[ @@toStringTag ]
3171 define(DataView
.prototype, $$toStringTag
, 'DataView');
3173 // 24.2.5 Properties of DataView Instances
3176 // ---------------------------------------
3177 // 24.3 The JSON Object
3178 // ---------------------------------------
3180 // 24.3.1 JSON.parse ( text [ , reviver ] )
3181 // 24.3.2 JSON.stringify ( value [ , replacer [ , space ] ] )
3182 // 24.3.3 JSON [ @@toStringTag ]
3183 define(JSON
, $$toStringTag
, 'JSON');
3185 // ---------------------------------------
3187 // ---------------------------------------
3189 // 25.1.1 Common Iteration Interfaces
3190 // 25.1.1.1 The Iterable Interface
3191 // 25.1.1.2 The Iterator Interface
3192 // 25.1.1.3 The IteratorResult Interface
3194 // 25.1.2 The %IteratorPrototype% Object
3195 // Defined earlier, so other prototypes can reference it.
3196 // 25.1.2.1 %IteratorPrototype% [ @@iterator ] ( )
3197 define($IteratorPrototype
$, $$iterator
, function() {
3202 // ---------------------------------------
3203 // 25.4 Promise Objects
3204 // ---------------------------------------
3207 // 25.4 Promise Objects
3209 // 25.4.1 Promise Abstract Operations
3211 // 25.4.1.1 PromiseCapability Records
3212 // 25.4.1.1.1 IfAbruptRejectPromise ( value, capability )
3214 function IfAbruptRejectPromise(value
, capability
) {
3215 var rejectResult
= capability
['[[Reject]]'].call(undefined, value
);
3216 return capability
['[[Promise]]'];
3219 // 25.4.1.2 PromiseReaction Records
3221 // 25.4.1.3 CreateResolvingFunctions ( promise )
3223 function CreateResolvingFunctions(promise
) {
3224 var alreadyResolved
= {'[[value]]': false};
3225 var resolve
= PromiseResolveFunction();
3226 set_internal(resolve
, '[[Promise]]', promise
);
3227 set_internal(resolve
, '[[AlreadyResolved]]', alreadyResolved
);
3228 var reject
= PromiseRejectFunction();
3229 set_internal(reject
, '[[Promise]]', promise
);
3230 set_internal(reject
, '[[AlreadyResolved]]', alreadyResolved
);
3231 return { '[[Resolve]]': resolve
, '[[Reject]]': reject
};
3234 // 25.4.1.3.1 Promise Reject Functions
3236 function PromiseRejectFunction() {
3237 var F = function(reason
) {
3238 console
.assert(Type(F
['[[Promise]]']) === 'object');
3239 var promise
= F
['[[Promise]]'];
3240 var alreadyResolved
= F
['[[AlreadyResolved]]'];
3241 if (alreadyResolved
['[[value]]']) return undefined;
3242 set_internal(alreadyResolved
, '[[value]]', true);
3243 return RejectPromise(promise
, reason
);
3248 // 25.4.1.3.2 Promise Resolve Functions
3250 function PromiseResolveFunction() {
3251 var F = function(resolution
) {
3252 console
.assert(Type(F
['[[Promise]]']) === 'object');
3253 var promise
= F
['[[Promise]]'];
3254 var alreadyResolved
= F
['[[AlreadyResolved]]'];
3255 if (alreadyResolved
['[[value]]']) return undefined;
3256 set_internal(alreadyResolved
, '[[value]]', true);
3258 if (SameValue(resolution
, promise
)) {
3259 var selfResolutionError
= TypeError();
3260 return RejectPromise(promise
, selfResolutionError
);
3262 if (Type(resolution
) !== 'object')
3263 return FulfillPromise(promise
, resolution
);
3265 var then
= resolution
['then'];
3267 return RejectPromise(promise
, then
);
3269 if (!IsCallable(then
))
3270 return FulfillPromise(promise
, resolution
);
3271 EnqueueJob('PromiseJobs', PromiseResolveThenableJob
, [promise
, resolution
, then
]);
3277 // 25.4.1.4 FulfillPromise ( promise, value )
3279 function FulfillPromise(promise
, value
) {
3280 console
.assert(promise
['[[PromiseState]]'] === 'pending');
3281 var reactions
= promise
['[[PromiseFulfillReactions]]'];
3282 set_internal(promise
, '[[PromiseResult]]', value
);
3283 set_internal(promise
, '[[PromiseFulfillReactions]]', undefined);
3284 set_internal(promise
, '[[PromiseRejectReactions]]', undefined);
3285 set_internal(promise
, '[[PromiseState]]', 'fulfilled');
3286 return TriggerPromiseReactions(reactions
, value
);
3289 // 25.4.1.5 NewPromiseCapability ( C )
3291 function NewPromiseCapability(c
) {
3292 // To keep Promise hermetic, this doesn't look much like the spec.
3293 return CreatePromiseCapabilityRecord(undefined, c
);
3296 // 25.4.1.5.1 CreatePromiseCapabilityRecord ( promise, constructor )
3298 function CreatePromiseCapabilityRecord(promise
, constructor) {
3299 // To keep Promise hermetic, this doesn't look much like the spec.
3300 console
.assert(IsConstructor(constructor));
3301 var promiseCapability
= {};
3302 set_internal(promiseCapability
, '[[Promise]]', promise
);
3303 set_internal(promiseCapability
, '[[Resolve]]', undefined);
3304 set_internal(promiseCapability
, '[[Reject]]', undefined);
3305 var executor
= GetCapabilitiesExecutor();
3306 set_internal(executor
, '[[Capability]]', promiseCapability
);
3308 // NOTE: Differs from spec; object is constructed here
3309 var constructorResult
= promise
= new constructor(executor
);
3310 set_internal(promiseCapability
, '[[Promise]]', promise
);
3312 if (!IsCallable(promiseCapability
['[[Resolve]]'])) throw TypeError();
3313 if (!IsCallable(promiseCapability
['[[Reject]]'])) throw TypeError();
3314 if (Type(constructorResult
) === 'object' && !SameValue(promise
, constructorResult
)) throw TypeError();
3315 return promiseCapability
;
3318 // 25.4.1.5.2 GetCapabilitiesExecutor Functions
3320 function GetCapabilitiesExecutor() {
3321 var F = function(resolve
, reject
) {
3322 console
.assert(F
['[[Capability]]']);
3323 var promiseCapability
= F
['[[Capability]]'];
3324 if (promiseCapability
['[[Resolve]]'] !== undefined) throw TypeError();
3325 if (promiseCapability
['[[Reject]]'] !== undefined) throw TypeError();
3326 set_internal(promiseCapability
, '[[Resolve]]', resolve
);
3327 set_internal(promiseCapability
, '[[Reject]]', reject
);
3333 // 25.4.1.6 IsPromise ( x )
3335 function IsPromise(x
) {
3336 if (Type(x
) !== 'object') return false;
3337 if (!('[[PromiseState]]' in x
)) return false;
3338 if (x
['[[PromiseState]]'] === undefined) return false;
3342 // 25.4.1.7 RejectPromise ( promise, reason )
3344 function RejectPromise(promise
, reason
) {
3345 console
.assert(promise
['[[PromiseState]]'] === 'pending');
3346 var reactions
= promise
['[[PromiseRejectReactions]]'];
3347 set_internal(promise
, '[[PromiseResult]]', reason
);
3348 set_internal(promise
, '[[PromiseFulfillReactions]]', undefined);
3349 set_internal(promise
, '[[PromiseRejectReactions]]', undefined);
3350 set_internal(promise
, '[[PromiseState]]', 'rejected');
3351 return TriggerPromiseReactions(reactions
, reason
);
3354 // 25.4.1.8 TriggerPromiseReactions ( reactions, argument )
3356 function TriggerPromiseReactions(reactions
, argument
) {
3357 for (var i
= 0, len
= reactions
.length
; i
< len
; ++i
)
3358 EnqueueJob('PromiseJobs', PromiseReactionJob
, [reactions
[i
], argument
]);
3362 // 25.4.2 Promise Jobs
3364 // 25.4.2.1 PromiseReactionJob ( reaction, argument )
3366 function PromiseReactionJob(reaction
, argument
) {
3367 var promiseCapability
= reaction
['[[Capabilities]]'];
3368 var handler
= reaction
['[[Handler]]'];
3369 var handlerResult
, status
;
3371 if (handler
=== 'Identity') handlerResult
= argument
;
3372 else if (handler
=== 'Thrower') throw argument
;
3373 else handlerResult
= handler
.call(undefined, argument
);
3374 } catch (handlerResult
) {
3375 status
= promiseCapability
['[[Reject]]'].call(undefined, handlerResult
);
3376 NextJob(status
); return;
3378 status
= promiseCapability
['[[Resolve]]'].call(undefined, handlerResult
);
3382 // 25.4.2.2 PromiseResolveThenableJob ( promiseToResolve, thenable, then)
3384 function PromiseResolveThenableJob(promiseToResolve
, thenable
, then
) {
3385 // SPEC BUG: promise vs. promiseToResolve
3386 var resolvingFunctions
= CreateResolvingFunctions(promiseToResolve
);
3388 var thenCallResult
= then
.call(thenable
, resolvingFunctions
['[[Resolve]]'],
3389 resolvingFunctions
['[[Reject]]']);
3390 } catch (thenCallResult
) {
3391 var status
= resolvingFunctions
['[[Reject]]'].call(undefined, thenCallResult
);
3392 NextJob(status
); return;
3394 NextJob(thenCallResult
);
3397 // 25.4.3 The Promise Constructor
3399 // 25.4.3.1 Promise ( executor )
3401 function Promise(executor
) {
3402 var config
= { configurable: false, enumerable: false, writable: true, value: undefined };
3403 Object
.defineProperty(this, '[[PromiseState]]', config
);
3404 Object
.defineProperty(this, '[[PromiseConstructor]]', config
);
3405 Object
.defineProperty(this, '[[PromiseResult]]', config
);
3406 Object
.defineProperty(this, '[[PromiseFulfillReactions]]', config
);
3407 Object
.defineProperty(this, '[[PromiseRejectReactions]]', config
);
3410 if (Type(promise
) !== 'object') throw new TypeError();
3411 if (!('[[PromiseState]]' in promise
)) throw TypeError();
3412 if (promise
['[[PromiseState]]'] !== undefined) throw TypeError();
3413 if (!IsCallable(executor
)) throw TypeError();
3415 set_internal(promise
, '[[PromiseConstructor]]', Promise
);
3417 return InitializePromise(promise
, executor
);
3420 // 25.4.3.1.1 InitializePromise ( promise, executor )
3422 function InitializePromise(promise
, executor
) {
3423 console
.assert('[[PromiseState]]' in promise
);
3424 console
.assert(IsCallable(executor
));
3425 set_internal(promise
, '[[PromiseState]]', 'pending');
3426 set_internal(promise
, '[[PromiseFulfillReactions]]', []);
3427 set_internal(promise
, '[[PromiseRejectReactions]]', []);
3428 var resolvingFunctions
= CreateResolvingFunctions(promise
);
3430 var completion
= executor
.call(undefined, resolvingFunctions
['[[Resolve]]'],
3431 resolvingFunctions
['[[Reject]]']);
3432 } catch (completion
) {
3433 var status
= resolvingFunctions
['[[Reject]]'].call(undefined, completion
);
3438 // 25.4.4 Properties of the Promise Constructor
3439 // 25.4.4.1 Promise.all ( iterable )
3441 define(Promise
, 'all', function all(iterable
) {
3442 var c
= strict(this);
3443 var promiseCapability
= NewPromiseCapability(c
);
3445 var iterator
= GetIterator(iterable
);
3447 promiseCapability
['[[Reject]]'].call(undefined, value
);
3448 return promiseCapability
['[[Promise]]'];
3451 var remainingElementsCount
= { value: 1 };
3455 var next
= IteratorStep(iterator
);
3457 promiseCapability
['[[Reject]]'].call(undefined, value
);
3458 return promiseCapability
['[[Promise]]'];
3461 remainingElementsCount
.value
-= 1;
3462 if (remainingElementsCount
.value
=== 0) {
3463 var resolveResult
= promiseCapability
['[[Resolve]]'].apply(undefined, values
);
3467 return promiseCapability
['[[Promise]]'];
3470 var nextValue
= IteratorValue(next
);
3472 promiseCapability
['[[Reject]]'].call(undefined, value
);
3473 return promiseCapability
['[[Promise]]'];
3476 var nextPromise
= c
.resolve(nextValue
);
3478 promiseCapability
['[[Reject]]'].call(undefined, value
);
3479 return promiseCapability
['[[Promise]]'];
3481 var resolveElement
= PromiseAllResolveElementFunction();
3482 set_internal(resolveElement
, '[[AlreadyCalled]]', { value: false });
3483 set_internal(resolveElement
, '[[Index]]', index
);
3484 set_internal(resolveElement
, '[[Values]]', values
);
3485 set_internal(resolveElement
, '[[Capabilities]]', promiseCapability
);
3486 set_internal(resolveElement
, '[[RemainingElements]]', remainingElementsCount
);
3487 remainingElementsCount
.value
+= 1;
3489 var result
= nextPromise
.then(resolveElement
, promiseCapability
['[[Reject]]']);
3491 promiseCapability
['[[Reject]]'].call(undefined, value
);
3492 return promiseCapability
['[[Promise]]'];
3498 // 25.4.4.1.1 Promise.all Resolve Element Functions
3500 function PromiseAllResolveElementFunction() {
3501 var F = function(x
) {
3502 var alreadyCalled
= F
['[[AlreadyCalled]]'];
3503 if (alreadyCalled
.value
) return undefined;
3504 alreadyCalled
.value
= true;
3505 var index
= F
['[[Index]]'];
3506 var values
= F
['[[Values]]'];
3507 var promiseCapability
= F
['[[Capabilities]]'];
3508 var remainingElementsCount
= F
['[[RemainingElements]]'];
3512 promiseCapability
['[[Reject]]'].call(undefined, result
);
3513 return promiseCapability
['[[Promise]]'];
3515 remainingElementsCount
.value
-= 1;
3516 if (remainingElementsCount
.value
=== 0)
3517 return promiseCapability
['[[Resolve]]'].call(undefined, values
);
3523 // 25.4.4.2 Promise.prototype
3525 Promise
.prototype = {};
3527 // 25.4.4.3 Promise.race ( iterable )
3529 define(Promise
, 'race', function race(iterable
) {
3530 var c
= strict(this);
3531 var promiseCapability
= NewPromiseCapability(c
);
3533 var iterator
= GetIterator(iterable
);
3535 promiseCapability
['[[Reject]]'].call(undefined, value
);
3536 return promiseCapability
['[[Promise]]'];
3540 var next
= IteratorStep(iterator
);
3542 promiseCapability
['[[Reject]]'].call(undefined, value
);
3543 return promiseCapability
['[[Promise]]'];
3545 if (!next
) return promiseCapability
['[[Promise]]'];
3547 var nextValue
= IteratorValue(next
);
3549 promiseCapability
['[[Reject]]'].call(undefined, value
);
3550 return promiseCapability
['[[Promise]]'];
3553 var nextPromise
= c
.resolve(nextValue
);
3555 promiseCapability
['[[Reject]]'].call(undefined, value
);
3556 return promiseCapability
['[[Promise]]'];
3559 nextPromise
.then(promiseCapability
['[[Resolve]]'], promiseCapability
['[[Reject]]']);
3561 promiseCapability
['[[Reject]]'].call(undefined, value
);
3562 return promiseCapability
['[[Promise]]'];
3567 // 25.4.4.4 Promise.reject ( r )
3569 define(Promise
, 'reject', function reject(r
) {
3570 var c
= strict(this);
3571 var promiseCapability
= NewPromiseCapability(c
);
3572 var rejectResult
= promiseCapability
['[[Reject]]'].call(undefined, r
);
3573 return promiseCapability
['[[Promise]]'];
3576 // 25.4.4.5 Promise.resolve ( x )
3578 define(Promise
, 'resolve', function resolve(x
) {
3579 var c
= strict(this);
3581 var constructor = x
['[[PromiseConstructor]]'];
3582 if (SameValue(constructor, c
)) return x
;
3584 var promiseCapability
= NewPromiseCapability(c
);
3585 var resolveResult
= promiseCapability
['[[Resolve]]'].call(undefined, x
);
3586 return promiseCapability
['[[Promise]]'];
3589 // 25.4.4.6 Promise [ @@create ] ( )
3590 // 25.4.4.6.1 AllocatePromise ( constructor )
3591 // 25.4.5 Properties of the Promise Prototype Object
3592 // 25.4.5.1 Promise.prototype.catch ( onRejected )
3594 define(Promise
.prototype, 'catch', function catch_(onRejected
) {
3596 return promise
.then(undefined, onRejected
);
3599 // 25.4.5.2 Promise.prototype.constructor
3601 Promise
.prototype.constructor = Promise
;
3603 // 25.4.5.3 Promise.prototype.then ( onFulfilled , onRejected )
3605 define(Promise
.prototype, 'then', function then(onFulfilled
, onRejected
) {
3607 if (!IsPromise(promise
)) throw TypeError();
3608 if (!IsCallable(onFulfilled
)) onFulfilled
= 'Identity';
3609 if (!IsCallable(onRejected
)) onRejected
= 'Thrower';
3610 var c
= promise
.constructor;
3611 var promiseCapability
= NewPromiseCapability(c
);
3612 var fulfillReaction
= { '[[Capabilities]]': promiseCapability
,
3613 '[[Handler]]': onFulfilled
};
3614 var rejectReaction
= { '[[Capabilities]]': promiseCapability
,
3615 '[[Handler]]': onRejected
};
3616 if (promise
['[[PromiseState]]'] === 'pending') {
3617 promise
['[[PromiseFulfillReactions]]'].push(fulfillReaction
);
3618 promise
['[[PromiseRejectReactions]]'].push(rejectReaction
);
3619 } else if (promise
['[[PromiseState]]'] === 'fulfilled') {
3620 var value
= promise
['[[PromiseResult]]'];
3621 EnqueueJob('PromiseJobs', PromiseReactionJob
, [fulfillReaction
, value
]);
3622 } else if (promise
['[[PromiseState]]'] === 'rejected') {
3623 var reason
= promise
['[[PromiseResult]]'];
3624 EnqueueJob('PromiseJobs', PromiseReactionJob
, [rejectReaction
, reason
]);
3626 return promiseCapability
['[[Promise]]'];
3629 // 25.4.6 Properties of Promise Instances
3631 if (!('Promise' in global
) || OVERRIDE_NATIVE_FOR_TESTING
)
3632 global
.Promise
= Promise
;
3634 // Patch early Promise.cast vs. Promise.resolve implementations
3635 if ('cast' in global
.Promise
) global
.Promise
.resolve
= global
.Promise
.cast
;
3638 // 25.4.5.1 Promise.prototype [ @@toStringTag ]
3639 define(Promise
.prototype, $$toStringTag
, 'Promise');
3641 // ---------------------------------------
3643 // ---------------------------------------
3646 // 26.1 The Reflect Object
3647 if (!('Reflect' in global
) || OVERRIDE_NATIVE_FOR_TESTING
)
3648 global
.Reflect
= {};
3650 // 26.1.1 Reflect.apply ( target, thisArgument, argumentsList )
3653 function apply(target
, thisArgument
, argumentsList
) {
3654 if (!IsCallable(target
)) throw TypeError();
3655 return Function
.prototype.apply
.call(target
, thisArgument
, argumentsList
);
3658 // 26.1.2 Reflect.construct ( target, argumentsList [, newTarget] )
3660 Reflect
, 'construct',
3661 function construct(target
, argumentsList
) {
3662 return __cons(target
, argumentsList
);
3665 // 26.1.3 Reflect.defineProperty ( target, propertyKey, attributes )
3667 Reflect
, 'defineProperty',
3668 function defineProperty(target
, propertyKey
, attributes
) {
3670 Object
.defineProperty(target
, propertyKey
, attributes
);
3677 // 26.1.4 Reflect.deleteProperty ( target, propertyKey )
3679 Reflect
, 'deleteProperty',
3680 function deleteProperty(target
,name
) {
3682 delete target
[name
];
3683 return !HasOwnProperty(target
, name
);
3689 // 26.1.5 Reflect.enumerate ( target )
3691 Reflect
, 'enumerate',
3692 function enumerate(target
) {
3693 target
= ToObject(target
);
3694 var iterator
= Enumerate(target
);
3698 // 26.1.6 Reflect.get ( target, propertyKey [ , receiver ])
3701 function get(target
, name
, receiver
) {
3702 target
= ToObject(target
);
3703 name
= String(name
);
3704 receiver
= (receiver
=== undefined) ? target : ToObject(receiver
);
3705 var desc
= getPropertyDescriptor(target
, name
);
3706 if (desc
&& 'get' in desc
)
3707 return Function
.prototype.call
.call(desc
['get'], receiver
);
3708 return target
[name
];
3711 // 26.1.7 Reflect.getOwnPropertyDescriptor ( target, propertyKey )
3713 Reflect
, 'getOwnPropertyDescriptor',
3714 Object
.getOwnPropertyDescriptor
);
3716 // 26.1.8 Reflect.getPrototypeOf ( target )
3718 Reflect
, 'getPrototypeOf',
3719 Object
.getPrototypeOf
);
3721 // 26.1.9 Reflect.has ( target, propertyKey )
3724 function has(target
,name
) {
3725 return String(name
) in ToObject(target
);
3728 // 26.1.10 Reflect.isExtensible (target)
3730 Reflect
, 'isExtensible',
3731 Object
.isExtensible
);
3733 // 26.1.11 Reflect.ownKeys ( target )
3736 function ownKeys(target
) {
3737 var obj
= ToObject(target
);
3738 return Object
.getOwnPropertyNames(obj
);
3741 // 26.1.12 Reflect.preventExtensions ( target )
3743 Reflect
, 'preventExtensions',
3744 function preventExtensions(target
) {
3745 try { Object
.preventExtensions(target
); return true; } catch (_
) { return false; }
3748 // 26.1.13 Reflect.set ( target, propertyKey, V [ , receiver ] )
3751 function set(target
, name
, value
, receiver
) {
3752 target
= ToObject(target
);
3753 name
= String(name
);
3754 receiver
= (receiver
=== undefined) ? target : ToObject(receiver
);
3755 var desc
= getPropertyDescriptor(target
, name
);
3757 if (desc
&& 'set' in desc
)
3758 Function
.prototype.call
.call(desc
['set'], receiver
, value
);
3760 target
[name
] = value
;
3767 // 26.1.14 Reflect.setPrototypeOf ( target, proto )
3769 Reflect
, 'setPrototypeOf',
3770 function setPrototypeOf(target
, proto
) {
3772 target
.__proto__
= proto
;
3773 return Reflect
.getPrototypeOf(target
) === proto
;
3781 // ---------------------------------------
3782 // 26.2 Proxy Objects
3783 // ---------------------------------------
3785 // Not polyfillable.
3789 // This helper is defined outside the main scope so that the use of
3790 // 'eval' does not taint the scope for minifiers.
3791 function __cons(t
, a
) {
3792 return eval('new t(' + a
.map(function(_
, i
) { return 'a[' + i
+ ']'; }).join(',') + ')');