diff options
Diffstat (limited to 'src/js/polyfill.es6.js')
-rw-r--r-- | src/js/polyfill.es6.js | 3793 |
1 files changed, 3793 insertions, 0 deletions
diff --git a/src/js/polyfill.es6.js b/src/js/polyfill.es6.js new file mode 100644 index 0000000..be05249 --- /dev/null +++ b/src/js/polyfill.es6.js | |||
@@ -0,0 +1,3793 @@ | |||
1 | // From | ||
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 | ||
7 | |||
8 | //---------------------------------------------------------------------- | ||
9 | // | ||
10 | // ECMAScript 2015 Polyfills | ||
11 | // | ||
12 | //---------------------------------------------------------------------- | ||
13 | |||
14 | (function (global) { | ||
15 | "use strict"; | ||
16 | |||
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; | ||
20 | |||
21 | var undefined = (void 0); // Paranoia | ||
22 | |||
23 | // Helpers | ||
24 | |||
25 | function strict(o) { | ||
26 | return o === global ? undefined : o; | ||
27 | } | ||
28 | |||
29 | function hook(o, p, f) { | ||
30 | var op = o[p]; | ||
31 | console.assert(typeof op === 'function', 'Hooking a non-function'); | ||
32 | o[p] = function() { | ||
33 | var o = strict(this); | ||
34 | var r = f.apply(o, arguments); | ||
35 | return r !== undefined ? r : op.apply(o, arguments); | ||
36 | }; | ||
37 | } | ||
38 | |||
39 | function isSymbol(s) { | ||
40 | return (typeof s === 'symbol') || ('Symbol' in global && s instanceof global.Symbol); | ||
41 | } | ||
42 | |||
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); | ||
49 | } | ||
50 | return desc; | ||
51 | } | ||
52 | |||
53 | var enqueue = (function(nativePromise, nativeSetImmediate) { | ||
54 | if (nativePromise) | ||
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'])); | ||
60 | |||
61 | function define(o, p, v, override) { | ||
62 | if (p in o && !override && !OVERRIDE_NATIVE_FOR_TESTING) | ||
63 | return; | ||
64 | |||
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, { | ||
69 | value: v, | ||
70 | configurable: true, | ||
71 | enumerable: false, | ||
72 | writable: true | ||
73 | }); | ||
74 | } else { | ||
75 | Object.defineProperty(o, p, { | ||
76 | value: v, | ||
77 | configurable: false, | ||
78 | enumerable: false, | ||
79 | writable: false | ||
80 | }); | ||
81 | } | ||
82 | } | ||
83 | |||
84 | function set_internal(o, p, v) { | ||
85 | Object.defineProperty(o, p, { | ||
86 | value: v, | ||
87 | configurable: false, | ||
88 | enumerable: false, | ||
89 | writable: true | ||
90 | }); | ||
91 | } | ||
92 | |||
93 | // Snapshot intrinsic functions | ||
94 | var $isNaN = global.isNaN, | ||
95 | $parseInt = global.parseInt, | ||
96 | $parseFloat = global.parseFloat; | ||
97 | |||
98 | var E = Math.E, | ||
99 | LOG10E = Math.LOG10E, | ||
100 | LOG2E = Math.LOG2E, | ||
101 | abs = Math.abs, | ||
102 | ceil = Math.ceil, | ||
103 | exp = Math.exp, | ||
104 | floor = Math.floor, | ||
105 | log = Math.log, | ||
106 | max = Math.max, | ||
107 | min = Math.min, | ||
108 | pow = Math.pow, | ||
109 | random = Math.random, | ||
110 | sqrt = Math.sqrt; | ||
111 | |||
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; | ||
116 | |||
117 | // These are used for implementing the polyfills, but not exported. | ||
118 | |||
119 | // Inspired by https://gist.github.com/1638059 | ||
120 | /** @constructor */ | ||
121 | function EphemeronTable() { | ||
122 | var secretKey = ObjectCreate(null); | ||
123 | |||
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); | ||
130 | }; | ||
131 | }(secretKey)), | ||
132 | configurable: true, | ||
133 | writeable: true, | ||
134 | enumerable: false | ||
135 | }); | ||
136 | return secrets; | ||
137 | } | ||
138 | |||
139 | function reveal(o) { | ||
140 | var v = typeof o.valueOf === 'function' && o.valueOf(secretKey); | ||
141 | return v === o ? null : v; | ||
142 | } | ||
143 | |||
144 | return { | ||
145 | clear: function() { | ||
146 | secretKey = ObjectCreate(null); | ||
147 | }, | ||
148 | remove: function(key) { | ||
149 | var secrets = reveal(key); | ||
150 | if (secrets && HasOwnProperty(secrets, 'value')) { | ||
151 | delete secrets.value; | ||
152 | return true; | ||
153 | } | ||
154 | return false; | ||
155 | }, | ||
156 | get: function(key, defaultValue) { | ||
157 | var secrets = reveal(key); | ||
158 | return (secrets && HasOwnProperty(secrets, 'value')) ? secrets.value : defaultValue; | ||
159 | }, | ||
160 | has: function(key) { | ||
161 | var secrets = reveal(key); | ||
162 | return Boolean(secrets && HasOwnProperty(secrets, 'value')); | ||
163 | }, | ||
164 | set: function(key, value) { | ||
165 | var secrets = reveal(key) || conceal(key); | ||
166 | secrets.value = value; | ||
167 | } | ||
168 | }; | ||
169 | } | ||
170 | |||
171 | var empty = Object.create(null); | ||
172 | |||
173 | //---------------------------------------------------------------------- | ||
174 | // | ||
175 | // ECMAScript 2015 | ||
176 | // http://www.ecma-international.org/ecma-262/6.0/ | ||
177 | // | ||
178 | //---------------------------------------------------------------------- | ||
179 | |||
180 | // --------------------------------------- | ||
181 | // 19.4 Symbol Objects | ||
182 | // --------------------------------------- | ||
183 | |||
184 | // NOTE: Symbols are defined here - out of spec order - since we need the | ||
185 | // properties and prototype to be populated for other polyfills. | ||
186 | |||
187 | // NOTE: Not secure, nor is obj[$$symbol] hidden from Object.keys() | ||
188 | |||
189 | var symbolForKey; | ||
190 | (function() { | ||
191 | var secret = Object.create(null); | ||
192 | var symbolMap = {}; | ||
193 | symbolForKey = function(k) { | ||
194 | return symbolMap[k]; | ||
195 | }; | ||
196 | |||
197 | var GlobalSymbolRegistry = []; | ||
198 | |||
199 | function unique(bits) { | ||
200 | return Array(bits + 1).join('x').replace(/x/g, function() { | ||
201 | return random() < 0.5 ? '\u200C' : '\u200D'; // JWNJ / ZWJ | ||
202 | }); | ||
203 | } | ||
204 | |||
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(); | ||
210 | |||
211 | var descString = description === undefined ? undefined : String(description); | ||
212 | |||
213 | set_internal(this, '[[SymbolData]]', unique(128)); | ||
214 | set_internal(this, '[[Description]]', descString); | ||
215 | |||
216 | symbolMap[this] = this; | ||
217 | return this; | ||
218 | } | ||
219 | |||
220 | if (!('Symbol' in global) || OVERRIDE_NATIVE_FOR_TESTING) | ||
221 | global.Symbol = Symbol; | ||
222 | |||
223 | // 19.4.2 Properties of the Symbol Constructor | ||
224 | |||
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]]']; | ||
231 | } | ||
232 | var newSymbol = Symbol(key); | ||
233 | GlobalSymbolRegistry.push({'[[key]]': stringKey, '[[symbol]]': newSymbol}); | ||
234 | return newSymbol; | ||
235 | }); | ||
236 | |||
237 | // 19.4.2.2 Symbol.hasInstance | ||
238 | // 19.4.2.3 Symbol.isConcatSpreadable | ||
239 | |||
240 | // 19.4.2.4 Symbol.iterator | ||
241 | define(global.Symbol, 'iterator', global.Symbol('Symbol.iterator')); | ||
242 | |||
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]]']; | ||
249 | } | ||
250 | return undefined; | ||
251 | }); | ||
252 | |||
253 | // 19.4.2.6 Symbol.match | ||
254 | define(global.Symbol, 'match', global.Symbol('Symbol.match')); | ||
255 | |||
256 | // 19.4.2.7 Symbol.prototype | ||
257 | |||
258 | // 19.4.2.8 Symbol.replace | ||
259 | define(global.Symbol, 'replace', global.Symbol('Symbol.replace')); | ||
260 | |||
261 | // 19.4.2.9 Symbol.search | ||
262 | define(global.Symbol, 'search', global.Symbol('Symbol.search')); | ||
263 | |||
264 | // 19.4.2.10 Symbol.species | ||
265 | |||
266 | // 19.4.2.11 Symbol.search | ||
267 | define(global.Symbol, 'split', global.Symbol('Symbol.split')); | ||
268 | |||
269 | // 19.4.2.12 Symbol.toPrimitive | ||
270 | |||
271 | // 19.4.2.13 Symbol.toStringTag | ||
272 | define(global.Symbol, 'toStringTag', global.Symbol('Symbol.toStringTag')); | ||
273 | |||
274 | // 19.4.2.14 Symbol.unscopables | ||
275 | |||
276 | // 19.4.3 Properties of the Symbol Prototype Object | ||
277 | // 19.4.3.1 Symbol.prototype.constructor | ||
278 | |||
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]]'] + ')'; | ||
285 | }, | ||
286 | configurable: true, writeable: true, enumerable: false }); | ||
287 | |||
288 | // 19.4.3.3 Symbol.prototype.valueOf ( ) | ||
289 | Object.defineProperty(Symbol.prototype, 'valueOf', { | ||
290 | value: function valueOf() { | ||
291 | // To prevent automatic string conversion: | ||
292 | throw TypeError(); | ||
293 | |||
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]]']; | ||
300 | }, | ||
301 | configurable: true, writeable: true, enumerable: false }); | ||
302 | |||
303 | // 19.4.3.4 Symbol.prototype [ @@toStringTag ] | ||
304 | // (Done later to polyfill partial implementations) | ||
305 | |||
306 | // 19.4.4 Properties of Symbol Instances | ||
307 | }()); | ||
308 | |||
309 | console.assert(typeof global.Symbol() === 'symbol' || symbolForKey(String(global.Symbol('x')))); | ||
310 | |||
311 | // Defined here so that other prototypes can reference it | ||
312 | // 25.1.2 The %IteratorPrototype% Object | ||
313 | var $IteratorPrototype$ = {}; | ||
314 | |||
315 | //---------------------------------------- | ||
316 | // 6 ECMAScript Data Types and Values | ||
317 | //---------------------------------------- | ||
318 | |||
319 | // 6.1 ECMAScript Language Types | ||
320 | |||
321 | // "Type(x)" is used as shorthand for "the type of x"... | ||
322 | function Type(v) { | ||
323 | switch (typeof v) { | ||
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'; | ||
329 | default: | ||
330 | if (v === null) return 'null'; | ||
331 | if (v instanceof global.Symbol) return 'symbol'; | ||
332 | return 'object'; | ||
333 | } | ||
334 | } | ||
335 | |||
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; | ||
343 | |||
344 | //---------------------------------------- | ||
345 | // 7 Abstract Operations | ||
346 | //---------------------------------------- | ||
347 | |||
348 | //---------------------------------------- | ||
349 | // 7.1 Type Conversion | ||
350 | //---------------------------------------- | ||
351 | |||
352 | // 7.1.1 ToPrimitive ( input [, PreferredType] ) | ||
353 | // just use valueOf() | ||
354 | |||
355 | // 7.1.2 ToBoolean ( argument ) | ||
356 | // just use Boolean() | ||
357 | |||
358 | // 7.1.3 ToNumber ( argument ) | ||
359 | // just use Number() | ||
360 | |||
361 | // 7.1.4 ToInteger ( argument ) | ||
362 | function ToInteger(n) { | ||
363 | n = Number(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)); | ||
367 | } | ||
368 | |||
369 | // 7.1.5 ToInt32 ( argument ) | ||
370 | function ToInt32(v) { return v >> 0; } | ||
371 | |||
372 | // 7.1.6 ToUint32 ( argument ) | ||
373 | function ToUint32(v) { return v >>> 0; } | ||
374 | |||
375 | // 7.1.7 ToInt16 ( argument ) | ||
376 | function ToInt16(v) { return (v << 16) >> 16; } | ||
377 | |||
378 | // 7.1.8 ToUint16 ( argument ) | ||
379 | function ToUint16(v) { return v & 0xFFFF; } | ||
380 | |||
381 | // 7.1.9 ToInt8 ( argument ) | ||
382 | function ToInt8(v) { return (v << 24) >> 24; } | ||
383 | |||
384 | // 7.1.10 ToUint8 ( argument ) | ||
385 | function ToUint8(v) { return v & 0xFF; } | ||
386 | |||
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; | ||
397 | return f; | ||
398 | } | ||
399 | |||
400 | // 7.1.12 ToString ( argument ) | ||
401 | // just use String() | ||
402 | |||
403 | // 7.1.13 ToObject ( argument ) | ||
404 | function ToObject(v) { | ||
405 | if (v === null || v === undefined) throw TypeError(); | ||
406 | return Object(v); | ||
407 | } | ||
408 | |||
409 | // 7.1.14 ToPropertyKey ( argument ) | ||
410 | function ToPropertyKey(v) { | ||
411 | return String(v); | ||
412 | } | ||
413 | |||
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 | ||
420 | } | ||
421 | |||
422 | // 7.1.16 CanonicalNumericIndexString ( argument ) | ||
423 | |||
424 | //---------------------------------------- | ||
425 | // 7.2 Testing and Comparison Operations | ||
426 | //---------------------------------------- | ||
427 | |||
428 | // 7.2.1 RequireObjectCoercible ( argument ) | ||
429 | // 7.2.2 IsArray ( argument ) | ||
430 | |||
431 | // 7.2.3 IsCallable ( argument ) | ||
432 | function IsCallable(o) { return typeof o === 'function'; } | ||
433 | |||
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'; | ||
441 | } | ||
442 | |||
443 | // 7.2.5 IsExtensible (O) | ||
444 | // 7.2.6 IsInteger ( argument ) | ||
445 | |||
446 | // 7.2.7 IsPropertyKey ( argument ) | ||
447 | function IsPropertyKey(argument) { | ||
448 | if (Type(argument) === 'string') return true; | ||
449 | if (Type(argument) === 'symbol') return true; | ||
450 | return false; | ||
451 | } | ||
452 | |||
453 | // 7.2.8 IsRegExp ( argument ) | ||
454 | // 7.2.5 IsConstructor ( argument ) | ||
455 | |||
456 | // 7.2.9 SameValue(x, y) | ||
457 | function SameValue(x, y) { | ||
458 | if (typeof x !== typeof y) return false; | ||
459 | switch (typeof x) { | ||
460 | case 'undefined': | ||
461 | return true; | ||
462 | case 'number': | ||
463 | if (x !== x && y !== y) return true; | ||
464 | if (x === 0 && y === 0) return 1/x === 1/y; | ||
465 | return x === y; | ||
466 | case 'boolean': | ||
467 | case 'string': | ||
468 | case 'object': | ||
469 | default: | ||
470 | return x === y; | ||
471 | } | ||
472 | } | ||
473 | |||
474 | // 7.2.10 SameValueZero(x, y) | ||
475 | function SameValueZero(x, y) { | ||
476 | if (typeof x !== typeof y) return false; | ||
477 | switch (typeof x) { | ||
478 | case 'undefined': | ||
479 | return true; | ||
480 | case 'number': | ||
481 | if (x !== x && y !== y) return true; | ||
482 | return x === y; | ||
483 | case 'boolean': | ||
484 | case 'string': | ||
485 | case 'object': | ||
486 | default: | ||
487 | return x === y; | ||
488 | } | ||
489 | } | ||
490 | |||
491 | //---------------------------------------- | ||
492 | // 7.3 Operations on Objects | ||
493 | //---------------------------------------- | ||
494 | |||
495 | // 7.3.1 Get (O, P) | ||
496 | // - just use o.p or o[p] | ||
497 | |||
498 | // 7.3.2 GetV (V, P) | ||
499 | function GetV(v, p) { | ||
500 | var o = ToObject(v); | ||
501 | return o[p]; | ||
502 | } | ||
503 | |||
504 | // 7.3.3 Set (O, P, V, Throw) | ||
505 | // - just use o.p = v or o[p] = v | ||
506 | |||
507 | |||
508 | |||
509 | |||
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(); | ||
515 | return func; | ||
516 | } | ||
517 | |||
518 | // 7.3.10 HasProperty (O, P) | ||
519 | function HasProperty(o, p) { | ||
520 | while (o) { | ||
521 | if (Object.prototype.hasOwnProperty.call(o, p)) return true; | ||
522 | if (Type(o) !== 'object') return false; | ||
523 | o = Object.getPrototypeOf(o); | ||
524 | } | ||
525 | return false; | ||
526 | } | ||
527 | |||
528 | // 7.3.11 HasOwnProperty (O, P) | ||
529 | function HasOwnProperty(o, p) { | ||
530 | return Object.prototype.hasOwnProperty.call(o, p); | ||
531 | } | ||
532 | |||
533 | //---------------------------------------- | ||
534 | // 7.4 Operations on Iterator Objects | ||
535 | //---------------------------------------- | ||
536 | |||
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(); | ||
543 | return iterator; | ||
544 | } | ||
545 | |||
546 | // 7.4.2 IteratorNext ( iterator, value ) | ||
547 | function IteratorNext(iterator, value) { | ||
548 | if (arguments.length < 2) | ||
549 | var result = iterator.next(); | ||
550 | else | ||
551 | result = iterator.next(value); | ||
552 | if (Type(result) !== 'object') throw TypeError(); | ||
553 | return result; | ||
554 | } | ||
555 | |||
556 | // 7.4.3 IteratorComplete ( iterResult ) | ||
557 | function IteratorComplete(iterResult) { | ||
558 | console.assert(Type(iterResult) === 'object'); | ||
559 | return Boolean(iterResult.done); | ||
560 | } | ||
561 | |||
562 | // 7.4.4 IteratorValue ( iterResult ) | ||
563 | function IteratorValue(iterResult) { | ||
564 | console.assert(Type(iterResult) === 'object'); | ||
565 | return iterResult.value; | ||
566 | } | ||
567 | |||
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; | ||
573 | return result; | ||
574 | } | ||
575 | |||
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; | ||
581 | try { | ||
582 | var innerResult = _return[iterator](); | ||
583 | } catch (result) { | ||
584 | // TODO: If completion.[[type]] is throw, return completion | ||
585 | return result; | ||
586 | } | ||
587 | if (Type(innerResult) !== 'object') throw TypeError(); | ||
588 | return completion; | ||
589 | } | ||
590 | |||
591 | // 7.4.7 CreateIterResultObject (value, done) | ||
592 | function CreateIterResultObject(value, done) { | ||
593 | console.assert(Type(done) === 'boolean'); | ||
594 | var obj = {}; | ||
595 | obj["value"] = value; | ||
596 | obj["done"] = done; | ||
597 | return obj; | ||
598 | } | ||
599 | |||
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( ) | ||
604 | |||
605 | //---------------------------------------- | ||
606 | // 8 Executable Code and Execution Contexts | ||
607 | //---------------------------------------- | ||
608 | |||
609 | //---------------------------------------- | ||
610 | // 8.4 Jobs and Job Queues | ||
611 | //---------------------------------------- | ||
612 | |||
613 | // 8.4.1 EnqueueJob ( queueName, job, arguments) | ||
614 | function EnqueueJob(queueName, job, args) { | ||
615 | var fn = function() { job.apply(undefined, args); }; | ||
616 | enqueue(fn); | ||
617 | } | ||
618 | |||
619 | // 8.4.2 NextJob result | ||
620 | function NextJob(result) { | ||
621 | // no-op | ||
622 | } | ||
623 | |||
624 | //---------------------------------------- | ||
625 | // 9 Ordinary and Exotic Objects Behaviors | ||
626 | //---------------------------------------- | ||
627 | |||
628 | // 9.1.11 [[Enumerate]] () | ||
629 | function Enumerate(obj) { | ||
630 | var e = []; | ||
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); | ||
637 | if (desc) { | ||
638 | visited.add(name); | ||
639 | if (desc.enumerable) e.push(name); | ||
640 | } | ||
641 | } | ||
642 | }); | ||
643 | obj = Object.getPrototypeOf(obj); | ||
644 | } | ||
645 | return e[$$iterator](); | ||
646 | } | ||
647 | |||
648 | // 9.1.12 [[OwnPropertyKeys]] ( ) | ||
649 | function OwnPropertyKeys(o) { | ||
650 | return Object.getOwnPropertyNames(o); | ||
651 | } | ||
652 | |||
653 | // 9.1.13 ObjectCreate(proto, internalSlotsList) | ||
654 | function ObjectCreate(proto, internalSlotsList) { | ||
655 | return Object.create(proto, internalSlotsList); | ||
656 | } | ||
657 | |||
658 | // --------------------------------------- | ||
659 | // 19 Fundamental Objects | ||
660 | // --------------------------------------- | ||
661 | |||
662 | // --------------------------------------- | ||
663 | // 19.1 Object Objects | ||
664 | // --------------------------------------- | ||
665 | |||
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 ) | ||
670 | define( | ||
671 | Object, 'assign', | ||
672 | function assign(target, /*...*/sources) { | ||
673 | var to = ToObject(target); | ||
674 | if (arguments.length < 2) return to; | ||
675 | |||
676 | var sourcesIndex = 1; | ||
677 | while (sourcesIndex < arguments.length) { | ||
678 | var nextSource = arguments[sourcesIndex++]; | ||
679 | if (nextSource === undefined || nextSource === null) { | ||
680 | var keys = []; | ||
681 | } else { | ||
682 | var from = ToObject(nextSource); | ||
683 | keys = OwnPropertyKeys(from); | ||
684 | } | ||
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; | ||
691 | } | ||
692 | } | ||
693 | } | ||
694 | return to; | ||
695 | }); | ||
696 | |||
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 ) | ||
702 | |||
703 | (function() { | ||
704 | var nativeSymbols = (typeof global.Symbol() === 'symbol'), | ||
705 | $getOwnPropertyNames = Object.getOwnPropertyNames, | ||
706 | $keys = Object.keys, | ||
707 | $window_names = (typeof window === 'object' ? $getOwnPropertyNames(window) : []); | ||
708 | |||
709 | function isStringKey(k) { return !symbolForKey(k); } | ||
710 | |||
711 | // 19.1.2.7 Object.getOwnPropertyNames ( O ) | ||
712 | define( | ||
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 | ||
718 | try { | ||
719 | return $getOwnPropertyNames(o).filter(isStringKey); | ||
720 | } catch (_) { | ||
721 | return $window_names.slice(); | ||
722 | } | ||
723 | } | ||
724 | return $getOwnPropertyNames(o).filter(isStringKey); | ||
725 | }, !nativeSymbols); | ||
726 | |||
727 | // 19.1.2.8 Object.getOwnPropertySymbols ( O ) | ||
728 | define( | ||
729 | Object, 'getOwnPropertySymbols', | ||
730 | function getOwnPropertySymbols(o) { | ||
731 | return $getOwnPropertyNames(o).filter(symbolForKey).map(symbolForKey); | ||
732 | }, !nativeSymbols); | ||
733 | |||
734 | // 19.1.2.14 Object.keys ( O ) | ||
735 | define( | ||
736 | Object, 'keys', | ||
737 | function keys(o) { | ||
738 | return $keys(o).filter(isStringKey); | ||
739 | }, !nativeSymbols); | ||
740 | }()); | ||
741 | |||
742 | // 19.1.2.9 Object.getPrototypeOf ( O ) | ||
743 | // 19.1.2.10 Object.is ( value1, value2 ) | ||
744 | define( | ||
745 | Object, 'is', | ||
746 | function is(value1, value2) { | ||
747 | return SameValue(value1, value2); | ||
748 | }); | ||
749 | |||
750 | // 19.1.2.11 Object.isExtensible ( O ) | ||
751 | // 19.1.2.12 Object.isFrozen ( O ) | ||
752 | // 19.1.2.13 Object.isSealed ( O ) | ||
753 | |||
754 | // 19.1.2.14 Object.keys ( O ) | ||
755 | // see above | ||
756 | |||
757 | // 19.1.2.15 Object.preventExtensions ( O ) | ||
758 | // 19.1.2.16 Object.prototype | ||
759 | // 19.1.2.17 Object.seal ( O ) | ||
760 | |||
761 | // 19.1.2.18 Object.setPrototypeOf ( O, proto ) | ||
762 | define( | ||
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(); | ||
767 | o.__proto__ = proto; | ||
768 | return o; | ||
769 | } | ||
770 | ); | ||
771 | |||
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', | ||
780 | function() { | ||
781 | var o = strict(this); | ||
782 | if (o === Object(o) && $$toStringTag in o) { | ||
783 | return '[object ' + o[$$toStringTag] + ']'; | ||
784 | } | ||
785 | return undefined; | ||
786 | }); | ||
787 | |||
788 | // 19.1.3.7 Object.prototype.valueOf ( ) | ||
789 | // 19.1.4 Properties of Object Instances | ||
790 | |||
791 | // --------------------------------------- | ||
792 | // 19.2 Function Objects | ||
793 | // --------------------------------------- | ||
794 | |||
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 | ||
808 | // 19.2.4.1 length | ||
809 | // 19.2.4.2 name | ||
810 | // 19.2.4.3 prototype | ||
811 | |||
812 | // (No polyfillable changes from ES5) | ||
813 | |||
814 | // --------------------------------------- | ||
815 | // 19.3 Boolean Objects | ||
816 | // --------------------------------------- | ||
817 | |||
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 | ||
827 | |||
828 | // (No polyfillable changes from ES5) | ||
829 | |||
830 | // --------------------------------------- | ||
831 | // 19.4 Symbol Objects | ||
832 | // --------------------------------------- | ||
833 | |||
834 | // Moved earlier in this script, so that other polyfills can depend on them. | ||
835 | |||
836 | // 19.4.3.4 Symbol.prototype [ @@toStringTag ] | ||
837 | define(global.Symbol.prototype, global.Symbol.toStringTag, 'Symbol'); | ||
838 | |||
839 | // --------------------------------------- | ||
840 | // 19.5 Error Objects | ||
841 | // --------------------------------------- | ||
842 | |||
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 | ||
860 | // 19.5.5.6 URIError | ||
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 | ||
869 | |||
870 | // (No polyfillable changes from ES5) | ||
871 | |||
872 | // --------------------------------------- | ||
873 | // 20 Numbers and Dates | ||
874 | // --------------------------------------- | ||
875 | |||
876 | // --------------------------------------- | ||
877 | // 20.1 Number Objects | ||
878 | // --------------------------------------- | ||
879 | |||
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 | ||
884 | |||
885 | // 20.1.2.1 Number.EPSILON | ||
886 | define( | ||
887 | Number, 'EPSILON', | ||
888 | (function () { | ||
889 | var next, result; | ||
890 | for (next = 1; 1 + next !== 1; next = next / 2) | ||
891 | result = next; | ||
892 | return result; | ||
893 | }())); | ||
894 | |||
895 | // 20.1.2.2 Number.isFinite ( number ) | ||
896 | define( | ||
897 | Number, 'isFinite', | ||
898 | function isFinite(number) { | ||
899 | if (Type(number) !== 'number') return false; | ||
900 | if (number !== number || number === +Infinity || number === -Infinity) return false; | ||
901 | return true; | ||
902 | }); | ||
903 | |||
904 | // 20.1.2.3 Number.isInteger ( number ) | ||
905 | define( | ||
906 | Number, 'isInteger', | ||
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; | ||
912 | return true; | ||
913 | }); | ||
914 | |||
915 | // 20.1.2.4 Number.isNaN ( number ) | ||
916 | define( | ||
917 | Number, 'isNaN', | ||
918 | function isNaN(number) { | ||
919 | if (Type(number) !== 'number') return false; | ||
920 | if (number !== number) return true; | ||
921 | return false; | ||
922 | }); | ||
923 | |||
924 | // 20.1.2.5 Number.isSafeInteger ( number ) | ||
925 | define( | ||
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 | ||
933 | return true; | ||
934 | return false; | ||
935 | }); | ||
936 | |||
937 | // 20.1.2.6 Number.MAX_SAFE_INTEGER | ||
938 | define( | ||
939 | Number, 'MAX_SAFE_INTEGER', | ||
940 | 9007199254740991); // 2^53-1 | ||
941 | |||
942 | // 20.1.2.7 Number.MAX_VALUE | ||
943 | |||
944 | // 20.1.2.8 Number.MIN_SAFE_INTEGER | ||
945 | define( | ||
946 | Number, 'MIN_SAFE_INTEGER', | ||
947 | -9007199254740991); // -2^53+1 | ||
948 | |||
949 | // 20.1.2.9 Number.MIN_VALUE | ||
950 | // 20.1.2.10 Number.NaN | ||
951 | // 20.1.2.11 Number.NEGATIVE_INFINITY | ||
952 | |||
953 | // 20.1.2.12 Number.parseFloat ( string ) | ||
954 | define(Number, 'parseFloat', $parseFloat); | ||
955 | |||
956 | // 20.1.2.13 Number.parseInt ( string, radix ) | ||
957 | define(Number, 'parseInt', $parseInt); | ||
958 | |||
959 | // 20.1.2.14 Number.POSITIVE_INFINITY | ||
960 | // 20.1.2.15 Number.prototype | ||
961 | |||
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 | ||
971 | |||
972 | // --------------------------------------- | ||
973 | // 20.2 The Math Object | ||
974 | // --------------------------------------- | ||
975 | |||
976 | // 20.2.1 Value Properties of the Math Object | ||
977 | // 20.2.1.1 Math.E | ||
978 | // 20.2.1.2 Math.LN10 | ||
979 | // 20.2.1.3 Math.LN2 | ||
980 | // 20.2.1.4 Math.LOG10E | ||
981 | // 20.2.1.5 Math.LOG2E | ||
982 | // 20.2.1.6 Math.PI | ||
983 | // 20.2.1.7 Math.SQRT1_2 | ||
984 | // 20.2.1.8 Math.SQRT2 | ||
985 | |||
986 | // 20.2.1.9 Math [ @@toStringTag ] | ||
987 | define(Math, $$toStringTag, 'Math'); | ||
988 | |||
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 ) | ||
992 | |||
993 | // 20.2.2.3 Math.acosh(x) | ||
994 | define( | ||
995 | Math, 'acosh', | ||
996 | function acosh(x) { | ||
997 | x = Number(x); | ||
998 | return log(x + sqrt(x * x - 1)); | ||
999 | }); | ||
1000 | |||
1001 | // 20.2.2.4 Math.asin ( x ) | ||
1002 | |||
1003 | // 20.2.2.5 Math.asinh( x ) | ||
1004 | define( | ||
1005 | Math, 'asinh', | ||
1006 | function asinh(x) { | ||
1007 | x = Number(x); | ||
1008 | if (SameValue(x, -0)) { | ||
1009 | return x; | ||
1010 | } | ||
1011 | var s = sqrt(x * x + 1); | ||
1012 | return (s === -x) ? log(0) : log(x + s); | ||
1013 | }); | ||
1014 | |||
1015 | // 20.2.2.6 Math.atan ( x ) | ||
1016 | |||
1017 | // 20.2.2.7 Math.atanh( x ) | ||
1018 | define( | ||
1019 | Math, 'atanh', | ||
1020 | function atanh(x) { | ||
1021 | x = Number(x); | ||
1022 | return (x === 0) ? x : log((1 + x) / (1 - x)) / 2; | ||
1023 | }); | ||
1024 | |||
1025 | // 20.2.2.8 Math.atan2 ( y, x ) | ||
1026 | |||
1027 | // 20.2.2.9 Math.cbrt ( x ) | ||
1028 | define( | ||
1029 | Math, 'cbrt', | ||
1030 | function cbrt(x) { | ||
1031 | x = Number(x); | ||
1032 | if ($isNaN(x/x)) { | ||
1033 | return x; | ||
1034 | } | ||
1035 | var r = pow(abs(x), 1/3); | ||
1036 | var t = x/r/r; | ||
1037 | return r + (r * (t-r) / (2*r + t)); | ||
1038 | }); | ||
1039 | |||
1040 | // 20.2.2.10 Math.ceil ( x ) | ||
1041 | |||
1042 | // 20.2.2.11 Math.clz32 ( x ) | ||
1043 | define( | ||
1044 | Math, 'clz32', | ||
1045 | function clz32(x) { | ||
1046 | function clz8(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); | ||
1049 | } | ||
1050 | x = ToUint32(x); | ||
1051 | return x & 0xff000000 ? clz8(x >> 24) : | ||
1052 | x & 0xff0000 ? clz8(x >> 16) + 8 : | ||
1053 | x & 0xff00 ? clz8(x >> 8) + 16 : clz8(x) + 24; | ||
1054 | }); | ||
1055 | |||
1056 | |||
1057 | |||
1058 | // 20.2.2.12 Math.cos ( x ) | ||
1059 | |||
1060 | // 20.2.2.13 Math.cosh ( x ) | ||
1061 | define( | ||
1062 | Math, 'cosh', | ||
1063 | function cosh(x) { | ||
1064 | x = Number(x); | ||
1065 | return (pow(E, x) + pow(E, -x)) / 2; | ||
1066 | }); | ||
1067 | |||
1068 | // 20.2.2.14 Math.exp ( x ) | ||
1069 | |||
1070 | // 20.2.2.15 Math.expm1 ( x ) | ||
1071 | define( | ||
1072 | Math, 'expm1', | ||
1073 | function expm1(x) { | ||
1074 | x = Number(x); | ||
1075 | // from: http://www.johndcook.com/cpp_log1p.html | ||
1076 | if (SameValue(x, -0)) { | ||
1077 | return -0; | ||
1078 | } else if (abs(x) < 1e-5) { | ||
1079 | return x + 0.5 * x * x; // two terms of Taylor expansion | ||
1080 | } else { | ||
1081 | return exp(x) - 1; | ||
1082 | } | ||
1083 | }); | ||
1084 | |||
1085 | // 20.2.2.16 Math.floor ( x ) | ||
1086 | |||
1087 | // 20.2.2.17 Math.fround ( x ) | ||
1088 | define( | ||
1089 | Math, 'fround', | ||
1090 | function fround(x) { | ||
1091 | if ($isNaN(x)) { | ||
1092 | return NaN; | ||
1093 | } | ||
1094 | if (1/x === +Infinity || 1/x === -Infinity || x === +Infinity || x === -Infinity) { | ||
1095 | return x; | ||
1096 | } | ||
1097 | return (new Float32Array([x]))[0]; | ||
1098 | }); | ||
1099 | |||
1100 | // 20.2.2.18 Math.hypot ( value1 [, value2 [ ... ] ] ) | ||
1101 | define( | ||
1102 | Math, 'hypot', | ||
1103 | function hypot() { | ||
1104 | var values = []; | ||
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; | ||
1110 | if (n > m) m = n; | ||
1111 | values[i] = n; | ||
1112 | } | ||
1113 | if (sawNaN) return NaN; | ||
1114 | if (m === 0) return +0; | ||
1115 | var sum = +0; | ||
1116 | for (i = 0; i < values.length; ++i) { | ||
1117 | var r = values[i] / m; | ||
1118 | sum = sum + r * r; | ||
1119 | } | ||
1120 | return m * sqrt(sum); | ||
1121 | }); | ||
1122 | |||
1123 | // 20.2.2.19 Math.imul ( x, y ) | ||
1124 | define( | ||
1125 | Math, 'imul', | ||
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 | ||
1136 | ); | ||
1137 | |||
1138 | // 20.2.2.20 Math.log ( x ) | ||
1139 | |||
1140 | // 20.2.2.21 Math.log1p ( x ) | ||
1141 | define( | ||
1142 | Math, 'log1p', | ||
1143 | function log1p(x) { | ||
1144 | x = Number(x); | ||
1145 | // from: http://www.johndcook.com/cpp_expm1.html | ||
1146 | if (x < -1) { | ||
1147 | return NaN; | ||
1148 | } else if (SameValue(x, -0)) { | ||
1149 | return -0; | ||
1150 | } else if (abs(x) > 1e-4) { | ||
1151 | return log(1 + x); | ||
1152 | } else { | ||
1153 | return (-0.5 * x + 1) * x; | ||
1154 | } | ||
1155 | }); | ||
1156 | |||
1157 | // 20.2.2.22 Math.log10 ( x ) | ||
1158 | define( | ||
1159 | Math, 'log10', | ||
1160 | function log10(x) { | ||
1161 | x = Number(x); | ||
1162 | return log(x) * LOG10E; | ||
1163 | }); | ||
1164 | |||
1165 | // 20.2.2.23 Math.log2 ( x ) | ||
1166 | define( | ||
1167 | Math, 'log2', | ||
1168 | function log2(x) { | ||
1169 | x = Number(x); | ||
1170 | return log(x) * LOG2E; | ||
1171 | }); | ||
1172 | |||
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 ) | ||
1178 | |||
1179 | // 20.2.2.29 Math.sign(x) | ||
1180 | define( | ||
1181 | Math, 'sign', | ||
1182 | function sign(x) { | ||
1183 | x = Number(x); | ||
1184 | return x < 0 ? -1 : x > 0 ? 1 : x; | ||
1185 | }); | ||
1186 | |||
1187 | // 20.2.2.30 Math.sin ( x ) | ||
1188 | |||
1189 | // 20.2.2.31 Math.sinh( x ) | ||
1190 | define( | ||
1191 | Math, 'sinh', | ||
1192 | function sinh(x) { | ||
1193 | x = Number(x); | ||
1194 | return SameValue(x, -0) ? x : (pow(E, x) - pow(E, -x)) / 2; | ||
1195 | }); | ||
1196 | |||
1197 | // 20.2.2.32 Math.sqrt ( x ) | ||
1198 | // 20.2.2.33 Math.tan ( x ) | ||
1199 | |||
1200 | // 20.2.2.34 Math.tanh ( x ) | ||
1201 | define( | ||
1202 | Math, 'tanh', | ||
1203 | function tanh(x) { | ||
1204 | x = Number(x); | ||
1205 | var n = pow(E, 2 * x) - 1, | ||
1206 | d = pow(E, 2 * x) + 1; | ||
1207 | if (SameValue(x, -0)) | ||
1208 | return x; | ||
1209 | return (n === d) ? 1 : n / d; // Handle Infinity/Infinity | ||
1210 | }); | ||
1211 | |||
1212 | // 20.2.2.35 Math.trunc ( x ) | ||
1213 | define( | ||
1214 | Math, 'trunc', | ||
1215 | function trunc(x) { | ||
1216 | x = Number(x); | ||
1217 | return $isNaN(x) ? NaN : | ||
1218 | x < 0 ? ceil(x) : floor(x); | ||
1219 | }); | ||
1220 | |||
1221 | // --------------------------------------- | ||
1222 | // 20.3 Date Objects | ||
1223 | // --------------------------------------- | ||
1224 | |||
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 | ||
1298 | |||
1299 | // (No polyfillable changes from ES5) | ||
1300 | |||
1301 | // --------------------------------------- | ||
1302 | // 21 Text Processing | ||
1303 | // --------------------------------------- | ||
1304 | |||
1305 | var string_regexp_dispatch = (function() { | ||
1306 | var faux = {}, secret = Symbol(); | ||
1307 | faux[Symbol.match] = function() { return secret; }; | ||
1308 | return ("").match(faux) === secret; | ||
1309 | }()); | ||
1310 | |||
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 ) | ||
1316 | |||
1317 | // 21.1.2.2 String.fromCodePoint ( ...codePoints ) | ||
1318 | define( | ||
1319 | String, 'fromCodePoint', | ||
1320 | function fromCodePoint(/*...codePoints*/) { | ||
1321 | var codePoints = arguments, | ||
1322 | length = codePoints.length, | ||
1323 | elements = [], | ||
1324 | nextIndex = 0; | ||
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); | ||
1331 | } | ||
1332 | if (nextCP < 0x10000) { | ||
1333 | elements.push(String.fromCharCode(nextCP)); | ||
1334 | } else { | ||
1335 | nextCP -= 0x10000; | ||
1336 | elements.push(String.fromCharCode((nextCP >> 10) + 0xD800)); | ||
1337 | elements.push(String.fromCharCode((nextCP % 0x400) + 0xDC00)); | ||
1338 | } | ||
1339 | nextIndex += 1; | ||
1340 | } | ||
1341 | return elements.join(''); | ||
1342 | }); | ||
1343 | |||
1344 | // 21.1.2.3 String.prototype | ||
1345 | |||
1346 | // 21.1.2.4 String.raw ( template , ...substitutions ) | ||
1347 | define( | ||
1348 | String, 'raw', | ||
1349 | function raw(template /*, ...substitutions*/) { | ||
1350 | var substitutions = [].slice.call(arguments, 1); | ||
1351 | |||
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 = []; | ||
1359 | var nextIndex = 0; | ||
1360 | while (true) { | ||
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; | ||
1370 | } | ||
1371 | }); | ||
1372 | |||
1373 | // See https://githib.com/inexorabletash/uate for a more useful version. | ||
1374 | |||
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 ) | ||
1378 | |||
1379 | // 21.1.3.3 String.prototype.codePointAt ( pos ) | ||
1380 | define( | ||
1381 | String.prototype, 'codePointAt', | ||
1382 | function codePointAt(pos) { | ||
1383 | var o = strict(this); | ||
1384 | var s = String(o); | ||
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; | ||
1393 | }); | ||
1394 | |||
1395 | // 21.1.3.4 String.prototype.concat ( ...args ) | ||
1396 | // 21.1.3.5 String.prototype.constructor | ||
1397 | |||
1398 | // 21.1.3.6 String.prototype.endsWith ( searchString [ , endPosition] ) | ||
1399 | define( | ||
1400 | String.prototype, 'endsWith', | ||
1401 | function endsWith(searchString) { | ||
1402 | var endPosition = arguments[1]; | ||
1403 | |||
1404 | var o = strict(this); | ||
1405 | var s = String(o); | ||
1406 | var searchStr = String(searchString); | ||
1407 | var len = s.length; | ||
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; | ||
1414 | return false; | ||
1415 | }); | ||
1416 | |||
1417 | // 21.1.3.7 String.prototype.includes ( searchString [ , position ] ) | ||
1418 | define( | ||
1419 | String.prototype, 'includes', | ||
1420 | function includes(searchString) { | ||
1421 | var position = arguments[1]; | ||
1422 | |||
1423 | var o = strict(this); | ||
1424 | var s = String(o); | ||
1425 | var searchStr = String(searchString); | ||
1426 | var pos = ToInteger(position); | ||
1427 | var len = s.length; | ||
1428 | var start = min(max(pos, 0), len); | ||
1429 | return s.indexOf(searchStr, start) !== -1; | ||
1430 | }); | ||
1431 | |||
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 ) | ||
1436 | define( | ||
1437 | String.prototype, 'match', | ||
1438 | function match(regexp) { | ||
1439 | var o = strict(this); | ||
1440 | var s = String(o); | ||
1441 | if (HasProperty(regexp, $$match)) var rx = regexp; | ||
1442 | else rx = new RegExp(regexp); | ||
1443 | return rx[$$match](s); | ||
1444 | }, !string_regexp_dispatch); | ||
1445 | |||
1446 | // 21.1.3.12 String.prototype.normalize ( [ form ] ) | ||
1447 | |||
1448 | // Not practical due to table sizes; if needed, pull in: | ||
1449 | // https://github.com/walling/unorm/ | ||
1450 | |||
1451 | // 21.1.3.13 String.prototype.repeat ( count ) | ||
1452 | define( | ||
1453 | String.prototype, 'repeat', | ||
1454 | function repeat(count) { | ||
1455 | var o = strict(this); | ||
1456 | var s = String(o); | ||
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); | ||
1461 | return t; | ||
1462 | }); | ||
1463 | |||
1464 | // 21.1.3.14 String.prototype.replace (searchValue, replaceValue ) | ||
1465 | define( | ||
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); | ||
1473 | |||
1474 | // 21.1.3.15 String.prototype.search ( regexp ) | ||
1475 | define( | ||
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); | ||
1484 | |||
1485 | // 21.1.3.16 String.prototype.slice ( start, end ) | ||
1486 | // 21.1.3.17 String.prototype.split ( separator, limit ) | ||
1487 | define( | ||
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); | ||
1495 | |||
1496 | // 21.1.3.18 String.prototype.startsWith ( searchString [, position ] ) | ||
1497 | define( | ||
1498 | String.prototype, 'startsWith', | ||
1499 | function startsWith(searchString) { | ||
1500 | var position = arguments[1]; | ||
1501 | |||
1502 | var o = strict(this); | ||
1503 | var s = String(o); | ||
1504 | var searchStr = String(searchString); | ||
1505 | var pos = ToInteger(position); | ||
1506 | var len = s.length; | ||
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; | ||
1511 | return false; | ||
1512 | }); | ||
1513 | |||
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 ( ) | ||
1522 | |||
1523 | // 21.1.3.27 String.prototype [ @@iterator ]( ) | ||
1524 | define( | ||
1525 | String.prototype, $$iterator, | ||
1526 | function entries() { | ||
1527 | return CreateStringIterator(this, 'value'); | ||
1528 | }); | ||
1529 | |||
1530 | // 21.1.4 Properties of String Instances | ||
1531 | // 21.1.4.1 length | ||
1532 | |||
1533 | // 21.1.5 String Iterator Objects | ||
1534 | /** @constructor */ | ||
1535 | function StringIterator() {} | ||
1536 | |||
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); | ||
1544 | return iterator; | ||
1545 | } | ||
1546 | |||
1547 | // 21.1.5.2 The %StringIteratorPrototype% Object | ||
1548 | var $StringIteratorPrototype$ = Object.create($IteratorPrototype$); | ||
1549 | StringIterator.prototype = $StringIteratorPrototype$; | ||
1550 | |||
1551 | // 21.1.5.2.1 %StringIteratorPrototype%.next ( ) | ||
1552 | define( | ||
1553 | $StringIteratorPrototype$, 'next', | ||
1554 | function next() { | ||
1555 | var o = ToObject(this); | ||
1556 | var s = String(o['[[IteratedString]]']), | ||
1557 | index = o['[[StringIteratorNextIndex]]'], | ||
1558 | len = s.length; | ||
1559 | if (index >= len) { | ||
1560 | set_internal(o, '[[StringIteratorNextIndex]]', Infinity); | ||
1561 | return CreateIterResultObject(undefined, true); | ||
1562 | } | ||
1563 | var cp = s.codePointAt(index); | ||
1564 | set_internal(o, '[[StringIteratorNextIndex]]', index + (cp > 0xFFFF ? 2 : 1)); | ||
1565 | return CreateIterResultObject(String.fromCodePoint(cp), false); | ||
1566 | }); | ||
1567 | |||
1568 | // 21.1.5.2.2 %StringIteratorPrototype% [ @@toStringTag ] | ||
1569 | define($StringIteratorPrototype$, $$toStringTag, 'String Iterator'); | ||
1570 | |||
1571 | // 21.1.5.3 Properties of String Iterator Instances | ||
1572 | |||
1573 | // --------------------------------------- | ||
1574 | // 21.2 RegExp (Regular Expression) Objects | ||
1575 | // --------------------------------------- | ||
1576 | |||
1577 | // 21.2.1 Patterns | ||
1578 | // 21.2.2 Pattern Semantics | ||
1579 | // 21.2.2.1 Notation | ||
1580 | // 21.2.2.2 Pattern | ||
1581 | // 21.2.2.3 Disjunction | ||
1582 | // 21.2.2.4 Alternative | ||
1583 | // 21.2.2.5 Term | ||
1584 | // 21.2.2.6 Assertion | ||
1585 | // 21.2.2.7 Quantifier | ||
1586 | // 21.2.2.8 Atom | ||
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 ) | ||
1607 | |||
1608 | // 21.2.5.3 get RegExp.prototype.flags | ||
1609 | if (!('flags' in RegExp.prototype)) { | ||
1610 | Object.defineProperty( | ||
1611 | RegExp.prototype, 'flags', { | ||
1612 | get: function() { | ||
1613 | var s = String(this); | ||
1614 | return s.substring(s.lastIndexOf('/') + 1); | ||
1615 | } | ||
1616 | }); | ||
1617 | } | ||
1618 | |||
1619 | // 21.2.5.4 get RegExp.prototype.global | ||
1620 | // 21.2.5.5 get RegExp.prototype.ignoreCase | ||
1621 | |||
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); | ||
1626 | }); | ||
1627 | |||
1628 | // 21.2.5.7 get RegExp.prototype.multiline | ||
1629 | |||
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); | ||
1634 | }); | ||
1635 | |||
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); | ||
1640 | }); | ||
1641 | |||
1642 | // 21.2.5.10 get RegExp.prototype.source | ||
1643 | |||
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); | ||
1648 | }); | ||
1649 | |||
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 | ||
1654 | |||
1655 | // 21.2.6 Properties of RegExp Instances | ||
1656 | // 21.2.6.1 lastIndex | ||
1657 | |||
1658 | // (No polyfillable changes from ES5) | ||
1659 | |||
1660 | // --------------------------------------- | ||
1661 | // 22 Indexed Collections | ||
1662 | // --------------------------------------- | ||
1663 | |||
1664 | // --------------------------------------- | ||
1665 | // 22.1 Array Objects | ||
1666 | // --------------------------------------- | ||
1667 | |||
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 ) | ||
1672 | |||
1673 | // 22.1.2 Properties of the Array Constructor | ||
1674 | |||
1675 | // 22.1.2.1 Array.from ( items [ , mapfn [ , thisArg ] ] ) | ||
1676 | define( | ||
1677 | Array, 'from', | ||
1678 | function from(items) { | ||
1679 | var mapfn = arguments[1]; | ||
1680 | var thisArg = arguments[2]; | ||
1681 | |||
1682 | var c = strict(this); | ||
1683 | if (mapfn === undefined) { | ||
1684 | var mapping = false; | ||
1685 | } else { | ||
1686 | if (!IsCallable(mapfn)) throw TypeError(); | ||
1687 | var t = thisArg; | ||
1688 | mapping = true; | ||
1689 | } | ||
1690 | var usingIterator = GetMethod(items, $$iterator); | ||
1691 | if (usingIterator !== undefined) { | ||
1692 | if (IsConstructor(c)) { | ||
1693 | var a = new c(); | ||
1694 | } else { | ||
1695 | a = new Array(0); | ||
1696 | } | ||
1697 | var iterator = GetIterator(items, usingIterator); | ||
1698 | var k = 0; | ||
1699 | while (true) { | ||
1700 | var next = IteratorStep(iterator); | ||
1701 | if (next === false) { | ||
1702 | a.length = k; | ||
1703 | return a; | ||
1704 | } | ||
1705 | var nextValue = IteratorValue(next); | ||
1706 | if (mapping) | ||
1707 | var mappedValue = mapfn.call(t, nextValue); | ||
1708 | else | ||
1709 | mappedValue = nextValue; | ||
1710 | a[k] = mappedValue; | ||
1711 | k += 1; | ||
1712 | } | ||
1713 | } | ||
1714 | var arrayLike = ToObject(items); | ||
1715 | var lenValue = arrayLike.length; | ||
1716 | var len = ToLength(lenValue); | ||
1717 | if (IsConstructor(c)) { | ||
1718 | a = new c(len); | ||
1719 | } else { | ||
1720 | a = new Array(len); | ||
1721 | } | ||
1722 | k = 0; | ||
1723 | while (k < len) { | ||
1724 | var kValue = arrayLike[k]; | ||
1725 | if (mapping) | ||
1726 | mappedValue = mapfn.call(t, kValue, k); | ||
1727 | else | ||
1728 | mappedValue = kValue; | ||
1729 | a[k] = mappedValue; | ||
1730 | k += 1; | ||
1731 | } | ||
1732 | a.length = len; | ||
1733 | return a; | ||
1734 | }); | ||
1735 | |||
1736 | // 22.1.2.2 Array.isArray ( arg ) | ||
1737 | |||
1738 | // 22.1.2.3 Array.of ( ...items ) | ||
1739 | define( | ||
1740 | Array, 'of', | ||
1741 | function of() { | ||
1742 | var items = arguments; | ||
1743 | |||
1744 | var lenValue = items.length; | ||
1745 | var len = ToUint32(lenValue); | ||
1746 | var c = strict(this), a; | ||
1747 | if (IsConstructor(c)) { | ||
1748 | a = new c(len); | ||
1749 | a = ToObject(a); | ||
1750 | } else { | ||
1751 | a = new Array(len); | ||
1752 | } | ||
1753 | var k = 0; | ||
1754 | while (k < len) { | ||
1755 | a[k] = items[k]; | ||
1756 | k += 1; | ||
1757 | } | ||
1758 | a.length = len; | ||
1759 | return a; | ||
1760 | }); | ||
1761 | |||
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 ] ) | ||
1769 | define( | ||
1770 | Array.prototype, 'copyWithin', | ||
1771 | function copyWithin(target, start/*, end*/) { | ||
1772 | var end = arguments[2]; | ||
1773 | |||
1774 | var o = ToObject(this); | ||
1775 | var lenVal = o.length; | ||
1776 | var len = ToLength(lenVal); | ||
1777 | len = max(len, 0); | ||
1778 | var relativeTarget = ToInteger(target); | ||
1779 | var to; | ||
1780 | if (relativeTarget < 0) | ||
1781 | to = max(len + relativeTarget, 0); | ||
1782 | else | ||
1783 | to = min(relativeTarget, len); | ||
1784 | var relativeStart = ToInteger(start); | ||
1785 | var from; | ||
1786 | if (relativeStart < 0) | ||
1787 | from = max(len + relativeStart, 0); | ||
1788 | else | ||
1789 | from = min(relativeStart, len); | ||
1790 | var relativeEnd; | ||
1791 | if (end === undefined) | ||
1792 | relativeEnd = len; | ||
1793 | else | ||
1794 | relativeEnd = ToInteger(end); | ||
1795 | var final; | ||
1796 | if (relativeEnd < 0) | ||
1797 | final = max(len + relativeEnd, 0); | ||
1798 | else | ||
1799 | final = min(relativeEnd, len); | ||
1800 | var count = min(final - from, len - to); | ||
1801 | var direction; | ||
1802 | if (from < to && to < from + count) { | ||
1803 | direction = -1; | ||
1804 | from = from + count - 1; | ||
1805 | to = to + count - 1; | ||
1806 | } else { | ||
1807 | direction = 1; | ||
1808 | } | ||
1809 | while (count > 0) { | ||
1810 | var fromKey = String(from); | ||
1811 | var toKey = String(to); | ||
1812 | var fromPresent = HasProperty(o, fromKey); | ||
1813 | if (fromPresent) { | ||
1814 | var fromVal = o[fromKey]; | ||
1815 | o[toKey] = fromVal; | ||
1816 | } else { | ||
1817 | delete o[toKey]; | ||
1818 | } | ||
1819 | from = from + direction; | ||
1820 | to = to + direction; | ||
1821 | count = count - 1; | ||
1822 | } | ||
1823 | return o; | ||
1824 | }); | ||
1825 | |||
1826 | // 22.1.3.4 Array.prototype.entries ( ) | ||
1827 | var nativeArrayIteratorMethods = | ||
1828 | ('entries' in Array.prototype && 'next' in [].entries()); | ||
1829 | |||
1830 | define( | ||
1831 | Array.prototype, 'entries', | ||
1832 | function entries() { | ||
1833 | return CreateArrayIterator(this, 'key+value'); | ||
1834 | }, !nativeArrayIteratorMethods); | ||
1835 | |||
1836 | // 22.1.3.5 Array.prototype.every ( callbackfn [ , thisArg] ) | ||
1837 | |||
1838 | // 22.1.3.6 Array.prototype.fill (value [ , start [ , end ] ] ) | ||
1839 | define( | ||
1840 | Array.prototype, 'fill', | ||
1841 | function fill(value/*, start, end*/) { | ||
1842 | var start = arguments[1], | ||
1843 | end = arguments[2]; | ||
1844 | |||
1845 | var o = ToObject(this); | ||
1846 | var lenVal = o.length; | ||
1847 | var len = ToLength(lenVal); | ||
1848 | len = max(len, 0); | ||
1849 | var relativeStart = ToInteger(start); | ||
1850 | var k; | ||
1851 | if (relativeStart < 0) | ||
1852 | k = max((len + relativeStart), 0); | ||
1853 | else | ||
1854 | k = min(relativeStart, len); | ||
1855 | var relativeEnd; | ||
1856 | if (end === undefined) | ||
1857 | relativeEnd = len; | ||
1858 | else | ||
1859 | relativeEnd = ToInteger(end); | ||
1860 | var final; | ||
1861 | if (relativeEnd < 0) | ||
1862 | final = max((len + relativeEnd), 0); | ||
1863 | else | ||
1864 | final = min(relativeEnd, len); | ||
1865 | while (k < final) { | ||
1866 | var pk = String(k); | ||
1867 | o[pk] = value; | ||
1868 | k += 1; | ||
1869 | } | ||
1870 | return o; | ||
1871 | }); | ||
1872 | |||
1873 | // 22.1.3.7 Array.prototype.filter ( callbackfn [ , thisArg ] ) | ||
1874 | |||
1875 | // 22.1.3.8 Array.prototype.find ( predicate [ , thisArg ] ) | ||
1876 | define( | ||
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; | ||
1884 | var k = 0; | ||
1885 | while (k < len) { | ||
1886 | var pk = String(k); | ||
1887 | var kPresent = HasProperty(o, pk); | ||
1888 | if (kPresent) { | ||
1889 | var kValue = o[pk]; | ||
1890 | var testResult = predicate.call(t, kValue, k, o); | ||
1891 | if (Boolean(testResult)) { | ||
1892 | return kValue; | ||
1893 | } | ||
1894 | } | ||
1895 | ++k; | ||
1896 | } | ||
1897 | return undefined; | ||
1898 | }); | ||
1899 | |||
1900 | // 22.1.3.9 Array.prototype.findIndex ( predicate [ , thisArg ] ) | ||
1901 | define( | ||
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; | ||
1909 | var k = 0; | ||
1910 | while (k < len) { | ||
1911 | var pk = String(k); | ||
1912 | var kPresent = HasProperty(o, pk); | ||
1913 | if (kPresent) { | ||
1914 | var kValue = o[pk]; | ||
1915 | var testResult = predicate.call(t, kValue, k, o); | ||
1916 | if (Boolean(testResult)) { | ||
1917 | return k; | ||
1918 | } | ||
1919 | } | ||
1920 | ++k; | ||
1921 | } | ||
1922 | return -1; | ||
1923 | }); | ||
1924 | |||
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) | ||
1928 | |||
1929 | // 22.1.3.13 Array.prototype.keys ( ) | ||
1930 | define( | ||
1931 | Array.prototype, 'keys', | ||
1932 | function keys() { | ||
1933 | return CreateArrayIterator(this, 'key'); | ||
1934 | }, !nativeArrayIteratorMethods); | ||
1935 | |||
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 ) | ||
1951 | |||
1952 | // 22.1.3.29 Array.prototype.values ( ) | ||
1953 | define( | ||
1954 | Array.prototype, 'values', | ||
1955 | function values() { | ||
1956 | return CreateArrayIterator(this, 'value'); | ||
1957 | }, !nativeArrayIteratorMethods); | ||
1958 | |||
1959 | // 22.1.3.30 Array.prototype [ @@iterator ] ( ) | ||
1960 | define( | ||
1961 | Array.prototype, $$iterator, | ||
1962 | Array.prototype.values | ||
1963 | ); | ||
1964 | |||
1965 | // 22.1.3.31 Array.prototype [ @@unscopables ] | ||
1966 | // 22.1.4 Properties of Array Instances | ||
1967 | // 22.1.4.1 length | ||
1968 | |||
1969 | // 22.1.5 Array Iterator Objects | ||
1970 | function ArrayIterator() {} | ||
1971 | |||
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); | ||
1979 | return iterator; | ||
1980 | } | ||
1981 | |||
1982 | // 22.1.5.2 The %ArrayIteratorPrototype% Object | ||
1983 | var $ArrayIteratorPrototype$ = Object.create($IteratorPrototype$); | ||
1984 | ArrayIterator.prototype = $ArrayIteratorPrototype$; | ||
1985 | |||
1986 | // 22.1.5.2.1 %ArrayIteratorPrototype%. next( ) | ||
1987 | define( | ||
1988 | $ArrayIteratorPrototype$, 'next', | ||
1989 | function 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), | ||
1997 | elementKey, | ||
1998 | elementValue; | ||
1999 | if (itemKind.indexOf('sparse') !== -1) { | ||
2000 | var found = false; | ||
2001 | while (!found && index < len) { | ||
2002 | elementKey = String(index); | ||
2003 | found = HasProperty(a, elementKey); | ||
2004 | if (!found) { | ||
2005 | index += 1; | ||
2006 | } | ||
2007 | } | ||
2008 | } | ||
2009 | if (index >= len) { | ||
2010 | set_internal(o, '[[ArrayIteratorNextIndex]]', Infinity); | ||
2011 | return CreateIterResultObject(undefined, true); | ||
2012 | } | ||
2013 | elementKey = index; | ||
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'); | ||
2024 | }); | ||
2025 | |||
2026 | // 22.1.5.2.2 %ArrayIteratorPrototype% [ @@toStringTag ] | ||
2027 | define($ArrayIteratorPrototype$, $$toStringTag, 'Array Iterator'); | ||
2028 | |||
2029 | // 22.1.5.3 Properties of Array Iterator Instances | ||
2030 | |||
2031 | |||
2032 | // --------------------------------------- | ||
2033 | // 22.2 TypedArray Objects | ||
2034 | // --------------------------------------- | ||
2035 | |||
2036 | // See typedarray.js for TypedArray polyfill | ||
2037 | |||
2038 | ['Int8Array', 'Uint8Array', 'Uint8ClampedArray', | ||
2039 | 'Int16Array', 'Uint16Array', | ||
2040 | 'Int32Array', 'Uint32Array', | ||
2041 | 'Float32Array', 'Float64Array'].forEach(function ($TypedArrayName$) { | ||
2042 | if (!($TypedArrayName$ in global)) | ||
2043 | return; | ||
2044 | var $TypedArray$ = global[$TypedArrayName$]; | ||
2045 | |||
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 | ||
2053 | |||
2054 | // 22.2.2.1 %TypedArray%.from ( source [ , mapfn [ , thisArg ] ] ) | ||
2055 | define( | ||
2056 | $TypedArray$, 'from', | ||
2057 | function from(source) { | ||
2058 | var mapfn = arguments[1]; | ||
2059 | var thisArg = arguments[2]; | ||
2060 | |||
2061 | var c = strict(this); | ||
2062 | if (!IsConstructor(c)) throw TypeError(); | ||
2063 | if (mapfn === undefined) { | ||
2064 | var mapping = false; | ||
2065 | } else { | ||
2066 | if (IsCallable(mapfn)) throw TypeError(); | ||
2067 | var t = thisArg; | ||
2068 | mapping = true; | ||
2069 | } | ||
2070 | var usingIterator = GetMethod(source, $$iterator); | ||
2071 | if (usingIterator !== undefined) { | ||
2072 | var iterator = GetIterator(source, usingIterator); | ||
2073 | var values = []; | ||
2074 | var next = true; | ||
2075 | while (next !== false) { | ||
2076 | next = IteratorStep(iterator); | ||
2077 | if (next !== false) { | ||
2078 | var nextValue = IteratorValue(next); | ||
2079 | values.push(nextValue); | ||
2080 | } | ||
2081 | } | ||
2082 | var len = values.length; | ||
2083 | var newObj = new c(len); | ||
2084 | var k = 0; | ||
2085 | while (k < len) { | ||
2086 | var kValue = values.shift(); | ||
2087 | if (mapping) { | ||
2088 | var mappedValue = mapfn.call(t, kValue); | ||
2089 | } else { | ||
2090 | mappedValue = kValue; | ||
2091 | } | ||
2092 | newObj[k] = mappedValue; | ||
2093 | ++k; | ||
2094 | } | ||
2095 | console.assert(values.length === 0); | ||
2096 | return newObj; | ||
2097 | } | ||
2098 | var arrayLike = ToObject(source); | ||
2099 | var lenValue = arrayLike.length; | ||
2100 | len = ToLength(lenValue); | ||
2101 | newObj = new c(len); | ||
2102 | k = 0; | ||
2103 | while (k < len) { | ||
2104 | kValue = arrayLike[k]; | ||
2105 | if (mapping) { | ||
2106 | mappedValue = mapfn.call(t, kValue, k); | ||
2107 | } else { | ||
2108 | mappedValue = kValue; | ||
2109 | } | ||
2110 | newObj[k] = mappedValue; | ||
2111 | ++k; | ||
2112 | } | ||
2113 | return newObj; | ||
2114 | }); | ||
2115 | |||
2116 | // 22.2.2.2 %TypedArray%.of ( ...items ) | ||
2117 | define( | ||
2118 | $TypedArray$, 'of', | ||
2119 | function of() { | ||
2120 | var items = arguments; | ||
2121 | |||
2122 | var len = items.length; | ||
2123 | var c = strict(this); | ||
2124 | var newObj = new c(len); | ||
2125 | var k = 0; | ||
2126 | while (k < len) { | ||
2127 | newObj[k] = items[k]; | ||
2128 | ++k; | ||
2129 | } | ||
2130 | return newObj; | ||
2131 | }); | ||
2132 | |||
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 | ||
2140 | |||
2141 | // 22.2.3.5 %TypedArray%.prototype.copyWithin (target, start [, end ] ) | ||
2142 | define($TypedArray$.prototype, 'copyWithin', Array.prototype.copyWithin); | ||
2143 | |||
2144 | // 22.2.3.6 %TypedArray%.prototype.entries ( ) | ||
2145 | define($TypedArray$.prototype, 'entries', Array.prototype.entries); | ||
2146 | |||
2147 | // 22.2.3.7 %TypedArray%.prototype.every ( callbackfn [ , thisArg ] ) | ||
2148 | define($TypedArray$.prototype, 'every', Array.prototype.every); | ||
2149 | |||
2150 | // 22.2.3.8 %TypedArray%.prototype.fill (value [ , start [ , end ] ] ) | ||
2151 | define( | ||
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], | ||
2156 | end = arguments[2]; | ||
2157 | |||
2158 | var o = ToObject(this); | ||
2159 | var lenVal = o.length; | ||
2160 | var len = ToLength(lenVal); | ||
2161 | len = max(len, 0); | ||
2162 | var relativeStart = ToInteger(start); | ||
2163 | var k; | ||
2164 | if (relativeStart < 0) k = max((len + relativeStart), 0); | ||
2165 | else k = min(relativeStart, len); | ||
2166 | var relativeEnd; | ||
2167 | if (end === undefined) relativeEnd = len; | ||
2168 | else relativeEnd = ToInteger(end); | ||
2169 | var final; | ||
2170 | if (relativeEnd < 0) final = max((len + relativeEnd), 0); | ||
2171 | else final = min(relativeEnd, len); | ||
2172 | while (k < final) { | ||
2173 | var pk = String(k); | ||
2174 | o[pk] = value; | ||
2175 | k += 1; | ||
2176 | } | ||
2177 | return o; | ||
2178 | }); | ||
2179 | |||
2180 | // 22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] ) | ||
2181 | define( | ||
2182 | $TypedArray$.prototype, 'filter', | ||
2183 | function filter(callbackfn) { | ||
2184 | var thisArg = arguments[1]; | ||
2185 | |||
2186 | var o = ToObject(this); | ||
2187 | var lenVal = o.length; | ||
2188 | var len = ToLength(lenVal); | ||
2189 | if (!IsCallable(callbackfn)) throw TypeError(); | ||
2190 | var t = thisArg; | ||
2191 | var c = o.constructor; | ||
2192 | var kept = []; | ||
2193 | var k = 0; | ||
2194 | var captured = 0; | ||
2195 | while (k < len) { | ||
2196 | var kValue = o[k]; | ||
2197 | var selected = callbackfn.call(t, kValue, k, o); | ||
2198 | if (selected) { | ||
2199 | kept.push(kValue); | ||
2200 | ++captured; | ||
2201 | } | ||
2202 | ++k; | ||
2203 | } | ||
2204 | var a = new c(captured); | ||
2205 | var n = 0; | ||
2206 | for (var i = 0; i < kept.length; ++i) { | ||
2207 | var e = kept[i]; | ||
2208 | a[n] = e; | ||
2209 | ++n; | ||
2210 | } | ||
2211 | return a; | ||
2212 | }); | ||
2213 | |||
2214 | // 22.2.3.10 %TypedArray%.prototype.find (predicate [ , thisArg ] ) | ||
2215 | define($TypedArray$.prototype, 'find', Array.prototype.find); | ||
2216 | |||
2217 | // 22.2.3.11 %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] ) | ||
2218 | define($TypedArray$.prototype, 'findIndex', Array.prototype.findIndex); | ||
2219 | |||
2220 | // 22.2.3.12 %TypedArray%.prototype.forEach ( callbackfn [ , thisArg ] ) | ||
2221 | define($TypedArray$.prototype, 'forEach', Array.prototype.forEach); | ||
2222 | |||
2223 | // 22.2.3.13 %TypedArray%.prototype.indexOf (searchElement [ , fromIndex ] ) | ||
2224 | define($TypedArray$.prototype, 'indexOf', Array.prototype.indexOf); | ||
2225 | |||
2226 | // 22.2.3.14 %TypedArray%.prototype.join ( separator ) | ||
2227 | define($TypedArray$.prototype, 'join', Array.prototype.join); | ||
2228 | |||
2229 | // 22.2.3.15 %TypedArray%.prototype.keys ( ) | ||
2230 | define($TypedArray$.prototype, 'keys', Array.prototype.keys); | ||
2231 | |||
2232 | // 22.2.3.16 %TypedArray%.prototype.lastIndexOf ( searchElement [ , fromIndex ] ) | ||
2233 | define($TypedArray$.prototype, 'lastIndexOf', Array.prototype.lastIndexOf); | ||
2234 | |||
2235 | // 22.2.3.17 get %TypedArray%.prototype.length | ||
2236 | |||
2237 | // 22.2.3.18 %TypedArray%.prototype.map ( callbackfn [ , thisArg ] ) | ||
2238 | define( | ||
2239 | $TypedArray$.prototype, 'map', | ||
2240 | function map(callbackfn) { | ||
2241 | var thisArg = arguments[1]; | ||
2242 | |||
2243 | var o = ToObject(this); | ||
2244 | var lenValue = o.length; | ||
2245 | var len = ToLength(lenValue); | ||
2246 | if (!IsCallable(callbackfn)) throw TypeError(); | ||
2247 | var t = thisArg; | ||
2248 | var a = undefined; | ||
2249 | var c = o.constructor; | ||
2250 | if (IsConstructor(c)) | ||
2251 | a = new c(len); | ||
2252 | if (a === undefined) | ||
2253 | a = new Array(len); | ||
2254 | var k = 0; | ||
2255 | while (k < len) { | ||
2256 | var kPresent = HasProperty(o, k); | ||
2257 | if (kPresent) { | ||
2258 | var kValue = o[k]; | ||
2259 | var mappedValue = callbackfn.call(t, kValue, k, o); | ||
2260 | a[k] = mappedValue; | ||
2261 | } | ||
2262 | ++k; | ||
2263 | } | ||
2264 | return a; | ||
2265 | }); | ||
2266 | |||
2267 | // 22.2.3.19 %TypedArray%.prototype.reduce ( callbackfn [, initialValue] ) | ||
2268 | define($TypedArray$.prototype, 'reduce', Array.prototype.reduce); | ||
2269 | |||
2270 | // 22.2.3.20 %TypedArray%.prototype.reduceRight ( callbackfn [, initialValue] ) | ||
2271 | define($TypedArray$.prototype, 'reduceRight', Array.prototype.reduceRight); | ||
2272 | |||
2273 | // 22.2.3.21 %TypedArray%.prototype.reverse ( ) | ||
2274 | define($TypedArray$.prototype, 'reverse', Array.prototype.reverse); | ||
2275 | |||
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 ] ) | ||
2279 | |||
2280 | // 22.2.3.23 %TypedArray%.prototype.slice ( start, end ) | ||
2281 | define( | ||
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); | ||
2295 | } else { | ||
2296 | throw TypeError(); | ||
2297 | } | ||
2298 | var n = 0; | ||
2299 | while (k < final) { | ||
2300 | var kValue = o[k]; | ||
2301 | a[n] = kValue; | ||
2302 | ++k; | ||
2303 | ++n; | ||
2304 | } | ||
2305 | return a; | ||
2306 | }); | ||
2307 | |||
2308 | // 22.2.3.24 %TypedArray%.prototype.some ( callbackfn [ , thisArg ] ) | ||
2309 | define($TypedArray$.prototype, 'some', Array.prototype.some); | ||
2310 | |||
2311 | // 22.2.3.25 %TypedArray%.prototype.sort ( comparefn ) | ||
2312 | define( | ||
2313 | $TypedArray$.prototype, 'sort', | ||
2314 | function sort() { | ||
2315 | var comparefn = arguments[0]; | ||
2316 | |||
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); | ||
2324 | } | ||
2325 | if (x < y) return -1; | ||
2326 | if (x > y) return 1; | ||
2327 | return +0; | ||
2328 | } | ||
2329 | return Array.prototype.sort.call(this, sortCompare); | ||
2330 | }); | ||
2331 | |||
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 ( ) | ||
2335 | |||
2336 | // 22.2.3.29 %TypedArray%.prototype.values ( ) | ||
2337 | define($TypedArray$.prototype, 'values', Array.prototype.values); | ||
2338 | |||
2339 | // 22.2.3.30 %TypedArray%.prototype [ @@iterator ] ( ) | ||
2340 | define( | ||
2341 | $TypedArray$.prototype, $$iterator, | ||
2342 | $TypedArray$.prototype.values | ||
2343 | ); | ||
2344 | |||
2345 | // 22.2.3.31 get %TypedArray%.prototype [ @@toStringTag ] | ||
2346 | define($TypedArray$.prototype, $$toStringTag, $TypedArrayName$); | ||
2347 | |||
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 | ||
2357 | }); | ||
2358 | |||
2359 | // --------------------------------------- | ||
2360 | // 23 Keyed Collection | ||
2361 | // --------------------------------------- | ||
2362 | |||
2363 | // --------------------------------------- | ||
2364 | // 23.1 Map Objects | ||
2365 | // --------------------------------------- | ||
2366 | |||
2367 | (function() { | ||
2368 | // 23.1.1 The Map Constructor | ||
2369 | |||
2370 | // 23.1.1.1 Map ( [ iterable ] ) | ||
2371 | /** @constructor */ | ||
2372 | function Map(/*iterable*/) { | ||
2373 | var map = strict(this); | ||
2374 | var iterable = arguments[0]; | ||
2375 | |||
2376 | if (Type(map) !== 'object') throw TypeError(); | ||
2377 | if ('[[MapData]]' in map) throw TypeError(); | ||
2378 | |||
2379 | if (iterable !== undefined) { | ||
2380 | var adder = map['set']; | ||
2381 | if (!IsCallable(adder)) throw TypeError(); | ||
2382 | var iter = GetIterator(ToObject(iterable)); | ||
2383 | } | ||
2384 | set_internal(map, '[[MapData]]', { keys: [], values: [] }); | ||
2385 | if (iter === undefined) return map; | ||
2386 | while (true) { | ||
2387 | var next = IteratorStep(iter); | ||
2388 | if (next === false) | ||
2389 | return map; | ||
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); | ||
2395 | } | ||
2396 | |||
2397 | return map; | ||
2398 | } | ||
2399 | |||
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)) | ||
2404 | global.Map = Map; | ||
2405 | |||
2406 | |||
2407 | function MapDataIndexOf(mapData, key) { | ||
2408 | var i; | ||
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; | ||
2413 | return -1; | ||
2414 | } | ||
2415 | |||
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$; | ||
2421 | |||
2422 | // 23.1.2.2 get Map [ @@species ] | ||
2423 | |||
2424 | // 23.1.3 Properties of the Map Prototype Object | ||
2425 | // 23.1.3.1 Map.prototype.clear () | ||
2426 | define( | ||
2427 | Map.prototype, 'clear', | ||
2428 | function 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; | ||
2436 | return undefined; | ||
2437 | }); | ||
2438 | |||
2439 | // 23.1.3.2 Map.prototype.constructor | ||
2440 | |||
2441 | // 23.1.3.3 Map.prototype.delete ( key ) | ||
2442 | define( | ||
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; | ||
2454 | return true; | ||
2455 | }); | ||
2456 | |||
2457 | // 23.1.3.4 Map.prototype.entries ( ) | ||
2458 | define( | ||
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'); | ||
2464 | }); | ||
2465 | |||
2466 | // 23.1.3.5 Map.prototype.forEach ( callbackfn [ , thisArg ] ) | ||
2467 | define( | ||
2468 | Map.prototype, 'forEach', | ||
2469 | function forEach(callbackfn /*, thisArg*/) { | ||
2470 | var thisArg = arguments[1]; | ||
2471 | |||
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]]']; | ||
2477 | |||
2478 | if (!IsCallable(callbackfn)) { | ||
2479 | throw TypeError('First argument to forEach is not callable.'); | ||
2480 | } | ||
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); | ||
2484 | } | ||
2485 | } | ||
2486 | return undefined; | ||
2487 | }); | ||
2488 | |||
2489 | // 23.1.3.6 Map.prototype.get ( key ) | ||
2490 | define( | ||
2491 | Map.prototype, 'get', | ||
2492 | function get(key) { | ||
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]; | ||
2500 | return undefined; | ||
2501 | }); | ||
2502 | |||
2503 | // 23.1.3.7 Map.prototype.has ( key ) | ||
2504 | define( | ||
2505 | Map.prototype, 'has', | ||
2506 | function has(key) { | ||
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; | ||
2513 | return false; | ||
2514 | }); | ||
2515 | |||
2516 | // 23.1.3.8 Map.prototype.keys ( ) | ||
2517 | define( | ||
2518 | Map.prototype, 'keys', | ||
2519 | function keys() { | ||
2520 | var m = strict(this); | ||
2521 | if (Type(m) !== 'object') throw TypeError(); | ||
2522 | return CreateMapIterator(m, 'key'); | ||
2523 | }); | ||
2524 | |||
2525 | // 23.1.3.9 Map.prototype.set ( key , value ) | ||
2526 | define( | ||
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; | ||
2539 | return m; | ||
2540 | }); | ||
2541 | |||
2542 | // 23.1.3.10 get Map.prototype.size | ||
2543 | Object.defineProperty( | ||
2544 | Map.prototype, 'size', { | ||
2545 | get: function() { | ||
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]]']; | ||
2551 | var count = 0; | ||
2552 | for (var i = 0; i < entries.keys.length; ++i) { | ||
2553 | if (entries.keys[i] !== empty) | ||
2554 | count = count + 1; | ||
2555 | } | ||
2556 | return count; | ||
2557 | } | ||
2558 | }); | ||
2559 | |||
2560 | // 23.1.3.11 Map.prototype.values ( ) | ||
2561 | define( | ||
2562 | Map.prototype, 'values', | ||
2563 | function values() { | ||
2564 | var m = strict(this); | ||
2565 | if (Type(m) !== 'object') throw TypeError(); | ||
2566 | return CreateMapIterator(m, 'value'); | ||
2567 | }); | ||
2568 | |||
2569 | // 23.1.3.12 Map.prototype [ @@iterator ]( ) | ||
2570 | define( | ||
2571 | Map.prototype, $$iterator, | ||
2572 | function() { | ||
2573 | var m = strict(this); | ||
2574 | if (Type(m) !== 'object') throw TypeError(); | ||
2575 | return CreateMapIterator(m, 'key+value'); | ||
2576 | }); | ||
2577 | |||
2578 | // 23.1.3.13 Map.prototype [ @@toStringTag ] | ||
2579 | define(global.Map.prototype, $$toStringTag, 'Map'); | ||
2580 | |||
2581 | // 23.1.4 Properties of Map Instances | ||
2582 | // 23.1.5 Map Iterator Objects | ||
2583 | |||
2584 | /** @constructor */ | ||
2585 | function MapIterator() {} | ||
2586 | |||
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); | ||
2596 | return iterator; | ||
2597 | } | ||
2598 | |||
2599 | // 23.1.5.2 The %MapIteratorPrototype% Object | ||
2600 | var $MapIteratorPrototype$ = Object.create($IteratorPrototype$); | ||
2601 | MapIterator.prototype = $MapIteratorPrototype$; | ||
2602 | |||
2603 | // 23.1.5.2.1 %MapIteratorPrototype%.next ( ) | ||
2604 | define( | ||
2605 | $MapIteratorPrototype$, 'next', | ||
2606 | function 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]}; | ||
2615 | index = index += 1; | ||
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); | ||
2622 | } else { | ||
2623 | return CreateIterResultObject([e.key, e.value], false); | ||
2624 | } | ||
2625 | } | ||
2626 | } | ||
2627 | return CreateIterResultObject(undefined, true); | ||
2628 | }); | ||
2629 | |||
2630 | // 23.1.5.2.2 %MapIteratorPrototype% [ @@toStringTag ] | ||
2631 | define($MapIteratorPrototype$, $$toStringTag, 'Map Iterator'); | ||
2632 | |||
2633 | // 23.1.5.3 Properties of Map Iterator Instances | ||
2634 | }()); | ||
2635 | |||
2636 | // --------------------------------------- | ||
2637 | // 23.2 Set Objects | ||
2638 | // --------------------------------------- | ||
2639 | |||
2640 | (function() { | ||
2641 | // 23.2.1 The Set Constructor | ||
2642 | // 23.2.1.1 Set ( [ iterable ] ) | ||
2643 | |||
2644 | /** @constructor */ | ||
2645 | function Set(/*iterable*/) { | ||
2646 | var set = strict(this); | ||
2647 | var iterable = arguments[0]; | ||
2648 | |||
2649 | if (Type(set) !== 'object') throw TypeError(); | ||
2650 | if ('[[SetData]]' in set) throw TypeError(); | ||
2651 | |||
2652 | if (iterable !== undefined) { | ||
2653 | var adder = set['add']; | ||
2654 | if (!IsCallable(adder)) throw TypeError(); | ||
2655 | var iter = GetIterator(ToObject(iterable)); | ||
2656 | } | ||
2657 | set_internal(set, '[[SetData]]', []); | ||
2658 | if (iter === undefined) return set; | ||
2659 | while (true) { | ||
2660 | var next = IteratorStep(iter); | ||
2661 | if (next === false) | ||
2662 | return set; | ||
2663 | var nextValue = IteratorValue(next); | ||
2664 | adder.call(set, nextValue); | ||
2665 | } | ||
2666 | |||
2667 | return set; | ||
2668 | } | ||
2669 | |||
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)) | ||
2673 | global.Set = Set; | ||
2674 | |||
2675 | function SetDataIndexOf(setData, key) { | ||
2676 | var i; | ||
2677 | if (key === 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; | ||
2682 | return -1; | ||
2683 | } | ||
2684 | |||
2685 | // 23.2.1.2 new Set ( ...argumentsList ) | ||
2686 | // 23.2.2 Properties of the Set Constructor | ||
2687 | |||
2688 | // 23.2.2.1 Set.prototype | ||
2689 | var $SetPrototype$ = {}; | ||
2690 | Set.prototype = $SetPrototype$; | ||
2691 | |||
2692 | // 23.2.2.2 get Set [ @@species ] | ||
2693 | // 23.2.3 Properties of the Set Prototype Object | ||
2694 | |||
2695 | // 23.2.3.1 Set.prototype.add (value ) | ||
2696 | define( | ||
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; | ||
2708 | |||
2709 | return s; | ||
2710 | }); | ||
2711 | |||
2712 | // 23.2.3.2 Set.prototype.clear () | ||
2713 | define( | ||
2714 | Set.prototype, 'clear', | ||
2715 | function 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]]']; | ||
2721 | entries.length = 0; | ||
2722 | return undefined; | ||
2723 | }); | ||
2724 | |||
2725 | // 23.2.3.3 Set.prototype.constructor | ||
2726 | // 23.2.3.4 Set.prototype.delete ( value ) | ||
2727 | define( | ||
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; | ||
2737 | entries[i] = empty; | ||
2738 | return true; | ||
2739 | }); | ||
2740 | |||
2741 | // 23.2.3.5 Set.prototype.entries ( ) | ||
2742 | define( | ||
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'); | ||
2748 | }); | ||
2749 | |||
2750 | // 23.2.3.6 Set.prototype.forEach ( callbackfn [ , thisArg ] ) | ||
2751 | define( | ||
2752 | Set.prototype, 'forEach', | ||
2753 | function forEach(callbackfn/*, thisArg*/) { | ||
2754 | var thisArg = arguments[1]; | ||
2755 | |||
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]]']; | ||
2761 | |||
2762 | if (!IsCallable(callbackfn)) { | ||
2763 | throw TypeError('First argument to forEach is not callable.'); | ||
2764 | } | ||
2765 | for (var i = 0; i < entries.length; ++i) { | ||
2766 | if (entries[i] !== empty) { | ||
2767 | callbackfn.call(thisArg, entries[i], entries[i], s); | ||
2768 | } | ||
2769 | } | ||
2770 | }); | ||
2771 | |||
2772 | // 23.2.3.7 Set.prototype.has ( value ) | ||
2773 | define( | ||
2774 | Set.prototype, 'has', | ||
2775 | function has(key) { | ||
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; | ||
2782 | }); | ||
2783 | |||
2784 | // 23.2.3.8 Set.prototype.keys ( ) | ||
2785 | // See Set.prototype.values | ||
2786 | |||
2787 | // 23.2.3.9 get Set.prototype.size | ||
2788 | Object.defineProperty( | ||
2789 | Set.prototype, 'size', { | ||
2790 | get: function() { | ||
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]]']; | ||
2796 | var count = 0; | ||
2797 | for (var i = 0; i < entries.length; ++i) { | ||
2798 | if (entries[i] !== empty) | ||
2799 | count = count + 1; | ||
2800 | } | ||
2801 | return count; | ||
2802 | } | ||
2803 | }); | ||
2804 | |||
2805 | // 23.2.3.10 Set.prototype.values ( ) | ||
2806 | define( | ||
2807 | Set.prototype, 'values', | ||
2808 | function values() { | ||
2809 | var s = strict(this); | ||
2810 | if (Type(s) !== 'object') throw TypeError(); | ||
2811 | return CreateSetIterator(s, 'value'); | ||
2812 | }); | ||
2813 | // NOTE: function name is still 'values': | ||
2814 | Set.prototype.keys = Set.prototype.values; | ||
2815 | |||
2816 | // 23.2.3.11 Set.prototype [@@iterator ] ( ) | ||
2817 | define( | ||
2818 | Set.prototype, $$iterator, | ||
2819 | function() { | ||
2820 | var s = strict(this); | ||
2821 | if (Type(s) !== 'object') throw TypeError(); | ||
2822 | return CreateSetIterator(s); | ||
2823 | }); | ||
2824 | |||
2825 | // 23.2.3.12 Set.prototype [ @@toStringTag ] | ||
2826 | define(global.Set.prototype, $$toStringTag, 'Set'); | ||
2827 | |||
2828 | // 23.2.4 Properties of Set Instances | ||
2829 | // 23.2.5 Set Iterator Objects | ||
2830 | /** @constructor */ | ||
2831 | function SetIterator() {} | ||
2832 | |||
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); | ||
2842 | return iterator; | ||
2843 | } | ||
2844 | |||
2845 | // 23.2.5.2 The %SetIteratorPrototype% Object | ||
2846 | var $SetIteratorPrototype$ = Object.create($IteratorPrototype$); | ||
2847 | SetIterator.prototype = $SetIteratorPrototype$; | ||
2848 | |||
2849 | // 23.2.5.2.1 %SetIteratorPrototype%.next( ) | ||
2850 | define( | ||
2851 | $SetIteratorPrototype$, 'next', | ||
2852 | function 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]; | ||
2861 | index = index += 1; | ||
2862 | set_internal(o, '[[SetNextIndex]]', index); | ||
2863 | if (e !== empty) { | ||
2864 | if (itemKind === 'key+value') | ||
2865 | return CreateIterResultObject([e, e], false); | ||
2866 | return CreateIterResultObject(e, false); | ||
2867 | } | ||
2868 | } | ||
2869 | return CreateIterResultObject(undefined, true); | ||
2870 | }); | ||
2871 | |||
2872 | // 23.2.5.2.2 %SetIteratorPrototype% [ @@toStringTag ] | ||
2873 | define($SetIteratorPrototype$, $$toStringTag, 'Set Iterator'); | ||
2874 | |||
2875 | // 23.2.5.3 Properties of Set Iterator Instances | ||
2876 | |||
2877 | }()); | ||
2878 | |||
2879 | // --------------------------------------- | ||
2880 | // 23.3 WeakMap Objects | ||
2881 | // --------------------------------------- | ||
2882 | |||
2883 | (function() { | ||
2884 | // 23.3.1 The WeakMap Constructor | ||
2885 | // 23.3.1.1 WeakMap ( [ iterable ] ) | ||
2886 | /** @constructor */ | ||
2887 | function WeakMap(/*iterable*/) { | ||
2888 | var map = strict(this); | ||
2889 | var iterable = arguments[0]; | ||
2890 | |||
2891 | if (Type(map) !== 'object') throw TypeError(); | ||
2892 | if ('[[WeakMapData]]' in map) throw TypeError(); | ||
2893 | |||
2894 | if (iterable !== undefined) { | ||
2895 | var adder = map['set']; | ||
2896 | if (!IsCallable(adder)) throw TypeError(); | ||
2897 | var iter = GetIterator(ToObject(iterable)); | ||
2898 | } | ||
2899 | set_internal(map, '[[WeakMapData]]', new EphemeronTable); | ||
2900 | if (iter === undefined) return map; | ||
2901 | while (true) { | ||
2902 | var next = IteratorStep(iter); | ||
2903 | if (next === false) | ||
2904 | return map; | ||
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); | ||
2910 | } | ||
2911 | |||
2912 | return map; | ||
2913 | } | ||
2914 | |||
2915 | if (!('WeakMap' in global) || OVERRIDE_NATIVE_FOR_TESTING) | ||
2916 | global.WeakMap = WeakMap; | ||
2917 | |||
2918 | // 23.3.2 Properties of the WeakMap Constructor | ||
2919 | // 23.3.2.1 WeakMap.prototype | ||
2920 | var $WeakMapPrototype$ = {}; | ||
2921 | WeakMap.prototype = $WeakMapPrototype$; | ||
2922 | |||
2923 | |||
2924 | |||
2925 | // 23.3.2.2 WeakMap[ @@create ] ( ) | ||
2926 | // 23.3.3 Properties of the WeakMap Prototype Object | ||
2927 | |||
2928 | // 23.3.3.1 WeakMap.prototype.constructor | ||
2929 | |||
2930 | // 23.3.3.2 WeakMap.prototype.delete ( key ) | ||
2931 | define( | ||
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); | ||
2939 | }); | ||
2940 | |||
2941 | // 23.3.3.3 WeakMap.prototype.get ( key ) | ||
2942 | define( | ||
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); | ||
2950 | }); | ||
2951 | |||
2952 | // 23.3.3.4 WeakMap.prototype.has ( key ) | ||
2953 | define( | ||
2954 | WeakMap.prototype, 'has', | ||
2955 | function has(key) { | ||
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); | ||
2961 | }); | ||
2962 | |||
2963 | // 23.3.3.5 WeakMap.prototype.set ( key , value ) | ||
2964 | define( | ||
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); | ||
2972 | return M; | ||
2973 | }); | ||
2974 | |||
2975 | // 23.3.3.6 WeakMap.prototype [ @@toStringTag ] | ||
2976 | define(global.WeakMap.prototype, $$toStringTag, 'WeakMap'); | ||
2977 | |||
2978 | // 23.3.4 Properties of WeakMap Instances | ||
2979 | |||
2980 | // Polyfills for incomplete native implementations: | ||
2981 | (function() { | ||
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); | ||
2986 | return this; | ||
2987 | }, wm.set({}, 0) !== wm); | ||
2988 | }()); | ||
2989 | }()); | ||
2990 | |||
2991 | // --------------------------------------- | ||
2992 | // 23.4 WeakSet Objects | ||
2993 | // --------------------------------------- | ||
2994 | |||
2995 | (function() { | ||
2996 | // 23.4.1 The WeakSet Constructor | ||
2997 | // 23.4.1.1 WeakSet ( [ iterable ] ) | ||
2998 | /** @constructor */ | ||
2999 | function WeakSet(/*iterable*/) { | ||
3000 | var set = strict(this); | ||
3001 | var iterable = arguments[0]; | ||
3002 | |||
3003 | if (Type(set) !== 'object') throw TypeError(); | ||
3004 | if ('[[WeakSetData]]' in set) throw TypeError(); | ||
3005 | |||
3006 | if (iterable !== undefined) { | ||
3007 | var adder = set['add']; | ||
3008 | if (!IsCallable(adder)) throw TypeError(); | ||
3009 | var iter = GetIterator(ToObject(iterable)); | ||
3010 | } | ||
3011 | set_internal(set, '[[WeakSetData]]', new EphemeronTable); | ||
3012 | if (iter === undefined) return set; | ||
3013 | while (true) { | ||
3014 | var next = IteratorStep(iter); | ||
3015 | if (next === false) | ||
3016 | return set; | ||
3017 | var nextValue = IteratorValue(next); | ||
3018 | adder.call(set, nextValue); | ||
3019 | } | ||
3020 | |||
3021 | return set; | ||
3022 | } | ||
3023 | |||
3024 | if (!('WeakSet' in global) || OVERRIDE_NATIVE_FOR_TESTING) | ||
3025 | global.WeakSet = WeakSet; | ||
3026 | |||
3027 | // 23.4.2 Properties of the WeakSet Constructor | ||
3028 | // 23.4.2.1 WeakSet.prototype | ||
3029 | var $WeakSetPrototype$ = {}; | ||
3030 | WeakSet.prototype = $WeakSetPrototype$; | ||
3031 | |||
3032 | // 23.4.3 Properties of the WeakSet Prototype Object | ||
3033 | // 23.4.3.1 WeakSet.prototype.add (value ) | ||
3034 | define( | ||
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); | ||
3042 | return S; | ||
3043 | }); | ||
3044 | |||
3045 | // 23.4.3.2 WeakSet.prototype.constructor | ||
3046 | // 23.4.3.3 WeakSet.prototype.delete ( value ) | ||
3047 | define( | ||
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); | ||
3055 | }); | ||
3056 | |||
3057 | // 23.4.3.4 WeakSet.prototype.has ( value ) | ||
3058 | define( | ||
3059 | WeakSet.prototype, 'has', | ||
3060 | function has(key) { | ||
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); | ||
3066 | }); | ||
3067 | |||
3068 | // 23.4.3.5 WeakSet.prototype [ @@toStringTag ] | ||
3069 | define(global.WeakSet.prototype, $$toStringTag, 'WeakSet'); | ||
3070 | |||
3071 | // 23.4.4 Properties of WeakSet Instances | ||
3072 | |||
3073 | // Polyfills for incomplete native implementations: | ||
3074 | (function() { | ||
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); | ||
3079 | return this; | ||
3080 | }, ws.add({}) !== ws); | ||
3081 | }()); | ||
3082 | }()); | ||
3083 | |||
3084 | // --------------------------------------- | ||
3085 | // 24 Structured Data | ||
3086 | // --------------------------------------- | ||
3087 | |||
3088 | // --------------------------------------- | ||
3089 | // 24.1 ArrayBuffer Objects | ||
3090 | // --------------------------------------- | ||
3091 | |||
3092 | // See typedarray.js for TypedArray polyfill | ||
3093 | |||
3094 | (function() { | ||
3095 | if (!('ArrayBuffer' in global)) | ||
3096 | return; | ||
3097 | |||
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 | ||
3108 | |||
3109 | // 24.1.3.1 ArrayBuffer.isView ( arg ) | ||
3110 | define( | ||
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; | ||
3115 | return false; | ||
3116 | }); | ||
3117 | |||
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) | ||
3124 | |||
3125 | // 24.1.4.4 ArrayBuffer.prototype [ @@toStringTag ] | ||
3126 | define(ArrayBuffer.prototype, $$toStringTag, 'ArrayBuffer'); | ||
3127 | |||
3128 | // 24.1.5 Properties of the ArrayBuffer Instances | ||
3129 | }()); | ||
3130 | |||
3131 | // --------------------------------------- | ||
3132 | // 24.2 DataView Objects | ||
3133 | // --------------------------------------- | ||
3134 | |||
3135 | // See typedarray.js for TypedArray polyfill | ||
3136 | |||
3137 | (function() { | ||
3138 | if (!('DataView' in global)) | ||
3139 | return; | ||
3140 | |||
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 ] ) | ||
3169 | |||
3170 | // 24.2.4.21 DataView.prototype[ @@toStringTag ] | ||
3171 | define(DataView.prototype, $$toStringTag, 'DataView'); | ||
3172 | |||
3173 | // 24.2.5 Properties of DataView Instances | ||
3174 | }()); | ||
3175 | |||
3176 | // --------------------------------------- | ||
3177 | // 24.3 The JSON Object | ||
3178 | // --------------------------------------- | ||
3179 | |||
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'); | ||
3184 | |||
3185 | // --------------------------------------- | ||
3186 | // 25.1 Iteration | ||
3187 | // --------------------------------------- | ||
3188 | |||
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 | ||
3193 | |||
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() { | ||
3198 | return this; | ||
3199 | }); | ||
3200 | |||
3201 | |||
3202 | // --------------------------------------- | ||
3203 | // 25.4 Promise Objects | ||
3204 | // --------------------------------------- | ||
3205 | |||
3206 | (function() { | ||
3207 | // 25.4 Promise Objects | ||
3208 | |||
3209 | // 25.4.1 Promise Abstract Operations | ||
3210 | |||
3211 | // 25.4.1.1 PromiseCapability Records | ||
3212 | // 25.4.1.1.1 IfAbruptRejectPromise ( value, capability ) | ||
3213 | |||
3214 | function IfAbruptRejectPromise(value, capability) { | ||
3215 | var rejectResult = capability['[[Reject]]'].call(undefined, value); | ||
3216 | return capability['[[Promise]]']; | ||
3217 | } | ||
3218 | |||
3219 | // 25.4.1.2 PromiseReaction Records | ||
3220 | |||
3221 | // 25.4.1.3 CreateResolvingFunctions ( promise ) | ||
3222 | |||
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}; | ||
3232 | } | ||
3233 | |||
3234 | // 25.4.1.3.1 Promise Reject Functions | ||
3235 | |||
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); | ||
3244 | }; | ||
3245 | return F; | ||
3246 | } | ||
3247 | |||
3248 | // 25.4.1.3.2 Promise Resolve Functions | ||
3249 | |||
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); | ||
3257 | |||
3258 | if (SameValue(resolution, promise)) { | ||
3259 | var selfResolutionError = TypeError(); | ||
3260 | return RejectPromise(promise, selfResolutionError); | ||
3261 | } | ||
3262 | if (Type(resolution) !== 'object') | ||
3263 | return FulfillPromise(promise, resolution); | ||
3264 | try { | ||
3265 | var then = resolution['then']; | ||
3266 | } catch(then) { | ||
3267 | return RejectPromise(promise, then); | ||
3268 | } | ||
3269 | if (!IsCallable(then)) | ||
3270 | return FulfillPromise(promise, resolution); | ||
3271 | EnqueueJob('PromiseJobs', PromiseResolveThenableJob, [promise, resolution, then]); | ||
3272 | return undefined; | ||
3273 | }; | ||
3274 | return F; | ||
3275 | } | ||
3276 | |||
3277 | // 25.4.1.4 FulfillPromise ( promise, value ) | ||
3278 | |||
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); | ||
3287 | } | ||
3288 | |||
3289 | // 25.4.1.5 NewPromiseCapability ( C ) | ||
3290 | |||
3291 | function NewPromiseCapability(c) { | ||
3292 | // To keep Promise hermetic, this doesn't look much like the spec. | ||
3293 | return CreatePromiseCapabilityRecord(undefined, c); | ||
3294 | } | ||
3295 | |||
3296 | // 25.4.1.5.1 CreatePromiseCapabilityRecord ( promise, constructor ) | ||
3297 | |||
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); | ||
3307 | |||
3308 | // NOTE: Differs from spec; object is constructed here | ||
3309 | var constructorResult = promise = new constructor(executor); | ||
3310 | set_internal(promiseCapability, '[[Promise]]', promise); | ||
3311 | |||
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; | ||
3316 | } | ||
3317 | |||
3318 | // 25.4.1.5.2 GetCapabilitiesExecutor Functions | ||
3319 | |||
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); | ||
3328 | return undefined; | ||
3329 | }; | ||
3330 | return F; | ||
3331 | } | ||
3332 | |||
3333 | // 25.4.1.6 IsPromise ( x ) | ||
3334 | |||
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; | ||
3339 | return true; | ||
3340 | } | ||
3341 | |||
3342 | // 25.4.1.7 RejectPromise ( promise, reason ) | ||
3343 | |||
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); | ||
3352 | } | ||
3353 | |||
3354 | // 25.4.1.8 TriggerPromiseReactions ( reactions, argument ) | ||
3355 | |||
3356 | function TriggerPromiseReactions(reactions, argument) { | ||
3357 | for (var i = 0, len = reactions.length; i < len; ++i) | ||
3358 | EnqueueJob('PromiseJobs', PromiseReactionJob, [reactions[i], argument]); | ||
3359 | return undefined; | ||
3360 | } | ||
3361 | |||
3362 | // 25.4.2 Promise Jobs | ||
3363 | |||
3364 | // 25.4.2.1 PromiseReactionJob ( reaction, argument ) | ||
3365 | |||
3366 | function PromiseReactionJob(reaction, argument) { | ||
3367 | var promiseCapability = reaction['[[Capabilities]]']; | ||
3368 | var handler = reaction['[[Handler]]']; | ||
3369 | var handlerResult, status; | ||
3370 | try { | ||
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; | ||
3377 | } | ||
3378 | status = promiseCapability['[[Resolve]]'].call(undefined, handlerResult); | ||
3379 | NextJob(status); | ||
3380 | } | ||
3381 | |||
3382 | // 25.4.2.2 PromiseResolveThenableJob ( promiseToResolve, thenable, then) | ||
3383 | |||
3384 | function PromiseResolveThenableJob(promiseToResolve, thenable, then) { | ||
3385 | // SPEC BUG: promise vs. promiseToResolve | ||
3386 | var resolvingFunctions = CreateResolvingFunctions(promiseToResolve); | ||
3387 | try { | ||
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; | ||
3393 | } | ||
3394 | NextJob(thenCallResult); | ||
3395 | } | ||
3396 | |||
3397 | // 25.4.3 The Promise Constructor | ||
3398 | |||
3399 | // 25.4.3.1 Promise ( executor ) | ||
3400 | |||
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); | ||
3408 | |||
3409 | var promise = this; | ||
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(); | ||
3414 | |||
3415 | set_internal(promise, '[[PromiseConstructor]]', Promise); | ||
3416 | |||
3417 | return InitializePromise(promise, executor); | ||
3418 | } | ||
3419 | |||
3420 | // 25.4.3.1.1 InitializePromise ( promise, executor ) | ||
3421 | |||
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); | ||
3429 | try { | ||
3430 | var completion = executor.call(undefined, resolvingFunctions['[[Resolve]]'], | ||
3431 | resolvingFunctions['[[Reject]]']); | ||
3432 | } catch (completion) { | ||
3433 | var status = resolvingFunctions['[[Reject]]'].call(undefined, completion); | ||
3434 | } | ||
3435 | return promise; | ||
3436 | } | ||
3437 | |||
3438 | // 25.4.4 Properties of the Promise Constructor | ||
3439 | // 25.4.4.1 Promise.all ( iterable ) | ||
3440 | |||
3441 | define(Promise, 'all', function all(iterable) { | ||
3442 | var c = strict(this); | ||
3443 | var promiseCapability = NewPromiseCapability(c); | ||
3444 | try { | ||
3445 | var iterator = GetIterator(iterable); | ||
3446 | } catch (value) { | ||
3447 | promiseCapability['[[Reject]]'].call(undefined, value); | ||
3448 | return promiseCapability['[[Promise]]']; | ||
3449 | } | ||
3450 | var values = []; | ||
3451 | var remainingElementsCount = { value: 1 }; | ||
3452 | var index = 0; | ||
3453 | while (true) { | ||
3454 | try { | ||
3455 | var next = IteratorStep(iterator); | ||
3456 | } catch (value) { | ||
3457 | promiseCapability['[[Reject]]'].call(undefined, value); | ||
3458 | return promiseCapability['[[Promise]]']; | ||
3459 | } | ||
3460 | if (!next) { | ||
3461 | remainingElementsCount.value -= 1; | ||
3462 | if (remainingElementsCount.value === 0) { | ||
3463 | var resolveResult = promiseCapability['[[Resolve]]'].apply(undefined, values); | ||
3464 | |||
3465 | |||
3466 | } | ||
3467 | return promiseCapability['[[Promise]]']; | ||
3468 | } | ||
3469 | try { | ||
3470 | var nextValue = IteratorValue(next); | ||
3471 | } catch (value) { | ||
3472 | promiseCapability['[[Reject]]'].call(undefined, value); | ||
3473 | return promiseCapability['[[Promise]]']; | ||
3474 | } | ||
3475 | try { | ||
3476 | var nextPromise = c.resolve(nextValue); | ||
3477 | } catch (value) { | ||
3478 | promiseCapability['[[Reject]]'].call(undefined, value); | ||
3479 | return promiseCapability['[[Promise]]']; | ||
3480 | } | ||
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; | ||
3488 | try { | ||
3489 | var result = nextPromise.then(resolveElement, promiseCapability['[[Reject]]']); | ||
3490 | } catch (value) { | ||
3491 | promiseCapability['[[Reject]]'].call(undefined, value); | ||
3492 | return promiseCapability['[[Promise]]']; | ||
3493 | } | ||
3494 | index += 1; | ||
3495 | } | ||
3496 | }); | ||
3497 | |||
3498 | // 25.4.4.1.1 Promise.all Resolve Element Functions | ||
3499 | |||
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]]']; | ||
3509 | try { | ||
3510 | values[index] = x; | ||
3511 | } catch (result) { | ||
3512 | promiseCapability['[[Reject]]'].call(undefined, result); | ||
3513 | return promiseCapability['[[Promise]]']; | ||
3514 | } | ||
3515 | remainingElementsCount.value -= 1; | ||
3516 | if (remainingElementsCount.value === 0) | ||
3517 | return promiseCapability['[[Resolve]]'].call(undefined, values); | ||
3518 | return undefined; | ||
3519 | }; | ||
3520 | return F; | ||
3521 | } | ||
3522 | |||
3523 | // 25.4.4.2 Promise.prototype | ||
3524 | |||
3525 | Promise.prototype = {}; | ||
3526 | |||
3527 | // 25.4.4.3 Promise.race ( iterable ) | ||
3528 | |||
3529 | define(Promise, 'race', function race(iterable) { | ||
3530 | var c = strict(this); | ||
3531 | var promiseCapability = NewPromiseCapability(c); | ||
3532 | try { | ||
3533 | var iterator = GetIterator(iterable); | ||
3534 | } catch (value) { | ||
3535 | promiseCapability['[[Reject]]'].call(undefined, value); | ||
3536 | return promiseCapability['[[Promise]]']; | ||
3537 | } | ||
3538 | while (true) { | ||
3539 | try { | ||
3540 | var next = IteratorStep(iterator); | ||
3541 | } catch (value) { | ||
3542 | promiseCapability['[[Reject]]'].call(undefined, value); | ||
3543 | return promiseCapability['[[Promise]]']; | ||
3544 | } | ||
3545 | if (!next) return promiseCapability['[[Promise]]']; | ||
3546 | try { | ||
3547 | var nextValue = IteratorValue(next); | ||
3548 | } catch (value) { | ||
3549 | promiseCapability['[[Reject]]'].call(undefined, value); | ||
3550 | return promiseCapability['[[Promise]]']; | ||
3551 | } | ||
3552 | try { | ||
3553 | var nextPromise = c.resolve(nextValue); | ||
3554 | } catch (value) { | ||
3555 | promiseCapability['[[Reject]]'].call(undefined, value); | ||
3556 | return promiseCapability['[[Promise]]']; | ||
3557 | } | ||
3558 | try { | ||
3559 | nextPromise.then(promiseCapability['[[Resolve]]'], promiseCapability['[[Reject]]']); | ||
3560 | } catch (value) { | ||
3561 | promiseCapability['[[Reject]]'].call(undefined, value); | ||
3562 | return promiseCapability['[[Promise]]']; | ||
3563 | } | ||
3564 | } | ||
3565 | }); | ||
3566 | |||
3567 | // 25.4.4.4 Promise.reject ( r ) | ||
3568 | |||
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]]']; | ||
3574 | }); | ||
3575 | |||
3576 | // 25.4.4.5 Promise.resolve ( x ) | ||
3577 | |||
3578 | define(Promise, 'resolve', function resolve(x) { | ||
3579 | var c = strict(this); | ||
3580 | if (IsPromise(x)) { | ||
3581 | var constructor = x['[[PromiseConstructor]]']; | ||
3582 | if (SameValue(constructor, c)) return x; | ||
3583 | } | ||
3584 | var promiseCapability = NewPromiseCapability(c); | ||
3585 | var resolveResult = promiseCapability['[[Resolve]]'].call(undefined, x); | ||
3586 | return promiseCapability['[[Promise]]']; | ||
3587 | }); | ||
3588 | |||
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 ) | ||
3593 | |||
3594 | define(Promise.prototype, 'catch', function catch_(onRejected) { | ||
3595 | var promise = this; | ||
3596 | return promise.then(undefined, onRejected); | ||
3597 | }); | ||
3598 | |||
3599 | // 25.4.5.2 Promise.prototype.constructor | ||
3600 | |||
3601 | Promise.prototype.constructor = Promise; | ||
3602 | |||
3603 | // 25.4.5.3 Promise.prototype.then ( onFulfilled , onRejected ) | ||
3604 | |||
3605 | define(Promise.prototype, 'then', function then(onFulfilled, onRejected) { | ||
3606 | var promise = this; | ||
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]); | ||
3625 | } | ||
3626 | return promiseCapability['[[Promise]]']; | ||
3627 | }); | ||
3628 | |||
3629 | // 25.4.6 Properties of Promise Instances | ||
3630 | |||
3631 | if (!('Promise' in global) || OVERRIDE_NATIVE_FOR_TESTING) | ||
3632 | global.Promise = Promise; | ||
3633 | |||
3634 | // Patch early Promise.cast vs. Promise.resolve implementations | ||
3635 | if ('cast' in global.Promise) global.Promise.resolve = global.Promise.cast; | ||
3636 | }()); | ||
3637 | |||
3638 | // 25.4.5.1 Promise.prototype [ @@toStringTag ] | ||
3639 | define(Promise.prototype, $$toStringTag, 'Promise'); | ||
3640 | |||
3641 | // --------------------------------------- | ||
3642 | // 26 Reflection | ||
3643 | // --------------------------------------- | ||
3644 | |||
3645 | (function() { | ||
3646 | // 26.1 The Reflect Object | ||
3647 | if (!('Reflect' in global) || OVERRIDE_NATIVE_FOR_TESTING) | ||
3648 | global.Reflect = {}; | ||
3649 | |||
3650 | // 26.1.1 Reflect.apply ( target, thisArgument, argumentsList ) | ||
3651 | define( | ||
3652 | Reflect, 'apply', | ||
3653 | function apply(target, thisArgument, argumentsList) { | ||
3654 | if (!IsCallable(target)) throw TypeError(); | ||
3655 | return Function.prototype.apply.call(target, thisArgument, argumentsList); | ||
3656 | }); | ||
3657 | |||
3658 | // 26.1.2 Reflect.construct ( target, argumentsList [, newTarget] ) | ||
3659 | define( | ||
3660 | Reflect, 'construct', | ||
3661 | function construct(target, argumentsList) { | ||
3662 | return __cons(target, argumentsList); | ||
3663 | }); | ||
3664 | |||
3665 | // 26.1.3 Reflect.defineProperty ( target, propertyKey, attributes ) | ||
3666 | define( | ||
3667 | Reflect, 'defineProperty', | ||
3668 | function defineProperty(target, propertyKey, attributes) { | ||
3669 | try { | ||
3670 | Object.defineProperty(target, propertyKey, attributes); | ||
3671 | return true; | ||
3672 | } catch (_) { | ||
3673 | return false; | ||
3674 | } | ||
3675 | }); | ||
3676 | |||
3677 | // 26.1.4 Reflect.deleteProperty ( target, propertyKey ) | ||
3678 | define( | ||
3679 | Reflect, 'deleteProperty', | ||
3680 | function deleteProperty(target,name) { | ||
3681 | try { | ||
3682 | delete target[name]; | ||
3683 | return !HasOwnProperty(target, name); | ||
3684 | } catch (_) { | ||
3685 | return false; | ||
3686 | } | ||
3687 | }); | ||
3688 | |||
3689 | // 26.1.5 Reflect.enumerate ( target ) | ||
3690 | define( | ||
3691 | Reflect, 'enumerate', | ||
3692 | function enumerate(target) { | ||
3693 | target = ToObject(target); | ||
3694 | var iterator = Enumerate(target); | ||
3695 | return iterator; | ||
3696 | }); | ||
3697 | |||
3698 | // 26.1.6 Reflect.get ( target, propertyKey [ , receiver ]) | ||
3699 | define( | ||
3700 | Reflect, 'get', | ||
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]; | ||
3709 | }); | ||
3710 | |||
3711 | // 26.1.7 Reflect.getOwnPropertyDescriptor ( target, propertyKey ) | ||
3712 | define( | ||
3713 | Reflect, 'getOwnPropertyDescriptor', | ||
3714 | Object.getOwnPropertyDescriptor); | ||
3715 | |||
3716 | // 26.1.8 Reflect.getPrototypeOf ( target ) | ||
3717 | define( | ||
3718 | Reflect, 'getPrototypeOf', | ||
3719 | Object.getPrototypeOf); | ||
3720 | |||
3721 | // 26.1.9 Reflect.has ( target, propertyKey ) | ||
3722 | define( | ||
3723 | Reflect, 'has', | ||
3724 | function has(target,name) { | ||
3725 | return String(name) in ToObject(target); | ||
3726 | }); | ||
3727 | |||
3728 | // 26.1.10 Reflect.isExtensible (target) | ||
3729 | define( | ||
3730 | Reflect, 'isExtensible', | ||
3731 | Object.isExtensible); | ||
3732 | |||
3733 | // 26.1.11 Reflect.ownKeys ( target ) | ||
3734 | define( | ||
3735 | Reflect, 'ownKeys', | ||
3736 | function ownKeys(target) { | ||
3737 | var obj = ToObject(target); | ||
3738 | return Object.getOwnPropertyNames(obj); | ||
3739 | }); | ||
3740 | |||
3741 | // 26.1.12 Reflect.preventExtensions ( target ) | ||
3742 | define( | ||
3743 | Reflect, 'preventExtensions', | ||
3744 | function preventExtensions(target) { | ||
3745 | try { Object.preventExtensions(target); return true; } catch (_) { return false; } | ||
3746 | }); | ||
3747 | |||
3748 | // 26.1.13 Reflect.set ( target, propertyKey, V [ , receiver ] ) | ||
3749 | define( | ||
3750 | Reflect, 'set', | ||
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); | ||
3756 | try { | ||
3757 | if (desc && 'set' in desc) | ||
3758 | Function.prototype.call.call(desc['set'], receiver, value); | ||
3759 | else | ||
3760 | target[name] = value; | ||
3761 | return true; | ||
3762 | } catch (_) { | ||
3763 | return false; | ||
3764 | } | ||
3765 | }); | ||
3766 | |||
3767 | // 26.1.14 Reflect.setPrototypeOf ( target, proto ) | ||
3768 | define( | ||
3769 | Reflect, 'setPrototypeOf', | ||
3770 | function setPrototypeOf(target, proto) { | ||
3771 | try { | ||
3772 | target.__proto__ = proto; | ||
3773 | return Reflect.getPrototypeOf(target) === proto; | ||
3774 | } catch(_) { | ||
3775 | return false; | ||
3776 | } | ||
3777 | }); | ||
3778 | |||
3779 | }()); | ||
3780 | |||
3781 | // --------------------------------------- | ||
3782 | // 26.2 Proxy Objects | ||
3783 | // --------------------------------------- | ||
3784 | |||
3785 | // Not polyfillable. | ||
3786 | |||
3787 | }(self)); | ||
3788 | |||
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(',') + ')'); | ||
3793 | } | ||